7 function handleLogin() {
10 if(window.swaggerUi.api.authSchemes
11 && window.swaggerUi.api.authSchemes.oauth2
12 && window.swaggerUi.api.authSchemes.oauth2.scopes) {
13 scopes = window.swaggerUi.api.authSchemes.oauth2.scopes;
16 if(window.swaggerUi.api
17 && window.swaggerUi.api.info) {
18 appName = window.swaggerUi.api.info.title;
21 if(popupDialog.length > 0)
22 popupDialog = popupDialog.last();
26 '<div class="api-popup-dialog">',
27 '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
28 '<div class="api-popup-content">',
29 '<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.',
30 '<a href="#">Learn how to use</a>',
32 '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
33 '<ul class="api-popup-scopes">',
35 '<p class="error-msg"></p>',
36 '<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>',
39 $(document.body).append(popupDialog);
41 popup = popupDialog.find('ul.api-popup-scopes').empty();
42 for (i = 0; i < scopes.length; i ++) {
44 str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
45 if (scope.description) {
46 str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
48 str += '</label></li>';
56 st = $win.scrollTop(),
57 dlgWd = popupDialog.outerWidth(),
58 dlgHt = popupDialog.outerHeight(),
59 top = (dh -dlgHt)/2 + st,
60 left = (dw - dlgWd)/2;
63 top: (top < 0? 0 : top) + 'px',
64 left: (left < 0? 0 : left) + 'px'
67 popupDialog.find('button.api-popup-cancel').click(function() {
71 popupDialog.find('button.api-popup-authbtn').click(function() {
75 var authSchemes = window.swaggerUi.api.authSchemes;
76 var host = window.location;
77 var redirectUrl = "o2c.html";
80 var p = window.swaggerUi.api.authSchemes;
82 if (p.hasOwnProperty(key)) {
83 var o = p[key].grantTypes;
85 if(o.hasOwnProperty(t) && t === 'implicit') {
87 url = dets.loginEndpoint.url + "?response_type=token";
88 window.swaggerUi.tokenName = dets.tokenName;
94 var o = $('.api-popup-scopes').find('input:checked');
96 for(k =0; k < o.length; k++) {
97 scopes.push($(o[k]).attr("scope"));
100 window.enabledScopes=scopes;
102 url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
103 url += '&realm=' + encodeURIComponent(realm);
104 url += '&client_id=' + encodeURIComponent(clientId);
105 url += '&scope=' + encodeURIComponent(scopes);
116 function handleLogout() {
117 for(key in window.authorizations.authz){
118 window.authorizations.remove(key)
120 window.enabledScopes = null;
121 $('.api-ic.ic-on').addClass('ic-off');
122 $('.api-ic.ic-on').removeClass('ic-on');
125 $('.api-ic.ic-warning').addClass('ic-error');
126 $('.api-ic.ic-warning').removeClass('ic-warning');
129 function initOAuth(opts) {
133 appName = (o.appName||errors.push("missing appName"));
134 popupMask = (o.popupMask||$('#api-common-mask'));
135 popupDialog = (o.popupDialog||$('.api-popup-dialog'));
136 clientId = (o.clientId||errors.push("missing client id"));
137 realm = (o.realm||errors.push("missing realm"));
139 if(errors.length > 0){
140 log("auth unable initialize oauth: " + errors);
144 $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
145 $('.api-ic').click(function(s) {
146 if($(s.target).hasClass('ic-off'))
155 function onOAuthComplete(token) {
158 var checkbox = $('input[type=checkbox],.secured')
159 checkbox.each(function(pos){
160 checkbox[pos].checked = false;
165 var b = token[window.swaggerUi.tokenName];
167 // if all roles are satisfied
169 $.each($('.auth #api_information_panel'), function(k, v) {
171 if(children && children.childNodes) {
172 var requiredScopes = [];
173 $.each((children.childNodes), function (k1, v1){
174 var inner = v1.innerHTML;
176 requiredScopes.push(inner);
179 for(var i=0; i < requiredScopes.length; i++) {
180 var s = requiredScopes[i];
181 if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
187 $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
188 $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
190 // sorry, not all scopes are satisfied
191 $(o).find('.api-ic').addClass('ic-warning');
192 $(o).find('.api-ic').removeClass('ic-error');
196 $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
197 $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
199 // all scopes are satisfied
200 $(o).find('.api-ic').addClass('ic-info');
201 $(o).find('.api-ic').removeClass('ic-warning');
202 $(o).find('.api-ic').removeClass('ic-error');
207 window.authorizations.add("key", new ApiKeyAuthorization("Authorization", "Bearer " + b, "header"));