Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

swagger-oauth.js 8.6 KiB

pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
pirms 10 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. // Implicit auth recommends a state parameter.
  129. var state = Math.random ();
  130. window.enabledScopes=scopes;
  131. redirect_uri = redirectUrl;
  132. url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
  133. url += '&realm=' + encodeURIComponent(realm);
  134. url += '&client_id=' + encodeURIComponent(clientId);
  135. url += '&scope=' + encodeURIComponent(scopes.join(' '));
  136. url += '&state=' + encodeURIComponent(state);
  137. window.open(url);
  138. });
  139. popupMask.show();
  140. popupDialog.show();
  141. return;
  142. }
  143. function handleLogout() {
  144. for(key in window.authorizations.authz){
  145. window.authorizations.remove(key)
  146. }
  147. window.enabledScopes = null;
  148. $('.api-ic.ic-on').addClass('ic-off');
  149. $('.api-ic.ic-on').removeClass('ic-on');
  150. // set the info box
  151. $('.api-ic.ic-warning').addClass('ic-error');
  152. $('.api-ic.ic-warning').removeClass('ic-warning');
  153. }
  154. function initOAuth(opts) {
  155. var o = (opts||{});
  156. var errors = [];
  157. appName = (o.appName||errors.push('missing appName'));
  158. popupMask = (o.popupMask||$('#api-common-mask'));
  159. popupDialog = (o.popupDialog||$('.api-popup-dialog'));
  160. clientId = (o.clientId||errors.push('missing client id'));
  161. realm = (o.realm||errors.push('missing realm'));
  162. if(errors.length > 0){
  163. log('auth unable initialize oauth: ' + errors);
  164. return;
  165. }
  166. $('pre code').each(function(i, e) {hljs.highlightBlock(e)});
  167. $('.api-ic').unbind();
  168. $('.api-ic').click(function(s) {
  169. if($(s.target).hasClass('ic-off'))
  170. handleLogin();
  171. else {
  172. handleLogout();
  173. }
  174. false;
  175. });
  176. }
  177. function processOAuthCode(data) {
  178. var params = {
  179. 'client_id': clientId,
  180. 'code': data.code,
  181. 'grant_type': 'authorization_code',
  182. 'redirect_uri': redirect_uri
  183. }
  184. $.ajax(
  185. {
  186. url : window.swaggerUi.tokenUrl,
  187. type: "POST",
  188. data: params,
  189. success:function(data, textStatus, jqXHR)
  190. {
  191. onOAuthComplete(data);
  192. },
  193. error: function(jqXHR, textStatus, errorThrown)
  194. {
  195. onOAuthComplete("");
  196. }
  197. });
  198. }
  199. function onOAuthComplete(token) {
  200. if(token) {
  201. if(token.error) {
  202. var checkbox = $('input[type=checkbox],.secured')
  203. checkbox.each(function(pos){
  204. checkbox[pos].checked = false;
  205. });
  206. alert(token.error);
  207. }
  208. else {
  209. var b = token[window.swaggerUi.tokenName];
  210. if(b){
  211. // if all roles are satisfied
  212. var o = null;
  213. $.each($('.auth #api_information_panel'), function(k, v) {
  214. var children = v;
  215. if(children && children.childNodes) {
  216. var requiredScopes = [];
  217. $.each((children.childNodes), function (k1, v1){
  218. var inner = v1.innerHTML;
  219. if(inner)
  220. requiredScopes.push(inner);
  221. });
  222. var diff = [];
  223. for(var i=0; i < requiredScopes.length; i++) {
  224. var s = requiredScopes[i];
  225. if(window.enabledScopes && window.enabledScopes.indexOf(s) == -1) {
  226. diff.push(s);
  227. }
  228. }
  229. if(diff.length > 0){
  230. o = v.parentNode;
  231. $(o.parentNode).find('.api-ic.ic-on').addClass('ic-off');
  232. $(o.parentNode).find('.api-ic.ic-on').removeClass('ic-on');
  233. // sorry, not all scopes are satisfied
  234. $(o).find('.api-ic').addClass('ic-warning');
  235. $(o).find('.api-ic').removeClass('ic-error');
  236. }
  237. else {
  238. o = v.parentNode;
  239. $(o.parentNode).find('.api-ic.ic-off').addClass('ic-on');
  240. $(o.parentNode).find('.api-ic.ic-off').removeClass('ic-off');
  241. // all scopes are satisfied
  242. $(o).find('.api-ic').addClass('ic-info');
  243. $(o).find('.api-ic').removeClass('ic-warning');
  244. $(o).find('.api-ic').removeClass('ic-error');
  245. }
  246. }
  247. });
  248. window.authorizations.add(oauth2KeyName, new ApiKeyAuthorization('Authorization', 'Bearer ' + b, 'header'));
  249. }
  250. }
  251. }
  252. }