Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

237 linhas
7.3 KiB

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