Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

21.8 KB
Raw Blame
import { Knex } from "knex";

import { OrgMembershipStatus, TAuthTokens, TAuthTokenSessions, TUsers } from "@app/db/schemas";
import { KeyStorePrefixes, KeyStoreTtls, TKeyStoreFactory } from "@app/keystore/keystore";
import { getConfig } from "@app/lib/config/env";
import { crypto } from "@app/lib/crypto/cryptography";
import { BadRequestError, ForbiddenRequestError, NotFoundError, UnauthorizedError } from "@app/lib/errors";
import { chunkArray } from "@app/lib/fn/array";
import { requestMemoKeys } from "@app/lib/request-context/memo-keys";
import { requestMemoize } from "@app/lib/request-context/request-memoizer";

import { ActorType, AuthModeJwtTokenPayload, AuthModeRefreshJwtTokenPayload, AuthTokenType } from "../auth/auth-type";
import { TMembershipUserDALFactory } from "../membership-user/membership-user-dal";
import { TOrgDALFactory } from "../org/org-dal";
import { TUserDALFactory } from "../user/user-dal";
import { TTokenDALFactory } from "./auth-token-dal";
import {
  TCreateTokenForUserDTO,
  TCreateTokensForUsersResult,
  TEmailSignupOtpPayload,
  TIssueAuthTokenDTO,
  TokenType,
  TValidateTokenForUserDTO
} from "./auth-token-types";

type TAuthTokenServiceFactoryDep = {
  tokenDAL: TTokenDALFactory;
  userDAL: Pick<TUserDALFactory, "findById" | "transaction">;
  orgDAL: Pick<TOrgDALFactory, "findOne" | "findById" | "findEffectiveOrgMembership">;
  membershipUserDAL: Pick<TMembershipUserDALFactory, "findOne">;
  keyStore: Pick<
    TKeyStoreFactory,
    "setItemWithExpiry" | "setItemWithExpiryNX" | "getItem" | "deleteItem" | "acquireLock" | "ttl"
  >;
};

export type TAuthTokenServiceFactory = ReturnType<typeof tokenServiceFactory>;

// Per-chunk size for createTokensForUsers. Each insert row binds ~5 values, so a chunk
// of this size stays well under Postgres's ~65k bind-parameter limit for a single statement.
const CREATE_TOKENS_CHUNK_SIZE = 1000;

const generateSixDigitToken = (): string => String(crypto.randomInt(10 ** 5, 10 ** 6 - 1));

const generateRandomHex = (size: number): string => crypto.randomBytes(size).toString("hex");

const computeHash = (key: string, pepper: string): string =>
  crypto.nativeCrypto.createHmac("sha256", pepper).update(key).digest("hex");

