Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,89 @@
|
||||
// Password-policy soft-banner helpers (Phase 01 / #27).
|
||||
//
|
||||
// validatePassword-based hard-enforcement is already shipped across the
|
||||
// registration / reset / profile / admin-reset / invitation write paths.
|
||||
// What lives here is the *soft* path: once a user has already authenticated
|
||||
// with a password that does not satisfy the current policy, we surface a
|
||||
// dismissible banner pointing them at the change-password page.
|
||||
//
|
||||
// Two inputs decide whether the banner renders:
|
||||
//
|
||||
// 1. A session flag `passwordPolicyOutdated`. Set at login time by
|
||||
// AuthService.loginFromRequest after validatePassword runs in
|
||||
// non-throwing mode against the plaintext the user just submitted.
|
||||
// 2. A cookie `pwPolicyRemindAfter=<unix-millis>` that the "Remind me
|
||||
// in 7 days" button sets via the api.password-policy-remind resource
|
||||
// route. While the timestamp is still in the future the banner stays
|
||||
// hidden even when the session flag is on.
|
||||
//
|
||||
// Everything here is server-only and dependency-free so it can be unit
|
||||
// tested without spinning up a React Router request.
|
||||
|
||||
export const PASSWORD_POLICY_REMIND_COOKIE = "pwPolicyRemindAfter";
|
||||
export const PASSWORD_POLICY_REMIND_DURATION_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
export type PasswordPolicyBannerInput = {
|
||||
/** Session flag captured at login time. */
|
||||
policyOutdated: boolean;
|
||||
/** Raw `Cookie` header value, may be null/undefined. */
|
||||
cookieHeader?: string | null;
|
||||
/** Override `Date.now()` for deterministic tests. */
|
||||
now?: number;
|
||||
};
|
||||
|
||||
export type PasswordPolicyBannerState = {
|
||||
show: boolean;
|
||||
reason?: "not_outdated" | "remind_active" | "show";
|
||||
};
|
||||
|
||||
/**
|
||||
* Pure decision function. The route loader passes the session flag plus the
|
||||
* raw Cookie header and gets back a `{ show }` it can return to the
|
||||
* component without any further work.
|
||||
*/
|
||||
export function evaluatePasswordPolicyBanner(input: PasswordPolicyBannerInput): PasswordPolicyBannerState {
|
||||
if (!input.policyOutdated) {
|
||||
return { show: false, reason: "not_outdated" };
|
||||
}
|
||||
const remindAfter = readRemindAfterCookie(input.cookieHeader ?? undefined);
|
||||
const now = input.now ?? Date.now();
|
||||
if (remindAfter !== undefined && remindAfter > now) {
|
||||
return { show: false, reason: "remind_active" };
|
||||
}
|
||||
return { show: true, reason: "show" };
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the snooze cookie. Returns undefined when the cookie is missing or
|
||||
* malformed — the caller treats that the same as "no snooze active".
|
||||
*/
|
||||
export function readRemindAfterCookie(cookieHeader: string | undefined): number | undefined {
|
||||
if (!cookieHeader) return undefined;
|
||||
for (const segment of cookieHeader.split(";")) {
|
||||
const trimmed = segment.trim();
|
||||
if (!trimmed.startsWith(`${PASSWORD_POLICY_REMIND_COOKIE}=`)) continue;
|
||||
const raw = trimmed.slice(PASSWORD_POLICY_REMIND_COOKIE.length + 1);
|
||||
if (!raw) return undefined;
|
||||
const decoded = (() => {
|
||||
try {
|
||||
return decodeURIComponent(raw);
|
||||
} catch {
|
||||
return raw;
|
||||
}
|
||||
})();
|
||||
const parsed = Number.parseInt(decoded, 10);
|
||||
if (Number.isFinite(parsed) && parsed > 0) return parsed;
|
||||
return undefined;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the `Set-Cookie` header value for the "Remind me in 7 days" action.
|
||||
* Kept here so tests cover the exact serialization shape.
|
||||
*/
|
||||
export function buildRemindCookie(now: number = Date.now()): string {
|
||||
const expires = now + PASSWORD_POLICY_REMIND_DURATION_MS;
|
||||
const maxAgeSeconds = Math.floor(PASSWORD_POLICY_REMIND_DURATION_MS / 1000);
|
||||
return `${PASSWORD_POLICY_REMIND_COOKIE}=${expires}; Path=/; Max-Age=${maxAgeSeconds}; SameSite=Lax`;
|
||||
}
|
||||
Reference in new issue
Block a user