SPB Git forge

spb/rareindex

Public
54commits 1branches 0releases
7.1 MBsize
maindefault branch
10 days agolast push
TypeScript 61.9% HTML 37.2% SQL 0.7%
1.0 KB · 24 lines typescript
Raw Blame History
1import * as OTPAuth from 'otpauth';23const ISSUER = 'RareIndex';45export function newTotpSecret(): string {6  return new OTPAuth.Secret({ size: 20 }).base32;7}89export function totpUri(secretB32: string, email: string): string {10  return new OTPAuth.TOTP({ issuer: ISSUER, label: email, algorithm: 'SHA1', digits: 6, period: 30, secret: OTPAuth.Secret.fromBase32(secretB32) }).toString();11}1213/** Verify a 6-digit TOTP with ±1 step tolerance. Returns the matched step delta or null. */14export function verifyTotp(secretB32: string, token: string, window = 1): number | null {15  const clean = token.replace(/\s+/g, '');16  if (!/^\d{6}$/.test(clean)) return null;17  const totp = new OTPAuth.TOTP({ issuer: ISSUER, algorithm: 'SHA1', digits: 6, period: 30, secret: OTPAuth.Secret.fromBase32(secretB32) });18  return totp.validate({ token: clean, window });19}2021export function currentTotp(secretB32: string): string {22  return new OTPAuth.TOTP({ issuer: ISSUER, algorithm: 'SHA1', digits: 6, period: 30, secret: OTPAuth.Secret.fromBase32(secretB32) }).generate();23}24