2 * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
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.
16 * Author: Zhaoxing Meng
17 * email: meng.zhaoxing1@zte.com.cn
27 function handleLogin() {
30 var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
36 if(auth.type === 'oauth2' && auth.scopes) {
39 if(Array.isArray(auth.scopes)) {
42 for(i = 0; i < auth.scopes.length; i++) {
43 scopes.push(auth.scopes[i]);
48 for(scope in auth.scopes) {
49 scopes.push({scope: scope, description: auth.scopes[scope]});
56 if(window.swaggerUi.api
57 && window.swaggerUi.api.info) {
58 appName = window.swaggerUi.api.info.title;
63 '<div class="api-popup-dialog">',
64 '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
65 '<div class="api-popup-content">',
66 '<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.',
67 '<a href="#">Learn how to use</a>',
69 '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
70 '<ul class="api-popup-scopes">',
72 '<p class="error-msg"></p>',
73 '<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>',
76 $(document.body).append(popupDialog);
78 popup = popupDialog.find('ul.api-popup-scopes').empty();
79 for (i = 0; i < scopes.length; i ++) {
81 str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
82 if (scope.description) {
83 str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
85 str += '</label></li>';
92 st = $win.scrollTop(),
93 dlgWd = popupDialog.outerWidth(),
94 dlgHt = popupDialog.outerHeight(),
95 top = (dh -dlgHt)/2 + st,
96 left = (dw - dlgWd)/2;
99 top: (top < 0? 0 : top) + 'px',
100 left: (left < 0? 0 : left) + 'px'
103 popupDialog.find('button.api-popup-cancel').click(function() {
110 $('button.api-popup-authbtn').unbind();
111 popupDialog.find('button.api-popup-authbtn').click(function() {
115 var authSchemes = window.swaggerUi.api.authSchemes;
116 var host = window.location;
117 var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
118 var redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
121 for (var key in authSchemes) {
122 if (authSchemes.hasOwnProperty(key)) {
123 var flow = authSchemes[key].flow;
125 if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
126 var dets = authSchemes[key];
127 url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
128 window.swaggerUi.tokenName = dets.tokenName || 'access_token';
129 window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
131 else if(authSchemes[key].grantTypes) {
133 var o = authSchemes[key].grantTypes;
135 if(o.hasOwnProperty(t) && t === 'implicit') {
137 var ep = dets.loginEndpoint.url;
138 url = dets.loginEndpoint.url + '?response_type=token';
139 window.swaggerUi.tokenName = dets.tokenName;
141 else if (o.hasOwnProperty(t) && t === 'accessCode') {
143 var ep = dets.tokenRequestEndpoint.url;
144 url = dets.tokenRequestEndpoint.url + '?response_type=code';
145 window.swaggerUi.tokenName = dets.tokenName;
152 var o = $('.api-popup-scopes').find('input:checked');
154 for(k =0; k < o.length; k++) {
155 var scope = $(o[k]).attr('scope');
157 if (scopes.indexOf(scope) === -1)
161 window.enabledScopes=scopes;
163 redirect_uri = redirectUrl;
165 url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
166 url += '&realm=' + encodeURIComponent(realm);
167 url += '&client_id=' + encodeURIComponent(clientId);
168 url += '&scope=' + encodeURIComponent(scopes);
179 function handleLogout() {
180 for(key in window.authorizations.authz){
181 window.authorizations.remove(key)
183 window.enabledScopes = null;
184 $('.api-ic.ic-on').addClass('ic-off');
185 $('.api-ic.ic-on').removeClass('ic-on');
188 $('.api-ic.ic-warning').addClass('ic-error');
189 $('.api-ic.ic-warning').removeClass('ic-warning');
192 function initOAuth(opts) {
196 appName = (o.appName||errors.push('missing appName'));
197 popupMask = (o.popupMask||$('#api-common-mask'));
198 popupDialog = (o.popupDialog||$('.api-popup-dialog'));
199 clientId = (o.clientId||errors.push('missing client id'));
200 realm = (o.realm||errors.push('missing realm'));
202 if(errors.length > 0){
203 log('auth unable initialize oauth: ' + errors);
207 $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
208 $('.api-ic').unbind();
209 $('.api-ic').click(function(s) {
210 if($(s.target).hasClass('ic-off'))
219 function processOAuthCode(data) {
221 'client_id': clientId,
223 'grant_type': 'authorization_code',
224 'redirect_uri': redirect_uri
228 url : window.swaggerUi.tokenUrl,
231 success:function(data, textStatus, jqXHR)
233 onOAuthComplete(data);
235 error: function(jqXHR, textStatus, errorThrown)
242 function onOAuthComplete(token) {
245 var checkbox = $('input[type=checkbox],.secured')
246 checkbox.each(function(pos){
247 checkbox[pos].checked = false;
252 var b = token[window.swaggerUi.tokenName];
254 // if all roles are satisfied
256 $.each($('.auth #api_information_panel'), function(k, v) {
258 if(children && children.childNodes) {
259 var requiredScopes = [];
260 $.each((children.childNodes), function (k1, v1){
261 var inner = v1.innerHTML;
263 requiredScopes.push(inner);
266 for(var i=0; i < requiredScopes.length; i++) {
267 var s = requiredScopes[i];
268 if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
274 $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
275 $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
277 // sorry, not all scopes are satisfied
278 $(o).find('.api-ic').addClass('ic-warning');
279 $(o).find('.api-ic').removeClass('ic-error');
283 $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
284 $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
286 // all scopes are satisfied
287 $(o).find('.api-ic').addClass('ic-info');
288 $(o).find('.api-ic').removeClass('ic-warning');
289 $(o).find('.api-ic').removeClass('ic-error');
293 window.authorizations.add(oauth2KeyName, new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));