export const getTokenConfig = (tokenType: TokenType) => {
  // generate random token based on specified token use-case
  // type [type]
  switch (tokenType) {
    case TokenType.TOKEN_EMAIL_VERIFICATION: {
      const token = generateSixDigitToken();
      const triesLeft = 3;
      const expiresAt = new Date(new Date().getTime() + 86400000);
      return { token, triesLeft, expiresAt };
    }
    case TokenType.TOKEN_EMAIL_CHANGE_OTP:
    case TokenType.TOKEN_EMAIL_CHANGE_CURRENT_OTP: {
      const token = generateSixDigitToken();
      const triesLeft = 1;
      const expiresAt = new Date(new Date().getTime() + 600000);
      return { token, triesLeft, expiresAt };
    }
    case TokenType.TOKEN_EMAIL_MFA: {
      const token = generateSixDigitToken();
      const triesLeft = 3;
      const expiresAt = new Date(new Date().getTime() + 300000);
      return { token, triesLeft, expiresAt };
    }
    case TokenType.TOKEN_EMAIL_ORG_INVITATION: {
      const token = generateRandomHex(16);
      const expiresAt = new Date(new Date().getTime() + 259200000);
      return { token, expiresAt };
    }
    case TokenType.TOKEN_EMAIL_PASSWORD_RESET: {
      const token = generateRandomHex(32);
      const expiresAt = new Date(new Date().getTime() + 86400000);
      return { token, expiresAt };
    }
    case TokenType.TOKEN_EMAIL_PASSWORD_SETUP: {
      const token = generateRandomHex(16);
      const expiresAt = new Date(new Date().getTime() + 86400000);
      return { token, expiresAt };
    }
    case TokenType.TOKEN_USER_UNLOCK: {
      const token = generateRandomHex(16);
      const expiresAt = new Date(new Date().getTime() + 259200000);
      return { token, expiresAt };
    }
    case TokenType.TOKEN_WEBAUTHN_SESSION: {
      const token = generateRandomHex(32);
      const triesLeft = 1;
      const expiresAt = new Date(new Date().getTime() + 60000); // 60 seconds
      return { token, triesLeft, expiresAt };
    }
    case TokenType.TOKEN_PAM_WS_TICKET: {
      const token = generateRandomHex(32);
      const triesLeft = 1;
      const expiresAt = new Date(new Date().getTime() + 30000); // 30 seconds
      return { token, triesLeft, expiresAt };
    }
    default: {
      const token = generateRandomHex(16);
      const expiresAt = new Date();
      return { token, expiresAt };
    }
  }
};

const isUserLocked = (user: Pick<TUsers, "isLocked" | "temporaryLockDateEnd">) =>
  Boolean(user.isLocked || (user.temporaryLockDateEnd && new Date() < user.temporaryLockDateEnd));

