Files
formbricks/apps/web/modules/auth/lib/totp.ts
Matti Nannt 5970ff917f chore: add testing infra to apps/web (#4563)
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
Co-authored-by: Dhruwang Jariwala <67850763+Dhruwang@users.noreply.github.com>
2025-01-09 13:00:16 +00:00

30 lines
988 B
TypeScript

import { Authenticator } from "@otplib/core";
import type { AuthenticatorOptions } from "@otplib/core/authenticator";
import { createDigest, createRandomBytes } from "@otplib/plugin-crypto";
import { keyDecoder, keyEncoder } from "@otplib/plugin-thirty-two";
/**
* Checks the validity of a TOTP token using a base32-encoded secret.
*
* @param token - The token.
* @param secret - The base32-encoded shared secret.
* @param opts - The AuthenticatorOptions object.
* @param opts.window - The amount of past and future tokens considered valid. Either a single value or array of `[past, future]`. Default: `[1, 0]`
*/
export const totpAuthenticatorCheck = (
token: string,
secret: string,
opts: Partial<AuthenticatorOptions> = {}
) => {
const { window = [1, 0], ...rest } = opts;
const authenticator = new Authenticator({
createDigest,
createRandomBytes,
keyDecoder,
keyEncoder,
window,
...rest,
});
return authenticator.check(token, secret);
};