[auth] added handling of oauth2 login
This commit is contained in:
@@ -1,5 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
/* global OAuthSchemeKeys */
|
||||
/* global redirect_uri */
|
||||
/* global clientId */
|
||||
/* global scopeSeparator */
|
||||
/* global additionalQueryStringParams */
|
||||
/* global clientSecret */
|
||||
/* global onOAuthComplete */
|
||||
/* global OAuthSchemeKeys */
|
||||
/* global realm */
|
||||
/*jshint unused:false*/
|
||||
|
||||
SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
events: {
|
||||
'click .auth_submit_button': 'authorizeClick',
|
||||
@@ -38,6 +49,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
|
||||
authorizeClick: function (e) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
if (this.collection.isValid()) {
|
||||
this.authorize();
|
||||
@@ -101,7 +113,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
||||
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
||||
} else if (type === 'oauth2') {
|
||||
//todo add handling login of oauth2
|
||||
this.handleOauth2Login(auth);
|
||||
}
|
||||
}, this);
|
||||
|
||||
@@ -118,5 +130,91 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
});
|
||||
|
||||
this.router.load();
|
||||
},
|
||||
|
||||
// taken from lib/swagger-oauth.js
|
||||
handleOauth2Login: function (auth) {
|
||||
var host = window.location;
|
||||
var pathname = location.pathname.substring(0, location.pathname.lastIndexOf('/'));
|
||||
var defaultRedirectUrl = host.protocol + '//' + host.host + pathname + '/o2c.html';
|
||||
var redirectUrl = window.oAuthRedirectUrl || defaultRedirectUrl;
|
||||
var url = null;
|
||||
var scopes = _.map(auth.get('scopes'), function (scope) {
|
||||
return scope.scope;
|
||||
});
|
||||
var OAuthSchemeKeys = [];
|
||||
var state, dets, ep;
|
||||
|
||||
window.enabledScopes = scopes;
|
||||
var flow = auth.get('flow');
|
||||
|
||||
if(auth.get('type') === 'oauth2' && flow && (flow === 'implicit' || flow === 'accessCode')) {
|
||||
dets = auth.attributes;
|
||||
url = dets.authorizationUrl + '?response_type=' + (flow === 'implicit' ? 'token' : 'code');
|
||||
window.swaggerUi.tokenName = dets.tokenName || 'access_token';
|
||||
window.swaggerUi.tokenUrl = (flow === 'accessCode' ? dets.tokenUrl : null);
|
||||
//state = key;
|
||||
}
|
||||
else if(auth.get('type') === 'oauth2' && flow && (flow === 'application')) {
|
||||
dets = auth.attributes;
|
||||
window.swaggerUi.tokenName = dets.tokenName || 'access_token';
|
||||
this.clientCredentialsFlow(scopes, dets.tokenUrl, '');
|
||||
return;
|
||||
}
|
||||
else if(auth.get('grantTypes')) {
|
||||
// 1.2 support
|
||||
var o = auth.get('grantTypes');
|
||||
for(var t in o) {
|
||||
if(o.hasOwnProperty(t) && t === 'implicit') {
|
||||
dets = o[t];
|
||||
ep = dets.loginEndpoint.url;
|
||||
url = dets.loginEndpoint.url + '?response_type=token';
|
||||
window.swaggerUi.tokenName = dets.tokenName;
|
||||
}
|
||||
else if (o.hasOwnProperty(t) && t === 'accessCode') {
|
||||
dets = o[t];
|
||||
ep = dets.tokenRequestEndpoint.url;
|
||||
url = dets.tokenRequestEndpoint.url + '?response_type=code';
|
||||
window.swaggerUi.tokenName = dets.tokenName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var redirect_uri = redirectUrl;
|
||||
|
||||
url += '&redirect_uri=' + encodeURIComponent(redirectUrl);
|
||||
url += '&realm=' + encodeURIComponent(realm);
|
||||
url += '&client_id=' + encodeURIComponent(clientId);
|
||||
url += '&scope=' + encodeURIComponent(scopes.join(scopeSeparator));
|
||||
url += '&state=' + encodeURIComponent(state);
|
||||
for (var key in additionalQueryStringParams) {
|
||||
url += '&' + key + '=' + encodeURIComponent(additionalQueryStringParams[key]);
|
||||
}
|
||||
|
||||
window.open(url);
|
||||
},
|
||||
|
||||
// taken from lib/swagger-oauth.js
|
||||
clientCredentialsFlow: function (scopes, tokenUrl, OAuthSchemeKey) {
|
||||
var params = {
|
||||
'client_id': clientId,
|
||||
'client_secret': clientSecret,
|
||||
'scope': scopes.join(' '),
|
||||
'grant_type': 'client_credentials'
|
||||
};
|
||||
$.ajax({
|
||||
url : tokenUrl,
|
||||
type: 'POST',
|
||||
data: params,
|
||||
success: function (data)
|
||||
{
|
||||
onOAuthComplete(data, OAuthSchemeKey);
|
||||
},
|
||||
error: function ()
|
||||
{
|
||||
onOAuthComplete('');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user