[auth] Added logout for auth with apiKey and basic auth

This commit is contained in:
bodnia
2016-03-02 01:04:56 +02:00
committed by Anna Bodnia
parent 8280cc6b34
commit 67881fb07e
15 changed files with 170 additions and 60 deletions

View File

@@ -3,7 +3,8 @@
SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to global SwaggerUi
events:{
'click .auth_submit_button' : 'applyApiKey'
'click .auth_submit_button' : 'applyApiKey',
'click .auth_logout__button' : 'clickLogout'
},
template: Handlebars.templates.apikey_button_view,
@@ -28,6 +29,11 @@ SwaggerUi.Views.ApiKeyButton = Backbone.View.extend({ // TODO: append this to gl
);
this.router.api.clientAuthorizations.add(this.model.name, keyAuth);
this.router.load();
},
clickLogout: function () {
window.swaggerUi.api.clientAuthorizations.remove(this.model.name);
this.remove();
}
});

View File

@@ -28,23 +28,23 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
authsModel = {
title: 'Available authorizations',
content: this.renderAuths()
content: this.renderAuths(this.model.auths)
};
this.popup = new SwaggerUi.Views.PopupView({model: authsModel});
this.popup.render();
},
renderAuths: function () {
renderAuths: function (auths) {
var name, authEl, auth;
var el = $('<div>');
//todo refactor, copy-pasted from MainView.js
for (name in this.model.auths) {
auth = this.model.auths[name];
for (name in auths) {
auth = auths[name];
if (auth.type === 'apiKey') {
authEl = new SwaggerUi.Views.ApiKeyButton({model: auth, router: this.router}).render().el;
authEl = new SwaggerUi.Views.ApiKeyButton({model: auth, router: this.router}).render().el;
el.append(authEl);
} else if (auth.type === 'basic' && el.find('.basic_auth_container').length === 0) {
authEl = new SwaggerUi.Views.BasicAuthButton({model: auth, router: this.router}).render().el;
@@ -57,16 +57,40 @@ SwaggerUi.Views.AuthView = Backbone.View.extend({
logoutClick: function (e) {
var authsModel;
e.preventDefault();
authsModel = {
title: 'Logout authorizations'
title: 'Logout authorizations',
content: this.renderAuths(this.getAuthMap())
};
this.popup = new SwaggerUi.Views.PopupView({model: authsModel});
this.popup.render();
},
console.log(window.swaggerUi.api.clientAuthorizations.authz);
getAuthMap: function () {
var authsMap = {};
_.forEach(window.swaggerUi.api.clientAuthorizations.authz, function (value, key) {
if (key === 'basic') {
authsMap.basic = {
type: key,
isLogout: true,
name: key
};
} else {
authsMap[key] = {
type: 'apiKey',
'in': value.type,
value: value.value,
isLogout: true,
name: key
};
}
});
return authsMap;
}
});

View File

@@ -17,7 +17,8 @@ SwaggerUi.Views.BasicAuthButton = Backbone.View.extend({
},
events: {
'submit .key_input_container' : 'applyPassword'
'submit .key_input_container' : 'applyPassword',
'click .auth_logout__button' : 'clickLogout'
},
applyPassword: function(event) {
@@ -27,6 +28,11 @@ SwaggerUi.Views.BasicAuthButton = Backbone.View.extend({
var basicAuth = new SwaggerClient.PasswordAuthorization('basic', 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.remove();
}
});

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'
});
},