Manual Reference Source Test

src/modules/identity-record.js

import IdentityUser from './identity-user';

/**
 * Identity storage name
 * @type {string}
 */
const _STORAGE_KEY = 'nbc-identity';

/**
 * Identity record
 *
 * @property {string} env - Identity environment
 * @property {string} key - Identity App key
 * @property {string} token - Identity session token
 * @property {IdentityUser} user - Identity user or null
 * @property {number} timestamp - Identity record timestamp
 */
class IdentityRecord {
  /**
   * Restore record from storage
   *
   * If env, key and/or token are specified they are verified to match with stored data.
   *
   * Session TTL is also verified by the stored timestamp and {@link IdentityConfig#sessionTimeout}.
   *
   * If the stored data is invalid then this returns null.
   *
   * If user TTL has expired according to {@link IdentityConfig#userTimeout} then {@link IdentityRecord#user}
   * will be null.
   *
   * @param {IdentityConfig} config - Identity config
   * @param {string} [env=null] - Identity environment
   * @param {string} [key=null] - Identity App key
   * @param {string} [token=null] - Identity session token
   * @return {IdentityRecord} - Identity record or null
   */
  static restore(config, env = null, key = null, token = null) {
    let stored = localStorage.getItem(_STORAGE_KEY);
    stored = stored ? JSON.parse(stored) : {};

    if (
      (Date.now() > stored.timestamp + (config.sessionTimeout * 1000)) ||
      (env && stored.env !== env) ||
      (key && stored.key !== key) ||
      (token && stored.token !== token)
    ) {
      return null;
    }

    if (Date.now() < stored.timestamp + (config.userTimeout * 1000)) {
      stored.user = new IdentityUser(config, stored.user);
    } else {
      stored.user = null;
    }

    return new IdentityRecord(stored);
  }

  /**
   * Create and persist record in storage
   *
   * @param {string} env - Identity environment
   * @param {string} key - Identity App key
   * @param {string} token - Identity session token
   * @param {IdentityUser} user - Identity user
   * @return {IdentityRecord} - Identity record
   */
  static store(env, key, token, user) {
    const record = new IdentityRecord({
      env,
      key,
      token,
      user,
      timestamp: Date.now(),
    });
    const str = JSON.stringify(record);
    localStorage.setItem(_STORAGE_KEY, str);
    return record;
  }

  /**
   * Clear record from storage
   */
  static clear() {
    localStorage.removeItem(_STORAGE_KEY);
  }

  /**
   * IdentityRecord constructor
   *
   * @param {Object} properties - Properties
   * @param {string} properties.env - Identity environment
   * @param {string} properties.key - Identity App key
   * @param {string} properties.token - Identity session token
   * @param {IdentityUser} properties.user - Identity user or null
   * @param {number} properties.timestamp - Identity record timestamp
   */
  constructor({
    env,
    key,
    token,
    user,
    timestamp,
  }) {
    Object.assign(this, {
      env,
      key,
      token,
      user,
      timestamp,
    });

    // Disable property modification
    Object.freeze(this);
  }
}

export default IdentityRecord;