@plasgos/oidc-client 0.0.0-stage → 0.1.0-next.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/LICENSE +15 -0
- package/README.md +145 -2
- package/dist/index.cjs +884 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +860 -0
- package/dist/index.js.map +1 -0
- package/dist/service.cjs +432 -0
- package/dist/service.cjs.map +1 -0
- package/dist/service.d.cts +88 -0
- package/dist/service.d.ts +88 -0
- package/dist/service.js +419 -0
- package/dist/service.js.map +1 -0
- package/dist/session.cjs +601 -0
- package/dist/session.cjs.map +1 -0
- package/dist/session.d.cts +289 -0
- package/dist/session.d.ts +289 -0
- package/dist/session.js +568 -0
- package/dist/session.js.map +1 -0
- package/dist/types-JE0OMyoi.d.cts +375 -0
- package/dist/types-JE0OMyoi.d.ts +375 -0
- package/docs/consumers.md +90 -0
- package/docs/contoh-hitungcuan.md +255 -0
- package/docs/contoh-plaslink.md +244 -0
- package/docs/error.md +428 -0
- package/docs/examples/auth-verify-readme.ts +18 -0
- package/docs/examples/config-error.ts +24 -0
- package/docs/examples/env.ts +99 -0
- package/docs/examples/failure-log.ts +18 -0
- package/docs/examples/login-client.ts +46 -0
- package/docs/examples/login-flow.ts +186 -0
- package/docs/examples/plaslink-connect.ts +48 -0
- package/docs/examples/readme.ts +60 -0
- package/docs/examples/service-client.ts +105 -0
- package/docs/examples/session-store.ts +53 -0
- package/docs/examples/webhook-express.ts +46 -0
- package/docs/examples/webhook-fetch.ts +60 -0
- package/docs/examples/webhook-nestjs.ts +59 -0
- package/docs/examples/webhook-testing.ts +60 -0
- package/docs/examples/webhook-verifier.ts +143 -0
- package/docs/login-web.md +618 -0
- package/docs/memulai.md +339 -0
- package/docs/server-ke-server.md +275 -0
- package/docs/webhook.md +625 -0
- package/package.json +67 -3
package/dist/session.cjs
ADDED
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/session/index.ts
|
|
21
|
+
var session_exports = {};
|
|
22
|
+
__export(session_exports, {
|
|
23
|
+
ConfigError: () => import_auth_shared2.ConfigError,
|
|
24
|
+
SessionTooLargeError: () => SessionTooLargeError,
|
|
25
|
+
createSessionStore: () => createSessionStore,
|
|
26
|
+
deriveKeyRing: () => deriveKeyRing,
|
|
27
|
+
memoryLockStore: () => memoryLockStore,
|
|
28
|
+
openCookie: () => openCookie,
|
|
29
|
+
sealCookie: () => sealCookie
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(session_exports);
|
|
32
|
+
|
|
33
|
+
// src/session/cookie-crypto.ts
|
|
34
|
+
var import_node_crypto = require("crypto");
|
|
35
|
+
var VERSION = "v1";
|
|
36
|
+
var IV_BYTES = 12;
|
|
37
|
+
var TAG_BYTES = 16;
|
|
38
|
+
var B64URL = /^[A-Za-z0-9_-]*$/;
|
|
39
|
+
function aad(cookie) {
|
|
40
|
+
return Buffer.from(`${VERSION}.${cookie}`, "utf8");
|
|
41
|
+
}
|
|
42
|
+
function deriveKeyRing(secrets, cookie, hkdfSalt) {
|
|
43
|
+
const all = /* @__PURE__ */ new Map();
|
|
44
|
+
let current;
|
|
45
|
+
for (const secret of secrets) {
|
|
46
|
+
const key = Buffer.from(
|
|
47
|
+
(0, import_node_crypto.hkdfSync)("sha256", Buffer.from(secret, "utf8"), hkdfSalt, `cookie:${cookie}:v1`, 32)
|
|
48
|
+
);
|
|
49
|
+
const kid = (0, import_node_crypto.createHash)("sha256").update(key).digest("base64url").slice(0, 8);
|
|
50
|
+
current ??= { kid, key };
|
|
51
|
+
if (!all.has(kid)) all.set(kid, key);
|
|
52
|
+
}
|
|
53
|
+
if (current === void 0) throw new Error("deriveKeyRing: at least one secret is required");
|
|
54
|
+
return { current, all };
|
|
55
|
+
}
|
|
56
|
+
function sealCookie(cookie, value, ring) {
|
|
57
|
+
const iv = (0, import_node_crypto.randomBytes)(IV_BYTES);
|
|
58
|
+
const cipher = (0, import_node_crypto.createCipheriv)("aes-256-gcm", ring.current.key, iv);
|
|
59
|
+
cipher.setAAD(aad(cookie));
|
|
60
|
+
const ct = Buffer.concat([cipher.update(JSON.stringify(value), "utf8"), cipher.final()]);
|
|
61
|
+
const tag = cipher.getAuthTag();
|
|
62
|
+
return [
|
|
63
|
+
VERSION,
|
|
64
|
+
ring.current.kid,
|
|
65
|
+
iv.toString("base64url"),
|
|
66
|
+
ct.toString("base64url"),
|
|
67
|
+
tag.toString("base64url")
|
|
68
|
+
].join(".");
|
|
69
|
+
}
|
|
70
|
+
function decode(part) {
|
|
71
|
+
if (!B64URL.test(part)) return null;
|
|
72
|
+
const decoded = Buffer.from(part, "base64url");
|
|
73
|
+
return decoded.toString("base64url") === part ? decoded : null;
|
|
74
|
+
}
|
|
75
|
+
function openCookie(cookie, sealed, ring) {
|
|
76
|
+
try {
|
|
77
|
+
const parts = sealed.split(".");
|
|
78
|
+
if (parts.length !== 5) return null;
|
|
79
|
+
const [version, kid, ivPart, ctPart, tagPart] = parts;
|
|
80
|
+
if (version !== VERSION) return null;
|
|
81
|
+
const key = ring.all.get(kid);
|
|
82
|
+
if (key === void 0) return null;
|
|
83
|
+
const iv = decode(ivPart);
|
|
84
|
+
const ct = decode(ctPart);
|
|
85
|
+
const tag = decode(tagPart);
|
|
86
|
+
if (iv === null || ct === null || tag === null) return null;
|
|
87
|
+
if (iv.length !== IV_BYTES || tag.length !== TAG_BYTES) return null;
|
|
88
|
+
const decipher = (0, import_node_crypto.createDecipheriv)("aes-256-gcm", key, iv, { authTagLength: TAG_BYTES });
|
|
89
|
+
decipher.setAAD(aad(cookie));
|
|
90
|
+
decipher.setAuthTag(tag);
|
|
91
|
+
const plain = Buffer.concat([decipher.update(ct), decipher.final()]).toString("utf8");
|
|
92
|
+
return { value: JSON.parse(plain), kid };
|
|
93
|
+
} catch {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/session/store.ts
|
|
99
|
+
var import_auth_shared = require("@plasgos/auth-shared");
|
|
100
|
+
|
|
101
|
+
// src/session/compact-keys.ts
|
|
102
|
+
var CORE_COMPACT_KEYS = Object.freeze({
|
|
103
|
+
auth: "a",
|
|
104
|
+
access_token: "at",
|
|
105
|
+
refresh_token: "rt",
|
|
106
|
+
expires_at: "e",
|
|
107
|
+
remember: "r",
|
|
108
|
+
user: "u",
|
|
109
|
+
id: "i",
|
|
110
|
+
name: "n",
|
|
111
|
+
avatar_url: "av",
|
|
112
|
+
email: "em",
|
|
113
|
+
id_token: "idt",
|
|
114
|
+
auth_time: "atm"
|
|
115
|
+
});
|
|
116
|
+
var KEY_PATTERN = /^[A-Za-z0-9_]{1,32}$/;
|
|
117
|
+
var RESERVED = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
118
|
+
var isKey = (value) => typeof value === "string" && KEY_PATTERN.test(value) && !RESERVED.has(value);
|
|
119
|
+
function mergedEntries(table) {
|
|
120
|
+
const merged = new Map(Object.entries(CORE_COMPACT_KEYS));
|
|
121
|
+
for (const [long, short] of Object.entries(table)) merged.set(long, short);
|
|
122
|
+
return [...merged];
|
|
123
|
+
}
|
|
124
|
+
function compactKeyProblems(table) {
|
|
125
|
+
if (table === null || typeof table !== "object" || Array.isArray(table)) {
|
|
126
|
+
return ["compactKeys must be an object"];
|
|
127
|
+
}
|
|
128
|
+
const entries = Object.entries(table);
|
|
129
|
+
if (entries.some(([long, short]) => !isKey(long) || !isKey(short))) {
|
|
130
|
+
return ["compactKeys: keys must match /^[A-Za-z0-9_]{1,32}$/ and must not be a reserved name"];
|
|
131
|
+
}
|
|
132
|
+
const problems = [];
|
|
133
|
+
const core = new Map(Object.entries(CORE_COMPACT_KEYS));
|
|
134
|
+
for (const [long, short] of entries) {
|
|
135
|
+
const expected = core.get(long);
|
|
136
|
+
if (expected !== void 0 && short !== expected) {
|
|
137
|
+
problems.push(`compactKeys: "${long}" must map to "${expected}"`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (problems.length > 0) return problems;
|
|
141
|
+
const merged = mergedEntries(table);
|
|
142
|
+
const longs = new Set(merged.map(([long]) => long));
|
|
143
|
+
const seen = /* @__PURE__ */ new Set();
|
|
144
|
+
const reported = /* @__PURE__ */ new Set();
|
|
145
|
+
for (const [, short] of merged) {
|
|
146
|
+
if (seen.has(short) && !reported.has(short)) {
|
|
147
|
+
reported.add(short);
|
|
148
|
+
problems.push(`compactKeys: short key "${short}" is used twice`);
|
|
149
|
+
}
|
|
150
|
+
seen.add(short);
|
|
151
|
+
if (longs.has(short)) problems.push(`compactKeys: short key "${short}" is also a long key`);
|
|
152
|
+
}
|
|
153
|
+
return problems;
|
|
154
|
+
}
|
|
155
|
+
function renameKeys(value, table) {
|
|
156
|
+
if (Array.isArray(value)) return value.map((item) => renameKeys(item, table));
|
|
157
|
+
if (value === null || typeof value !== "object") return value;
|
|
158
|
+
const out = {};
|
|
159
|
+
for (const [key, item] of Object.entries(value)) {
|
|
160
|
+
const mapped = table.get(key);
|
|
161
|
+
if (mapped !== void 0) out[mapped] = renameKeys(item, table);
|
|
162
|
+
}
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
var IDENTITY = { compact: (value) => value, expand: (value) => value };
|
|
166
|
+
function createKeyCodec(table) {
|
|
167
|
+
if (table === void 0) return IDENTITY;
|
|
168
|
+
const merged = mergedEntries(table);
|
|
169
|
+
const shorts = new Map(merged);
|
|
170
|
+
const longs = new Map(merged.map(([long, short]) => [short, long]));
|
|
171
|
+
return {
|
|
172
|
+
compact: (value) => renameKeys(value, shorts),
|
|
173
|
+
expand: (value) => renameKeys(value, longs)
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// src/session/cookie-header.ts
|
|
178
|
+
function cookieValue(header, name) {
|
|
179
|
+
if (header === null || header === void 0 || header === "") return null;
|
|
180
|
+
for (const pair of header.split(";")) {
|
|
181
|
+
const eq = pair.indexOf("=");
|
|
182
|
+
if (eq === -1) continue;
|
|
183
|
+
if (pair.slice(0, eq).trim() === name) return pair.slice(eq + 1).trim();
|
|
184
|
+
}
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
function serialize(name, value, maxAge, secure) {
|
|
188
|
+
const attrs = [`${name}=${value}`];
|
|
189
|
+
if (maxAge !== void 0) attrs.push(`Max-Age=${String(maxAge)}`);
|
|
190
|
+
attrs.push("Path=/", "HttpOnly");
|
|
191
|
+
if (secure) attrs.push("Secure");
|
|
192
|
+
attrs.push("SameSite=Lax");
|
|
193
|
+
return attrs.join("; ");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// src/session/handle.ts
|
|
197
|
+
var without = (bag, drop) => Object.fromEntries(Object.entries(bag).filter(([key, value]) => !drop(key, value)));
|
|
198
|
+
function createSessionHandle(data, authDirty, restDirty) {
|
|
199
|
+
let bag = { ...data };
|
|
200
|
+
let auth = authDirty;
|
|
201
|
+
let rest = restDirty;
|
|
202
|
+
return {
|
|
203
|
+
get data() {
|
|
204
|
+
return bag;
|
|
205
|
+
},
|
|
206
|
+
get dirty() {
|
|
207
|
+
return auth || rest;
|
|
208
|
+
},
|
|
209
|
+
get authDirty() {
|
|
210
|
+
return auth;
|
|
211
|
+
},
|
|
212
|
+
get restDirty() {
|
|
213
|
+
return rest;
|
|
214
|
+
},
|
|
215
|
+
update(patch) {
|
|
216
|
+
const changes = { ...patch };
|
|
217
|
+
if (Object.hasOwn(changes, "auth") && JSON.stringify(changes["auth"]) !== JSON.stringify(bag["auth"])) {
|
|
218
|
+
auth = true;
|
|
219
|
+
}
|
|
220
|
+
if (Object.keys(changes).some((key) => key !== "auth")) rest = true;
|
|
221
|
+
bag = without({ ...bag, ...changes }, (_key, value) => value === void 0);
|
|
222
|
+
},
|
|
223
|
+
unset(key) {
|
|
224
|
+
if (!Object.hasOwn(bag, key)) return;
|
|
225
|
+
bag = without(bag, (candidate) => candidate === key);
|
|
226
|
+
if (key === "auth") auth = true;
|
|
227
|
+
else rest = true;
|
|
228
|
+
},
|
|
229
|
+
destroy() {
|
|
230
|
+
bag = {};
|
|
231
|
+
auth = true;
|
|
232
|
+
rest = true;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// src/session/parsers.ts
|
|
238
|
+
var TOKEN_MAX = 4096;
|
|
239
|
+
var TEXT_MAX = 512;
|
|
240
|
+
function isRecord(value) {
|
|
241
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
242
|
+
}
|
|
243
|
+
var own = (record, key) => Object.hasOwn(record, key) ? record[key] : void 0;
|
|
244
|
+
var isToken = (value) => typeof value === "string" && value.length >= 1 && value.length <= TOKEN_MAX;
|
|
245
|
+
var isText = (value) => typeof value === "string" && value.length <= TEXT_MAX;
|
|
246
|
+
var isRequiredText = (value) => isText(value) && value.length >= 1;
|
|
247
|
+
var isCount = (value) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
|
248
|
+
function parseUser(input) {
|
|
249
|
+
if (!isRecord(input)) return null;
|
|
250
|
+
const id = own(input, "id");
|
|
251
|
+
const name = own(input, "name");
|
|
252
|
+
const email = own(input, "email");
|
|
253
|
+
const avatar = own(input, "avatar_url");
|
|
254
|
+
if (!isRequiredText(id) || !isText(name)) return null;
|
|
255
|
+
if (email !== void 0 && email !== null && !isText(email)) return null;
|
|
256
|
+
if (avatar !== null && !isText(avatar)) return null;
|
|
257
|
+
return { id, name, ...email !== void 0 ? { email } : {}, avatar_url: avatar };
|
|
258
|
+
}
|
|
259
|
+
function parseAuthState(input) {
|
|
260
|
+
if (!isRecord(input)) return null;
|
|
261
|
+
const accessToken = own(input, "access_token");
|
|
262
|
+
const refreshToken = own(input, "refresh_token");
|
|
263
|
+
const expiresAt = own(input, "expires_at");
|
|
264
|
+
const authTime = own(input, "auth_time");
|
|
265
|
+
const remember = own(input, "remember");
|
|
266
|
+
const user = parseUser(own(input, "user"));
|
|
267
|
+
if (!isToken(accessToken) || !isCount(expiresAt) || user === null) return null;
|
|
268
|
+
if (refreshToken !== void 0 && !isToken(refreshToken)) return null;
|
|
269
|
+
if (authTime !== void 0 && !isCount(authTime)) return null;
|
|
270
|
+
if (remember !== void 0 && typeof remember !== "boolean") return null;
|
|
271
|
+
return {
|
|
272
|
+
access_token: accessToken,
|
|
273
|
+
...refreshToken !== void 0 ? { refresh_token: refreshToken } : {},
|
|
274
|
+
expires_at: expiresAt,
|
|
275
|
+
...remember !== void 0 ? { remember } : {},
|
|
276
|
+
user,
|
|
277
|
+
...authTime !== void 0 ? { auth_time: authTime } : {}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function parseAuthPart(input) {
|
|
281
|
+
if (!isRecord(input)) return null;
|
|
282
|
+
const raw = own(input, "auth");
|
|
283
|
+
if (raw === void 0) return {};
|
|
284
|
+
const auth = parseAuthState(raw);
|
|
285
|
+
return auth === null ? null : { auth };
|
|
286
|
+
}
|
|
287
|
+
function parseIdTokenPart(input) {
|
|
288
|
+
if (!isRecord(input)) return null;
|
|
289
|
+
const idToken = own(input, "id_token");
|
|
290
|
+
return isToken(idToken) ? { id_token: idToken } : null;
|
|
291
|
+
}
|
|
292
|
+
function parseTransaction(input) {
|
|
293
|
+
if (!isRecord(input)) return null;
|
|
294
|
+
const state = own(input, "state");
|
|
295
|
+
const nonce = own(input, "nonce");
|
|
296
|
+
const codeVerifier = own(input, "code_verifier");
|
|
297
|
+
const returnTo = own(input, "return_to");
|
|
298
|
+
const createdAt = own(input, "created_at");
|
|
299
|
+
const reauth = own(input, "reauth");
|
|
300
|
+
const maxAge = own(input, "max_age");
|
|
301
|
+
if (!isRequiredText(state) || !isRequiredText(nonce) || !isRequiredText(codeVerifier)) return null;
|
|
302
|
+
if (!isText(returnTo) || !isCount(createdAt)) return null;
|
|
303
|
+
if (reauth !== void 0 && typeof reauth !== "boolean") return null;
|
|
304
|
+
if (maxAge !== void 0 && !isCount(maxAge)) return null;
|
|
305
|
+
return {
|
|
306
|
+
state,
|
|
307
|
+
nonce,
|
|
308
|
+
code_verifier: codeVerifier,
|
|
309
|
+
return_to: returnTo,
|
|
310
|
+
created_at: createdAt,
|
|
311
|
+
...reauth !== void 0 ? { reauth } : {},
|
|
312
|
+
...maxAge !== void 0 ? { max_age: maxAge } : {}
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// src/session/store.ts
|
|
317
|
+
var MAX_SET_COOKIE_CHARS = 4e3;
|
|
318
|
+
var TRANSACTION_MAX_AGE_SECONDS = 600;
|
|
319
|
+
var CLOCK_SKEW_MS = 5e3;
|
|
320
|
+
var MIN_SECRET_BYTES = 32;
|
|
321
|
+
var COOKIE_NAME = /^[a-z0-9-]{1,32}$/;
|
|
322
|
+
var ROLES = ["app", "auth", "idToken", "transaction"];
|
|
323
|
+
var SessionTooLargeError = class extends Error {
|
|
324
|
+
/** Length of the refused `Set-Cookie` value, in characters. */
|
|
325
|
+
size;
|
|
326
|
+
/** Message "Session cookie too large: <size> chars (limit 4000)"; never the cookie value. */
|
|
327
|
+
constructor(size) {
|
|
328
|
+
super(`Session cookie too large: ${String(size)} chars (limit ${String(MAX_SET_COOKIE_CHARS)})`);
|
|
329
|
+
this.name = "SessionTooLargeError";
|
|
330
|
+
this.size = size;
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
var isPositiveInteger = (value) => typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
334
|
+
var isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
335
|
+
function readOptions(raw) {
|
|
336
|
+
if (!isObject(raw)) throw new import_auth_shared.ConfigError(["options must be an object"]);
|
|
337
|
+
const problems = [];
|
|
338
|
+
const { environment, hostPrefix, hkdfSalt, secrets, cookies, authMaxAgeSeconds } = raw;
|
|
339
|
+
const { appSchema, compactKeys, onFailure } = raw;
|
|
340
|
+
if (typeof environment !== "string" || !import_auth_shared.ENVIRONMENTS.includes(environment)) {
|
|
341
|
+
problems.push('environment must be "sandbox" or "production"');
|
|
342
|
+
}
|
|
343
|
+
if (hostPrefix !== void 0 && typeof hostPrefix !== "boolean") {
|
|
344
|
+
problems.push("hostPrefix must be a boolean");
|
|
345
|
+
} else if (hostPrefix === false && environment === "production") {
|
|
346
|
+
problems.push('hostPrefix: false is not allowed when environment is "production"');
|
|
347
|
+
}
|
|
348
|
+
if (hkdfSalt === void 0 || hkdfSalt === null || hkdfSalt === "") {
|
|
349
|
+
problems.push("hkdfSalt is required");
|
|
350
|
+
} else if (typeof hkdfSalt !== "string") {
|
|
351
|
+
problems.push("hkdfSalt must be a string");
|
|
352
|
+
}
|
|
353
|
+
const secretList = Array.isArray(secrets) && secrets.every((secret) => typeof secret === "string") ? secrets : [];
|
|
354
|
+
if (secretList.length < 1 || secretList.length > 2) {
|
|
355
|
+
problems.push("secrets must hold one or two values");
|
|
356
|
+
} else if (secretList.some((secret) => Buffer.byteLength(secret, "utf8") < MIN_SECRET_BYTES)) {
|
|
357
|
+
problems.push("secrets values must each be at least 32 bytes");
|
|
358
|
+
}
|
|
359
|
+
const names = {};
|
|
360
|
+
const given = isObject(cookies) ? cookies : {};
|
|
361
|
+
for (const role of ROLES) {
|
|
362
|
+
const name = Object.hasOwn(given, role) ? given[role] : void 0;
|
|
363
|
+
if (typeof name === "string" && COOKIE_NAME.test(name)) names[role] = name;
|
|
364
|
+
else problems.push(`cookies.${role} must match /^[a-z0-9-]{1,32}$/`);
|
|
365
|
+
}
|
|
366
|
+
const nameList = Object.values(names);
|
|
367
|
+
if (nameList.length === ROLES.length && new Set(nameList).size !== ROLES.length) {
|
|
368
|
+
problems.push("cookies names must be distinct");
|
|
369
|
+
}
|
|
370
|
+
if (!isPositiveInteger(authMaxAgeSeconds) && typeof authMaxAgeSeconds !== "function") {
|
|
371
|
+
problems.push("authMaxAgeSeconds must be a positive integer or a function");
|
|
372
|
+
}
|
|
373
|
+
const parse = isObject(appSchema) ? appSchema["parse"] : void 0;
|
|
374
|
+
if (typeof parse !== "function") problems.push("appSchema must have a parse function");
|
|
375
|
+
if (compactKeys !== void 0) problems.push(...compactKeyProblems(compactKeys));
|
|
376
|
+
if (onFailure !== void 0 && typeof onFailure !== "function") {
|
|
377
|
+
problems.push("onFailure must be a function");
|
|
378
|
+
}
|
|
379
|
+
if (problems.length > 0) throw new import_auth_shared.ConfigError(problems);
|
|
380
|
+
return {
|
|
381
|
+
secrets: secretList,
|
|
382
|
+
hkdfSalt,
|
|
383
|
+
cookies: names,
|
|
384
|
+
secure: hostPrefix !== false,
|
|
385
|
+
authMaxAge: authMaxAgeSeconds,
|
|
386
|
+
parseApp: (input) => appSchema.parse(input),
|
|
387
|
+
compactKeys,
|
|
388
|
+
onFailure
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
function checkSize(header) {
|
|
392
|
+
if (header.length > MAX_SET_COOKIE_CHARS) throw new SessionTooLargeError(header.length);
|
|
393
|
+
return header;
|
|
394
|
+
}
|
|
395
|
+
function withFallback(cookie, commit, drop, report) {
|
|
396
|
+
try {
|
|
397
|
+
return commit();
|
|
398
|
+
} catch (error) {
|
|
399
|
+
if (!(error instanceof SessionTooLargeError)) throw error;
|
|
400
|
+
report({ kind: "session_too_large", cookie, size: error.size });
|
|
401
|
+
drop();
|
|
402
|
+
return commit();
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
function createSessionStore(options) {
|
|
406
|
+
const settings = readOptions(options);
|
|
407
|
+
const { cookies, secure } = settings;
|
|
408
|
+
const codec = createKeyCodec(settings.compactKeys);
|
|
409
|
+
const ring = (role) => rings[role];
|
|
410
|
+
const rings = {
|
|
411
|
+
app: deriveKeyRing(settings.secrets, cookies.app, settings.hkdfSalt),
|
|
412
|
+
auth: deriveKeyRing(settings.secrets, cookies.auth, settings.hkdfSalt),
|
|
413
|
+
idToken: deriveKeyRing(settings.secrets, cookies.idToken, settings.hkdfSalt),
|
|
414
|
+
transaction: deriveKeyRing(settings.secrets, cookies.transaction, settings.hkdfSalt)
|
|
415
|
+
};
|
|
416
|
+
const wireName = (role) => secure ? `__Host-${cookies[role]}` : cookies[role];
|
|
417
|
+
const cookieNames = Object.freeze({
|
|
418
|
+
app: wireName("app"),
|
|
419
|
+
auth: wireName("auth"),
|
|
420
|
+
idToken: wireName("idToken"),
|
|
421
|
+
transaction: wireName("transaction")
|
|
422
|
+
});
|
|
423
|
+
const headerOf = (input) => typeof input === "string" ? input : input?.headers.get("cookie") ?? null;
|
|
424
|
+
const open = (header, role) => {
|
|
425
|
+
const raw = cookieValue(header, cookieNames[role]);
|
|
426
|
+
if (raw === null || raw === "") return null;
|
|
427
|
+
return openCookie(cookies[role], raw, ring(role));
|
|
428
|
+
};
|
|
429
|
+
const seal = (role, value, maxAge) => serialize(cookieNames[role], sealCookie(cookies[role], value, ring(role)), maxAge, secure);
|
|
430
|
+
const expire = (role) => serialize(cookieNames[role], "", 0, secure);
|
|
431
|
+
const parseApp = (value) => {
|
|
432
|
+
let parsed;
|
|
433
|
+
try {
|
|
434
|
+
parsed = settings.parseApp(value);
|
|
435
|
+
} catch {
|
|
436
|
+
return null;
|
|
437
|
+
}
|
|
438
|
+
if (!isObject(parsed)) return null;
|
|
439
|
+
return Object.fromEntries(Object.entries(parsed).filter(([key]) => key !== "auth"));
|
|
440
|
+
};
|
|
441
|
+
const authMaxAge = (auth) => {
|
|
442
|
+
const { authMaxAge: setting } = settings;
|
|
443
|
+
if (typeof setting === "number") return setting;
|
|
444
|
+
const seconds = setting(auth);
|
|
445
|
+
if (!isPositiveInteger(seconds)) {
|
|
446
|
+
throw new import_auth_shared.ConfigError(["authMaxAgeSeconds must return a positive integer"]);
|
|
447
|
+
}
|
|
448
|
+
return seconds;
|
|
449
|
+
};
|
|
450
|
+
const authCookie = (auth) => {
|
|
451
|
+
const maxAge = authMaxAge(auth);
|
|
452
|
+
const sealed = Object.fromEntries(Object.entries(auth).filter(([key]) => key !== "id_token"));
|
|
453
|
+
return seal("auth", codec.compact({ auth: sealed }), maxAge);
|
|
454
|
+
};
|
|
455
|
+
const idTokenCookie = (auth, idToken) => seal("idToken", codec.compact({ id_token: idToken }), authMaxAge(auth));
|
|
456
|
+
const report = (event) => {
|
|
457
|
+
try {
|
|
458
|
+
settings.onFailure?.(event);
|
|
459
|
+
} catch {
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
const store = {
|
|
463
|
+
cookieNames,
|
|
464
|
+
read(input) {
|
|
465
|
+
const header = headerOf(input);
|
|
466
|
+
const appOpened = open(header, "app");
|
|
467
|
+
const rest = appOpened === null ? null : parseApp(codec.expand(appOpened.value));
|
|
468
|
+
const authOpened = open(header, "auth");
|
|
469
|
+
let auth = authOpened === null ? void 0 : parseAuthPart(codec.expand(authOpened.value))?.auth;
|
|
470
|
+
if (auth !== void 0) {
|
|
471
|
+
const idOpened = open(header, "idToken");
|
|
472
|
+
const idPart = idOpened === null ? null : parseIdTokenPart(codec.expand(idOpened.value));
|
|
473
|
+
if (idPart !== null) auth = { ...auth, id_token: idPart.id_token };
|
|
474
|
+
}
|
|
475
|
+
const data = { ...rest, ...auth !== void 0 ? { auth } : {} };
|
|
476
|
+
const migrate = rest !== null && appOpened?.kid !== ring("app").current.kid;
|
|
477
|
+
return createSessionHandle(data, false, migrate);
|
|
478
|
+
},
|
|
479
|
+
commitApp(session) {
|
|
480
|
+
const rest = Object.fromEntries(
|
|
481
|
+
Object.entries(session.data).filter(([key, value]) => key !== "auth" && value !== void 0)
|
|
482
|
+
);
|
|
483
|
+
if (Object.keys(rest).length === 0) return expire("app");
|
|
484
|
+
return checkSize(seal("app", codec.compact(rest), void 0));
|
|
485
|
+
},
|
|
486
|
+
commitAuth(session) {
|
|
487
|
+
const { auth } = session.data;
|
|
488
|
+
if (auth === void 0) return expire("auth");
|
|
489
|
+
return checkSize(authCookie(auth));
|
|
490
|
+
},
|
|
491
|
+
commitIdToken(session) {
|
|
492
|
+
const { auth } = session.data;
|
|
493
|
+
if (auth?.id_token === void 0 || auth.id_token === "") return expire("idToken");
|
|
494
|
+
return checkSize(idTokenCookie(auth, auth.id_token));
|
|
495
|
+
},
|
|
496
|
+
fits(auth) {
|
|
497
|
+
const idToken = auth.id_token;
|
|
498
|
+
return {
|
|
499
|
+
auth: authCookie(auth).length <= MAX_SET_COOKIE_CHARS,
|
|
500
|
+
idToken: idToken === void 0 || idToken === "" || idTokenCookie(auth, idToken).length <= MAX_SET_COOKIE_CHARS
|
|
501
|
+
};
|
|
502
|
+
},
|
|
503
|
+
commit(session) {
|
|
504
|
+
const out = [];
|
|
505
|
+
if (session.restDirty) {
|
|
506
|
+
out.push(
|
|
507
|
+
withFallback(
|
|
508
|
+
"app",
|
|
509
|
+
() => store.commitApp(session),
|
|
510
|
+
() => {
|
|
511
|
+
for (const key of Object.keys(session.data)) {
|
|
512
|
+
if (key !== "auth") session.unset(key);
|
|
513
|
+
}
|
|
514
|
+
},
|
|
515
|
+
report
|
|
516
|
+
)
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
if (!session.authDirty) return out;
|
|
520
|
+
out.push(
|
|
521
|
+
withFallback(
|
|
522
|
+
"auth",
|
|
523
|
+
() => store.commitAuth(session),
|
|
524
|
+
() => {
|
|
525
|
+
session.unset("auth");
|
|
526
|
+
},
|
|
527
|
+
report
|
|
528
|
+
)
|
|
529
|
+
);
|
|
530
|
+
out.push(
|
|
531
|
+
withFallback(
|
|
532
|
+
"idToken",
|
|
533
|
+
() => store.commitIdToken(session),
|
|
534
|
+
() => {
|
|
535
|
+
const { auth } = session.data;
|
|
536
|
+
if (auth === void 0) return;
|
|
537
|
+
const kept = Object.fromEntries(Object.entries(auth).filter(([key]) => key !== "id_token"));
|
|
538
|
+
session.update({ auth: kept });
|
|
539
|
+
},
|
|
540
|
+
report
|
|
541
|
+
)
|
|
542
|
+
);
|
|
543
|
+
return out;
|
|
544
|
+
},
|
|
545
|
+
readTransaction(input, now = Date.now()) {
|
|
546
|
+
const tx = parseTransaction(open(headerOf(input), "transaction")?.value);
|
|
547
|
+
if (tx === null) return null;
|
|
548
|
+
const age = now - tx.created_at;
|
|
549
|
+
if (!(age >= -CLOCK_SKEW_MS && age <= TRANSACTION_MAX_AGE_SECONDS * 1e3)) return null;
|
|
550
|
+
return tx;
|
|
551
|
+
},
|
|
552
|
+
commitTransaction(tx) {
|
|
553
|
+
return seal("transaction", tx, TRANSACTION_MAX_AGE_SECONDS);
|
|
554
|
+
},
|
|
555
|
+
clearTransaction() {
|
|
556
|
+
return expire("transaction");
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
return store;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// src/core/refresh-lock.ts
|
|
563
|
+
function memoryLockStore(options = {}) {
|
|
564
|
+
const now = options.now ?? Date.now;
|
|
565
|
+
const locks = /* @__PURE__ */ new Map();
|
|
566
|
+
return {
|
|
567
|
+
run(key, refresh, reuseMs) {
|
|
568
|
+
const t = now();
|
|
569
|
+
for (const [storedKey, entry2] of locks) {
|
|
570
|
+
if (entry2.settledAt !== void 0 && t - entry2.settledAt >= entry2.reuseMs) locks.delete(storedKey);
|
|
571
|
+
}
|
|
572
|
+
const hit = locks.get(key);
|
|
573
|
+
if (hit) return hit.promise;
|
|
574
|
+
const entry = { promise: refresh(), reuseMs };
|
|
575
|
+
locks.set(key, entry);
|
|
576
|
+
entry.promise.then(
|
|
577
|
+
() => {
|
|
578
|
+
entry.settledAt = now();
|
|
579
|
+
},
|
|
580
|
+
() => {
|
|
581
|
+
if (locks.get(key) === entry) locks.delete(key);
|
|
582
|
+
}
|
|
583
|
+
);
|
|
584
|
+
return entry.promise;
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
// src/session/index.ts
|
|
590
|
+
var import_auth_shared2 = require("@plasgos/auth-shared");
|
|
591
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
592
|
+
0 && (module.exports = {
|
|
593
|
+
ConfigError,
|
|
594
|
+
SessionTooLargeError,
|
|
595
|
+
createSessionStore,
|
|
596
|
+
deriveKeyRing,
|
|
597
|
+
memoryLockStore,
|
|
598
|
+
openCookie,
|
|
599
|
+
sealCookie
|
|
600
|
+
});
|
|
601
|
+
//# sourceMappingURL=session.cjs.map
|