2 * Copyright 2016 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 function handleLogin() {
28 var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
34 if(auth.type === 'oauth2' && auth.scopes) {
37 if(Array.isArray(auth.scopes)) {
40 for(i = 0; i < auth.scopes.length; i++) {
41 scopes.push(auth.scopes[i]);
46 for(scope in auth.scopes) {
47 scopes.push({scope: scope, description: auth.scopes[scope]});
54 if(window.swaggerUi.api
55 && window.swaggerUi.api.info) {
56 appName = window.swaggerUi.api.info.title;
61 '<div class="api-popup-dialog">',
62 '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
63 '<div class="api-popup-content">',
64 '<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.',
65 '<a href="#">Learn how to use</a>',
67 '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
68 '<ul class="api-popup-scopes">',
70 '<p class="error-msg"></p>',
71 '<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>',
74 $(document.body).append(popupDialog);
76 popup = popupDialog.find('ul.api-popup-scopes').empty();
77 for (i = 0; i < scopes.length; i ++) {
79 str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
80 if (scope.description) {
81 str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
83 str += '</label></li>';
90 st = $win.scrollTop(),
91 dlgWd = popupDialog.outerWidth(),
92 dlgHt = popupDialog.outerHeight(),
93 top = (dh -dlgHt)/2 + st,
94 left = (dw - dlgWd)/2;
97 top: (top < 0? 0 : top) + 'px',
98 left: (left < 0? 0 : left) + 'px'
101 popupDialog.find('button.api-popup-cancel').click(function() {
108 $('button.api-popup-authbtn').unbind();
109 popupDialog.find('button.api-popup-authbtn').click(function() {
113 var authSchemes = window.swaggerUi.api.authSchemes;
114 var host = window.location;
115 var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
116 var redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
119 for (var key in authSchemes) {
120 if (authSchemes.hasOwnProperty(key)) {
121 var flow = authSchemes[key].flow;
123 if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
124 var dets = authSchemes[key];
125 url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
126 window.swaggerUi.tokenName = dets.tokenName || 'access_token';
127 window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
129 else if(authSchemes[key].grantTypes) {
131 var o = authSchemes[key].grantTypes;
133 if(o.hasOwnProperty(t) && t === 'implicit') {
135 var ep = dets.loginEndpoint.url;
136 url = dets.loginEndpoint.url + '?response_type=token';
137 window.swaggerUi.tokenName = dets.tokenName;
139 else if (o.hasOwnProperty(t) && t === 'accessCode') {
141 var ep = dets.tokenRequestEndpoint.url;
142 url = dets.tokenRequestEndpoint.url + '?response_type=code';
143 window.swaggerUi.tokenName = dets.tokenName;
150 var o = $('.api-popup-scopes').find('input:checked');
152 for(k =0; k < o.length; k++) {
153 var scope = $(o[k]).attr('scope');
155 if (scopes.indexOf(scope) === -1)
159 window.enabledScopes=scopes;
161 redirect_uri = redirectUrl;
163 url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
164 url += '&realm=' + encodeURIComponent(realm);
165 url += '&client_id=' + encodeURIComponent(clientId);
166 url += '&scope=' + encodeURIComponent(scopes);
177 function handleLogout() {
178 for(key in window.authorizations.authz){
179 window.authorizations.remove(key)
181 window.enabledScopes = null;
182 $('.api-ic.ic-on').addClass('ic-off');
183 $('.api-ic.ic-on').removeClass('ic-on');
186 $('.api-ic.ic-warning').addClass('ic-error');
187 $('.api-ic.ic-warning').removeClass('ic-warning');
190 function initOAuth(opts) {
194 appName = (o.appName||errors.push('missing appName'));
195 popupMask = (o.popupMask||$('#api-common-mask'));
196 popupDialog = (o.popupDialog||$('.api-popup-dialog'));
197 clientId = (o.clientId||errors.push('missing client id'));
198 realm = (o.realm||errors.push('missing realm'));
200 if(errors.length > 0){
201 log('auth unable initialize oauth: ' + errors);
205 $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
206 $('.api-ic').unbind();
207 $('.api-ic').click(function(s) {
208 if($(s.target).hasClass('ic-off'))
217 function processOAuthCode(data) {
219 'client_id': clientId,
221 'grant_type': 'authorization_code',
222 'redirect_uri': redirect_uri
226 url : window.swaggerUi.tokenUrl,
229 success:function(data, textStatus, jqXHR)
231 onOAuthComplete(data);
233 error: function(jqXHR, textStatus, errorThrown)
240 function onOAuthComplete(token) {
243 var checkbox = $('input[type=checkbox],.secured')
244 checkbox.each(function(pos){
245 checkbox[pos].checked = false;
250 var b = token[window.swaggerUi.tokenName];
252 // if all roles are satisfied
254 $.each($('.auth #api_information_panel'), function(k, v) {
256 if(children && children.childNodes) {
257 var requiredScopes = [];
258 $.each((children.childNodes), function (k1, v1){
259 var inner = v1.innerHTML;
261 requiredScopes.push(inner);
264 for(var i=0; i < requiredScopes.length; i++) {
265 var s = requiredScopes[i];
266 if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
272 $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
273 $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
275 // sorry, not all scopes are satisfied
276 $(o).find('.api-ic').addClass('ic-warning');
277 $(o).find('.api-ic').removeClass('ic-error');
281 $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
282 $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
284 // all scopes are satisfied
285 $(o).find('.api-ic').addClass('ic-info');
286 $(o).find('.api-ic').removeClass('ic-warning');
287 $(o).find('.api-ic').removeClass('ic-error');
291 window.authorizations.add(oauth2KeyName, new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));