You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
864 B
31 lines
864 B
import type { H3Event } from "h3";
|
|
import { randomBytes } from "node:crypto";
|
|
|
|
const TEMP_TOKEN_COOKIE = "agent_temp_token";
|
|
const TEMP_TOKEN_TTL_MS = 2 * 60 * 60 * 1000;
|
|
|
|
export function generateTempToken(): string {
|
|
return randomBytes(32).toString("hex");
|
|
}
|
|
|
|
export function setTempTokenCookie(event: H3Event, token: string): void {
|
|
setCookie(event, TEMP_TOKEN_COOKIE, token, {
|
|
httpOnly: true,
|
|
secure: process.env.NODE_ENV === "production",
|
|
sameSite: "lax",
|
|
maxAge: TEMP_TOKEN_TTL_MS / 1000,
|
|
path: "/",
|
|
});
|
|
}
|
|
|
|
export function getTempTokenFromCookie(event: H3Event): string | null {
|
|
return getCookie(event, TEMP_TOKEN_COOKIE) ?? null;
|
|
}
|
|
|
|
export function clearTempTokenCookie(event: H3Event): void {
|
|
deleteCookie(event, TEMP_TOKEN_COOKIE, { path: "/" });
|
|
}
|
|
|
|
export function getTempTokenTtlMs(): number {
|
|
return TEMP_TOKEN_TTL_MS;
|
|
}
|
|
|