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.

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. }
  47. var $win = $(window),
  48. dw = $win.width(),
  49. dh = $win.height(),
  50. st = $win.scrollTop(),
  51. dlgWd = popupDialog.outerWidth(),
  52. dlgHt = popupDialog.outerHeight(),
  53. top = (dh -dlgHt)/2 + st,
  54. left = (dw - dlgWd)/2;
  55. popupDialog.css({
  56. top: (top < 0? 0 : top) + 'px',
  57. left: (left < 0? 0 : left) + 'px'
  58. });
  59. popupDialog.find('button.api-popup-cancel').click(function() {
  60. popupMask.hide();
  61. popupDialog.hide();
  62. });
  63. popupDialog.find('button.api-popup-authbtn').click(function() {
  64. popupMask.hide();
  65. popupDialog.hide();
  66. var authSchemes = window.swaggerUi.api.authSchemes;
  67. var host = window.location;
  68. var pathname = location.pathname.substring(0, location.pathname.lastIndexOf("/"));
  69. var redirectUrl = host.protocol + '//' + host.host + pathname + "/o2c.html";
  70. var url = null;
  71. for (var key in authSchemes) {
  72. if (authSchemes.hasOwnProperty(key)) {
  73. var o = authSchemes[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 o = $('.api-popup-scopes').find('input:checked');
  85. for(k =0; k < o.length; k++) {
  86. scopes.push($(o[k]).attr("scope"));
  87. }
  88. window.enabledScopes=scopes;
  89. url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
  90. url += '&realm=' + encodeURIComponent(realm);
  91. url += '&client_id=' + encodeURIComponent(clientId);
  92. url += '&scope=' + encodeURIComponent(scopes);
  93. window.open(url);
  94. });
  95. popupMask.show();
  96. popupDialog.show();
  97. return;
  98. }
  99. function handleLogout() {
  100. for(key in window.authorizations.authz){
  101. window.authorizations.remove(key)
  102. }
  103. window.enabledScopes = null;
  104. $('.api-ic.ic-on').addClass('ic-off');
  105. $('.api-ic.ic-on').removeClass('ic-on');
  106. // set the info box
  107. $('.api-ic.ic-warning').addClass('ic-error');
  108. $('.api-ic.ic-warning').removeClass('ic-warning');
  109. }
  110. function initOAuth(opts) {
  111. var o = (opts||{});
  112. var errors = [];
  113. appName = (o.appName||errors.push("missing appName"));
  114. popupMask = (o.popupMask||$('#api-common-mask'));
  115. popupDialog = (o.popupDialog||$('.api-popup-dialog'));
  116. clientId = (o.clientId||errors.push("missing client id"));
  117. realm = (o.realm||errors.push("missing realm"));
  118. if(errors.length > 0){
  119. log("auth unable initialize oauth: " + errors);
  120. return;
  121. }
  122. $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
  123. $('.api-ic').click(function(s) {
  124. if($(s.target).hasClass('ic-off'))
  125. handleLogin();
  126. else {
  127. handleLogout();
  128. }
  129. false;
  130. });
  131. }
  132. function onOAuthComplete(token) {
  133. if(token) {
  134. if(token.error) {
  135. var checkbox = $('input[type=checkbox],.secured')
  136. checkbox.each(function(pos){
  137. checkbox[pos].checked = false;
  138. });
  139. alert(token.error);
  140. }
  141. else {
  142. var b = token[window.swaggerUi.tokenName];
  143. if(b){
  144. // if all roles are satisfied
  145. var o = null;
  146. $.each($('.auth #api_information_panel'), function(k, v) {
  147. var children = v;
  148. if(children && children.childNodes) {
  149. var requiredScopes = [];
  150. $.each((children.childNodes), function (k1, v1){
  151. var inner = v1.innerHTML;
  152. if(inner)
  153. requiredScopes.push(inner);
  154. });
  155. var diff = [];
  156. for(var i=0; i < requiredScopes.length; i++) {
  157. var s = requiredScopes[i];
  158. if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
  159. diff.push(s);
  160. }
  161. }
  162. if(diff.length > 0){
  163. o = v.parentNode;
  164. $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
  165. $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
  166. // sorry, not all scopes are satisfied
  167. $(o).find('.api-ic').addClass('ic-warning');
  168. $(o).find('.api-ic').removeClass('ic-error');
  169. }
  170. else {
  171. o = v.parentNode;
  172. $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
  173. $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
  174. // all scopes are satisfied
  175. $(o).find('.api-ic').addClass('ic-info');
  176. $(o).find('.api-ic').removeClass('ic-warning');
  177. $(o).find('.api-ic').removeClass('ic-error');
  178. }
  179. }
  180. });
  181. window.authorizations.add("oauth2", new ApiKeyAuthorization("Authorization", "Bearer " + b, "header"));
  182. }
  183. }
  184. }
  185. }