[auth] added support of oauth2 in security popup

This commit is contained in:
Anna Bodnia
2016-03-07 21:09:40 +02:00
parent d2b94d5a01
commit 4a49b783f7
6 changed files with 56 additions and 32 deletions

40
dist/swagger-ui.js vendored
View File

@@ -18893,15 +18893,13 @@ window.SwaggerUi = Backbone.Router.extend({
router: this router: this
}).render(); }).render();
if (!_.isEmpty(this.api.securityDefinitions)){ if (!_.isEmpty(this.api.securityDefinitions)){
authsModel = { authsModel = _.map(this.api.securityDefinitions, function (auth, name) {
auths: _.map(this.api.securityDefinitions, function (auth, name) {
var result = {}; var result = {};
result[name] = auth; result[name] = auth;
return result; return result;
}) });
};
this.authView = new SwaggerUi.Views.AuthButtonView({ this.authView = new SwaggerUi.Views.AuthButtonView({
model: authsModel, data: SwaggerUi.utils.parseSecurityDefinitions(authsModel),
router: this router: this
}); });
$('#auth_container').append(this.authView.render().el); $('#auth_container').append(this.authView.render().el);
@@ -19057,34 +19055,46 @@ 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 = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
var result = []; var oauth2Arr = [];
var authsArr = [];
if (!Array.isArray(security)) { return null; } if (!Array.isArray(security)) { return null; }
security.forEach(function (item) { security.forEach(function (item) {
var singleSecurity = {}; var singleSecurity = {};
var singleOauth2Security = {};
for (var key in item) { for (var key in item) {
if (Array.isArray(item[key])) { if (Array.isArray(item[key])) {
if (!auths[key]) { continue; } if (!auths[key]) { continue; }
auths[key] = auths[key] || {}; auths[key] = auths[key] || {};
singleSecurity[key] = auths[key];
if (auths[key].type === 'oauth2') { if (auths[key].type === 'oauth2') {
for (var i in singleSecurity[key].scopes) { singleOauth2Security[key] = auths[key];
for (var i in singleOauth2Security[key].scopes) {
if (item[key].indexOf(i) < 0) { if (item[key].indexOf(i) < 0) {
delete singleSecurity[key].scopes[i]; delete singleOauth2Security[key].scopes[i];
} }
} }
} else {
singleSecurity[key] = auths[key];
} }
} else {
if (item[key].type === 'oauth2') {
singleOauth2Security[key] = item[key];
} else { } else {
singleSecurity[key] = item[key]; singleSecurity[key] = item[key];
} }
} }
}
result.push(singleSecurity); authsArr.push(singleSecurity);
oauth2Arr.push(singleOauth2Security);
}); });
return result; return {
auths : authsArr,
oauth2: oauth2Arr
};
} }
}; };
'use strict'; 'use strict';
@@ -19134,7 +19144,9 @@ SwaggerUi.Views.AuthButtonView = Backbone.View.extend({
initialize: function(opts) { initialize: function(opts) {
this.options = opts || {}; this.options = opts || {};
this.options.data = this.options.data || {};
this.router = this.options.router; this.router = this.options.router;
this.auths = this.options.data.auths;
}, },
render: function () { render: function () {
@@ -19150,7 +19162,7 @@ SwaggerUi.Views.AuthButtonView = Backbone.View.extend({
authsModel = { authsModel = {
title: 'Available authorizations', title: 'Available authorizations',
content: this.renderAuths(this.model.auths) content: this.renderAuths(this.auths)
}; };
this.popup = new SwaggerUi.Views.PopupView({model: authsModel}); this.popup = new SwaggerUi.Views.PopupView({model: authsModel});
@@ -19797,10 +19809,10 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
} }
if (Array.isArray(this.model.security)) { if (Array.isArray(this.model.security)) {
var authsModel = { auths: SwaggerUi.utils.parseSecurityDefinitions(this.model.security) }; var authsModel = SwaggerUi.utils.parseSecurityDefinitions(this.model.security);
authsModel.isLogout = !_.isEmpty(window.swaggerUi.api.clientAuthorizations.authz); authsModel.isLogout = !_.isEmpty(window.swaggerUi.api.clientAuthorizations.authz);
this.authView = new SwaggerUi.Views.AuthButtonView({model: authsModel, router: this.router}); this.authView = new SwaggerUi.Views.AuthButtonView({data: authsModel, router: this.router});
this.$('.authorize-wrapper').append(this.authView.render().el); this.$('.authorize-wrapper').append(this.authView.render().el);
} }

File diff suppressed because one or more lines are too long

View File

@@ -146,15 +146,13 @@ window.SwaggerUi = Backbone.Router.extend({
router: this router: this
}).render(); }).render();
if (!_.isEmpty(this.api.securityDefinitions)){ if (!_.isEmpty(this.api.securityDefinitions)){
authsModel = { authsModel = _.map(this.api.securityDefinitions, function (auth, name) {
auths: _.map(this.api.securityDefinitions, function (auth, name) {
var result = {}; var result = {};
result[name] = auth; result[name] = auth;
return result; return result;
}) });
};
this.authView = new SwaggerUi.Views.AuthButtonView({ this.authView = new SwaggerUi.Views.AuthButtonView({
model: authsModel, data: SwaggerUi.utils.parseSecurityDefinitions(authsModel),
router: this router: this
}); });
$('#auth_container').append(this.authView.render().el); $('#auth_container').append(this.authView.render().el);

View File

@@ -3,33 +3,45 @@
window.SwaggerUi.utils = { window.SwaggerUi.utils = {
parseSecurityDefinitions: function (security) { parseSecurityDefinitions: function (security) {
var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions; var auths = window.swaggerUi.api.authSchemes || window.swaggerUi.api.securityDefinitions;
var result = []; var oauth2Arr = [];
var authsArr = [];
if (!Array.isArray(security)) { return null; } if (!Array.isArray(security)) { return null; }
security.forEach(function (item) { security.forEach(function (item) {
var singleSecurity = {}; var singleSecurity = {};
var singleOauth2Security = {};
for (var key in item) { for (var key in item) {
if (Array.isArray(item[key])) { if (Array.isArray(item[key])) {
if (!auths[key]) { continue; } if (!auths[key]) { continue; }
auths[key] = auths[key] || {}; auths[key] = auths[key] || {};
singleSecurity[key] = auths[key];
if (auths[key].type === 'oauth2') { if (auths[key].type === 'oauth2') {
for (var i in singleSecurity[key].scopes) { singleOauth2Security[key] = auths[key];
for (var i in singleOauth2Security[key].scopes) {
if (item[key].indexOf(i) < 0) { if (item[key].indexOf(i) < 0) {
delete singleSecurity[key].scopes[i]; delete singleOauth2Security[key].scopes[i];
} }
} }
} else {
singleSecurity[key] = auths[key];
} }
} else {
if (item[key].type === 'oauth2') {
singleOauth2Security[key] = item[key];
} else { } else {
singleSecurity[key] = item[key]; singleSecurity[key] = item[key];
} }
} }
}
result.push(singleSecurity); authsArr.push(singleSecurity);
oauth2Arr.push(singleOauth2Security);
}); });
return result; return {
auths : authsArr,
oauth2: oauth2Arr
};
} }
}; };

View File

@@ -12,7 +12,9 @@ SwaggerUi.Views.AuthButtonView = Backbone.View.extend({
initialize: function(opts) { initialize: function(opts) {
this.options = opts || {}; this.options = opts || {};
this.options.data = this.options.data || {};
this.router = this.options.router; this.router = this.options.router;
this.auths = this.options.data.auths;
}, },
render: function () { render: function () {
@@ -28,7 +30,7 @@ SwaggerUi.Views.AuthButtonView = Backbone.View.extend({
authsModel = { authsModel = {
title: 'Available authorizations', title: 'Available authorizations',
content: this.renderAuths(this.model.auths) content: this.renderAuths(this.auths)
}; };
this.popup = new SwaggerUi.Views.PopupView({model: authsModel}); this.popup = new SwaggerUi.Views.PopupView({model: authsModel});

View File

@@ -258,10 +258,10 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
} }
if (Array.isArray(this.model.security)) { if (Array.isArray(this.model.security)) {
var authsModel = { auths: SwaggerUi.utils.parseSecurityDefinitions(this.model.security) }; var authsModel = SwaggerUi.utils.parseSecurityDefinitions(this.model.security);
authsModel.isLogout = !_.isEmpty(window.swaggerUi.api.clientAuthorizations.authz); authsModel.isLogout = !_.isEmpty(window.swaggerUi.api.clientAuthorizations.authz);
this.authView = new SwaggerUi.Views.AuthButtonView({model: authsModel, router: this.router}); this.authView = new SwaggerUi.Views.AuthButtonView({data: authsModel, router: this.router});
this.$('.authorize-wrapper').append(this.authView.render().el); this.$('.authorize-wrapper').append(this.authView.render().el);
} }