File

lib/common/auth/auth.constants.ts

Description

Abstract service class for authentication and authorization. Defines the methods that must be implemented by any derived service.

Index

Methods

Methods

Abstract authenticate
authenticate(data: LoginPayload)

Authenticate the user with the provided login payload.

Parameters :
Name Type Optional Description
data LoginPayload No
  • LoginPayload object with user login information.
Returns : Promise<JwtDto>

A Promise that resolves to a JwtDto containing access and refresh tokens.

Abstract exchangeToken
exchangeToken(refreshToken: string)

Exchange the provided refresh token for a new access token.

Parameters :
Name Type Optional Description
refreshToken string No
  • The refresh token to exchange.

A Promise that resolves to a Partial containing new access and refresh tokens.

Abstract invalidateToken
invalidateToken(accessToken: string)

Invalidate the specified access token.

Parameters :
Name Type Optional Description
accessToken string No
  • The access token to invalidate.
Returns : any

A Promise that resolves to true if the token was invalidated successfully, or throws an error.

import { JwtDto, LoginPayload } from "./auth.types";

/**
 * Error message for access token related errors.
 * @constant
 */
export const ACCESS_TOKEN_ERROR_MSG = "ERR_TOKEN_A";

/**
 * Error message for refresh token related errors.
 * @constant
 */
export const REFRESH_TOKEN_ERROR_MSG = "ERR_TOKEN_R";

/**
 * Placeholder for unknown IP addresses.
 * @constant
 */
export const UNKNOWN_IP = "unknown";

/**
 * Prefix for JWT cache keys.
 * @constant
 */
export const AUTH_JWT_CACHE_PREFIX = "jwt";

/**
 * Prefix for access token cache keys.
 * @constant
 */
export const AUTH_ACCESS_TOKEN_PREFIX = "access_token";

/**
 * Prefix for refresh token cache keys.
 * @constant
 */
export const AUTH_REFRESH_TOKEN_PREFIX = "refresh_token";

/**
 * Prefix for brute force protection cache keys.
 * @constant
 */
export const BRUTEFORCE_JWT_CACHE_PREFIX = "bruteforce";

/**
 * Generates a cache key for an access token.
 * @param accessToken - The access token.
 * @returns The generated cache key.
 */
export const jwtAccessTokenKey = (accessToken: string) => {
  return `${AUTH_JWT_CACHE_PREFIX}:${AUTH_ACCESS_TOKEN_PREFIX}:${accessToken}`;
};

/**
 * Generates a cache key for a refresh token.
 * @param accessToken - The access token.
 * @param refreshToken - The refresh token.
 * @returns The generated cache key.
 */
export const jwtRefreshTokenKey = (
  accessToken: string,
  refreshToken: string,
) => {
  return `${AUTH_JWT_CACHE_PREFIX}:${AUTH_REFRESH_TOKEN_PREFIX}:${accessToken}:${refreshToken}`;
};

/**
 * Generates a cache key for tracking brute force login attempts by login.
 * @param login - The user's login.
 * @returns The generated cache key.
 */
export const bruteForceLoginKey = (login: string) =>
  `${BRUTEFORCE_JWT_CACHE_PREFIX}:login:${login}`;

/**
 * Generates a cache key for tracking brute force login attempts by IP address.
 * @param ipAddress - The IP address of the user.
 * @returns The generated cache key.
 */
export const bruteForceIPKey = (ipAddress: string) =>
  `${BRUTEFORCE_JWT_CACHE_PREFIX}:ip:${ipAddress}`;

/**
 * Abstract service class for authentication and authorization.
 * Defines the methods that must be implemented by any derived service.
 */
export abstract class AuthService {
  /**
   * Authenticate the user with the provided login payload.
   * @param data - LoginPayload object with user login information.
   * @returns A Promise that resolves to a JwtDto containing access and refresh tokens.
   */
  abstract authenticate(data: LoginPayload): Promise<JwtDto>;

  /**
   * Invalidate the specified access token.
   * @param accessToken - The access token to invalidate.
   * @returns A Promise that resolves to true if the token was invalidated successfully, or throws an error.
   */
  abstract invalidateToken(accessToken: string);

  /**
   * Exchange the provided refresh token for a new access token.
   * @param refreshToken - The refresh token to exchange.
   * @returns A Promise that resolves to a Partial<JwtDto> containing new access and refresh tokens.
   */
  abstract exchangeToken(refreshToken: string): Promise<Partial<JwtDto>>;
}

results matching ""

    No results matching ""