All files / src/impl SimplePrincipal.js

98.63% Statements 72/73
96.88% Branches 31/32
96.77% Functions 30/31
98.63% Lines 72/73
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233                              3x 3x 3x         69x   69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x 69x               54x       15x       30x                       15x 6x     15x 6x     15x                       3x                         57x 3x     57x 51x 51x 3x 3x   6x 3x 3x 3x       51x                     3x       12x       6x       6x       6x       6x       6x       3x       6x       9x       24x       27x       24x       6x       15x 12x 3x 9x 3x 6x 3x     15x       30x       6x       30x       6x 3x   3x         6x       6x       6x       6x       3x  
/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';
 
var winston = require('winston');
var Validate = require('../util/Validate');
var config = require('../../config/config')();
 
class SimplePrincipal {
  /*eslint max-params: ["error", 6]*/
  constructor(domain, name, creds, roles, issueTime,authority) {
    winston.level = config.logLevel;
 
    this._domain = domain;
    this._name = name;
    this._creds = creds;
    this._roles = roles;
    this._issueTime = Number(issueTime);
    this._authority = authority;
    this._fullName = null;
    this._unsignedCreds = null;
    this._ip = null;
    this._authorizedService = null;
    this._originalRequestor = null;
    this._keyService = null;
    this._keyId = null;
    this._x509Certificate = null;
  }
 
  static setConfig(c) {
    config = Object.assign({}, config, c.auth_core);
  }
 
  static _simplePrincipal(domain, name, creds, issueTime, authority) {
    return new SimplePrincipal(domain, name, creds, null, issueTime, authority);
  }
 
  static _simplePrincipalByRoles(domain, creds, roles, authority) {
    return new SimplePrincipal(domain, null, creds, roles, 0, authority);
  }
 
  static create(domain, name, creds) {
    return this.createByUserIdentity(domain, name, creds, 0, null);
  }
 
  /**
   * Create a Principal based on a given RoleToken
   * @param domain Domain name that the RoleToken was issued for
   * @param creds Credentials of the principal (RoleToken)
   * @param roles List of roles defined in the token
   * @param authority authority responsible for the credentials (RoleAuthority)
   * @return a Principal for the given set of roles in a domain
   */
  static createByRoles(domain, creds, roles, authority) {
    if (!Validate.domainName(domain)) {
      winston.warn('WARNING: domain name doesn\'t validate: ' + creds);
    }
 
    if (!roles || roles.length === 0) {
      winston.warn('WARNING: zero roles: ' + creds);
    }
 
    return this._simplePrincipalByRoles(domain, creds, roles, authority);
  }
 
  /**
   * Create a Principal for the given identity
   * @param domain Domain name for the identity
   * @param name Name of the identity
   * @param creds Credentials of the principal (PrincipalToken which could be either UserToken or ServiceToken)
   * @param authority authority responsible for the credentials (e.g. PrincipalAuthority)
   * @return a Principal for the identity
   */
  static createByIdentity(domain, name, creds, authority) {
    return this.createByUserIdentity(domain, name, creds, '0', authority);
  }
 
  /**
   * Create a Principal for the given user identity
   * @param domain Domain name for the identity (For users this will always be user)
   * @param name Name of the identity
   * @param creds Credentials of the principal (e.g. Cookie.User)
   * @param issueTime when the User Cookie/Credentials was issued
   * @param authority authority responsible for the credentials (e.g. UserAuthority)
   * @return a Principal for the identity
   */
  static createByUserIdentity(domain, name, creds, issueTime, authority) {
    if (!Validate.principalName(name)) {
      winston.warn('WARNING: principal name doesn\'t validate: ' + name);
    }
 
    if (domain) {
      var matchDomain = (!authority) ? null : authority.getDomain();
      if (matchDomain && domain !== matchDomain) {
        winston.warn('FAIL: domain mismatch for user ' + name + ' in authority + ' + authority);
        return null;
      }
    } else if (authority) {
      Eif (authority.getDomain()) {
        winston.warn('FAIL: domain mismatch for user ' + name + ' in authority + ' + authority);
        return null;
      }
    }
 
    return this._simplePrincipal(domain, name, creds, issueTime, authority);
  }
 
  /**
   * Create a Principal for the given host identity
   * @param appId Application identifer
   * @param creds Credentials of the principal
   * @param authority authority responsible for the credentials (e.g. HostAuthority)
   * @return a Principal for the host identity
   */
  static createByHostIdentity(appId, creds, authority) {
    return this._simplePrincipal(null, appId, creds, 0, authority);
  }
 
  setUnsignedCreds(unsignedCreds) {
    this._unsignedCreds = unsignedCreds;
  }
 
  setAuthorizedService(authorizedService) {
    this._authorizedService = authorizedService;
  }
 
  setIP(ip) {
    this._ip = ip;
  }
 
  setOriginalRequestor(originalRequestor) {
    this._originalRequestor = originalRequestor;
  }
 
  setKeyService(keyService) {
    this._keyService = keyService;
  }
 
  setKeyId(keyId) {
    this._keyId = keyId;
  }
 
  setX509Certificate(x509Certificate) {
    this._x509Certificate = x509Certificate;
  }
 
  getIP() {
    return this._ip;
  }
 
  getUnsignedCreds() {
    return this._unsignedCreds;
  }
 
  getAuthority() {
    return this._authority;
  }
 
  getDomain() {
    return this._domain;
  }
 
  getName() {
    return this._name;
  }
 
  getOriginalRequestor() {
    return this._originalRequestor;
  }
 
  getFullName() {
    if (!this._fullName) {
      if (this._domain && this._name) {
        this._fullName = this._domain + '.' + this._name;
      } else if (this._domain) {
        this._fullName = this._domain;
      } else if (this._name) {
        this._fullName = this._name;
      }
    }
    return this._fullName;
  }
 
  getCredentials() {
    return this._creds;
  }
 
  getRoles() {
    return this._roles;
  }
 
  getIssueTime() {
    return this._issueTime;
  }
 
  toString() {
    if (!this._roles) {
      return this._domain + '.' + this._name;
    } else {
      return 'ZToken_' + this._domain + '~' + this._roles.toString().replace('[', '').replace(']', '');
    }
  }
 
  getAuthorizedService() {
    return this._authorizedService;
  }
 
  getKeyService() {
    return this._keyService;
  }
 
  getKeyId() {
    return this._keyId;
  }
 
  getX509Certificate() {
    return this._x509Certificate;
  }
}
 
module.exports = SimplePrincipal;