[auth] Added api key and basic authorization

This commit is contained in:
Anna Bodnia
2016-03-04 20:55:57 +02:00
parent 0e698af5b1
commit 42be315276
17 changed files with 433 additions and 294 deletions

View File

@@ -1,39 +0,0 @@
'use strict';
SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to global SwaggerUi
events:{
'click .auth_submit_button' : 'applyApiKey',
'click .auth_logout__button' : 'clickLogout'
},
template: Handlebars.templates.apikey_button_view,
initialize: function(opts) {
this.options = opts || {};
this.router = this.options.router;
},
render: function(){
this.$el.html(this.template(this.model));
return this;
},
applyApiKey: function() {
var keyAuth = new SwaggerClient.ApiKeyAuthorization(
this.model.name,
this.$('.input_apiKey_entry').val(),
this.model.in
);
this.router.api.clientAuthorizations.add(this.model.name, keyAuth);
this.router.load();
},
clickLogout: function () {
window.swaggerUi.api.clientAuthorizations.remove(this.model.name);
this.router.load();
}
});

View File

@@ -23,6 +23,7 @@ SwaggerUi.Views.AuthButtonView = Backbone.View.extend({
authorizeBtnClick: function (e) {
var authsModel;
e.preventDefault();
authsModel = {
@@ -35,30 +36,14 @@ SwaggerUi.Views.AuthButtonView = Backbone.View.extend({
},
renderAuths: function (auths) {
var name, authEl, authModel;
var el = $('<div>');
var authz = window.swaggerUi.api.clientAuthorizations.authz;
var $el = $('<div>');
for (name in auths) {
authModel = _.extend({}, auths[name]);
auths.forEach(function (auth) {
var authEl = new SwaggerUi.Views.AuthView({data: auth, router: this.router}).render().el;
$el.append(authEl);
}, this);
if (authz[name]) {
_.extend(authModel, {
isLogout: true,
value: authz[name].value
});
}
if (authModel.type === 'apiKey') {
authEl = new SwaggerUi.Views.ApiKeyButton({model: authModel, router: this.router}).render().el;
el.append(authEl);
} else if (authModel.type === 'basic' && el.find('.basic_auth_container').length === 0) {
authEl = new SwaggerUi.Views.BasicAuthButton({model: authModel, router: this.router}).render().el;
el.append(authEl);
}
}
return el;
return $el;
}
});

View File

@@ -0,0 +1,126 @@
'use strict';
SwaggerUi.Views.AuthView = Backbone.View.extend({
events: {
'click .auth_submit_button': 'authorizeClick',
'click .auth_logout__button': 'logoutClick'
},
tpls: {
main: Handlebars.templates.auth_view
},
selectors: {
innerEl: '.auth_inner'
},
initialize: function(opts) {
this.options = opts || {};
opts.data = opts.data || {};
this.router = this.options.router;
this.collection = new Backbone.Collection();
this.collection.add(this.parseData(opts.data));
this.$el.html(this.tpls.main({isLogout: this.isAuthorizedCollection()}));
this.$innerEl = this.$(this.selectors.innerEl);
},
render: function () {
this.renderAuths();
if (!this.$innerEl.html()) {
this.$el.html('');
}
return this;
},
authorizeClick: function (e) {
e.preventDefault();
if (this.isValidCollection()) {
this.authorize();
}
},
parseData: function (data) {
var authz = window.swaggerUi.api.clientAuthorizations.authz;
return _.map(data, function (auth, name) {
var isBasic = authz.basic && auth.type === 'basic';
if (authz[name] || isBasic) {
_.extend(auth, {
isLogout: true,
value: isBasic ? '' : authz[name].value,
valid: true
});
}
return auth;
});
},
renderAuths: function () {
this.collection.each(function (auth) {
this.renderOneAuth(auth);
}, this);
},
renderOneAuth: function (authModel) {
var authEl;
var type = authModel.get('type');
if (type === 'apiKey') {
authEl = new SwaggerUi.Views.ApiKeyAuthView({model: authModel, router: this.router}).render().el;
this.$innerEl.append(authEl);
} else if (type === 'basic' && this.$innerEl.find('.basic_auth_container').length === 0) {
authEl = new SwaggerUi.Views.BasicAuthView({model: authModel, router: this.router}).render().el;
this.$innerEl.append(authEl);
}
},
isValidCollection: function () {
return this.collection.length === this.collection.where({ valid: true }).length;
},
authorize: function () {
this.collection.forEach(function (auth) {
var keyAuth, basicAuth;
var type = auth.get('type');
if (type === 'apiKey') {
keyAuth = new SwaggerClient.ApiKeyAuthorization(
auth.get('name'),
auth.get('value'),
auth.get('in')
);
this.router.api.clientAuthorizations.add(auth.get('name'), keyAuth);
} else if (type === 'basic') {
basicAuth = new SwaggerClient.PasswordAuthorization(auth.get('username'), auth.get('password'));
this.router.api.clientAuthorizations.add(auth.get('type'), basicAuth);
}
}, this);
this.router.load();
},
isAuthorizedCollection: function () {
return this.collection.length === this.collection.where({ isLogout: true }).length;
},
logoutClick: function (e) {
e.preventDefault();
this.collection.forEach(function (auth) {
var name = auth.get('name');
window.swaggerUi.api.clientAuthorizations.remove(name);
});
this.router.load();
}
});

View File

@@ -1,38 +0,0 @@
'use strict';
SwaggerUi.Views.BasicAuthButton = Backbone.View.extend({
initialize: function (opts) {
this.options = opts || {};
this.router = this.options.router;
},
template: Handlebars.templates.basic_auth_button_view,
render: function(){
$(this.el).html(this.template(this.model));
return this;
},
events: {
'submit .key_input_container' : 'applyPassword',
'click .auth_logout__button' : 'clickLogout'
},
applyPassword: function(event) {
event.preventDefault();
var username = this.$('.basic_auth__username').val();
var password = this.$('.basic_auth__password').val();
var basicAuth = new SwaggerClient.PasswordAuthorization(username, password);
this.router.api.clientAuthorizations.add(this.model.type, basicAuth);
this.router.load();
},
clickLogout: function () {
window.swaggerUi.api.clientAuthorizations.remove(this.model.name);
this.router.load();
}
});

View File

@@ -12,7 +12,7 @@ SwaggerUi.Views.HeaderView = Backbone.View.extend({
showPetStore: function(){
this.trigger('update-swagger-ui', {
url:'http://petstore.swagger.io/v2/swagger.json'
url:'http://localhost:3001/swagger.json'
});
},

View File

@@ -84,13 +84,19 @@ SwaggerUi.Views.MainView = Backbone.View.extend({
render: function () {
// Render the outer container for resources
var authsModel;
var authsModel, parsedDefinitions;
$(this.el).html(Handlebars.templates.main(this.model));
this.model.securityDefinitions = this.model.securityDefinitions || {};
if (!_.isEmpty(this.model.securityDefinitions)) {
authsModel = { auths: this.model.securityDefinitions };
parsedDefinitions = _.map(this.model.securityDefinitions, function (auth, name) {
var result = {};
result[name] = auth;
return result;
});
authsModel = { auths: parsedDefinitions };
authsModel.isLogout = !_.isEmpty(window.swaggerUi.api.clientAuthorizations.authz);
this.authView = new SwaggerUi.Views.AuthButtonView({model: authsModel, router: this.router});