/src/main/webapp/bower_components/swagger-ui/src/main/javascript/view/AuthView.js

https://gitlab.com/atomfrede/jhipster-ci-example-gradle · JavaScript · 178 lines · 141 code · 26 blank · 11 comment · 29 complexity · d8f994aec05278edb4b4fb52fcc21ac5 MD5 · raw file

  1. 'use strict';
  2. /* global redirect_uri:true */
  3. /* global clientId */
  4. /* global scopeSeparator */
  5. /* global additionalQueryStringParams */
  6. /* global clientSecret */
  7. /* global onOAuthComplete */
  8. /* global realm */
  9. /*jshint unused:false*/
  10. SwaggerUi.Views.AuthView = Backbone.View.extend({
  11. events: {
  12. 'click .auth_submit__button': 'authorizeClick',
  13. 'click .auth_logout__button': 'logoutClick'
  14. },
  15. tpls: {
  16. main: Handlebars.templates.auth_view
  17. },
  18. selectors: {
  19. innerEl: '.auth_inner',
  20. authBtn: '.auth_submit__button'
  21. },
  22. initialize: function(opts) {
  23. this.options = opts || {};
  24. opts.data = opts.data || {};
  25. this.router = this.options.router;
  26. this.authsCollectionView = new SwaggerUi.Views.AuthsCollectionView({data: opts.data});
  27. this.$el.html(this.tpls.main({
  28. isLogout: this.authsCollectionView.collection.isAuthorized(),
  29. isAuthorized: this.authsCollectionView.collection.isPartiallyAuthorized()
  30. }));
  31. this.$innerEl = this.$(this.selectors.innerEl);
  32. this.isLogout = this.authsCollectionView.collection.isPartiallyAuthorized();
  33. },
  34. render: function () {
  35. this.$innerEl.html(this.authsCollectionView.render().el);
  36. return this;
  37. },
  38. authorizeClick: function (e) {
  39. e.preventDefault();
  40. e.stopPropagation();
  41. if (this.authsCollectionView.collection.isValid()) {
  42. this.authorize();
  43. } else {
  44. this.authsCollectionView.highlightInvalid();
  45. }
  46. },
  47. authorize: function () {
  48. this.authsCollectionView.collection.forEach(function (auth) {
  49. var keyAuth, basicAuth;
  50. var type = auth.get('type');
  51. if (type === 'apiKey') {
  52. keyAuth = new SwaggerClient.ApiKeyAuthorization(
  53. auth.get('name'),
  54. auth.get('value'),
  55. auth.get('in')
  56. );
  57. this.router.api.clientAuthorizations.add(auth.get('title'), keyAuth);
  58. } else if (type === 'basic') {
  59. basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
  60. this.router.api.clientAuthorizations.add(auth.get('title'), basicAuth);
  61. } else if (type === 'oauth2') {
  62. this.handleOauth2Login(auth);
  63. }
  64. }, this);
  65. this.router.load();
  66. },
  67. logoutClick: function (e) {
  68. e.preventDefault();
  69. this.authsCollectionView.collection.forEach(function (auth) {
  70. window.swaggerUi.api.clientAuthorizations.remove(auth.get('title'));
  71. });
  72. this.router.load();
  73. },
  74. // taken from lib/swagger-oauth.js
  75. handleOauth2Login: function (auth) {
  76. var host = window.location;
  77. var pathname = location.pathname.substring(0, location.pathname.lastIndexOf('/'));
  78. var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
  79. var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl;
  80. var url = null;
  81. var scopes = _.map(auth.get('scopes'), function (scope) {
  82. return scope.scope;
  83. });
  84. var state, dets, ep;
  85. window.OAuthSchemeKey = auth.get('title');
  86. window.enabledScopes = scopes;
  87. var flow = auth.get('flow');
  88. if(auth.get('type') === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
  89. dets = auth.attributes;
  90. url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
  91. window.swaggerUi.tokenName = dets.tokenName || 'access_token';
  92. window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
  93. state = window.OAuthSchemeKey;
  94. }
  95. else if(auth.get('type') === 'oauth2' && flow && (flow === 'application')) {
  96. dets = auth.attributes;
  97. window.swaggerUi.tokenName = dets.tokenName || 'access_token';
  98. this.clientCredentialsFlow(scopes, dets.tokenUrl, window.OAuthSchemeKey);
  99. return;
  100. }
  101. else if(auth.get('grantTypes')) {
  102. // 1.2 support
  103. var o = auth.get('grantTypes');
  104. for(var t in o) {
  105. if(o.hasOwnProperty(t) && t === 'implicit') {
  106. dets = o[t];
  107. 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. dets = o[t];
  113. ep = dets.tokenRequestEndpoint.url;
  114. url = dets.tokenRequestEndpoint.url + '?response_type=code';
  115. window.swaggerUi.tokenName = dets.tokenName;
  116. }
  117. }
  118. }
  119. redirect_uri = redirectUrl;
  120. url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
  121. url += '&realm=' + encodeURIComponent(realm);
  122. url += '&client_id=' + encodeURIComponent(clientId);
  123. url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator));
  124. url += '&state=' + encodeURIComponent(state);
  125. for (var key in additionalQueryStringParams) {
  126. url += '&' + key + '=' + encodeURIComponent(additionalQueryStringParams[key]);
  127. }
  128. window.open(url);
  129. },
  130. // taken from lib/swagger-oauth.js
  131. clientCredentialsFlow: function (scopes, tokenUrl, OAuthSchemeKey) {
  132. var params = {
  133. 'client_id': clientId,
  134. 'client_secret': clientSecret,
  135. 'scope': scopes.join(' '),
  136. 'grant_type': 'client_credentials'
  137. };
  138. $.ajax({
  139. url : tokenUrl,
  140. type: 'POST',
  141. data: params,
  142. success: function (data)
  143. {
  144. onOAuthComplete(data, OAuthSchemeKey);
  145. },
  146. error: function ()
  147. {
  148. onOAuthComplete('');
  149. }
  150. });
  151. }
  152. });