github.com/qubitproducts/logspray@v0.2.14/server/swagger-ui/src/main/javascript/view/AuthView.js (about) 1 'use strict'; 2 3 /* global redirect_uri:true */ 4 /* global clientId */ 5 /* global scopeSeparator */ 6 /* global additionalQueryStringParams */ 7 /* global clientSecret */ 8 /* global onOAuthComplete */ 9 /* global realm */ 10 /*jshint unused:false*/ 11 12 SwaggerUi.Views.AuthView = Backbone.View.extend({ 13 events: { 14 'click .auth_submit__button': 'authorizeClick', 15 'click .auth_logout__button': 'logoutClick' 16 }, 17 18 tpls: { 19 main: Handlebars.templates.auth_view 20 }, 21 22 selectors: { 23 innerEl: '.auth_inner', 24 authBtn: '.auth_submit__button' 25 }, 26 27 initialize: function(opts) { 28 this.options = opts || {}; 29 opts.data = opts.data || {}; 30 this.router = this.options.router; 31 32 this.authsCollectionView = new SwaggerUi.Views.AuthsCollectionView({data: opts.data}); 33 34 this.$el.html(this.tpls.main({ 35 isLogout: this.authsCollectionView.collection.isAuthorized(), 36 isAuthorized: this.authsCollectionView.collection.isPartiallyAuthorized() 37 })); 38 this.$innerEl = this.$(this.selectors.innerEl); 39 this.isLogout = this.authsCollectionView.collection.isPartiallyAuthorized(); 40 }, 41 42 render: function () { 43 this.$innerEl.html(this.authsCollectionView.render().el); 44 45 return this; 46 }, 47 48 authorizeClick: function (e) { 49 e.preventDefault(); 50 e.stopPropagation(); 51 52 if (this.authsCollectionView.collection.isValid()) { 53 this.authorize(); 54 } else { 55 this.authsCollectionView.highlightInvalid(); 56 } 57 }, 58 59 authorize: function () { 60 this.authsCollectionView.collection.forEach(function (auth) { 61 var keyAuth, basicAuth; 62 var type = auth.get('type'); 63 64 if (type === 'apiKey') { 65 keyAuth = new SwaggerClient.ApiKeyAuthorization( 66 auth.get('name'), 67 auth.get('value'), 68 auth.get('in') 69 ); 70 71 this.router.api.clientAuthorizations.add(auth.get('title'), keyAuth); 72 } else if (type === 'basic') { 73 basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password')); 74 this.router.api.clientAuthorizations.add(auth.get('title'), basicAuth); 75 } else if (type === 'oauth2') { 76 this.handleOauth2Login(auth); 77 } 78 }, this); 79 80 this.router.load(); 81 }, 82 83 logoutClick: function (e) { 84 e.preventDefault(); 85 86 this.authsCollectionView.collection.forEach(function (auth) { 87 window.swaggerUi.api.clientAuthorizations.remove(auth.get('title')); 88 }); 89 90 this.router.load(); 91 }, 92 93 // taken from lib/swagger-oauth.js 94 handleOauth2Login: function (auth) { 95 var host = window.location; 96 var pathname = location.pathname.substring(0, location.pathname.lastIndexOf('/')); 97 var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html'; 98 var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl; 99 var url = null; 100 var scopes = _.map(auth.get('scopes'), function (scope) { 101 if(scope.checked) { 102 return scope.scope; 103 } 104 }); 105 var container = window.swaggerUiAuth || (window.swaggerUiAuth = {}); 106 var state, dets, ep; 107 container.OAuthSchemeKey = auth.get('title'); 108 109 window.enabledScopes = scopes; 110 var flow = auth.get('flow'); 111 112 if(auth.get('type') === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) { 113 dets = auth.attributes; 114 url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code'); 115 container.tokenName = dets.tokenName || 'access_token'; 116 container.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null); 117 state = container.OAuthSchemeKey; 118 } 119 else if(auth.get('type') === 'oauth2' && flow && (flow === 'application')) { 120 dets = auth.attributes; 121 container.tokenName = dets.tokenName || 'access_token'; 122 this.clientCredentialsFlow(scopes, dets, container.OAuthSchemeKey); 123 return; 124 } 125 else if(auth.get('type') === 'oauth2' && flow && (flow === 'password')) { 126 dets = auth.attributes; 127 container.tokenName = dets.tokenName || 'access_token'; 128 this.passwordFlow(scopes, dets, container.OAuthSchemeKey); 129 return; 130 } 131 else if(auth.get('grantTypes')) { 132 // 1.2 support 133 var o = auth.get('grantTypes'); 134 for(var t in o) { 135 if(o.hasOwnProperty(t) && t === 'implicit') { 136 dets = o[t]; 137 ep = dets.loginEndpoint.url; 138 url = dets.loginEndpoint.url + '?response_type=token'; 139 container.tokenName = dets.tokenName; 140 } 141 else if (o.hasOwnProperty(t) && t === 'accessCode') { 142 dets = o[t]; 143 ep = dets.tokenRequestEndpoint.url; 144 url = dets.tokenRequestEndpoint.url + '?response_type=code'; 145 container.tokenName = dets.tokenName; 146 } 147 } 148 } 149 150 redirect_uri = redirectUrl; 151 152 url += '&redirect_uri=' + encodeURIComponent(redirectUrl); 153 url += '&realm=' + encodeURIComponent(realm); 154 url += '&client_id=' + encodeURIComponent(clientId); 155 url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator)); 156 url += '&state=' + encodeURIComponent(state); 157 for (var key in additionalQueryStringParams) { 158 url += '&' + key + '=' + encodeURIComponent(additionalQueryStringParams[key]); 159 } 160 161 window.open(url); 162 }, 163 164 // taken from lib/swagger-oauth.js 165 clientCredentialsFlow: function (scopes, oauth, OAuthSchemeKey) { 166 this.accessTokenRequest(scopes, oauth, OAuthSchemeKey, 'client_credentials'); 167 }, 168 169 passwordFlow: function (scopes, oauth, OAuthSchemeKey) { 170 this.accessTokenRequest(scopes, oauth, OAuthSchemeKey, 'password', { 171 'username': oauth.username, 172 'password': oauth.password 173 }); 174 }, 175 176 accessTokenRequest: function (scopes, oauth, OAuthSchemeKey, grantType, params) { 177 params = $.extend({}, { 178 'scope': scopes.join(' '), 179 'grant_type': grantType 180 }, params); 181 182 var headers= {}; 183 184 switch (oauth.clientAuthenticationType) { 185 case 'basic': 186 headers.Authorization = 'Basic ' + btoa(oauth.clientId + ':' + oauth.clientSecret); 187 break; 188 case 'request-body': 189 params.client_id = oauth.clientId; 190 params.client_secret = oauth.clientSecret; 191 break; 192 } 193 194 $.ajax({ 195 url : oauth.tokenUrl, 196 type: 'POST', 197 data: params, 198 headers: headers, 199 success: function (data) 200 { 201 onOAuthComplete(data, OAuthSchemeKey); 202 }, 203 error: function () 204 { 205 onOAuthComplete(''); 206 } 207 }); 208 } 209 });