export const tokenServiceFactory = ({ tokenDAL, userDAL, orgDAL, keyStore }: TAuthTokenServiceFactoryDep) => {
  const createTokenForUser = async ({ type, userId, orgId, aliasId, payload }: TCreateTokenForUserDTO) => {
    const { token, ...tkCfg } = getTokenConfig(type);
    const appCfg = getConfig();
    const tokenHash = await crypto.hashing().createHash(token, appCfg.SALT_ROUNDS);
    await tokenDAL.transaction(async (tx) => {
      await tokenDAL.delete({ userId, type, orgId: orgId || null }, tx);
      const newToken = await tokenDAL.create(
        {
          tokenHash,
          expiresAt: tkCfg.expiresAt,
          type,
          userId,
          orgId,
          triesLeft: tkCfg?.triesLeft,
          aliasId,
          payload
        },
        tx
      );
      return newToken;
    });

    return token;
  };

  /**
   * Batched variant of createTokenForUser: refreshes tokens for many users using
   * bulk deletes/inserts (one transaction per chunk) instead of one transaction per
   * user. This avoids exhausting the connection pool when called for a large set of
   * users (e.g. the daily invited-user reminder job).
   *
   * Work is processed in chunks so a single multi-row INSERT / composite IN can't grow
   * past Postgres's bind-parameter ceiling, and chunks run sequentially so pool usage
   * stays at one connection at a time. Token hashing is CPU-bound and does not hold a
   * DB connection, so it runs up front per chunk in parallel. Each entry must include
   * an orgId.
   */
  const createTokensForUsers = async (
    entries: (TCreateTokenForUserDTO & { orgId: string })[]
  ): Promise<TCreateTokensForUsersResult> => {
    if (!entries.length) return [];
    const appCfg = getConfig();

    const result: TCreateTokensForUsersResult = [];

    for (const chunk of chunkArray(entries, CREATE_TOKENS_CHUNK_SIZE)) {
      // eslint-disable-next-line no-await-in-loop
      const prepared = await Promise.all(
        chunk.map(async (entry) => {
          const { token, ...tkCfg } = getTokenConfig(entry.type);
          const tokenHash = await crypto.hashing().createHash(token, appCfg.SALT_ROUNDS);
          return { entry, token, tokenHash, tkCfg };
        })
      );

      // eslint-disable-next-line no-await-in-loop
      await tokenDAL.transaction(async (tx) => {
        await tokenDAL.deleteTokensForUsers(
          prepared.map(({ entry }) => ({ type: entry.type, userId: entry.userId, orgId: entry.orgId })),
          tx
        );
        await tokenDAL.insertMany(
          prepared.map(({ entry, tokenHash, tkCfg }) => ({
            tokenHash,
            expiresAt: tkCfg.expiresAt,
            type: entry.type,
            userId: entry.userId,
            orgId: entry.orgId,
            triesLeft: tkCfg?.triesLeft,
            aliasId: entry.aliasId,
            payload: entry.payload
          })),
          tx
        );
      });

      result.push(...prepared.map(({ entry, token }) => ({ userId: entry.userId, orgId: entry.orgId, token })));
    }

    return result;
  };

  const DUMMY_HASH = "$2b$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy";
  const validateTokenForUser = async ({
    type,
    userId,
    code,
    orgId
  }: TValidateTokenForUserDTO): Promise<TAuthTokens | undefined> => {
    const token = await tokenDAL.findOne({ type, userId, orgId: orgId || null });

    // Always perform a hash comparison, even if no token exists.
    // Use a dummy hash so the timing is indistinguishable.
    const hashToCompare = token?.tokenHash ?? DUMMY_HASH;
    const isValidToken = await crypto.hashing().compareHash(code, hashToCompare);

    // Now perform the logical checks *after* the constant-time work is done.
    if (!token) throw new BadRequestError({ message: "Invalid token" });

    if (!token.expiresAt || new Date(token.expiresAt) < new Date()) {
      await tokenDAL.delete({ type, userId, orgId });
      throw new BadRequestError({ message: "Invalid token" });
    }

    if (!isValidToken) {
      if (token.triesLeft !== undefined && token.triesLeft !== null) {
        if (token.triesLeft <= 1) {
          await tokenDAL.deleteTokenForUser({ type, userId, orgId: orgId || null });
        } else {
          await tokenDAL.decrementTriesField({ type, userId, orgId: orgId || null });
        }
      }
      throw new BadRequestError({ message: "Invalid token" });
    }

    const deletedToken = await tokenDAL.delete({ type, userId, orgId: orgId || null });
    return deletedToken?.[0];
  };

  const getUserTokenSession = async (
    { userId, ip, userAgent }: TIssueAuthTokenDTO,
    tx?: Knex
  ): Promise<TAuthTokenSessions | undefined> => {
    let session = await tokenDAL.findOneTokenSession({ userId, ip, userAgent }, tx);
    if (!session) {
      session = await tokenDAL.insertTokenSession(userId, ip, userAgent, tx);
    }
    return session;
  };

  const clearTokenSessionById = async (userId: string, sessionId: string): Promise<TAuthTokenSessions | undefined> =>
    tokenDAL.incrementTokenSessionVersion(userId, sessionId);

  const getUserTokenSessionById = async (id: string, userId: string) => tokenDAL.findOneTokenSession({ id, userId });

  const getTokenSessionByUser = async (userId: string) => tokenDAL.findTokenSessions({ userId });

  const revokeAllMySessions = async (userId: string) => tokenDAL.deleteTokenSession({ userId });

  const revokeMySessionById = async (userId: string, sessionId: string) =>
    tokenDAL.deleteTokenSession({ userId, id: sessionId });

  const revokeSessionsByUserAgent = async (userAgent: string, tx?: Knex) =>
    tokenDAL.deleteTokenSession({ userAgent }, tx);

  const validateRefreshToken = async (refreshToken?: string, opts?: { allowOauthClientToken?: boolean }) => {
    const appCfg = getConfig();
    if (!refreshToken) {
      throw new NotFoundError({
        name: "AuthTokenNotFound",
        message: "Invalid token"
      });
    }

    const decodedToken = crypto.jwt().verify(refreshToken, appCfg.AUTH_SECRET) as AuthModeRefreshJwtTokenPayload;

    if (decodedToken.authTokenType !== AuthTokenType.REFRESH_TOKEN) {
      throw new UnauthorizedError({
        message: "The token provided is not a refresh token",
        name: "InvalidToken"
      });
    }

    if (decodedToken.oauthClientId && !opts?.allowOauthClientToken) {
      throw new UnauthorizedError({
        message: "This refresh token can only be used at the OAuth token endpoint",
        name: "InvalidToken"
      });
    }

    const tokenVersion = await getUserTokenSessionById(decodedToken.tokenVersionId, decodedToken.userId);

    if (!tokenVersion) {
      throw new UnauthorizedError({
        message: "Invalid token",
        name: "InvalidToken"
      });
    }

    if (decodedToken.refreshVersion !== tokenVersion.refreshVersion) {
      // Check grace period for multi-tab scenarios
      const graceKey = KeyStorePrefixes.RefreshTokenGrace(tokenVersion.id);
      const graceValue = await keyStore.getItem(graceKey);

      if (graceValue && Number(graceValue) === decodedToken.refreshVersion) {
        // Grace hit: old token used within grace window (e.g. another browser tab)
        return { decodedToken, tokenVersion, isGraceHit: true };
      }

      // Reuse detection: old token used outside grace window — potential theft
      await tokenDAL.deleteTokenSession({ id: tokenVersion.id, userId: decodedToken.userId });
      await keyStore.deleteItem(graceKey);

      throw new UnauthorizedError({
        message: "Refresh token reuse detected. Session has been revoked for security.",
        name: "TokenReuse"
      });
    }

    return { decodedToken, tokenVersion, isGraceHit: false };
  };

  const rotateRefreshToken = async (decodedToken: AuthModeRefreshJwtTokenPayload, tokenVersion: TAuthTokenSessions) => {
    const appCfg = getConfig();
    const oldRefreshVersion = tokenVersion.refreshVersion;

    // Store grace entry in Redis so the old token is still accepted briefly
    const graceKey = KeyStorePrefixes.RefreshTokenGrace(tokenVersion.id);
    await keyStore.setItemWithExpiry(graceKey, KeyStoreTtls.RefreshTokenGraceInSeconds, String(oldRefreshVersion));

    // Increment refreshVersion in DB
    const updatedSession = await tokenDAL.incrementRefreshVersion(tokenVersion.id, decodedToken.userId);
    if (!updatedSession)
      throw new UnauthorizedError({ message: "Failed to rotate refresh token", name: "RotationFailed" });

    // Determine refresh token expiry
    let refreshTokenExpiresIn: string | number = appCfg.JWT_REFRESH_LIFETIME;
    if (decodedToken.organizationId) {
      const org = await orgDAL.findOne({ id: decodedToken.organizationId });
      if (org?.userTokenExpiration) {
        refreshTokenExpiresIn = org.userTokenExpiration;
      }
    }

    // Sign new refresh token
    const newRefreshToken = crypto.jwt().sign(
      {
        authMethod: decodedToken.authMethod,
        authTokenType: AuthTokenType.REFRESH_TOKEN,
        userId: decodedToken.userId,
        tokenVersionId: updatedSession.id,
        refreshVersion: updatedSession.refreshVersion,
        organizationId: decodedToken.organizationId,
        ...(decodedToken.subOrganizationId && { subOrganizationId: decodedToken.subOrganizationId }),
        issuingRootOrgId: decodedToken.issuingRootOrgId,
        isMfaVerified: decodedToken.isMfaVerified,
        mfaMethod: decodedToken.mfaMethod,
        idpMfaOrgId: decodedToken.idpMfaOrgId
      },
      appCfg.AUTH_SECRET,
      { expiresIn: refreshTokenExpiresIn }
    );

    return { newRefreshToken, updatedSession };
  };

  const sessionNoLongerValid = () =>
    new UnauthorizedError({
      name: "InvalidToken",
      message: "Your session is no longer valid, please re-authenticate"
    });

  const validateUserSessionFreshness = async ({
    userId,
    tokenVersionId,
    accessVersion,
    readFromPrimary = false
  }: {
    userId: string;
    tokenVersionId: string;
    accessVersion: number;
    readFromPrimary?: boolean;
  }) => {
    const session = await tokenDAL.findOneTokenSession({ id: tokenVersionId, userId }, undefined, {
      readFromPrimary
    });
    if (!session) throw sessionNoLongerValid();
    if (accessVersion !== session.accessVersion) throw sessionNoLongerValid();

    const user = await requestMemoize(requestMemoKeys.userFindById(session.userId), () =>
      userDAL.findById(session.userId)
    );
    if (!user || !user.isAccepted) throw sessionNoLongerValid();

    if (isUserLocked(user)) {
      throw new UnauthorizedError({ message: "Account is locked" });
    }

    return { user };
  };

  const assertActiveOrgMembership = async (userId: string, orgId: string, { acceptAnyStatus = false } = {}) => {
    const orgMembership = await orgDAL.findEffectiveOrgMembership({
      actorType: ActorType.USER,
      actorId: userId,
      orgId,
      ...(acceptAnyStatus ? { acceptAnyStatus } : { status: OrgMembershipStatus.Accepted })
    });

    if (!orgMembership) {
      throw new ForbiddenRequestError({ message: "User not member of organization" });
    }

    if (!orgMembership.isActive) {
      throw new ForbiddenRequestError({ message: "User organization membership is inactive" });
    }
  };

  const validateTokenOrgScope = async ({
    userId,
    organizationId,
    subOrganizationId
  }: {
    userId: string;
    organizationId?: string;
    subOrganizationId?: string;
  }) => {
    if (!organizationId) {
      if (subOrganizationId) throw new UnauthorizedError({ message: "Invalid token" });
      return { orgId: "", orgName: "", rootOrgId: "", parentOrgId: "" };
    }

    if (!subOrganizationId) {
      const organization = await requestMemoize(requestMemoKeys.orgFindById(organizationId), () =>
        orgDAL.findById(organizationId)
      );

      if (!organization) {
        throw new UnauthorizedError({
          message: `The organization with ID '${organizationId}' no longer exists. Log in again.`
        });
      }
      await assertActiveOrgMembership(userId, organizationId);

      return {
        orgId: organizationId,
        orgName: organization.name,
        rootOrgId: organizationId,
        parentOrgId: organizationId
      };
    }

    const subOrganization = await requestMemoize(requestMemoKeys.orgFindById(subOrganizationId), () =>
      orgDAL.findById(subOrganizationId)
    );
    if (!subOrganization) {
      throw new UnauthorizedError({
        message: `The sub-organization with ID '${subOrganizationId}' no longer exists. Log in again.`
      });
    }
    if (subOrganization.rootOrgId !== organizationId || subOrganization.id === organizationId) {
      throw new ForbiddenRequestError({ message: "Sub-organization does not belong to the token's organization" });
    }

    // Mirrors selectOrganization, which lets a still-Invited root member into a sub-org: selecting a sub-org
    // never promotes the root membership, so only deactivation is a reason to refuse here.
    await assertActiveOrgMembership(userId, organizationId, { acceptAnyStatus: true });
    await assertActiveOrgMembership(userId, subOrganization.id);

    return {
      orgId: subOrganization.id,
      orgName: subOrganization.name,
      rootOrgId: organizationId,
      parentOrgId: subOrganization.parentOrgId as string
    };
  };

  const fnValidateJwtIdentity = async (token: AuthModeJwtTokenPayload) => {
    const { user } = await validateUserSessionFreshness({
      userId: token.userId,
      tokenVersionId: token.tokenVersionId,
      accessVersion: token.accessVersion
    });

    const { orgId, orgName, rootOrgId, parentOrgId } = await validateTokenOrgScope({
      userId: user.id,
      organizationId: token.organizationId,
      subOrganizationId: token.subOrganizationId
    });

    return { user, tokenVersionId: token.tokenVersionId, orgId, orgName, rootOrgId, parentOrgId };
  };

  const validateRefreshTokenAccess = async (decodedToken: AuthModeRefreshJwtTokenPayload) => {
    const user = await requestMemoize(requestMemoKeys.userFindById(decodedToken.userId), () =>
      userDAL.findById(decodedToken.userId)
    );
    if (!user || !user.isAccepted) throw new UnauthorizedError({ message: "Invalid token", name: "InvalidToken" });
    if (isUserLocked(user)) throw new UnauthorizedError({ message: "Account is locked" });

    await validateTokenOrgScope({
      userId: user.id,
      organizationId: decodedToken.organizationId,
      subOrganizationId: decodedToken.subOrganizationId
    });
  };

  const createEmailSignupToken = async (emailHash: string): Promise<string> => {
    const appCfg = getConfig();
    const token = generateSixDigitToken();
    const tokenHash = computeHash(token, appCfg.AUTH_SECRET);
    const ttlSeconds = KeyStoreTtls.EmailSignupOtpInSeconds;
    const payload: TEmailSignupOtpPayload = {
      tokenHash,
      triesLeft: 3,
      expiresAt: Date.now() + ttlSeconds * 1000
    };

    await keyStore.setItemWithExpiry(
      KeyStorePrefixes.EmailSignupOtpHash(emailHash),
      ttlSeconds,
      JSON.stringify(payload)
    );

    return token;
  };

  const validateEmailSignupToken = async (email: string, code: string): Promise<void> => {
    const appCfg = getConfig();
    const emailHash = computeHash(email, appCfg.AUTH_SECRET);
    const key = KeyStorePrefixes.EmailSignupOtpHash(emailHash);

    // Acquire a short-lived lock to make the tries-decrement / delete atomic.
    const lock = await keyStore.acquireLock([KeyStorePrefixes.EmailSignupOtpLock(emailHash)], 5000);
    try {
      const computedHash = computeHash(code, appCfg.AUTH_SECRET);

      const raw = await keyStore.getItem(key);
      if (!raw) {
        // Always compute the HMAC and compare with timingSafeEqual for constant-time behaviour.
        crypto.nativeCrypto.timingSafeEqual(Buffer.from(computedHash, "hex"), Buffer.from("0".repeat(64), "hex"));
        throw new UnauthorizedError({
          message: "Invalid token",
          name: "InvalidToken"
        });
      }

      const parsed = JSON.parse(raw) as TEmailSignupOtpPayload;
      const isValidToken = crypto.nativeCrypto.timingSafeEqual(
        Buffer.from(computedHash, "hex"),
        Buffer.from(parsed.tokenHash, "hex")
      );

      if (Date.now() > parsed.expiresAt) {
        await keyStore.deleteItem(key);
        throw new UnauthorizedError({
          message: "Invalid token",
          name: "InvalidToken"
        });
      }

      if (!isValidToken) {
        const remainingTries = parsed.triesLeft - 1;
        if (remainingTries <= 0) {
          await keyStore.deleteItem(key);
        } else {
          const remainingTtlSec = Math.max(1, Math.ceil((parsed.expiresAt - Date.now()) / 1000));
          await keyStore.setItemWithExpiry(
            key,
            remainingTtlSec,
            JSON.stringify({ ...parsed, triesLeft: remainingTries })
          );
        }
        throw new UnauthorizedError({
          message: "Invalid token",
          name: "InvalidToken"
        });
      }

      await keyStore.deleteItem(key);
    } finally {
      await lock.release();
    }
  };

  return {
    createTokenForUser,
    createTokensForUsers,
    validateTokenForUser,
    getUserTokenSession,
    clearTokenSessionById,
    getTokenSessionByUser,
    revokeAllMySessions,
    revokeMySessionById,
    revokeSessionsByUserAgent,
    validateRefreshToken,
    rotateRefreshToken,
    validateUserSessionFreshness,
    validateRefreshTokenAccess,
    fnValidateJwtIdentity,
    getUserTokenSessionById,
    createEmailSignupToken,
    validateEmailSignupToken
  };
};