Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

utils.ts

132 lines4.2 KB

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