--- /dev/null
+/*\r
+ * Copyright 2016 ZTE Corporation.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+var appName;\r
+var popupMask;\r
+var popupDialog;\r
+var clientId;\r
+var realm;\r
+var oauth2KeyName;\r
+var redirect_uri;\r
+\r
+function handleLogin() {\r
+ var scopes = [];\r
+\r
+ var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;\r
+ if(auths) {\r
+ var key;\r
+ var defs = auths;\r
+ for(key in defs) {\r
+ var auth = defs[key];\r
+ if(auth.type === 'oauth2' && auth.scopes) {\r
+ oauth2KeyName = key;\r
+ var scope;\r
+ if(Array.isArray(auth.scopes)) {\r
+ // 1.2 support\r
+ var i;\r
+ for(i = 0; i < auth.scopes.length; i++) {\r
+ scopes.push(auth.scopes[i]);\r
+ }\r
+ }\r
+ else {\r
+ // 2.0 support\r
+ for(scope in auth.scopes) {\r
+ scopes.push({scope: scope, description: auth.scopes[scope]});\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ if(window.swaggerUi.api\r
+ && window.swaggerUi.api.info) {\r
+ appName = window.swaggerUi.api.info.title;\r
+ }\r
+\r
+ popupDialog = $(\r
+ [\r
+ '<div class="api-popup-dialog">',\r
+ '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',\r
+ '<div class="api-popup-content">',\r
+ '<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
+ '<a href="#">Learn how to use</a>',\r
+ '</p>',\r
+ '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',\r
+ '<ul class="api-popup-scopes">',\r
+ '</ul>',\r
+ '<p class="error-msg"></p>',\r
+ '<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
+ '</div>',\r
+ '</div>'].join(''));\r
+ $(document.body).append(popupDialog);\r
+\r
+ popup = popupDialog.find('ul.api-popup-scopes').empty();\r
+ for (i = 0; i < scopes.length; i ++) {\r
+ scope = scopes[i];\r
+ str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;\r
+ if (scope.description) {\r
+ str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';\r
+ }\r
+ str += '</label></li>';\r
+ popup.append(str);\r
+ }\r
+\r
+ var $win = $(window),\r
+ dw = $win.width(),\r
+ dh = $win.height(),\r
+ st = $win.scrollTop(),\r
+ dlgWd = popupDialog.outerWidth(),\r
+ dlgHt = popupDialog.outerHeight(),\r
+ top = (dh -dlgHt)/2 + st,\r
+ left = (dw - dlgWd)/2;\r
+\r
+ popupDialog.css({\r
+ top: (top < 0? 0 : top) + 'px',\r
+ left: (left < 0? 0 : left) + 'px'\r
+ });\r
+\r
+ popupDialog.find('button.api-popup-cancel').click(function() {\r
+ popupMask.hide();\r
+ popupDialog.hide();\r
+ popupDialog.empty();\r
+ popupDialog = [];\r
+ });\r
+\r
+ $('button.api-popup-authbtn').unbind();\r
+ popupDialog.find('button.api-popup-authbtn').click(function() {\r
+ popupMask.hide();\r
+ popupDialog.hide();\r
+\r
+ var authSchemes = window.swaggerUi.api.authSchemes;\r
+ var host = window.location;\r
+ var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));\r
+ var redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';\r
+ var url = null;\r
+\r
+ for (var key in authSchemes) {\r
+ if (authSchemes.hasOwnProperty(key)) {\r
+ var flow = authSchemes[key].flow;\r
+ \r
+ if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {\r
+ var dets = authSchemes[key];\r
+ url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');\r
+ window.swaggerUi.tokenName = dets.tokenName || 'access_token';\r
+ window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null); \r
+ }\r
+ else if(authSchemes[key].grantTypes) {\r
+ // 1.2 support\r
+ var o = authSchemes[key].grantTypes;\r
+ for(var t in o) {\r
+ if(o.hasOwnProperty(t) && t === 'implicit') {\r
+ var dets = o[t];\r
+ var ep = dets.loginEndpoint.url;\r
+ url = dets.loginEndpoint.url + '?response_type=token';\r
+ window.swaggerUi.tokenName = dets.tokenName;\r
+ }\r
+ else if (o.hasOwnProperty(t) && t === 'accessCode') {\r
+ var dets = o[t];\r
+ var ep = dets.tokenRequestEndpoint.url;\r
+ url = dets.tokenRequestEndpoint.url + '?response_type=code';\r
+ window.swaggerUi.tokenName = dets.tokenName;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ var scopes = []\r
+ var o = $('.api-popup-scopes').find('input:checked');\r
+\r
+ for(k =0; k < o.length; k++) {\r
+ var scope = $(o[k]).attr('scope');\r
+ \r
+ if (scopes.indexOf(scope) === -1)\r
+ scopes.push(scope);\r
+ }\r
+\r
+ window.enabledScopes=scopes;\r
+\r
+ redirect_uri = redirectUrl;\r
+\r
+ url += '&redirect_uri=' + encodeURIComponent(redirectUrl);\r
+ url += '&realm=' + encodeURIComponent(realm);\r
+ url += '&client_id=' + encodeURIComponent(clientId);\r
+ url += '&scope=' + encodeURIComponent(scopes);\r
+\r
+ window.open(url);\r
+ });\r
+\r
+ popupMask.show();\r
+ popupDialog.show();\r
+ return;\r
+}\r
+\r
+\r
+function handleLogout() {\r
+ for(key in window.authorizations.authz){\r
+ window.authorizations.remove(key)\r
+ }\r
+ window.enabledScopes = null;\r
+ $('.api-ic.ic-on').addClass('ic-off');\r
+ $('.api-ic.ic-on').removeClass('ic-on');\r
+\r
+ // set the info box\r
+ $('.api-ic.ic-warning').addClass('ic-error');\r
+ $('.api-ic.ic-warning').removeClass('ic-warning');\r
+}\r
+\r
+function initOAuth(opts) {\r
+ var o = (opts||{});\r
+ var errors = [];\r
+\r
+ appName = (o.appName||errors.push('missing appName'));\r
+ popupMask = (o.popupMask||$('#api-common-mask'));\r
+ popupDialog = (o.popupDialog||$('.api-popup-dialog'));\r
+ clientId = (o.clientId||errors.push('missing client id'));\r
+ realm = (o.realm||errors.push('missing realm'));\r
+\r
+ if(errors.length > 0){\r
+ log('auth unable initialize oauth: ' + errors);\r
+ return;\r
+ }\r
+\r
+ $('pre code').each(function(i, e) {hljs.highlightBlock(e)});\r
+ $('.api-ic').unbind();\r
+ $('.api-ic').click(function(s) {\r
+ if($(s.target).hasClass('ic-off'))\r
+ handleLogin();\r
+ else {\r
+ handleLogout();\r
+ }\r
+ false;\r
+ });\r
+}\r
+\r
+function processOAuthCode(data) {\r
+ var params = {\r
+ 'client_id': clientId,\r
+ 'code': data.code,\r
+ 'grant_type': 'authorization_code',\r
+ 'redirect_uri': redirect_uri\r
+ }\r
+ $.ajax(\r
+ {\r
+ url : window.swaggerUi.tokenUrl,\r
+ type: "POST",\r
+ data: params,\r
+ success:function(data, textStatus, jqXHR) \r
+ {\r
+ onOAuthComplete(data);\r
+ },\r
+ error: function(jqXHR, textStatus, errorThrown) \r
+ {\r
+ onOAuthComplete("");\r
+ }\r
+ });\r
+}\r
+\r
+function onOAuthComplete(token) {\r
+ if(token) {\r
+ if(token.error) {\r
+ var checkbox = $('input[type=checkbox],.secured')\r
+ checkbox.each(function(pos){\r
+ checkbox[pos].checked = false;\r
+ });\r
+ alert(token.error);\r
+ }\r
+ else {\r
+ var b = token[window.swaggerUi.tokenName];\r
+ if(b){\r
+ // if all roles are satisfied\r
+ var o = null;\r
+ $.each($('.auth #api_information_panel'), function(k, v) {\r
+ var children = v;\r
+ if(children && children.childNodes) {\r
+ var requiredScopes = [];\r
+ $.each((children.childNodes), function (k1, v1){\r
+ var inner = v1.innerHTML;\r
+ if(inner)\r
+ requiredScopes.push(inner);\r
+ });\r
+ var diff = [];\r
+ for(var i=0; i < requiredScopes.length; i++) {\r
+ var s = requiredScopes[i];\r
+ if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {\r
+ diff.push(s);\r
+ }\r
+ }\r
+ if(diff.length > 0){\r
+ o = v.parentNode;\r
+ $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');\r
+ $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');\r
+\r
+ // sorry, not all scopes are satisfied\r
+ $(o).find('.api-ic').addClass('ic-warning');\r
+ $(o).find('.api-ic').removeClass('ic-error');\r
+ }\r
+ else {\r
+ o = v.parentNode;\r
+ $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');\r
+ $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');\r
+\r
+ // all scopes are satisfied\r
+ $(o).find('.api-ic').addClass('ic-info');\r
+ $(o).find('.api-ic').removeClass('ic-warning');\r
+ $(o).find('.api-ic').removeClass('ic-error'); \r
+ }\r
+ }\r
+ });\r
+ window.authorizations.add(oauth2KeyName, new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));\r
+ }\r
+ }\r
+ }\r
+}
\ No newline at end of file