You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

295 regels
9.0 KiB

  1. var appName;
  2. var popupMask;
  3. var popupDialog;
  4. var clientId;
  5. var realm;
  6. var oauth2KeyName;
  7. var redirect_uri;
  8. var clientSecret;
  9. var scopeSeparator;
  10. function handleLogin() {
  11. var scopes = [];
  12. var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
  13. if(auths) {
  14. var key;
  15. var defs = auths;
  16. for(key in defs) {
  17. var auth = defs[key];
  18. if(auth.type === 'oauth2' && auth.scopes) {
  19. oauth2KeyName = key;
  20. var scope;
  21. if(Array.isArray(auth.scopes)) {
  22. // 1.2 support
  23. var i;
  24. for(i = 0; i < auth.scopes.length; i++) {
  25. scopes.push(auth.scopes[i]);
  26. }
  27. }
  28. else {
  29. // 2.0 support
  30. for(scope in auth.scopes) {
  31. scopes.push({scope: scope, description: auth.scopes[scope]});
  32. }
  33. }
  34. }
  35. }
  36. }
  37. if(window.swaggerUi.api
  38. && window.swaggerUi.api.info) {
  39. appName = window.swaggerUi.api.info.title;
  40. }
  41. $('.api-popup-dialog').remove();
  42. popupDialog = $(
  43. [
  44. '<div class="api-popup-dialog">',
  45. '<div class="api-popup-title">Select OAuth2.0 Scopes</div>',
  46. '<div class="api-popup-content">',
  47. '<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.',
  48. '<a href="#">Learn how to use</a>',
  49. '</p>',
  50. '<p><strong>' + appName + '</strong> API requires the following scopes. Select which ones you want to grant to Swagger UI.</p>',
  51. '<ul class="api-popup-scopes">',
  52. '</ul>',
  53. '<p class="error-msg"></p>',
  54. '<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>',
  55. '</div>',
  56. '</div>'].join(''));
  57. $(document.body).append(popupDialog);
  58. popup = popupDialog.find('ul.api-popup-scopes').empty();
  59. for (i = 0; i < scopes.length; i ++) {
  60. scope = scopes[i];
  61. str = '<li><input type="checkbox" id="scope_' + i + '" scope="' + scope.scope + '"/>' + '<label for="scope_' + i + '">' + scope.scope;
  62. if (scope.description) {
  63. str += '<br/><span class="api-scope-desc">' + scope.description + '</span>';
  64. }
  65. str += '</label></li>';
  66. popup.append(str);
  67. }
  68. var $win = $(window),
  69. dw = $win.width(),
  70. dh = $win.height(),
  71. st = $win.scrollTop(),
  72. dlgWd = popupDialog.outerWidth(),
  73. dlgHt = popupDialog.outerHeight(),
  74. top = (dh -dlgHt)/2 + st,
  75. left = (dw - dlgWd)/2;
  76. popupDialog.css({
  77. top: (top < 0? 0 : top) + 'px',
  78. left: (left < 0? 0 : left) + 'px'
  79. });
  80. popupDialog.find('button.api-popup-cancel').click(function() {
  81. popupMask.hide();
  82. popupDialog.hide();
  83. popupDialog.empty();
  84. popupDialog = [];
  85. });
  86. $('button.api-popup-authbtn').unbind();
  87. popupDialog.find('button.api-popup-authbtn').click(function() {
  88. popupMask.hide();
  89. popupDialog.hide();
  90. var authSchemes = window.swaggerUi.api.authSchemes;
  91. var host = window.location;
  92. var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
  93. var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
  94. var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl;
  95. var url = null;
  96. for (var key in authSchemes) {
  97. if (authSchemes.hasOwnProperty(key)) {
  98. var flow = authSchemes[key].flow;
  99. if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
  100. var dets = authSchemes[key];
  101. url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
  102. window.swaggerUi.tokenName = dets.tokenName || 'access_token';
  103. window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
  104. }
  105. else if(authSchemes[key].grantTypes) {
  106. // 1.2 support
  107. var o = authSchemes[key].grantTypes;
  108. for(var t in o) {
  109. if(o.hasOwnProperty(t) && t === 'implicit') {
  110. var dets = o[t];
  111. var ep = dets.loginEndpoint.url;
  112. url = dets.loginEndpoint.url + '?response_type=token';
  113. window.swaggerUi.tokenName = dets.tokenName;
  114. }
  115. else if (o.hasOwnProperty(t) && t === 'accessCode') {
  116. var dets = o[t];
  117. var ep = dets.tokenRequestEndpoint.url;
  118. url = dets.tokenRequestEndpoint.url + '?response_type=code';
  119. window.swaggerUi.tokenName = dets.tokenName;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. var scopes = []
  126. var o = $('.api-popup-scopes').find('input:checked');
  127. for(k =0; k < o.length; k++) {
  128. var scope = $(o[k]).attr('scope');
  129. if (scopes.indexOf(scope) === -1)
  130. scopes.push(scope);
  131. }
  132. // Implicit auth recommends a state parameter.
  133. var state = Math.random ();
  134. window.enabledScopes=scopes;
  135. redirect_uri = redirectUrl;
  136. url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
  137. url += '&realm=' + encodeURIComponent(realm);
  138. url += '&client_id=' + encodeURIComponent(clientId);
  139. url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator));
  140. url += '&state=' + encodeURIComponent(state);
  141. window.open(url);
  142. });
  143. popupMask.show();
  144. popupDialog.show();
  145. return;
  146. }
  147. function handleLogout() {
  148. for(key in window.swaggerUi.api.clientAuthorizations.authz){
  149. window.swaggerUi.api.clientAuthorizations.remove(key)
  150. }
  151. window.enabledScopes = null;
  152. $('.api-ic.ic-on').addClass('ic-off');
  153. $('.api-ic.ic-on').removeClass('ic-on');
  154. // set the info box
  155. $('.api-ic.ic-warning').addClass('ic-error');
  156. $('.api-ic.ic-warning').removeClass('ic-warning');
  157. }
  158. function initOAuth(opts) {
  159. var o = (opts||{});
  160. var errors = [];
  161. appName = (o.appName||errors.push('missing appName'));
  162. popupMask = (o.popupMask||$('#api-common-mask'));
  163. popupDialog = (o.popupDialog||$('.api-popup-dialog'));
  164. clientId = (o.clientId||errors.push('missing client id'));
  165. clientSecret = (o.clientSecret||null);
  166. realm = (o.realm||errors.push('missing realm'));
  167. scopeSeparator = (o.scopeSeparator||' ');
  168. if(errors.length > 0){
  169. log('auth unable initialize oauth: ' + errors);
  170. return;
  171. }
  172. $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
  173. $('.api-ic').unbind();
  174. $('.api-ic').click(function(s) {
  175. if($(s.target).hasClass('ic-off'))
  176. handleLogin();
  177. else {
  178. handleLogout();
  179. }
  180. false;
  181. });
  182. }
  183. window.processOAuthCode = function processOAuthCode(data) {
  184. var params = {
  185. 'client_id': clientId,
  186. 'code': data.code,
  187. 'grant_type': 'authorization_code',
  188. 'redirect_uri': redirect_uri
  189. };
  190. if (clientSecret) {
  191. params.client_secret = clientSecret;
  192. }
  193. $.ajax(
  194. {
  195. url : window.swaggerUi.tokenUrl,
  196. type: "POST",
  197. data: params,
  198. success:function(data, textStatus, jqXHR)
  199. {
  200. onOAuthComplete(data);
  201. },
  202. error: function(jqXHR, textStatus, errorThrown)
  203. {
  204. onOAuthComplete("");
  205. }
  206. });
  207. };
  208. window.onOAuthComplete = function onOAuthComplete(token) {
  209. if(token) {
  210. if(token.error) {
  211. var checkbox = $('input[type=checkbox],.secured')
  212. checkbox.each(function(pos){
  213. checkbox[pos].checked = false;
  214. });
  215. alert(token.error);
  216. }
  217. else {
  218. var b = token[window.swaggerUi.tokenName];
  219. if(b){
  220. // if all roles are satisfied
  221. var o = null;
  222. $.each($('.auth .api-ic .api_information_panel'), function(k, v) {
  223. var children = v;
  224. if(children && children.childNodes) {
  225. var requiredScopes = [];
  226. $.each((children.childNodes), function (k1, v1){
  227. var inner = v1.innerHTML;
  228. if(inner)
  229. requiredScopes.push(inner);
  230. });
  231. var diff = [];
  232. for(var i=0; i < requiredScopes.length; i++) {
  233. var s = requiredScopes[i];
  234. if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
  235. diff.push(s);
  236. }
  237. }
  238. if(diff.length > 0){
  239. o = v.parentNode.parentNode;
  240. $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
  241. $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
  242. // sorry, not all scopes are satisfied
  243. $(o).find('.api-ic').addClass('ic-warning');
  244. $(o).find('.api-ic').removeClass('ic-error');
  245. }
  246. else {
  247. o = v.parentNode.parentNode;
  248. $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
  249. $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
  250. // all scopes are satisfied
  251. $(o).find('.api-ic').addClass('ic-info');
  252. $(o).find('.api-ic').removeClass('ic-warning');
  253. $(o).find('.api-ic').removeClass('ic-error');
  254. }
  255. }
  256. });
  257. window.swaggerUi.api.clientAuthorizations.add(oauth2KeyName, new SwaggerClient.ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));
  258. }
  259. }
  260. }
  261. };