All files / src/access-token Token.js

100% Statements 44/44
100% Branches 24/24
100% Functions 10/10
100% Lines 44/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                7x   7x 7x 7x 7x 7x 7x 7x 7x 7x             1x 1x             1x 5x             1x 1x                           1x 15x                                 1x 15x 15x 15x 15x 15x 15x 15x 15x               1x 4x 4x 4x 4x 4x 4x 4x 4x               1x 10x 10x             1x 7x             1x 3x     1x  
/**
 
 Copyright (c) 2018 Intuit
 #
 # 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.
 
 */
 
/**
 * @namespace Token
 */
 
'use strict';
 
/**
 * @param {Cache} options.cache
 * @param {string} options.cacheId
 * @constructor
 * @property {Cache} _cache
 * @property {string} _cacheId
 */
function Token(params) {
  params = params || {};
 
  this.realmId = params.realmId || '';
  this.token_type = params.token_type || '';
  this.access_token = params.access_token || '';
  this.refresh_token = params.refresh_token || '';
  this.expires_in = params.expires_in || 0;
  this.x_refresh_token_expires_in = params.x_refresh_token_expires_in || 0;
  this.id_token = params.id_token || '';
  this.latency = params.latency || 60 * 1000;
  this.createdAt = params.createdAt || Date.now();
}
 
/**
 * get accessToken()
 * @returns {string} access_token
 */
Token.prototype.accessToken = function accessToken() {
  return this.getToken().access_token;
};
 
/**
 * get refreshToken()
 * @returns {string} refresh_token
 */
Token.prototype.refreshToken = function refreshToken() {
  return this.getToken().refresh_token;
};
 
/**
 * get tokenType()
 * @returns {string} token_type
 */
Token.prototype.tokenType = function tokenType() {
  return this.getToken().token_type;
};
 
 
/**
 * Helper Method to get accessToken { get Token Object }
 * @returns {{
 *  token_type: *,
 *  access_token: *,
 *  expires_in: *,
 *  refresh_token: *,
 *  x_refresh_token_expires_in: *
 * }}
 */
Token.prototype.getToken = function getToken() {
  return {
    token_type: this.token_type,
    access_token: this.access_token,
    expires_in: this.expires_in,
    refresh_token: this.refresh_token,
    x_refresh_token_expires_in: this.x_refresh_token_expires_in,
    realmId: this.realmId,
    id_token: this.id_token,
    createdAt: this.createdAt,
  };
};
 
/**
 * Helper Method to set accessToken { set Token Object }
 * @param tokenData
 * @returns {Token}
 */
Token.prototype.setToken = function setToken(tokenData) {
  this.access_token = tokenData.access_token;
  this.refresh_token = tokenData.refresh_token;
  this.token_type = tokenData.token_type;
  this.expires_in = tokenData.expires_in;
  this.x_refresh_token_expires_in = tokenData.x_refresh_token_expires_in;
  this.id_token = tokenData.id_token || '';
  this.createdAt = tokenData.createdAt || Date.now();
  return this;
};
 
/**
 * Helper Method to clear accessToken { clear Token Object }
 * @param
 * @returns {Token}
 */
Token.prototype.clearToken = function clearToken() {
  this.access_token = '';
  this.refresh_token = '';
  this.token_type = '';
  this.expires_in = 0;
  this.x_refresh_token_expires_in = 0;
  this.id_token = '';
  this.createdAt = 0;
  return this;
};
 
/**
 * Helper Method to check token expiry { set Token Object }
 * @param seconds
 * @returns {boolean}
 */
Token.prototype._checkExpiry = function _checkExpiry(seconds) {
  const expiry = this.createdAt + (seconds * 1000);
  return (expiry - this.latency > Date.now());
};
 
/**
 * Check if access_token is valid
 * @returns {boolean}
 */
Token.prototype.isAccessTokenValid = function isAccessTokenValid() {
  return this._checkExpiry(this.expires_in);
};
 
/**
 * Check if there is a valid (not expired) access token
 * @return {boolean}
 */
Token.prototype.isRefreshTokenValid = function isRefreshTokenValid() {
  return this._checkExpiry(this.x_refresh_token_expires_in);
};
 
module.exports = Token;