All files auth.ts

100% Statements 37/37
100% Branches 16/16
100% Functions 8/8
100% Lines 31/31

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            2x           2x       5x 5x 5x         2x 5x 1x     4x       4x               2x 6x 4x         2x             1x 2x             2x 4x 1x   3x   2x 2x   1x               2x 2x 1x         2x 4x 3x 2x   2x  
import {
  AuthAccessToken,
  AuthRefreshToken,
  AuthServerOptions,
  StringAnyMap
} from './interfaces';
import {
  MISSING_AT_CREATE_MSG,
  MISSING_AT_VERIFY_MSG,
  MISSING_RT_MSG
} from './internals';
 
export default class AuthServer<CookieOptions = StringAnyMap> {
  public accessToken: AuthAccessToken<CookieOptions>;
  public refreshToken?: AuthRefreshToken<CookieOptions>;
 
  constructor({ accessToken, refreshToken }: AuthServerOptions<CookieOptions>) {
    this.accessToken = accessToken;
    this.refreshToken = refreshToken;
  }
  /**
   * Creates a new accessToken
   */
  public createAccessToken(data: StringAnyMap) {
    if (typeof this.accessToken.create !== 'function') {
      throw new Error(MISSING_AT_CREATE_MSG);
    }
 
    const payload = this.accessToken.getPayload
      ? this.accessToken.getPayload(data)
      : data;
 
    return {
      accessToken: this.accessToken.create(payload),
      payload
    };
  }
  /**
   * Creates a new refreshToken
   */
  public createRefreshToken(data: StringAnyMap) {
    if (!this.refreshToken) throw new Error(MISSING_RT_MSG);
    return this.refreshToken.create(data);
  }
  /**
   * Creates both accessToken and refreshToken
   */
  public async createTokens(
    data: StringAnyMap
  ): Promise<{
    refreshToken: string;
    accessToken: string;
    payload: StringAnyMap;
  }> {
    return {
      refreshToken: await this.createRefreshToken(data),
      ...this.createAccessToken(data)
    };
  }
  /**
   * Decodes and returns the payload of an accessToken
   */
  public verify(accessToken: string) {
    if (typeof this.accessToken.verify !== 'function') {
      throw new Error(MISSING_AT_VERIFY_MSG);
    }
    if (!accessToken) return null;
 
    try {
      return this.accessToken.verify(accessToken);
    } catch (error) {
      return null;
    }
  }
  /**
   * Returns the payload in a refreshToken that can be used to create an
   * accessToken
   * @param reset Refresh the cookie of the refreshToken
   */
  public getPayload(refreshToken: string, reset: () => void) {
    if (!this.refreshToken) throw new Error(MISSING_RT_MSG);
    return this.refreshToken.getPayload(refreshToken, reset);
  }
  /**
   * Removes an active refreshToken
   */
  public removeRefreshRoken(refreshToken: string): Promise<boolean> | boolean {
    if (!this.refreshToken) throw new Error(MISSING_RT_MSG);
    if (!refreshToken) return false;
    return this.refreshToken.remove(refreshToken);
  }
}