github.com/qubitproducts/logspray@v0.2.14/server/swagger-ui/src/main/javascript/view/AuthsCollection.js (about) 1 'use strict'; 2 3 SwaggerUi.Collections.AuthsCollection = Backbone.Collection.extend({ 4 constructor: function() { 5 var args = Array.prototype.slice.call(arguments); 6 7 args[0] = this.parse(args[0]); 8 9 Backbone.Collection.apply(this, args); 10 }, 11 12 add: function (model) { 13 var args = Array.prototype.slice.call(arguments); 14 15 if (Array.isArray(model)) { 16 args[0] = _.map(model, function(val) { 17 return this.handleOne(val); 18 }, this); 19 } else { 20 args[0] = this.handleOne(model); 21 } 22 23 Backbone.Collection.prototype.add.apply(this, args); 24 }, 25 26 handleOne: function (model) { 27 var result = model; 28 29 if (! (model instanceof Backbone.Model) ) { 30 switch (model.type) { 31 case 'oauth2': 32 result = new SwaggerUi.Models.Oauth2Model(model); 33 break; 34 case 'basic': 35 result = new SwaggerUi.Models.BasicAuthModel(model); 36 break; 37 case 'apiKey': 38 result = new SwaggerUi.Models.ApiKeyAuthModel(model); 39 break; 40 default: 41 result = new Backbone.Model(model); 42 } 43 } 44 45 return result; 46 }, 47 48 isValid: function () { 49 var valid = true; 50 51 this.models.forEach(function(model) { 52 if (!model.validate()) { 53 valid = false; 54 } 55 }); 56 57 return valid; 58 }, 59 60 isAuthorized: function () { 61 return this.length === this.where({ isLogout: true }).length; 62 }, 63 64 isPartiallyAuthorized: function () { 65 return this.where({ isLogout: true }).length > 0; 66 }, 67 68 parse: function (data) { 69 var authz = {}; 70 71 if(typeof window.swaggerUi !== 'undefined') { 72 authz = Object.assign({}, window.swaggerUi.api.clientAuthorizations.authz); 73 } 74 75 return _.map(data, function (auth, name) { 76 var isBasic = authz[name] && auth.type === 'basic' && authz[name].username && authz[name].password; 77 78 _.extend(auth, { 79 title: name 80 }); 81 82 if (authz[name] || isBasic) { 83 _.extend(auth, { 84 isLogout: true, 85 value: isBasic ? undefined : authz[name].value, 86 username: isBasic ? authz[name].username : undefined, 87 password: isBasic ? authz[name].password : undefined, 88 valid: true 89 }); 90 } 91 92 return auth; 93 }); 94 } 95 });