[auth] added handling of oauth2 login
This commit is contained in:
112
dist/swagger-ui.js
vendored
112
dist/swagger-ui.js
vendored
@@ -25113,7 +25113,7 @@ window.SwaggerUi.utils = {};
|
|||||||
|
|
||||||
window.SwaggerUi.utils = {
|
window.SwaggerUi.utils = {
|
||||||
parseSecurityDefinitions: function (security) {
|
parseSecurityDefinitions: function (security) {
|
||||||
var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
|
var auths = Object.assign({}, window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions);
|
||||||
var oauth2Arr = [];
|
var oauth2Arr = [];
|
||||||
var authsArr = [];
|
var authsArr = [];
|
||||||
var utils = window.SwaggerUi.utils;
|
var utils = window.SwaggerUi.utils;
|
||||||
@@ -25129,7 +25129,7 @@ window.SwaggerUi.utils = {
|
|||||||
if (!auths[key]) { continue; }
|
if (!auths[key]) { continue; }
|
||||||
auths[key] = auths[key] || {};
|
auths[key] = auths[key] || {};
|
||||||
if (auths[key].type === 'oauth2') {
|
if (auths[key].type === 'oauth2') {
|
||||||
singleOauth2Security[key] = auths[key];
|
singleOauth2Security[key] = Object.assign({}, auths[key]);
|
||||||
for (var i in singleOauth2Security[key].scopes) {
|
for (var i in singleOauth2Security[key].scopes) {
|
||||||
if (item[key].indexOf(i) < 0) {
|
if (item[key].indexOf(i) < 0) {
|
||||||
delete singleOauth2Security[key].scopes[i];
|
delete singleOauth2Security[key].scopes[i];
|
||||||
@@ -25137,11 +25137,11 @@ window.SwaggerUi.utils = {
|
|||||||
}
|
}
|
||||||
singleOauth2Security[key].scopes = utils.parseOauth2Scopes(singleOauth2Security[key].scopes);
|
singleOauth2Security[key].scopes = utils.parseOauth2Scopes(singleOauth2Security[key].scopes);
|
||||||
} else {
|
} else {
|
||||||
singleSecurity[key] = auths[key];
|
singleSecurity[key] = Object.assign({}, auths[key]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (item[key].type === 'oauth2') {
|
if (item[key].type === 'oauth2') {
|
||||||
singleOauth2Security[key] = item[key];
|
singleOauth2Security[key] = Object.assign({}, item[key]);
|
||||||
singleOauth2Security[key].scopes = utils.parseOauth2Scopes(singleOauth2Security[key].scopes);
|
singleOauth2Security[key].scopes = utils.parseOauth2Scopes(singleOauth2Security[key].scopes);
|
||||||
} else {
|
} else {
|
||||||
singleSecurity[key] = item[key];
|
singleSecurity[key] = item[key];
|
||||||
@@ -25159,7 +25159,8 @@ window.SwaggerUi.utils = {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
parseOauth2Scopes: function (scopes) {
|
parseOauth2Scopes: function (data) {
|
||||||
|
var scopes = Object.assign({}, data);
|
||||||
var result = [];
|
var result = [];
|
||||||
var key;
|
var key;
|
||||||
|
|
||||||
@@ -25298,6 +25299,17 @@ SwaggerUi.Collections.AuthsCollection = Backbone.Collection.extend({
|
|||||||
});
|
});
|
||||||
'use strict';
|
'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({
|
SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||||
events: {
|
events: {
|
||||||
'click .auth_submit_button': 'authorizeClick',
|
'click .auth_submit_button': 'authorizeClick',
|
||||||
@@ -25336,6 +25348,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
|
|
||||||
authorizeClick: function (e) {
|
authorizeClick: function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
|
||||||
if (this.collection.isValid()) {
|
if (this.collection.isValid()) {
|
||||||
this.authorize();
|
this.authorize();
|
||||||
@@ -25399,7 +25412,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
||||||
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
||||||
} else if (type === 'oauth2') {
|
} else if (type === 'oauth2') {
|
||||||
//todo add handling login of oauth2
|
this.handleOauth2Login(auth);
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
@@ -25416,7 +25429,93 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.router.load();
|
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('');
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
@@ -25678,6 +25777,7 @@ SwaggerUi.Models.Oauth2Model = Backbone.Model.extend({
|
|||||||
auth.scopes[index].checked = val;
|
auth.scopes[index].checked = val;
|
||||||
|
|
||||||
this.set(auth);
|
this.set(auth);
|
||||||
|
this.validate();
|
||||||
},
|
},
|
||||||
|
|
||||||
validate: function () {
|
validate: function () {
|
||||||
|
|||||||
18
dist/swagger-ui.min.js
vendored
18
dist/swagger-ui.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
window.SwaggerUi.utils = {
|
window.SwaggerUi.utils = {
|
||||||
parseSecurityDefinitions: function (security) {
|
parseSecurityDefinitions: function (security) {
|
||||||
var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
|
var auths = Object.assign({}, window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions);
|
||||||
var oauth2Arr = [];
|
var oauth2Arr = [];
|
||||||
var authsArr = [];
|
var authsArr = [];
|
||||||
var utils = window.SwaggerUi.utils;
|
var utils = window.SwaggerUi.utils;
|
||||||
@@ -18,7 +18,7 @@ window.SwaggerUi.utils = {
|
|||||||
if (!auths[key]) { continue; }
|
if (!auths[key]) { continue; }
|
||||||
auths[key] = auths[key] || {};
|
auths[key] = auths[key] || {};
|
||||||
if (auths[key].type === 'oauth2') {
|
if (auths[key].type === 'oauth2') {
|
||||||
singleOauth2Security[key] = auths[key];
|
singleOauth2Security[key] = Object.assign({}, auths[key]);
|
||||||
for (var i in singleOauth2Security[key].scopes) {
|
for (var i in singleOauth2Security[key].scopes) {
|
||||||
if (item[key].indexOf(i) < 0) {
|
if (item[key].indexOf(i) < 0) {
|
||||||
delete singleOauth2Security[key].scopes[i];
|
delete singleOauth2Security[key].scopes[i];
|
||||||
@@ -26,11 +26,11 @@ window.SwaggerUi.utils = {
|
|||||||
}
|
}
|
||||||
singleOauth2Security[key].scopes = utils.parseOauth2Scopes(singleOauth2Security[key].scopes);
|
singleOauth2Security[key].scopes = utils.parseOauth2Scopes(singleOauth2Security[key].scopes);
|
||||||
} else {
|
} else {
|
||||||
singleSecurity[key] = auths[key];
|
singleSecurity[key] = Object.assign({}, auths[key]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (item[key].type === 'oauth2') {
|
if (item[key].type === 'oauth2') {
|
||||||
singleOauth2Security[key] = item[key];
|
singleOauth2Security[key] = Object.assign({}, item[key]);
|
||||||
singleOauth2Security[key].scopes = utils.parseOauth2Scopes(singleOauth2Security[key].scopes);
|
singleOauth2Security[key].scopes = utils.parseOauth2Scopes(singleOauth2Security[key].scopes);
|
||||||
} else {
|
} else {
|
||||||
singleSecurity[key] = item[key];
|
singleSecurity[key] = item[key];
|
||||||
@@ -48,7 +48,8 @@ window.SwaggerUi.utils = {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
parseOauth2Scopes: function (scopes) {
|
parseOauth2Scopes: function (data) {
|
||||||
|
var scopes = Object.assign({}, data);
|
||||||
var result = [];
|
var result = [];
|
||||||
var key;
|
var key;
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,16 @@
|
|||||||
'use strict';
|
'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({
|
SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||||
events: {
|
events: {
|
||||||
'click .auth_submit_button': 'authorizeClick',
|
'click .auth_submit_button': 'authorizeClick',
|
||||||
@@ -38,6 +49,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
|
|
||||||
authorizeClick: function (e) {
|
authorizeClick: function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
e.stopPropagation();
|
||||||
|
|
||||||
if (this.collection.isValid()) {
|
if (this.collection.isValid()) {
|
||||||
this.authorize();
|
this.authorize();
|
||||||
@@ -101,7 +113,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
||||||
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
||||||
} else if (type === 'oauth2') {
|
} else if (type === 'oauth2') {
|
||||||
//todo add handling login of oauth2
|
this.handleOauth2Login(auth);
|
||||||
}
|
}
|
||||||
}, this);
|
}, this);
|
||||||
|
|
||||||
@@ -118,5 +130,91 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.router.load();
|
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('');
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ SwaggerUi.Models.Oauth2Model = Backbone.Model.extend({
|
|||||||
auth.scopes[index].checked = val;
|
auth.scopes[index].checked = val;
|
||||||
|
|
||||||
this.set(auth);
|
this.set(auth);
|
||||||
|
this.validate();
|
||||||
},
|
},
|
||||||
|
|
||||||
validate: function () {
|
validate: function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user