11 function handleLogin() {
14 var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
20 if(auth.type === 'oauth2' && auth.scopes) {
23 if(Array.isArray(auth.scopes)) {
26 for(i = 0; i < auth.scopes.length; i++) {
27 scopes.push(auth.scopes[i]);
32 for(scope in auth.scopes) {
33 scopes.push({scope: scope, description: auth.scopes[scope]});
40 if(window.swaggerUi.api
41 && window.swaggerUi.api.info) {
42 appName = window.swaggerUi.api.info.title;
45 $('.api-popup-dialog').remove();
48 '<div class="api-popup-dialog">',
49 '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
50 '<div class="api-popup-content">',
51 '<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.',
52 '<a href="#">Learn how to use</a>',
54 '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
55 '<ul class="api-popup-scopes">',
57 '<p class="error-msg"></p>',
58 '<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>',
61 $(document.body).append(popupDialog);
63 popup = popupDialog.find('ul.api-popup-scopes').empty();
64 for (i = 0; i < scopes.length; i ++) {
66 str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
67 if (scope.description) {
68 str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
70 str += '</label></li>';
77 st = $win.scrollTop(),
78 dlgWd = popupDialog.outerWidth(),
79 dlgHt = popupDialog.outerHeight(),
80 top = (dh -dlgHt)/2 + st,
81 left = (dw - dlgWd)/2;
84 top: (top < 0? 0 : top) + 'px',
85 left: (left < 0? 0 : left) + 'px'
88 popupDialog.find('button.api-popup-cancel').click(function() {
95 $('button.api-popup-authbtn').unbind();
96 popupDialog.find('button.api-popup-authbtn').click(function() {
100 var authSchemes = window.swaggerUi.api.authSchemes;
101 var host = window.location;
102 var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
103 var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
104 var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl;
107 for (var key in authSchemes) {
108 if (authSchemes.hasOwnProperty(key)) {
109 var flow = authSchemes[key].flow;
111 if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
112 var dets = authSchemes[key];
113 url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
114 window.swaggerUi.tokenName = dets.tokenName || 'access_token';
115 window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
117 else if(authSchemes[key].grantTypes) {
119 var o = authSchemes[key].grantTypes;
121 if(o.hasOwnProperty(t) && t === 'implicit') {
123 var ep = dets.loginEndpoint.url;
124 url = dets.loginEndpoint.url + '?response_type=token';
125 window.swaggerUi.tokenName = dets.tokenName;
127 else if (o.hasOwnProperty(t) && t === 'accessCode') {
129 var ep = dets.tokenRequestEndpoint.url;
130 url = dets.tokenRequestEndpoint.url + '?response_type=code';
131 window.swaggerUi.tokenName = dets.tokenName;
138 var o = $('.api-popup-scopes').find('input:checked');
140 for(k =0; k < o.length; k++) {
141 var scope = $(o[k]).attr('scope');
143 if (scopes.indexOf(scope) === -1)
147 // Implicit auth recommends a state parameter.
148 var state = Math.random ();
150 window.enabledScopes=scopes;
152 redirect_uri = redirectUrl;
154 url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
155 url += '&realm=' + encodeURIComponent(realm);
156 url += '&client_id=' + encodeURIComponent(clientId);
157 url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator));
158 url += '&state=' + encodeURIComponent(state);
169 function handleLogout() {
170 for(key in window.swaggerUi.api.clientAuthorizations.authz){
171 window.swaggerUi.api.clientAuthorizations.remove(key)
173 window.enabledScopes = null;
174 $('.api-ic.ic-on').addClass('ic-off');
175 $('.api-ic.ic-on').removeClass('ic-on');
178 $('.api-ic.ic-warning').addClass('ic-error');
179 $('.api-ic.ic-warning').removeClass('ic-warning');
182 function initOAuth(opts) {
186 appName = (o.appName||errors.push('missing appName'));
187 popupMask = (o.popupMask||$('#api-common-mask'));
188 popupDialog = (o.popupDialog||$('.api-popup-dialog'));
189 clientId = (o.clientId||errors.push('missing client id'));
190 clientSecret = (o.clientSecret||errors.push('missing client secret'));
191 realm = (o.realm||errors.push('missing realm'));
192 scopeSeparator = (o.scopeSeparator||' ');
194 if(errors.length > 0){
195 log('auth unable initialize oauth: ' + errors);
199 $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
200 $('.api-ic').unbind();
201 $('.api-ic').click(function(s) {
202 if($(s.target).hasClass('ic-off'))
211 window.processOAuthCode = function processOAuthCode(data) {
213 'client_id': clientId,
214 'client_secret': clientSecret,
216 'grant_type': 'authorization_code',
217 'redirect_uri': redirect_uri
221 url : window.swaggerUi.tokenUrl,
224 success:function(data, textStatus, jqXHR)
226 onOAuthComplete(data);
228 error: function(jqXHR, textStatus, errorThrown)
235 window.onOAuthComplete = function onOAuthComplete(token) {
238 var checkbox = $('input[type=checkbox],.secured')
239 checkbox.each(function(pos){
240 checkbox[pos].checked = false;
245 var b = token[window.swaggerUi.tokenName];
247 // if all roles are satisfied
249 $.each($('.auth .api-ic .api_information_panel'), function(k, v) {
251 if(children && children.childNodes) {
252 var requiredScopes = [];
253 $.each((children.childNodes), function (k1, v1){
254 var inner = v1.innerHTML;
256 requiredScopes.push(inner);
259 for(var i=0; i < requiredScopes.length; i++) {
260 var s = requiredScopes[i];
261 if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
266 o = v.parentNode.parentNode;
267 $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
268 $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
270 // sorry, not all scopes are satisfied
271 $(o).find('.api-ic').addClass('ic-warning');
272 $(o).find('.api-ic').removeClass('ic-error');
275 o = v.parentNode.parentNode;
276 $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
277 $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
279 // all scopes are satisfied
280 $(o).find('.api-ic').addClass('ic-info');
281 $(o).find('.api-ic').removeClass('ic-warning');
282 $(o).find('.api-ic').removeClass('ic-error');
286 window.swaggerUi.api.clientAuthorizations.add(oauth2KeyName, new SwaggerClient.ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));