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.
 
 
 
 

285 regels
8.7 KiB

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