Download tasks/linkedin/tests/oracle-runtime/clone/lib/session.ts from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 2.85 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/tests/oracle-runtime/clone/lib/session.ts
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/linkedin/tests/oracle-runtime/clone/lib/session.ts
-
curl -L -o session.ts https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/tests/oracle-runtime/clone/lib/session.ts
2.85 kB
| import { createHmac, randomBytes } from "node:crypto"; | |
| import { and, eq, gt, lt } from "drizzle-orm"; | |
| import { NextRequest, NextResponse } from "next/server"; | |
| import { getDb } from "@/db"; | |
| import { sessions, users } from "@/db/schema"; | |
| import { localUserFromRequest } from "@/lib/public-local-account"; | |
| const cookieName = "linkedin_session"; | |
| const sessionDays = 30; | |
| function sessionSecret() { | |
| const configured = process.env.SESSION_SECRET?.trim(); | |
| if (configured && configured.length >= 32) return configured; | |
| if (process.env.NODE_ENV === "production") throw new Error("SESSION_SECRET must contain at least 32 characters in production."); | |
| return "local-development-session-secret-change-me"; | |
| } | |
| export function assertSessionConfigured() { | |
| sessionSecret(); | |
| } | |
| function tokenHash(token: string) { | |
| return createHmac("sha256", sessionSecret()).update(token).digest("hex"); | |
| } | |
| function secureCookie() { | |
| if (process.env.SESSION_COOKIE_SECURE === "false") return false; | |
| return process.env.NODE_ENV === "production"; | |
| } | |
| async function pruneExpiredSessions(now = new Date()) { | |
| await getDb().delete(sessions).where(lt(sessions.expiresAt, now)); | |
| } | |
| export async function issueSession(response: NextResponse, userId: string) { | |
| const token = randomBytes(32).toString("base64url"); | |
| const expiresAt = new Date(Date.now() + sessionDays * 86400_000); | |
| await pruneExpiredSessions(); | |
| await getDb().insert(sessions).values({ userId, tokenHash: tokenHash(token), expiresAt }); | |
| response.cookies.set(cookieName, token, { | |
| httpOnly: true, | |
| sameSite: "lax", | |
| secure: secureCookie(), | |
| path: "/", | |
| expires: expiresAt, | |
| }); | |
| } | |
| export async function getSessionUser(request: NextRequest | Request) { | |
| const localUser = localUserFromRequest(request); | |
| if (localUser) return { user: localUser, sessionId: "local-test-session" }; | |
| const token = request instanceof NextRequest | |
| ? request.cookies.get(cookieName)?.value | |
| : request.headers.get("cookie")?.match(/(?:^|;\s*)linkedin_session=([^;]+)/)?.[1]; | |
| if (!token) return null; | |
| const rows = await getDb().select({ user: users, sessionId: sessions.id }) | |
| .from(sessions) | |
| .innerJoin(users, eq(users.id, sessions.userId)) | |
| .where(and(eq(sessions.tokenHash, tokenHash(token)), gt(sessions.expiresAt, new Date()))) | |
| .limit(1); | |
| const session = rows[0] ?? null; | |
| if (session) { | |
| await getDb().update(sessions).set({ lastSeenAt: new Date() }).where(eq(sessions.id, session.sessionId)); | |
| } | |
| return session; | |
| } | |
| export async function revokeSession(request: NextRequest, response: NextResponse) { | |
| const token = request.cookies.get(cookieName)?.value; | |
| if (token) await getDb().delete(sessions).where(eq(sessions.tokenHash, tokenHash(token))); | |
| response.cookies.set(cookieName, "", { httpOnly: true, sameSite: "lax", secure: secureCookie(), path: "/", maxAge: 0 }); | |
| } | |