Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -3,13 +3,16 @@ import { loadRequiredWebConfig } from "@hivegrid/config";
|
||||
import CryptoApi from "~/utils/api/.server/CryptoApi";
|
||||
import {
|
||||
activateUserTotpPendingSecret,
|
||||
clearUserTotpPendingSecret,
|
||||
getUserTotpPendingEnrollment,
|
||||
setUserMfaRecoveryCodes,
|
||||
setUserTotpPendingSecret,
|
||||
type UserTotpPendingEnrollment,
|
||||
} from "~/utils/db/users.db.server";
|
||||
import { generateRecoveryCodes } from "~/utils/auth/mfa/recovery-codes.server";
|
||||
|
||||
export const TOTP_RECOVERY_CODE_COUNT = 10;
|
||||
export const TOTP_PENDING_ENROLLMENT_TTL_MS = 10 * 60 * 1000;
|
||||
const TOTP_DIGITS = 6;
|
||||
const TOTP_PERIOD_SECONDS = 30;
|
||||
const DEFAULT_WINDOW = 1;
|
||||
@@ -36,6 +39,11 @@ export type TotpEnrollmentResult = {
|
||||
secret: string;
|
||||
};
|
||||
|
||||
export type FreshTotpPendingEnrollment = UserTotpPendingEnrollment & {
|
||||
totpPendingSecretEncrypted: string;
|
||||
totpPendingCreatedAt: Date;
|
||||
};
|
||||
|
||||
type OtpauthOptions = {
|
||||
appName?: string;
|
||||
issuer?: string;
|
||||
@@ -148,6 +156,28 @@ export async function createTotpEnrollmentForUser({
|
||||
};
|
||||
}
|
||||
|
||||
export async function getFreshTotpPendingEnrollmentForUser({
|
||||
userId,
|
||||
now = new Date(),
|
||||
}: {
|
||||
userId: string;
|
||||
now?: Date;
|
||||
}): Promise<FreshTotpPendingEnrollment | null> {
|
||||
const pending = await getUserTotpPendingEnrollment(userId);
|
||||
if (!pending) {
|
||||
return null;
|
||||
}
|
||||
const pendingCreatedAtMs = pending.totpPendingCreatedAt?.getTime();
|
||||
const ageMs = pendingCreatedAtMs === undefined ? Number.NaN : now.getTime() - pendingCreatedAtMs;
|
||||
if (pending.totpPendingSecretEncrypted && Number.isFinite(ageMs) && ageMs >= 0 && ageMs <= TOTP_PENDING_ENROLLMENT_TTL_MS) {
|
||||
return pending as FreshTotpPendingEnrollment;
|
||||
}
|
||||
if (pending.totpPendingSecretEncrypted || pending.totpPendingCreatedAt) {
|
||||
await clearUserTotpPendingSecret(userId, pending);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function activatePendingTotpForUser({
|
||||
userId,
|
||||
token,
|
||||
@@ -157,8 +187,8 @@ export async function activatePendingTotpForUser({
|
||||
token: string;
|
||||
now?: Date;
|
||||
}): Promise<{ activated: boolean; recoveryCodes: string[] }> {
|
||||
const pending = await getUserTotpPendingEnrollment(userId);
|
||||
if (!pending?.totpPendingSecretEncrypted) {
|
||||
const pending = await getFreshTotpPendingEnrollmentForUser({ userId, now });
|
||||
if (!pending) {
|
||||
return { activated: false, recoveryCodes: [] };
|
||||
}
|
||||
if (!verifyTotpCode(pending.totpPendingSecretEncrypted, token, { timestamp: now.getTime() })) {
|
||||
@@ -169,4 +199,3 @@ export async function activatePendingTotpForUser({
|
||||
await setUserMfaRecoveryCodes(userId, recovery.entries, recovery.generatedAt);
|
||||
return { activated: true, recoveryCodes: recovery.codes };
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user