9 var additionalQueryStringParams;
11 function handleLogin() {
14 var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
20 if(auth.type === 'oauth2' && auth.scopes) {
22 if(Array.isArray(auth.scopes)) {
25 for(i = 0; i < auth.scopes.length; i++) {
26 scopes.push(auth.scopes[i]);
31 for(scope in auth.scopes) {
32 scopes.push({scope: scope, description: auth.scopes[scope], OAuthSchemeKey: key});
39 if(window.swaggerUi.api
40 && window.swaggerUi.api.info) {
41 appName = window.swaggerUi.api.info.title;
44 $('.api-popup-dialog').remove();
47 '<div class="api-popup-dialog">',
48 '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
49 '<div class="api-popup-content">',
50 '<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.',
51 '<a href="#">Learn how to use</a>',
53 '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
54 '<ul class="api-popup-scopes">',
56 '<p class="error-msg"></p>',
57 '<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>',
60 $(document.body).append(popupDialog);
62 //TODO: only display applicable scopes (will need to pass them into handleLogin)
63 popup = popupDialog.find('ul.api-popup-scopes').empty();
64 for (i = 0; i < scopes.length; i ++) {
66 str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"' +'" oauthtype="' + scope.OAuthSchemeKey +'"/>' + '<label for="scope_' + i + '">' + scope.scope ;
67 if (scope.description) {
68 if ($.map(auths, function(n, i) { return i; }).length > 1) //if we have more than one scheme, display schemes
69 str += '<br/><span class="api-scope-desc">' + scope.description + ' ('+ scope.OAuthSchemeKey+')' +'</span>';
71 str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
73 str += '</label></li>';
80 st = $win.scrollTop(),
81 dlgWd = popupDialog.outerWidth(),
82 dlgHt = popupDialog.outerHeight(),
83 top = (dh -dlgHt)/2 + st,
84 left = (dw - dlgWd)/2;
87 top: (top < 0? 0 : top) + 'px',
88 left: (left < 0? 0 : left) + 'px'
91 popupDialog.find('button.api-popup-cancel').click(function() {
98 $('button.api-popup-authbtn').unbind();
99 popupDialog.find('button.api-popup-authbtn').click(function() {
103 var authSchemes = window.swaggerUi.api.authSchemes;
104 var host = window.location;
105 var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
106 var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
107 var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl;
110 var o = popup.find('input:checked');
111 var OAuthSchemeKeys = [];
113 for(k =0; k < o.length; k++) {
114 var scope = $(o[k]).attr('scope');
115 if (scopes.indexOf(scope) === -1)
117 var OAuthSchemeKey = $(o[k]).attr('oauthtype');
118 if (OAuthSchemeKeys.indexOf(OAuthSchemeKey) === -1)
119 OAuthSchemeKeys.push(OAuthSchemeKey);
122 //TODO: merge not replace if scheme is different from any existing
123 //(needs to be aware of schemes to do so correctly)
124 window.enabledScopes=scopes;
126 for (var key in authSchemes) {
127 if (authSchemes.hasOwnProperty(key) && OAuthSchemeKeys.indexOf(key) != -1) { //only look at keys that match this scope.
128 var flow = authSchemes[key].flow;
130 if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
131 var dets = authSchemes[key];
132 url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
133 window.swaggerUi.tokenName = dets.tokenName || 'access_token';
134 window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
137 else if(authSchemes[key].type === 'oauth2' && flow && (flow === 'application')) {
138 var dets = authSchemes[key];
139 window.swaggerUi.tokenName = dets.tokenName || 'access_token';
140 clientCredentialsFlow(scopes, dets.tokenUrl, key);
143 else if(authSchemes[key].grantTypes) {
145 var o = authSchemes[key].grantTypes;
147 if(o.hasOwnProperty(t) && t === 'implicit') {
149 var ep = dets.loginEndpoint.url;
150 url = dets.loginEndpoint.url + '?response_type=token';
151 window.swaggerUi.tokenName = dets.tokenName;
153 else if (o.hasOwnProperty(t) && t === 'accessCode') {
155 var ep = dets.tokenRequestEndpoint.url;
156 url = dets.tokenRequestEndpoint.url + '?response_type=code';
157 window.swaggerUi.tokenName = dets.tokenName;
164 redirect_uri = redirectUrl;
166 url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
167 url += '&realm=' + encodeURIComponent(realm);
168 url += '&client_id=' + encodeURIComponent(clientId);
169 url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator));
170 url += '&state=' + encodeURIComponent(state);
171 for (var key in additionalQueryStringParams) {
172 url += '&' + key + '=' + encodeURIComponent(additionalQueryStringParams[key]);
184 function handleLogout() {
185 for(key in window.swaggerUi.api.clientAuthorizations.authz){
186 window.swaggerUi.api.clientAuthorizations.remove(key)
188 window.enabledScopes = null;
189 $('.api-ic.ic-on').addClass('ic-off');
190 $('.api-ic.ic-on').removeClass('ic-on');
193 $('.api-ic.ic-warning').addClass('ic-error');
194 $('.api-ic.ic-warning').removeClass('ic-warning');
197 function initOAuth(opts) {
201 appName = (o.appName||errors.push('missing appName'));
202 popupMask = (o.popupMask||$('#api-common-mask'));
203 popupDialog = (o.popupDialog||$('.api-popup-dialog'));
204 clientId = (o.clientId||errors.push('missing client id'));
205 clientSecret = (o.clientSecret||null);
206 realm = (o.realm||errors.push('missing realm'));
207 scopeSeparator = (o.scopeSeparator||' ');
208 additionalQueryStringParams = (o.additionalQueryStringParams||{});
210 if(errors.length > 0){
211 log('auth unable initialize oauth: ' + errors);
215 $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
216 $('.api-ic').unbind();
217 $('.api-ic').click(function(s) {
218 if($(s.target).hasClass('ic-off'))
227 function clientCredentialsFlow(scopes, tokenUrl, OAuthSchemeKey) {
229 'client_id': clientId,
230 'client_secret': clientSecret,
231 'scope': scopes.join(' '),
232 'grant_type': 'client_credentials'
239 success:function(data, textStatus, jqXHR)
241 onOAuthComplete(data,OAuthSchemeKey);
243 error: function(jqXHR, textStatus, errorThrown)
251 window.processOAuthCode = function processOAuthCode(data) {
252 var OAuthSchemeKey = data.state;
254 'client_id': clientId,
256 'grant_type': 'authorization_code',
257 'redirect_uri': redirect_uri
261 params.client_secret = clientSecret;
266 url : window.swaggerUi.tokenUrl,
269 success:function(data, textStatus, jqXHR)
271 onOAuthComplete(data, OAuthSchemeKey);
273 error: function(jqXHR, textStatus, errorThrown)
280 window.onOAuthComplete = function onOAuthComplete(token,OAuthSchemeKey) {
283 var checkbox = $('input[type=checkbox],.secured')
284 checkbox.each(function(pos){
285 checkbox[pos].checked = false;
290 var b = token[window.swaggerUi.tokenName];
291 if (!OAuthSchemeKey){
292 OAuthSchemeKey = token.state;
295 // if all roles are satisfied
297 $.each($('.auth .api-ic .api_information_panel'), function(k, v) {
299 if(children && children.childNodes) {
300 var requiredScopes = [];
301 $.each((children.childNodes), function (k1, v1){
302 var inner = v1.innerHTML;
304 requiredScopes.push(inner);
307 for(var i=0; i < requiredScopes.length; i++) {
308 var s = requiredScopes[i];
309 if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
314 o = v.parentNode.parentNode;
315 $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
316 $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
318 // sorry, not all scopes are satisfied
319 $(o).find('.api-ic').addClass('ic-warning');
320 $(o).find('.api-ic').removeClass('ic-error');
323 o = v.parentNode.parentNode;
324 $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
325 $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
327 // all scopes are satisfied
328 $(o).find('.api-ic').addClass('ic-info');
329 $(o).find('.api-ic').removeClass('ic-warning');
330 $(o).find('.api-ic').removeClass('ic-error');
334 window.swaggerUi.api.clientAuthorizations.add(window.OAuthSchemeKey, new SwaggerClient.ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));
335 window.swaggerUi.load();