2 * Copyright 2016 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
24 function handleLogin() {
\r
27 var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
\r
32 var auth = defs[key];
\r
33 if(auth.type === 'oauth2' && auth.scopes) {
\r
34 oauth2KeyName = key;
\r
36 if(Array.isArray(auth.scopes)) {
\r
39 for(i = 0; i < auth.scopes.length; i++) {
\r
40 scopes.push(auth.scopes[i]);
\r
45 for(scope in auth.scopes) {
\r
46 scopes.push({scope: scope, description: auth.scopes[scope]});
\r
53 if(window.swaggerUi.api
\r
54 && window.swaggerUi.api.info) {
\r
55 appName = window.swaggerUi.api.info.title;
\r
60 '<div class="api-popup-dialog">',
\r
61 '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
\r
62 '<div class="api-popup-content">',
\r
63 '<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.',
\r
64 '<a href="#">Learn how to use</a>',
\r
66 '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
\r
67 '<ul class="api-popup-scopes">',
\r
69 '<p class="error-msg"></p>',
\r
70 '<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>',
\r
72 '</div>'].join(''));
\r
73 $(document.body).append(popupDialog);
\r
75 popup = popupDialog.find('ul.api-popup-scopes').empty();
\r
76 for (i = 0; i < scopes.length; i ++) {
\r
78 str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
\r
79 if (scope.description) {
\r
80 str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
\r
82 str += '</label></li>';
\r
86 var $win = $(window),
\r
89 st = $win.scrollTop(),
\r
90 dlgWd = popupDialog.outerWidth(),
\r
91 dlgHt = popupDialog.outerHeight(),
\r
92 top = (dh -dlgHt)/2 + st,
\r
93 left = (dw - dlgWd)/2;
\r
96 top: (top < 0? 0 : top) + 'px',
\r
97 left: (left < 0? 0 : left) + 'px'
\r
100 popupDialog.find('button.api-popup-cancel').click(function() {
\r
102 popupDialog.hide();
\r
103 popupDialog.empty();
\r
107 $('button.api-popup-authbtn').unbind();
\r
108 popupDialog.find('button.api-popup-authbtn').click(function() {
\r
110 popupDialog.hide();
\r
112 var authSchemes = window.swaggerUi.api.authSchemes;
\r
113 var host = window.location;
\r
114 var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
\r
115 var redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
\r
118 for (var key in authSchemes) {
\r
119 if (authSchemes.hasOwnProperty(key)) {
\r
120 var flow = authSchemes[key].flow;
\r
122 if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
\r
123 var dets = authSchemes[key];
\r
124 url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
\r
125 window.swaggerUi.tokenName = dets.tokenName || 'access_token';
\r
126 window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
\r
128 else if(authSchemes[key].grantTypes) {
\r
130 var o = authSchemes[key].grantTypes;
\r
132 if(o.hasOwnProperty(t) && t === 'implicit') {
\r
134 var ep = dets.loginEndpoint.url;
\r
135 url = dets.loginEndpoint.url + '?response_type=token';
\r
136 window.swaggerUi.tokenName = dets.tokenName;
\r
138 else if (o.hasOwnProperty(t) && t === 'accessCode') {
\r
140 var ep = dets.tokenRequestEndpoint.url;
\r
141 url = dets.tokenRequestEndpoint.url + '?response_type=code';
\r
142 window.swaggerUi.tokenName = dets.tokenName;
\r
149 var o = $('.api-popup-scopes').find('input:checked');
\r
151 for(k =0; k < o.length; k++) {
\r
152 var scope = $(o[k]).attr('scope');
\r
154 if (scopes.indexOf(scope) === -1)
\r
155 scopes.push(scope);
\r
158 window.enabledScopes=scopes;
\r
160 redirect_uri = redirectUrl;
\r
162 url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
\r
163 url += '&realm=' + encodeURIComponent(realm);
\r
164 url += '&client_id=' + encodeURIComponent(clientId);
\r
165 url += '&scope=' + encodeURIComponent(scopes);
\r
171 popupDialog.show();
\r
176 function handleLogout() {
\r
177 for(key in window.authorizations.authz){
\r
178 window.authorizations.remove(key)
\r
180 window.enabledScopes = null;
\r
181 $('.api-ic.ic-on').addClass('ic-off');
\r
182 $('.api-ic.ic-on').removeClass('ic-on');
\r
184 // set the info box
\r
185 $('.api-ic.ic-warning').addClass('ic-error');
\r
186 $('.api-ic.ic-warning').removeClass('ic-warning');
\r
189 function initOAuth(opts) {
\r
190 var o = (opts||{});
\r
193 appName = (o.appName||errors.push('missing appName'));
\r
194 popupMask = (o.popupMask||$('#api-common-mask'));
\r
195 popupDialog = (o.popupDialog||$('.api-popup-dialog'));
\r
196 clientId = (o.clientId||errors.push('missing client id'));
\r
197 realm = (o.realm||errors.push('missing realm'));
\r
199 if(errors.length > 0){
\r
200 log('auth unable initialize oauth: ' + errors);
\r
204 $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
\r
205 $('.api-ic').unbind();
\r
206 $('.api-ic').click(function(s) {
\r
207 if($(s.target).hasClass('ic-off'))
\r
216 function processOAuthCode(data) {
\r
218 'client_id': clientId,
\r
220 'grant_type': 'authorization_code',
\r
221 'redirect_uri': redirect_uri
\r
225 url : window.swaggerUi.tokenUrl,
\r
228 success:function(data, textStatus, jqXHR)
\r
230 onOAuthComplete(data);
\r
232 error: function(jqXHR, textStatus, errorThrown)
\r
234 onOAuthComplete("");
\r
239 function onOAuthComplete(token) {
\r
242 var checkbox = $('input[type=checkbox],.secured')
\r
243 checkbox.each(function(pos){
\r
244 checkbox[pos].checked = false;
\r
246 alert(token.error);
\r
249 var b = token[window.swaggerUi.tokenName];
\r
251 // if all roles are satisfied
\r
253 $.each($('.auth #api_information_panel'), function(k, v) {
\r
255 if(children && children.childNodes) {
\r
256 var requiredScopes = [];
\r
257 $.each((children.childNodes), function (k1, v1){
\r
258 var inner = v1.innerHTML;
\r
260 requiredScopes.push(inner);
\r
263 for(var i=0; i < requiredScopes.length; i++) {
\r
264 var s = requiredScopes[i];
\r
265 if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
\r
269 if(diff.length > 0){
\r
271 $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
\r
272 $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
\r
274 // sorry, not all scopes are satisfied
\r
275 $(o).find('.api-ic').addClass('ic-warning');
\r
276 $(o).find('.api-ic').removeClass('ic-error');
\r
280 $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
\r
281 $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
\r
283 // all scopes are satisfied
\r
284 $(o).find('.api-ic').addClass('ic-info');
\r
285 $(o).find('.api-ic').removeClass('ic-warning');
\r
286 $(o).find('.api-ic').removeClass('ic-error');
\r
290 window.authorizations.add(oauth2KeyName, new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));
\r