aboutsummaryrefslogtreecommitdiff
path: root/src/lib/server/auth.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/server/auth.ts')
-rw-r--r--src/lib/server/auth.ts17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/lib/server/auth.ts b/src/lib/server/auth.ts
index f80eda0..3db93f0 100644
--- a/src/lib/server/auth.ts
+++ b/src/lib/server/auth.ts
@@ -30,15 +30,19 @@ export function verifyPassword(candidate: string): boolean {
return timingSafeEqual(actual, expected);
}
-export function createSession(now = Date.now()): string {
- const expiresAt = now + SESSION_MAX_AGE_MS;
+export function createSession(
+ now: Temporal.Instant = Temporal.Now.instant(),
+): string {
+ const expiresAt = now.add({
+ milliseconds: SESSION_MAX_AGE_MS,
+ }).epochMilliseconds;
const payload = String(expiresAt);
return `${payload}.${sign(payload)}`;
}
export function isValidSession(
value: string | undefined,
- now = Date.now(),
+ now: Temporal.Instant = Temporal.Now.instant(),
): boolean {
if (!value) return false;
@@ -46,7 +50,8 @@ export function isValidSession(
if (!expiresAtText || !signature || extra) return false;
const expiresAt = Number(expiresAtText);
- if (!Number.isSafeInteger(expiresAt) || expiresAt <= now) return false;
+ if (!Number.isSafeInteger(expiresAt) || expiresAt <= now.epochMilliseconds)
+ return false;
const expected = Buffer.from(sign(expiresAtText), 'base64url');
const actual = Buffer.from(signature, 'base64url');
@@ -58,7 +63,7 @@ export function isValidSession(
export function renewSession(
cookies: Cookies,
secure: boolean,
- now = Date.now(),
+ now: Temporal.Instant = Temporal.Now.instant(),
): boolean {
const current = cookies.get(AUTH_COOKIE_NAME);
if (!isValidSession(current, now)) return false;
@@ -76,7 +81,7 @@ export function renewSession(
export function setSession(
cookies: Cookies,
secure: boolean,
- now = Date.now(),
+ now: Temporal.Instant = Temporal.Now.instant(),
): void {
cookies.set(AUTH_COOKIE_NAME, createSession(now), {
path: '/',