Open History to see the last commit
1import type { AuthClientMetadata, AuthClientMetadataDeviceType } from "@t3tools/contracts";
2import type * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";
3import * as Crypto from "node:crypto";
4
5export const SESSION_COOKIE_NAME = "t3_session";
6
7export function base64UrlEncode(input: string | Uint8Array): string {
8 const buffer = typeof input === "string" ? Buffer.from(input, "utf8") : Buffer.from(input);
9 return buffer.toString("base64url");
10}
11
12export function base64UrlDecodeUtf8(input: string): string {
13 return Buffer.from(input, "base64url").toString("utf8");
14}
15
16export function signPayload(payload: string, secret: Uint8Array): string {
17 return Crypto.createHmac("sha256", Buffer.from(secret)).update(payload).digest("base64url");
18}
19
20export function timingSafeEqualBase64Url(left: string, right: string): boolean {
21 const leftBuffer = Buffer.from(left, "base64url");
22 const rightBuffer = Buffer.from(right, "base64url");
23 if (leftBuffer.length !== rightBuffer.length) {
24 return false;
25 }
26 return Crypto.timingSafeEqual(leftBuffer, rightBuffer);
27}
28
29function normalizeNonEmptyString(value: string | null | undefined): string | undefined {
30 if (typeof value !== "string") {
31 return undefined;
32 }
33 const trimmed = value.trim();
34 return trimmed.length > 0 ? trimmed : undefined;
35}
36
37function normalizeIpAddress(value: string | null | undefined): string | undefined {
38 const normalized = normalizeNonEmptyString(value);
39 if (!normalized) {
40 return undefined;
41 }
42 return normalized.startsWith("::ffff:") ? normalized.slice("::ffff:".length) : normalized;
43}
44
45function inferDeviceType(userAgent: string | undefined): AuthClientMetadataDeviceType {
46 if (!userAgent) {
47 return "unknown";
48 }
49
50 const normalized = userAgent.toLowerCase();
51 if (/bot|crawler|spider|slurp|curl|wget/.test(normalized)) {
52 return "bot";
53 }
54 if (/ipad|tablet/.test(normalized)) {
55 return "tablet";
56 }
57 if (/iphone|android.+mobile|mobile/.test(normalized)) {
58 return "mobile";
59 }
60 return "desktop";
61}
62
63function inferBrowser(userAgent: string | undefined): string | undefined {
64 if (!userAgent) {
65 return undefined;
66 }
67 const normalized = userAgent.toLowerCase();
68 if (/edg\//.test(normalized)) return "Edge";
69 if (/opr\//.test(normalized)) return "Opera";
70 if (/firefox\//.test(normalized)) return "Firefox";
71 if (/electron\//.test(normalized)) return "Electron";
72 if (/chrome\//.test(normalized) || /crios\//.test(normalized)) return "Chrome";
73 if (/safari\//.test(normalized) && !/chrome\//.test(normalized)) return "Safari";
74 return undefined;
75}
76
77function inferOs(userAgent: string | undefined): string | undefined {
78 if (!userAgent) {
79 return undefined;
80 }
81 const normalized = userAgent.toLowerCase();
82 if (/iphone|ipad|ipod/.test(normalized)) return "iOS";
83 if (/android/.test(normalized)) return "Android";
84 if (/mac os x|macintosh/.test(normalized)) return "macOS";
85 if (/windows nt/.test(normalized)) return "Windows";
86 if (/linux/.test(normalized)) return "Linux";
87 return undefined;
88}
89
90function readRemoteAddressFromSource(source: unknown): string | undefined {
91 if (!source || typeof source !== "object") {
92 return undefined;
93 }
94
95 const candidate = source as {
96 readonly remoteAddress?: string | null;
97 readonly socket?: {
98 readonly remoteAddress?: string | null;
99 };
100 };
101
102 return normalizeIpAddress(candidate.socket?.remoteAddress ?? candidate.remoteAddress);
103}
104
105export function deriveAuthClientMetadata(input: {
106 readonly request: HttpServerRequest.HttpServerRequest;
107 readonly label?: string;
108}): AuthClientMetadata {
109 const userAgent = normalizeNonEmptyString(input.request.headers["user-agent"]);
110 const ipAddress = readRemoteAddressFromSource(input.request.source);
111 const os = inferOs(userAgent);
112 const browser = inferBrowser(userAgent);
113 return {
114 ...(input.label ? { label: input.label } : {}),
115 ...(ipAddress ? { ipAddress } : {}),
116 ...(userAgent ? { userAgent } : {}),
117 deviceType: inferDeviceType(userAgent),
118 ...(os ? { os } : {}),
119 ...(browser ? { browser } : {}),
120 };
121}