All files / src/utils captchaTokenExpiry.ts

69.23% Statements 9/13
44.44% Branches 4/9
50% Functions 1/2
72.72% Lines 8/11

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      88x 88x 88x 1x 1x 1x 1x 1x                          
export function captchaTokenExpiryMs(
  token: string | null | undefined
): number | null {
  Iif (!token) return null;
  const payload = token.split(".")[1];
  if (!payload) return null;
  try {
    const normalized = payload.replace(/-/g, "+").replace(/_/g, "/");
    const padded = normalized + "=".repeat((4 - (normalized.length % 4)) % 4);
    const json = JSON.parse(atob(padded)) as { exp?: unknown };
    return typeof json.exp === "number" ? json.exp * 1000 : null;
  } catch {
    return null;
  }
}
 
export function isCaptchaTokenExpired(
  token: string | null | undefined,
  nowMs: number = Date.now()
): boolean {
  const expiryMs = captchaTokenExpiryMs(token);
  return expiryMs != null && nowMs >= expiryMs;
}