2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
31 function handleLogin() {
34 var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
40 if(auth.type === 'oauth2' && auth.scopes) {
43 if(Array.isArray(auth.scopes)) {
46 for(i = 0; i < auth.scopes.length; i++) {
47 scopes.push(auth.scopes[i]);
52 for(scope in auth.scopes) {
53 scopes.push({scope: scope, description: auth.scopes[scope]});
60 if(window.swaggerUi.api
61 && window.swaggerUi.api.info) {
62 appName = window.swaggerUi.api.info.title;
65 $('.api-popup-dialog').remove();
68 '<div class="api-popup-dialog">',
69 '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
70 '<div class="api-popup-content">',
71 '<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.',
72 '<a href="#">Learn how to use</a>',
74 '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
75 '<ul class="api-popup-scopes">',
77 '<p class="error-msg"></p>',
78 '<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>',
81 $(document.body).append(popupDialog);
83 popup = popupDialog.find('ul.api-popup-scopes').empty();
84 for (i = 0; i < scopes.length; i ++) {
86 str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
87 if (scope.description) {
88 str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
90 str += '</label></li>';
97 st = $win.scrollTop(),
98 dlgWd = popupDialog.outerWidth(),
99 dlgHt = popupDialog.outerHeight(),
100 top = (dh -dlgHt)/2 + st,
101 left = (dw - dlgWd)/2;
104 top: (top < 0? 0 : top) + 'px',
105 left: (left < 0? 0 : left) + 'px'
108 popupDialog.find('button.api-popup-cancel').click(function() {
115 $('button.api-popup-authbtn').unbind();
116 popupDialog.find('button.api-popup-authbtn').click(function() {
120 var authSchemes = window.swaggerUi.api.authSchemes;
121 var host = window.location;
122 var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
123 var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
124 var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl;
127 for (var key in authSchemes) {
128 if (authSchemes.hasOwnProperty(key)) {
129 var flow = authSchemes[key].flow;
131 if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
132 var dets = authSchemes[key];
133 url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
134 window.swaggerUi.tokenName = dets.tokenName || 'access_token';
135 window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
137 else if(authSchemes[key].grantTypes) {
139 var o = authSchemes[key].grantTypes;
141 if(o.hasOwnProperty(t) && t === 'implicit') {
143 var ep = dets.loginEndpoint.url;
144 url = dets.loginEndpoint.url + '?response_type=token';
145 window.swaggerUi.tokenName = dets.tokenName;
147 else if (o.hasOwnProperty(t) && t === 'accessCode') {
149 var ep = dets.tokenRequestEndpoint.url;
150 url = dets.tokenRequestEndpoint.url + '?response_type=code';
151 window.swaggerUi.tokenName = dets.tokenName;
158 var o = $('.api-popup-scopes').find('input:checked');
160 for(k =0; k < o.length; k++) {
161 var scope = $(o[k]).attr('scope');
163 if (scopes.indexOf(scope) === -1)
167 // Implicit auth recommends a state parameter.
168 var state = Math.random ();
170 window.enabledScopes=scopes;
172 redirect_uri = redirectUrl;
174 url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
175 url += '&realm=' + encodeURIComponent(realm);
176 url += '&client_id=' + encodeURIComponent(clientId);
177 url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator));
178 url += '&state=' + encodeURIComponent(state);
189 function handleLogout() {
190 for(key in window.swaggerUi.api.clientAuthorizations.authz){
191 window.swaggerUi.api.clientAuthorizations.remove(key)
193 window.enabledScopes = null;
194 $('.api-ic.ic-on').addClass('ic-off');
195 $('.api-ic.ic-on').removeClass('ic-on');
198 $('.api-ic.ic-warning').addClass('ic-error');
199 $('.api-ic.ic-warning').removeClass('ic-warning');
202 function initOAuth(opts) {
206 appName = (o.appName||errors.push('missing appName'));
207 popupMask = (o.popupMask||$('#api-common-mask'));
208 popupDialog = (o.popupDialog||$('.api-popup-dialog'));
209 clientId = (o.clientId||errors.push('missing client id'));
210 clientSecret = (o.clientSecret||errors.push('missing client secret'));
211 realm = (o.realm||errors.push('missing realm'));
212 scopeSeparator = (o.scopeSeparator||' ');
214 if(errors.length > 0){
215 log('auth unable initialize oauth: ' + errors);
219 $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
220 $('.api-ic').unbind();
221 $('.api-ic').click(function(s) {
222 if($(s.target).hasClass('ic-off'))
231 window.processOAuthCode = function processOAuthCode(data) {
233 'client_id': clientId,
234 'client_secret': clientSecret,
236 'grant_type': 'authorization_code',
237 'redirect_uri': redirect_uri
241 url : window.swaggerUi.tokenUrl,
244 success:function(data, textStatus, jqXHR)
246 onOAuthComplete(data);
248 error: function(jqXHR, textStatus, errorThrown)
255 window.onOAuthComplete = function onOAuthComplete(token) {
258 var checkbox = $('input[type=checkbox],.secured')
259 checkbox.each(function(pos){
260 checkbox[pos].checked = false;
265 var b = token[window.swaggerUi.tokenName];
267 // if all roles are satisfied
269 $.each($('.auth .api-ic .api_information_panel'), function(k, v) {
271 if(children && children.childNodes) {
272 var requiredScopes = [];
273 $.each((children.childNodes), function (k1, v1){
274 var inner = v1.innerHTML;
276 requiredScopes.push(inner);
279 for(var i=0; i < requiredScopes.length; i++) {
280 var s = requiredScopes[i];
281 if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
286 o = v.parentNode.parentNode;
287 $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
288 $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
290 // sorry, not all scopes are satisfied
291 $(o).find('.api-ic').addClass('ic-warning');
292 $(o).find('.api-ic').removeClass('ic-error');
295 o = v.parentNode.parentNode;
296 $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
297 $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
299 // all scopes are satisfied
300 $(o).find('.api-ic').addClass('ic-info');
301 $(o).find('.api-ic').removeClass('ic-warning');
302 $(o).find('.api-ic').removeClass('ic-error');
306 window.swaggerUi.api.clientAuthorizations.add(oauth2KeyName, new SwaggerClient.ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));