Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

279 Zeilen
8.5 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 redirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
  91. var url = null;
  92. for (var key in authSchemes) {
  93. if (authSchemes.hasOwnProperty(key)) {
  94. var flow = authSchemes[key].flow;
  95. if(authSchemes[key].type === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
  96. var dets = authSchemes[key];
  97. url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
  98. window.swaggerUi.tokenName = dets.tokenName || 'access_token';
  99. window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
  100. }
  101. else if(authSchemes[key].grantTypes) {
  102. // 1.2 support
  103. var o = authSchemes[key].grantTypes;
  104. for(var t in o) {
  105. if(o.hasOwnProperty(t) && t === 'implicit') {
  106. var dets = o[t];
  107. var ep = dets.loginEndpoint.url;
  108. url = dets.loginEndpoint.url + '?response_type=token';
  109. window.swaggerUi.tokenName = dets.tokenName;
  110. }
  111. else if (o.hasOwnProperty(t) && t === 'accessCode') {
  112. var dets = o[t];
  113. var ep = dets.tokenRequestEndpoint.url;
  114. url = dets.tokenRequestEndpoint.url + '?response_type=code';
  115. window.swaggerUi.tokenName = dets.tokenName;
  116. }
  117. }
  118. }
  119. }
  120. }
  121. var scopes = []
  122. var o = $('.api-popup-scopes').find('input:checked');
  123. for(k =0; k < o.length; k++) {
  124. var scope = $(o[k]).attr('scope');
  125. if (scopes.indexOf(scope) === -1)
  126. scopes.push(scope);
  127. }
  128. window.enabledScopes=scopes;
  129. redirect_uri = redirectUrl;
  130. url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
  131. url += '&realm=' + encodeURIComponent(realm);
  132. url += '&client_id=' + encodeURIComponent(clientId);
  133. url += '&scope=' + encodeURIComponent(scopes);
  134. window.open(url);
  135. });
  136. popupMask.show();
  137. popupDialog.show();
  138. return;
  139. }
  140. function handleLogout() {
  141. for(key in window.authorizations.authz){
  142. window.authorizations.remove(key)
  143. }
  144. window.enabledScopes = null;
  145. $('.api-ic.ic-on').addClass('ic-off');
  146. $('.api-ic.ic-on').removeClass('ic-on');
  147. // set the info box
  148. $('.api-ic.ic-warning').addClass('ic-error');
  149. $('.api-ic.ic-warning').removeClass('ic-warning');
  150. }
  151. function initOAuth(opts) {
  152. var o = (opts||{});
  153. var errors = [];
  154. appName = (o.appName||errors.push('missing appName'));
  155. popupMask = (o.popupMask||$('#api-common-mask'));
  156. popupDialog = (o.popupDialog||$('.api-popup-dialog'));
  157. clientId = (o.clientId||errors.push('missing client id'));
  158. realm = (o.realm||errors.push('missing realm'));
  159. if(errors.length > 0){
  160. log('auth unable initialize oauth: ' + errors);
  161. return;
  162. }
  163. $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
  164. $('.api-ic').unbind();
  165. $('.api-ic').click(function(s) {
  166. if($(s.target).hasClass('ic-off'))
  167. handleLogin();
  168. else {
  169. handleLogout();
  170. }
  171. false;
  172. });
  173. }
  174. function processOAuthCode(data) {
  175. var params = {
  176. 'client_id': clientId,
  177. 'code': data.code,
  178. 'grant_type': 'authorization_code',
  179. 'redirect_uri': redirect_uri
  180. }
  181. $.ajax(
  182. {
  183. url : window.swaggerUi.tokenUrl,
  184. type: "POST",
  185. data: params,
  186. success:function(data, textStatus, jqXHR)
  187. {
  188. onOAuthComplete(data);
  189. },
  190. error: function(jqXHR, textStatus, errorThrown)
  191. {
  192. onOAuthComplete("");
  193. }
  194. });
  195. }
  196. function onOAuthComplete(token) {
  197. if(token) {
  198. if(token.error) {
  199. var checkbox = $('input[type=checkbox],.secured')
  200. checkbox.each(function(pos){
  201. checkbox[pos].checked = false;
  202. });
  203. alert(token.error);
  204. }
  205. else {
  206. var b = token[window.swaggerUi.tokenName];
  207. if(b){
  208. // if all roles are satisfied
  209. var o = null;
  210. $.each($('.auth #api_information_panel'), function(k, v) {
  211. var children = v;
  212. if(children && children.childNodes) {
  213. var requiredScopes = [];
  214. $.each((children.childNodes), function (k1, v1){
  215. var inner = v1.innerHTML;
  216. if(inner)
  217. requiredScopes.push(inner);
  218. });
  219. var diff = [];
  220. for(var i=0; i < requiredScopes.length; i++) {
  221. var s = requiredScopes[i];
  222. if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
  223. diff.push(s);
  224. }
  225. }
  226. if(diff.length > 0){
  227. o = v.parentNode;
  228. $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
  229. $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
  230. // sorry, not all scopes are satisfied
  231. $(o).find('.api-ic').addClass('ic-warning');
  232. $(o).find('.api-ic').removeClass('ic-error');
  233. }
  234. else {
  235. o = v.parentNode;
  236. $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
  237. $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
  238. // all scopes are satisfied
  239. $(o).find('.api-ic').addClass('ic-info');
  240. $(o).find('.api-ic').removeClass('ic-warning');
  241. $(o).find('.api-ic').removeClass('ic-error');
  242. }
  243. }
  244. });
  245. window.authorizations.add(oauth2KeyName, new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));
  246. }
  247. }
  248. }
  249. }