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.
 
 
 
 

218 lines
6.8 KiB

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