Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
185 lines (164 loc) · 6.24 KB
/
Copy pathutils.ts
File metadata and controls
185 lines (164 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import type {
AuthClientMetadata,
AuthClientMetadataDeviceType,
AuthClientPresentationMetadata,
} from "@t3tools/contracts";
import type * as HttpServerRequest from "effect/http/HttpServerRequest";
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Digest is effectful; the cookie name stays a plain function.
import * as NodeCrypto from "node:crypto";
import * as NodeNet from "node:net";
import { normalizeHostname } from "@t3tools/shared/hostClassification";
import * as Mac from "effect/crypto/Mac";
import * as Base64Url from "effect/encoding/Base64Url";
import * as Result from "effect/Result";
const SESSION_COOKIE_NAME = "t3_session";
/** Cookies span ports; use separate names for servers sharing a hostname. */
export function resolveSessionCookieName(input: {
readonly mode: "web" | "desktop";
readonly port: number;
readonly host: string | undefined;
readonly instanceKey: string;
readonly environmentId: string;
readonly development: boolean;
}): string {
if (input.mode === "desktop") {
return `${SESSION_COOKIE_NAME}_${input.port}`;
}
const instanceHash = NodeCrypto.createHash("sha256")
.update(
!input.development && isRemoteReachableHost(input.host)
? input.environmentId
: input.instanceKey,
)
.digest("hex")
.slice(0, 12);
if (!input.development && isRemoteReachableHost(input.host)) {
return `${SESSION_COOKIE_NAME}_${instanceHash}`;
}
return `${SESSION_COOKIE_NAME}_${input.port}_${instanceHash}`;
}
export function resolveLegacySessionCookieName(input: {
readonly mode: "web" | "desktop";
readonly host: string | undefined;
readonly development: boolean;
}): string | undefined {
return input.mode === "web" && !input.development && isRemoteReachableHost(input.host)
? SESSION_COOKIE_NAME
: undefined;
}
/**
* The loopback address a server given `host` listens on, or undefined when it may listen beyond
* this machine. Unset means 127.0.0.1. Only a loopback literal or `localhost` counts: Node listens
* on every interface for an empty host, and any other name resolves through DNS.
*/
export function loopbackBindAddress(host: string | undefined): string | undefined {
if (host === undefined) return "127.0.0.1";
const address = normalizeHostname(host);
if (address === "localhost") return "127.0.0.1";
return (NodeNet.isIPv4(address) && address.startsWith("127.")) || address === "::1"
? address
: undefined;
}
export const isRemoteReachableHost = (host: string | undefined): boolean =>
loopbackBindAddress(host) === undefined;
export function base64UrlEncode(input: string | Uint8Array): string {
return typeof input === "string"
? Base64Url.encode(new TextEncoder().encode(input))
: Base64Url.encode(input);
}
export function base64UrlDecodeUtf8(input: string): string {
return Result.getOrThrow(Base64Url.decodeString(input));
}
export function timingSafeEqualBase64Url(left: string, right: string): boolean {
const leftBytes = Base64Url.decode(left);
const rightBytes = Base64Url.decode(right);
return (
Result.isSuccess(leftBytes) &&
Result.isSuccess(rightBytes) &&
Mac.timingSafeEqual(leftBytes.success, rightBytes.success)
);
}
function normalizeNonEmptyString(value: string | null | undefined): string | undefined {
if (typeof value !== "string") {
return undefined;
}
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
function normalizeIpAddress(value: string | null | undefined): string | undefined {
const normalized = normalizeNonEmptyString(value);
if (!normalized) {
return undefined;
}
return normalized.startsWith("::ffff:") ? normalized.slice("::ffff:".length) : normalized;
}
function inferDeviceType(userAgent: string | undefined): AuthClientMetadataDeviceType {
if (!userAgent) {
return "unknown";
}
const normalized = userAgent.toLowerCase();
if (/bot|crawler|spider|slurp|curl|wget/.test(normalized)) {
return "bot";
}
if (/ipad|tablet/.test(normalized)) {
return "tablet";
}
if (/iphone|android.+mobile|mobile/.test(normalized)) {
return "mobile";
}
return "desktop";
}
function inferBrowser(userAgent: string | undefined): string | undefined {
if (!userAgent) {
return undefined;
}
const normalized = userAgent.toLowerCase();
if (/edg(?:e|a|ios)?\//.test(normalized)) return "Edge";
if (/opr\/|opios\//.test(normalized)) return "Opera";
if (/firefox\/|fxios\//.test(normalized)) return "Firefox";
if (/electron\//.test(normalized)) return "Electron";
if (/chrome\//.test(normalized) || /crios\//.test(normalized)) return "Chrome";
if (/safari\//.test(normalized) && !/chrome\//.test(normalized)) return "Safari";
return undefined;
}
function inferOs(userAgent: string | undefined): string | undefined {
if (!userAgent) {
return undefined;
}
const normalized = userAgent.toLowerCase();
if (/iphone|ipad|ipod/.test(normalized)) return "iOS";
if (/android/.test(normalized)) return "Android";
if (/mac os x|macintosh/.test(normalized)) return "macOS";
if (/windows nt/.test(normalized)) return "Windows";
if (/linux/.test(normalized)) return "Linux";
return undefined;
}
function readRemoteAddressFromSource(source: unknown): string | undefined {
if (!source || typeof source !== "object") {
return undefined;
}
const candidate = source as {
readonly remoteAddress?: string | null;
readonly socket?: {
readonly remoteAddress?: string | null;
};
};
return normalizeIpAddress(candidate.socket?.remoteAddress ?? candidate.remoteAddress);
}
export function deriveAuthClientMetadata(input: {
readonly request: HttpServerRequest.HttpServerRequest;
readonly presented?: AuthClientPresentationMetadata;
}): AuthClientMetadata {
const userAgent = normalizeNonEmptyString(input.request.headers["user-agent"]);
const ipAddress = readRemoteAddressFromSource(input.request.source);
const os = input.presented?.os ?? inferOs(userAgent);
const browser = inferBrowser(userAgent);
return {
...(input.presented?.label ? { label: input.presented.label } : {}),
...(ipAddress ? { ipAddress } : {}),
...(userAgent ? { userAgent } : {}),
deviceType: input.presented?.deviceType ?? inferDeviceType(userAgent),
...(os ? { os } : {}),
...(browser ? { browser } : {}),
};
}