[auth] UI changes
This commit is contained in:
@@ -38,8 +38,13 @@ window.SwaggerUi.utils = {
|
||||
}
|
||||
}
|
||||
|
||||
authsArr.push(singleSecurity);
|
||||
oauth2Arr.push(singleOauth2Security);
|
||||
if (!_.isEmpty(singleSecurity)) {
|
||||
authsArr.push(singleSecurity);
|
||||
}
|
||||
|
||||
if (!_.isEmpty(singleOauth2Security)){
|
||||
oauth2Arr.push(singleOauth2Security);
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
22
src/main/javascript/view/ApiKeyAuthModel.js
Normal file
22
src/main/javascript/view/ApiKeyAuthModel.js
Normal file
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
SwaggerUi.Models.ApiKeyAuthModel = Backbone.Model.extend({
|
||||
defaults: {
|
||||
'in': '',
|
||||
name: '',
|
||||
title: '',
|
||||
value: ''
|
||||
},
|
||||
|
||||
initialize: function () {
|
||||
this.on('change', this.validate);
|
||||
},
|
||||
|
||||
validate: function () {
|
||||
var valid = !!this.get('value');
|
||||
|
||||
this.set('valid', valid);
|
||||
|
||||
return valid;
|
||||
}
|
||||
});
|
||||
@@ -22,12 +22,11 @@ SwaggerUi.Views.ApiKeyAuthView = Backbone.View.extend({ // TODO: append this to
|
||||
apiKeyChange: function (e) {
|
||||
var val = $(e.target).val();
|
||||
|
||||
this.model.set('valid', !!val);
|
||||
this.model.set('value', val);
|
||||
},
|
||||
|
||||
isValid: function () {
|
||||
return this.get('valid');
|
||||
return this.model.validate();
|
||||
}
|
||||
|
||||
});
|
||||
@@ -33,7 +33,10 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
this.collection = new SwaggerUi.Collections.AuthsCollection();
|
||||
this.collection.add(this.parseData(opts.data));
|
||||
|
||||
this.$el.html(this.tpls.main({isLogout: this.collection.isAuthorized()}));
|
||||
this.$el.html(this.tpls.main({
|
||||
isLogout: this.collection.isAuthorized(),
|
||||
isAuthorized: this.collection.isPartiallyAuthorized()
|
||||
}));
|
||||
this.$innerEl = this.$(this.selectors.innerEl);
|
||||
},
|
||||
|
||||
@@ -57,15 +60,21 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
},
|
||||
|
||||
parseData: function (data) {
|
||||
var authz = window.swaggerUi.api.clientAuthorizations.authz;
|
||||
var authz = Object.assign({}, window.swaggerUi.api.clientAuthorizations.authz);
|
||||
|
||||
return _.map(data, function (auth, name) {
|
||||
var isBasic = authz.basic && auth.type === 'basic';
|
||||
|
||||
_.extend(auth, {
|
||||
title: name
|
||||
});
|
||||
|
||||
if (authz[name] || isBasic) {
|
||||
_.extend(auth, {
|
||||
isLogout: true,
|
||||
value: isBasic ? '' : authz[name].value,
|
||||
value: isBasic ? undefined : authz[name].value,
|
||||
username: isBasic ? authz.basic.username : undefined,
|
||||
password: isBasic ? authz.basic.password : undefined,
|
||||
valid: true
|
||||
});
|
||||
}
|
||||
@@ -108,7 +117,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
auth.get('in')
|
||||
);
|
||||
|
||||
this.router.api.clientAuthorizations.add(auth.get('name'), keyAuth);
|
||||
this.router.api.clientAuthorizations.add(auth.get('title'), keyAuth);
|
||||
} else if (type === 'basic') {
|
||||
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
|
||||
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
|
||||
@@ -124,7 +133,7 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
|
||||
e.preventDefault();
|
||||
|
||||
this.collection.forEach(function (auth) {
|
||||
var name = auth.get('name');
|
||||
var name = auth.get('type') === 'basic' ? 'basic' : auth.get('title');
|
||||
|
||||
window.swaggerUi.api.clientAuthorizations.remove(name);
|
||||
});
|
||||
|
||||
@@ -23,6 +23,12 @@ SwaggerUi.Collections.AuthsCollection = Backbone.Collection.extend({
|
||||
case 'oauth2':
|
||||
result = new SwaggerUi.Models.Oauth2Model(model);
|
||||
break;
|
||||
case 'basic':
|
||||
result = new SwaggerUi.Models.BasicAuthModel(model);
|
||||
break;
|
||||
case 'apiKey':
|
||||
result = new SwaggerUi.Models.ApiKeyAuthModel(model);
|
||||
break;
|
||||
default:
|
||||
result = new Backbone.Model(model);
|
||||
}
|
||||
@@ -37,5 +43,9 @@ SwaggerUi.Collections.AuthsCollection = Backbone.Collection.extend({
|
||||
|
||||
isAuthorized: function () {
|
||||
return this.length === this.where({ isLogout: true }).length;
|
||||
},
|
||||
|
||||
isPartiallyAuthorized: function () {
|
||||
return this.where({ isLogout: true }).length > 0;
|
||||
}
|
||||
});
|
||||
21
src/main/javascript/view/BasicAuthModel.js
Normal file
21
src/main/javascript/view/BasicAuthModel.js
Normal file
@@ -0,0 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
SwaggerUi.Models.BasicAuthModel = Backbone.Model.extend({
|
||||
defaults: {
|
||||
username: '',
|
||||
password: '',
|
||||
title: 'basic'
|
||||
},
|
||||
|
||||
initialize: function () {
|
||||
this.on('change', this.validate);
|
||||
},
|
||||
|
||||
validate: function () {
|
||||
var valid = !!this.get('password') && !!this.get('username');
|
||||
|
||||
this.set('valid', valid);
|
||||
|
||||
return valid;
|
||||
}
|
||||
});
|
||||
@@ -25,11 +25,10 @@ SwaggerUi.Views.BasicAuthView = Backbone.View.extend({
|
||||
var attr = $el.prop('name');
|
||||
|
||||
this.model.set(attr, val);
|
||||
this.model.set('valid', !!this.model.get('password') && !!this.model.get('username'));
|
||||
},
|
||||
|
||||
isValid: function () {
|
||||
return this.get('valid');
|
||||
return this.model.validate();
|
||||
}
|
||||
|
||||
});
|
||||
@@ -136,7 +136,7 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
|
||||
|
||||
onLinkClick: function (e) {
|
||||
var el = e.target;
|
||||
if (el.tagName === 'A') {
|
||||
if (el.tagName === 'A' && el.href) {
|
||||
if (location.hostname !== el.hostname || location.port !== el.port) {
|
||||
e.preventDefault();
|
||||
window.open(el.href, '_blank');
|
||||
|
||||
Reference in New Issue
Block a user