9 function handleLogin() {
12 var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
18 if(auth.type === 'oauth2' && auth.scopes) {
21 if(Array.isArray(auth.scopes)) {
24 for(i = 0; i < auth.scopes.length; i++) {
25 scopes.push(auth.scopes[i]);
30 for(scope in auth.scopes) {
31 scopes.push({scope: scope, description: auth.scopes[scope]});
38 if(window.swaggerUi.api
39 && window.swaggerUi.api.info) {
40 appName = window.swaggerUi.api.info.title;
45 '<div class="api-popup-dialog">',
46 '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
47 '<div class="api-popup-content">',
48 '<p>Scopes are used to grant an application different levels of access to data on behalf of the end user. Each API may declare one or more scopes.',
49 '<a href="#">Learn how to use</a>',
51 '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
52 '<ul class="api-popup-scopes">',
54 '<p class="error-msg"></p>',
55 '<div class="api-popup-actions"><button class="api-popup-authbtn api-button green" type="button">Authorize</button><button class="api-popup-cancel api-button gray" type="button">Cancel</button></div>',
58 $(document.body).append(popupDialog);
60 popup = popupDialog.find('ul.api-popup-scopes').empty();
61 for (i = 0; i < scopes.length; i ++) {
63 str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
64 if (scope.description) {
65 str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
67 str += '</label></li>';
74 st = $win.scrollTop(),
75 dlgWd = popupDialog.outerWidth(),
76 dlgHt = popupDialog.outerHeight(),
77 top = (dh -dlgHt)/2 + st,
78 left = (dw - dlgWd)/2;
81 top: (top < 0? 0 : top) + 'px',
82 left: (left < 0? 0 : left) + 'px'
85 popupDialog.find('button.api-popup-cancel').click(function() {
92 $('button.api-popup-authbtn').unbind();
93 popupDialog.find('button.api-popup-authbtn').click(function() {
97 var authSchemes = window.swaggerUi.api.authSchemes;
98 var host = window.location;
99 var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
100 var redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
103 for (var key in authSchemes) {
104 if (authSchemes.hasOwnProperty(key)) {
105 var flow = authSchemes[key].flow;
107 if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
108 var dets = authSchemes[key];
109 url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
110 window.swaggerUi.tokenName = dets.tokenName || 'access_token';
111 window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
113 else if(authSchemes[key].grantTypes) {
115 var o = authSchemes[key].grantTypes;
117 if(o.hasOwnProperty(t) && t === 'implicit') {
119 var ep = dets.loginEndpoint.url;
120 url = dets.loginEndpoint.url + '?response_type=token';
121 window.swaggerUi.tokenName = dets.tokenName;
123 else if (o.hasOwnProperty(t) && t === 'accessCode') {
125 var ep = dets.tokenRequestEndpoint.url;
126 url = dets.tokenRequestEndpoint.url + '?response_type=code';
127 window.swaggerUi.tokenName = dets.tokenName;
134 var o = $('.api-popup-scopes').find('input:checked');
136 for(k =0; k < o.length; k++) {
137 var scope = $(o[k]).attr('scope');
139 if (scopes.indexOf(scope) === -1)
143 window.enabledScopes=scopes;
145 redirect_uri = redirectUrl;
147 url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
148 url += '&realm=' + encodeURIComponent(realm);
149 url += '&client_id=' + encodeURIComponent(clientId);
150 url += '&scope=' + encodeURIComponent(scopes);
161 function handleLogout() {
162 for(key in window.authorizations.authz){
163 window.authorizations.remove(key)
165 window.enabledScopes = null;
166 $('.api-ic.ic-on').addClass('ic-off');
167 $('.api-ic.ic-on').removeClass('ic-on');
170 $('.api-ic.ic-warning').addClass('ic-error');
171 $('.api-ic.ic-warning').removeClass('ic-warning');
174 function initOAuth(opts) {
178 appName = (o.appName||errors.push('missing appName'));
179 popupMask = (o.popupMask||$('#api-common-mask'));
180 popupDialog = (o.popupDialog||$('.api-popup-dialog'));
181 clientId = (o.clientId||errors.push('missing client id'));
182 realm = (o.realm||errors.push('missing realm'));
184 if(errors.length > 0){
185 log('auth unable initialize oauth: ' + errors);
189 $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
190 $('.api-ic').unbind();
191 $('.api-ic').click(function(s) {
192 if($(s.target).hasClass('ic-off'))
201 function processOAuthCode(data) {
203 'client_id': clientId,
205 'grant_type': 'authorization_code',
206 'redirect_uri': redirect_uri
210 url : window.swaggerUi.tokenUrl,
213 success:function(data, textStatus, jqXHR)
215 onOAuthComplete(data);
217 error: function(jqXHR, textStatus, errorThrown)
224 function onOAuthComplete(token) {
227 var checkbox = $('input[type=checkbox],.secured')
228 checkbox.each(function(pos){
229 checkbox[pos].checked = false;
234 var b = token[window.swaggerUi.tokenName];
236 // if all roles are satisfied
238 $.each($('.auth #api_information_panel'), function(k, v) {
240 if(children && children.childNodes) {
241 var requiredScopes = [];
242 $.each((children.childNodes), function (k1, v1){
243 var inner = v1.innerHTML;
245 requiredScopes.push(inner);
248 for(var i=0; i < requiredScopes.length; i++) {
249 var s = requiredScopes[i];
250 if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
256 $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
257 $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
259 // sorry, not all scopes are satisfied
260 $(o).find('.api-ic').addClass('ic-warning');
261 $(o).find('.api-ic').removeClass('ic-error');
265 $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
266 $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
268 // all scopes are satisfied
269 $(o).find('.api-ic').addClass('ic-info');
270 $(o).find('.api-ic').removeClass('ic-warning');
271 $(o).find('.api-ic').removeClass('ic-error');
275 window.authorizations.add(oauth2KeyName, new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));