github.com/qubitproducts/logspray@v0.2.14/server/swagger-ui/src/main/javascript/view/Oauth2Model.js (about)

     1  'use strict';
     2  
     3  SwaggerUi.Models.Oauth2Model = Backbone.Model.extend({
     4      defaults: {
     5          scopes: {},
     6          isPasswordFlow: false,
     7          clientAuthenticationType: 'none'
     8      },
     9  
    10      initialize: function () {
    11          if(this.attributes && this.attributes.scopes) {
    12              var attributes = _.cloneDeep(this.attributes);
    13              var i, scopes = [];
    14              for(i in attributes.scopes) {
    15                  var scope = attributes.scopes[i];
    16                  if(typeof scope.description === 'string') {
    17                      scopes[scope] = attributes.scopes[i];
    18                      scopes.push(attributes.scopes[i]);
    19                  }
    20              }
    21              attributes.scopes = scopes;
    22              this.attributes = attributes;
    23          }
    24  
    25          if (this.attributes && this.attributes.flow) {
    26              var flow = this.attributes.flow;
    27              this.set('isPasswordFlow', flow === 'password');
    28              this.set('requireClientAuthentication', flow === 'application');
    29              this.set('clientAuthentication', flow === 'password' || flow === 'application');
    30          }
    31          this.on('change', this.validate);
    32      },
    33  
    34      setScopes: function (name, val) {
    35          var auth = _.extend({}, this.attributes);
    36          var index = _.findIndex(auth.scopes, function(o) {
    37              return o.scope === name;
    38          });
    39          auth.scopes[index].checked = val;
    40  
    41          this.set(auth);
    42          this.validate();
    43      },
    44  
    45      validate: function () {
    46        var valid = false;
    47        if (this.get('isPasswordFlow') &&
    48            (!this.get('username'))) {
    49            return false;
    50        }
    51  
    52        if (this.get('clientAuthenticationType') in ['basic', 'request-body'] &&
    53            (!this.get('clientId'))) {
    54            return false;
    55        }
    56  
    57        var scp = this.get('scopes');
    58        var idx =  _.findIndex(scp, function (o) {
    59           return o.checked === true;
    60        });
    61  
    62        if(scp.length > 0 && idx >= 0) {
    63            valid = true;
    64        }
    65  
    66        if(scp.length === 0) {
    67            valid = true;
    68        }
    69  
    70        this.set('valid', valid);
    71  
    72        return valid;
    73      }
    74  });