PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/third_party/swagger-ui/lib/swagger-oauth.js

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