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