@oxog/authkeeper 1.5.1 → 1.6.0
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 +9 -0
- package/README.md +59 -2
- package/dist/{auth-source-CXqrhLDO.d.ts → auth-source-BwPxq1Za.d.ts} +1 -1
- package/dist/{auth-source-rjMWiP1Q.d.cts → auth-source-MSiTDST6.d.cts} +1 -1
- package/dist/{chunk-DLCD3SBV.js → chunk-K3XXGKGO.js} +2 -2
- package/dist/chunk-K3XXGKGO.js.map +1 -0
- package/dist/{chunk-XGKVBZSF.cjs → chunk-MONA6MGX.cjs} +2 -2
- package/dist/chunk-MONA6MGX.cjs.map +1 -0
- package/dist/{chunk-VVSZPARR.cjs → chunk-SI7KITBT.cjs} +323 -273
- package/dist/chunk-SI7KITBT.cjs.map +1 -0
- package/dist/{chunk-7B77NZGY.js → chunk-ZZMKSCXQ.js} +312 -263
- package/dist/chunk-ZZMKSCXQ.js.map +1 -0
- package/dist/{index-CQwGO-Wk.d.ts → index-BqxH4X40.d.ts} +52 -6
- package/dist/{index-C4pGS-TI.d.cts → index-ChJtNH74.d.cts} +52 -6
- package/dist/index.cjs +461 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +32 -4
- package/dist/index.d.ts +32 -4
- package/dist/index.js +416 -7
- package/dist/index.js.map +1 -1
- package/dist/plugins/index.cjs +15 -15
- package/dist/plugins/index.d.cts +2 -2
- package/dist/plugins/index.d.ts +2 -2
- package/dist/plugins/index.js +2 -2
- package/dist/react/index.cjs +3 -3
- package/dist/react/index.d.cts +2 -2
- package/dist/react/index.d.ts +2 -2
- package/dist/react/index.js +1 -1
- package/dist/svelte/index.d.cts +2 -2
- package/dist/svelte/index.d.ts +2 -2
- package/dist/{types-DMPBl-Ym.d.ts → types-DWB4dbp8.d.cts} +26 -4
- package/dist/{types-DMPBl-Ym.d.cts → types-DWB4dbp8.d.ts} +26 -4
- package/dist/vue/index.cjs +2 -2
- package/dist/vue/index.d.cts +2 -2
- package/dist/vue/index.d.ts +2 -2
- package/dist/vue/index.js +1 -1
- package/package.json +1 -1
- package/dist/chunk-7B77NZGY.js.map +0 -1
- package/dist/chunk-DLCD3SBV.js.map +0 -1
- package/dist/chunk-VVSZPARR.cjs.map +0 -1
- package/dist/chunk-XGKVBZSF.cjs.map +0 -1
|
@@ -1,6 +1,253 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkMONA6MGX_cjs = require('./chunk-MONA6MGX.cjs');
|
|
4
|
+
|
|
5
|
+
// src/plugins/core/storage-memory.ts
|
|
6
|
+
var MemoryStorageAdapter = class {
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
this.store = /* @__PURE__ */ new Map();
|
|
9
|
+
this.prefix = options.prefix || "authkeeper:";
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Get value by key
|
|
13
|
+
*/
|
|
14
|
+
get(key) {
|
|
15
|
+
return this.store.get(this.prefix + key) || null;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Set value by key
|
|
19
|
+
*/
|
|
20
|
+
set(key, value) {
|
|
21
|
+
this.store.set(this.prefix + key, value);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Remove value by key
|
|
25
|
+
*/
|
|
26
|
+
remove(key) {
|
|
27
|
+
this.store.delete(this.prefix + key);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Clear all values
|
|
31
|
+
*/
|
|
32
|
+
clear() {
|
|
33
|
+
const keysToDelete = [];
|
|
34
|
+
for (const key of this.store.keys()) {
|
|
35
|
+
if (key.startsWith(this.prefix)) {
|
|
36
|
+
keysToDelete.push(key);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
keysToDelete.forEach((key) => this.store.delete(key));
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
function createMemoryStorage(options) {
|
|
43
|
+
return new MemoryStorageAdapter(options);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/utils/cookie.ts
|
|
47
|
+
function parseCookies(cookieString) {
|
|
48
|
+
const cookies = {};
|
|
49
|
+
if (!cookieString) {
|
|
50
|
+
return cookies;
|
|
51
|
+
}
|
|
52
|
+
cookieString.split(";").forEach((cookie) => {
|
|
53
|
+
const [key, ...valueParts] = cookie.split("=");
|
|
54
|
+
const trimmedKey = key?.trim();
|
|
55
|
+
const value = valueParts.join("=").trim();
|
|
56
|
+
if (trimmedKey) {
|
|
57
|
+
cookies[safeDecode(trimmedKey)] = safeDecode(value);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
return cookies;
|
|
61
|
+
}
|
|
62
|
+
function safeDecode(value) {
|
|
63
|
+
try {
|
|
64
|
+
return decodeURIComponent(value);
|
|
65
|
+
} catch {
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
var INVALID_ATTRIBUTE_VALUE = /[;\r\n\0]/;
|
|
70
|
+
function assertAttributeValue(name, value) {
|
|
71
|
+
if (INVALID_ATTRIBUTE_VALUE.test(value)) {
|
|
72
|
+
throw new TypeError(`Invalid cookie ${name}: must not contain ';', CR, LF or NUL`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function getCookie(name) {
|
|
76
|
+
if (typeof document === "undefined") {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
const cookies = parseCookies(document.cookie);
|
|
80
|
+
return cookies[name] || null;
|
|
81
|
+
}
|
|
82
|
+
function serializeCookie(name, value, attributes = {}) {
|
|
83
|
+
let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
84
|
+
if (attributes.path) {
|
|
85
|
+
assertAttributeValue("path", attributes.path);
|
|
86
|
+
cookie += `; Path=${attributes.path}`;
|
|
87
|
+
}
|
|
88
|
+
if (attributes.domain) {
|
|
89
|
+
assertAttributeValue("domain", attributes.domain);
|
|
90
|
+
cookie += `; Domain=${attributes.domain}`;
|
|
91
|
+
}
|
|
92
|
+
if (attributes.maxAge !== void 0) {
|
|
93
|
+
cookie += `; Max-Age=${attributes.maxAge}`;
|
|
94
|
+
}
|
|
95
|
+
if (attributes.expires) {
|
|
96
|
+
cookie += `; Expires=${attributes.expires.toUTCString()}`;
|
|
97
|
+
}
|
|
98
|
+
if (attributes.secure || attributes.sameSite === "none") {
|
|
99
|
+
cookie += "; Secure";
|
|
100
|
+
}
|
|
101
|
+
if (attributes.sameSite) {
|
|
102
|
+
const sameSite = attributes.sameSite.charAt(0).toUpperCase() + attributes.sameSite.slice(1);
|
|
103
|
+
cookie += `; SameSite=${sameSite}`;
|
|
104
|
+
}
|
|
105
|
+
if (attributes.httpOnly) {
|
|
106
|
+
cookie += "; HttpOnly";
|
|
107
|
+
}
|
|
108
|
+
return cookie;
|
|
109
|
+
}
|
|
110
|
+
function setCookie(name, value, attributes = {}) {
|
|
111
|
+
if (typeof document === "undefined") {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
document.cookie = serializeCookie(name, value, attributes);
|
|
115
|
+
}
|
|
116
|
+
function deleteCookie(name, attributes = {}) {
|
|
117
|
+
if (typeof document === "undefined") {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
document.cookie = serializeCookie(name, "", {
|
|
121
|
+
...attributes,
|
|
122
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/utils/storage.ts
|
|
127
|
+
function isQuotaExceeded(error) {
|
|
128
|
+
if (typeof error !== "object" || error === null) return false;
|
|
129
|
+
const { name, code } = error;
|
|
130
|
+
return name === "QuotaExceededError" || code === 22;
|
|
131
|
+
}
|
|
132
|
+
function isStorageAvailable(storage) {
|
|
133
|
+
if (!storage) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
try {
|
|
137
|
+
const testKey = "__storage_test__";
|
|
138
|
+
storage.setItem(testKey, "test");
|
|
139
|
+
storage.removeItem(testKey);
|
|
140
|
+
return true;
|
|
141
|
+
} catch (error) {
|
|
142
|
+
return isQuotaExceeded(error) && storage.length > 0;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function safeGetItem(storage, key) {
|
|
146
|
+
try {
|
|
147
|
+
return storage.get(key);
|
|
148
|
+
} catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function safeSetItem(storage, key, value) {
|
|
153
|
+
try {
|
|
154
|
+
storage.set(key, value);
|
|
155
|
+
return true;
|
|
156
|
+
} catch {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function safeRemoveItem(storage, key) {
|
|
161
|
+
try {
|
|
162
|
+
storage.remove(key);
|
|
163
|
+
return true;
|
|
164
|
+
} catch {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/plugins/core/storage-web.ts
|
|
170
|
+
var WebStorageAdapter = class {
|
|
171
|
+
constructor(storage, options = {}) {
|
|
172
|
+
this.storage = storage;
|
|
173
|
+
this.prefix = options.prefix ?? "authkeeper:";
|
|
174
|
+
}
|
|
175
|
+
get(key) {
|
|
176
|
+
return this.storage.getItem(this.prefix + key);
|
|
177
|
+
}
|
|
178
|
+
set(key, value) {
|
|
179
|
+
this.storage.setItem(this.prefix + key, value);
|
|
180
|
+
}
|
|
181
|
+
remove(key) {
|
|
182
|
+
this.storage.removeItem(this.prefix + key);
|
|
183
|
+
}
|
|
184
|
+
clear() {
|
|
185
|
+
const keys = [];
|
|
186
|
+
for (let i = 0; i < this.storage.length; i++) {
|
|
187
|
+
const key = this.storage.key(i);
|
|
188
|
+
if (key !== null && key.startsWith(this.prefix)) {
|
|
189
|
+
keys.push(key);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
keys.forEach((key) => this.storage.removeItem(key));
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
var CookieStorageAdapter = class {
|
|
196
|
+
constructor(options = {}) {
|
|
197
|
+
this.keys = /* @__PURE__ */ new Set();
|
|
198
|
+
this.prefix = options.prefix ?? "authkeeper_";
|
|
199
|
+
this.attributes = { path: "/", secure: true, sameSite: "strict", ...options.attributes };
|
|
200
|
+
}
|
|
201
|
+
get(key) {
|
|
202
|
+
return getCookie(this.prefix + key);
|
|
203
|
+
}
|
|
204
|
+
set(key, value) {
|
|
205
|
+
setCookie(this.prefix + key, value, this.attributes);
|
|
206
|
+
this.keys.add(key);
|
|
207
|
+
if (typeof document !== "undefined" && this.get(key) !== value) {
|
|
208
|
+
throw new chunkMONA6MGX_cjs.AuthError(
|
|
209
|
+
"STORAGE_ERROR",
|
|
210
|
+
`Cookie "${this.prefix + key}" was not stored (too large or blocked)`
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
remove(key) {
|
|
215
|
+
const { path, domain, secure, sameSite } = this.attributes;
|
|
216
|
+
deleteCookie(this.prefix + key, { path, domain, secure, sameSite });
|
|
217
|
+
this.keys.delete(key);
|
|
218
|
+
}
|
|
219
|
+
clear() {
|
|
220
|
+
Array.from(this.keys).forEach((key) => this.remove(key));
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
function getWebStorage(type) {
|
|
224
|
+
try {
|
|
225
|
+
return typeof window !== "undefined" ? window[type] : void 0;
|
|
226
|
+
} catch {
|
|
227
|
+
return void 0;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function resolveStorage(storage) {
|
|
231
|
+
if (storage === void 0 || storage === "memory") {
|
|
232
|
+
return void 0;
|
|
233
|
+
}
|
|
234
|
+
if (typeof storage === "object" && storage !== null) {
|
|
235
|
+
return storage;
|
|
236
|
+
}
|
|
237
|
+
switch (storage) {
|
|
238
|
+
case "localStorage":
|
|
239
|
+
case "sessionStorage": {
|
|
240
|
+
const webStorage = getWebStorage(storage);
|
|
241
|
+
return webStorage && isStorageAvailable(webStorage) ? new WebStorageAdapter(webStorage) : createMemoryStorage();
|
|
242
|
+
}
|
|
243
|
+
case "cookie":
|
|
244
|
+
return typeof document !== "undefined" ? new CookieStorageAdapter() : createMemoryStorage();
|
|
245
|
+
default:
|
|
246
|
+
throw new chunkMONA6MGX_cjs.AuthError("CONFIG_ERROR", `Unknown storage type: ${String(storage)}`, {
|
|
247
|
+
context: { storage }
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
4
251
|
|
|
5
252
|
// src/utils/base64.ts
|
|
6
253
|
function base64UrlEncode(buffer) {
|
|
@@ -54,7 +301,7 @@ function decodeJwt(token) {
|
|
|
54
301
|
const parts = token.split(".");
|
|
55
302
|
const [headerPart, payloadPart] = parts;
|
|
56
303
|
if (parts.length !== 3 || headerPart === void 0 || payloadPart === void 0) {
|
|
57
|
-
throw new
|
|
304
|
+
throw new chunkMONA6MGX_cjs.AuthError("TOKEN_DECODE_FAILED", "Invalid JWT format: must have 3 parts", {
|
|
58
305
|
context: { parts: parts.length }
|
|
59
306
|
});
|
|
60
307
|
}
|
|
@@ -65,7 +312,7 @@ function decodeJwt(token) {
|
|
|
65
312
|
const payload = JSON.parse(payloadJson);
|
|
66
313
|
return { header, payload };
|
|
67
314
|
} catch (error) {
|
|
68
|
-
throw new
|
|
315
|
+
throw new chunkMONA6MGX_cjs.AuthError("TOKEN_DECODE_FAILED", "Failed to decode JWT", {
|
|
69
316
|
cause: error
|
|
70
317
|
});
|
|
71
318
|
}
|
|
@@ -151,6 +398,16 @@ function getClaims(token, claims) {
|
|
|
151
398
|
}
|
|
152
399
|
}
|
|
153
400
|
|
|
401
|
+
// src/plugins/core/stored-tokens.ts
|
|
402
|
+
function isStoredTokens(value) {
|
|
403
|
+
if (value === null || typeof value !== "object") return false;
|
|
404
|
+
const v = value;
|
|
405
|
+
return typeof v["accessToken"] === "string" && v["accessToken"] !== "" && (v["refreshToken"] === null || typeof v["refreshToken"] === "string") && typeof v["tokenType"] === "string" && (v["expiresAt"] === null || isFiniteNumber(v["expiresAt"])) && isFiniteNumber(v["setAt"]) && isFiniteNumber(v["refreshCount"]);
|
|
406
|
+
}
|
|
407
|
+
function isFiniteNumber(value) {
|
|
408
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
409
|
+
}
|
|
410
|
+
|
|
154
411
|
// src/plugins/core/token-store.ts
|
|
155
412
|
var TokenStorePlugin = class {
|
|
156
413
|
constructor(options = {}) {
|
|
@@ -189,7 +446,12 @@ var TokenStorePlugin = class {
|
|
|
189
446
|
if (!this.tokens?.setAt) return null;
|
|
190
447
|
return new Date(this.tokens.setAt);
|
|
191
448
|
},
|
|
192
|
-
getRefreshCount: () => this.tokens?.refreshCount || 0
|
|
449
|
+
getRefreshCount: () => this.tokens?.refreshCount || 0,
|
|
450
|
+
replace: (tokens) => {
|
|
451
|
+
this.tokens = tokens === null ? null : { ...tokens };
|
|
452
|
+
},
|
|
453
|
+
readPersisted: () => this.readPersisted(),
|
|
454
|
+
getStorage: () => this.options.storage ? { adapter: this.options.storage, key: this.storageKey } : null
|
|
193
455
|
};
|
|
194
456
|
}
|
|
195
457
|
/**
|
|
@@ -197,15 +459,15 @@ var TokenStorePlugin = class {
|
|
|
197
459
|
*/
|
|
198
460
|
set(tokens) {
|
|
199
461
|
if (typeof tokens?.accessToken !== "string" || tokens.accessToken === "") {
|
|
200
|
-
throw new
|
|
462
|
+
throw new chunkMONA6MGX_cjs.AuthError("TOKEN_INVALID", "accessToken must be a non-empty string");
|
|
201
463
|
}
|
|
202
464
|
if (this.options.validateOnSet && !isValidJwtFormat(tokens.accessToken)) {
|
|
203
|
-
throw new
|
|
465
|
+
throw new chunkMONA6MGX_cjs.AuthError("TOKEN_INVALID", "accessToken is not a valid JWT");
|
|
204
466
|
}
|
|
205
467
|
let expiresAt;
|
|
206
|
-
if (
|
|
468
|
+
if (isFiniteNumber2(tokens.expiresAt)) {
|
|
207
469
|
expiresAt = tokens.expiresAt * 1e3;
|
|
208
|
-
} else if (
|
|
470
|
+
} else if (isFiniteNumber2(tokens.expiresIn)) {
|
|
209
471
|
expiresAt = Date.now() + tokens.expiresIn * 1e3;
|
|
210
472
|
} else {
|
|
211
473
|
expiresAt = getTokenExpiry(tokens.accessToken)?.getTime() ?? null;
|
|
@@ -271,6 +533,15 @@ var TokenStorePlugin = class {
|
|
|
271
533
|
this.reportStorageError("Failed to persist tokens", error);
|
|
272
534
|
}
|
|
273
535
|
}
|
|
536
|
+
/** The tokens in the storage adapter, if readable and well-formed */
|
|
537
|
+
readPersisted() {
|
|
538
|
+
try {
|
|
539
|
+
const parsed = JSON.parse(this.options.storage?.get(this.storageKey) ?? "null");
|
|
540
|
+
return isStoredTokens(parsed) ? parsed : null;
|
|
541
|
+
} catch {
|
|
542
|
+
return null;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
274
545
|
/**
|
|
275
546
|
* Load tokens from the storage adapter. Corrupt or malformed entries are
|
|
276
547
|
* removed rather than trusted.
|
|
@@ -303,23 +574,18 @@ var TokenStorePlugin = class {
|
|
|
303
574
|
this.reportStorageError("Discarded malformed persisted tokens");
|
|
304
575
|
}
|
|
305
576
|
reportStorageError(message, cause) {
|
|
306
|
-
const error = new
|
|
577
|
+
const error = new chunkMONA6MGX_cjs.AuthError("STORAGE_ERROR", message, {
|
|
307
578
|
cause: (
|
|
308
579
|
// eslint-disable-next-line @typescript-eslint/no-base-to-string -- a non-Error cause is kept as its string form
|
|
309
580
|
cause === void 0 ? void 0 : cause instanceof Error ? cause : new Error(String(cause))
|
|
310
581
|
)
|
|
311
582
|
});
|
|
312
583
|
if (this.kernel && typeof this.kernel.emit === "function") {
|
|
313
|
-
this.kernel.emit({ type: "error", error, context: "storage", timestamp: Date.now() });
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
};
|
|
317
|
-
function
|
|
318
|
-
if (value === null || typeof value !== "object") return false;
|
|
319
|
-
const v = value;
|
|
320
|
-
return typeof v["accessToken"] === "string" && v["accessToken"] !== "" && (v["refreshToken"] === null || typeof v["refreshToken"] === "string") && typeof v["tokenType"] === "string" && (v["expiresAt"] === null || isFiniteNumber(v["expiresAt"])) && isFiniteNumber(v["setAt"]) && isFiniteNumber(v["refreshCount"]);
|
|
321
|
-
}
|
|
322
|
-
function isFiniteNumber(value) {
|
|
584
|
+
this.kernel.emit({ type: "error", error, context: "storage", timestamp: Date.now() });
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
};
|
|
588
|
+
function isFiniteNumber2(value) {
|
|
323
589
|
return typeof value === "number" && Number.isFinite(value);
|
|
324
590
|
}
|
|
325
591
|
function createTokenStorePlugin(options) {
|
|
@@ -464,6 +730,7 @@ var RefreshEnginePlugin = class {
|
|
|
464
730
|
this.backoffs = /* @__PURE__ */ new Set();
|
|
465
731
|
this.uninstalled = false;
|
|
466
732
|
this.scheduledRefreshTimeout = null;
|
|
733
|
+
this.coordinator = null;
|
|
467
734
|
this.options = options;
|
|
468
735
|
this.refreshFn = options.refreshFn;
|
|
469
736
|
this.threshold = options.threshold ?? 60;
|
|
@@ -485,6 +752,9 @@ var RefreshEnginePlugin = class {
|
|
|
485
752
|
},
|
|
486
753
|
setThreshold: (seconds) => {
|
|
487
754
|
this.threshold = seconds;
|
|
755
|
+
},
|
|
756
|
+
setCoordinator: (coordinator) => {
|
|
757
|
+
this.coordinator = coordinator;
|
|
488
758
|
}
|
|
489
759
|
};
|
|
490
760
|
}
|
|
@@ -500,7 +770,8 @@ var RefreshEnginePlugin = class {
|
|
|
500
770
|
}
|
|
501
771
|
this.endBackoffs();
|
|
502
772
|
this.isRefreshingFlag = true;
|
|
503
|
-
const
|
|
773
|
+
const coordinator = this.coordinator;
|
|
774
|
+
const promise = coordinator && refreshToken ? this.coordinatedRefresh(coordinator, refreshToken) : this.executeRefresh();
|
|
504
775
|
this.refreshPromise = promise;
|
|
505
776
|
this.refreshPromiseToken = refreshToken;
|
|
506
777
|
try {
|
|
@@ -514,6 +785,24 @@ var RefreshEnginePlugin = class {
|
|
|
514
785
|
}
|
|
515
786
|
}
|
|
516
787
|
}
|
|
788
|
+
/**
|
|
789
|
+
* Refresh while holding the cross-tab lock. Another tab may have refreshed
|
|
790
|
+
* this session meanwhile: its result is used instead of refreshing again
|
|
791
|
+
* (with rotating refresh tokens a second refresh would be rejected).
|
|
792
|
+
*/
|
|
793
|
+
coordinatedRefresh(coordinator, refreshToken) {
|
|
794
|
+
return coordinator.exclusive(async () => {
|
|
795
|
+
if (this.kernel.getRefreshToken() === refreshToken) {
|
|
796
|
+
const adopted = await coordinator.beforeRefresh(refreshToken);
|
|
797
|
+
if (adopted) return adopted;
|
|
798
|
+
}
|
|
799
|
+
return this.executeRefresh(0, refreshToken);
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
/** The session another tab replaced `refreshToken` with, if any */
|
|
803
|
+
replacementFor(refreshToken) {
|
|
804
|
+
return this.coordinator?.replacement(refreshToken) ?? null;
|
|
805
|
+
}
|
|
517
806
|
/** Wait before a retry; endBackoffs() cuts the wait short */
|
|
518
807
|
waitBackoff(ms) {
|
|
519
808
|
return new Promise((resolve) => {
|
|
@@ -540,13 +829,15 @@ var RefreshEnginePlugin = class {
|
|
|
540
829
|
async executeRefresh(retryCount = 0, initialRefreshToken) {
|
|
541
830
|
const refreshToken = this.uninstalled ? null : this.kernel.getRefreshToken();
|
|
542
831
|
if (initialRefreshToken !== void 0 && refreshToken !== initialRefreshToken) {
|
|
832
|
+
const replacement = this.replacementFor(initialRefreshToken);
|
|
833
|
+
if (replacement) return replacement;
|
|
543
834
|
throw supersededError();
|
|
544
835
|
}
|
|
545
836
|
if (this.options.onRefreshStart) {
|
|
546
837
|
this.options.onRefreshStart();
|
|
547
838
|
}
|
|
548
839
|
if (!refreshToken) {
|
|
549
|
-
const error = new
|
|
840
|
+
const error = new chunkMONA6MGX_cjs.AuthError("REFRESH_TOKEN_MISSING", "No refresh token available");
|
|
550
841
|
this.kernel.emit({
|
|
551
842
|
type: "error",
|
|
552
843
|
error,
|
|
@@ -559,6 +850,8 @@ var RefreshEnginePlugin = class {
|
|
|
559
850
|
const previousExpiresAt = this.kernel.getExpiresAt();
|
|
560
851
|
const newTokens = await this.refreshFn(refreshToken);
|
|
561
852
|
if (this.kernel.getRefreshToken() !== refreshToken) {
|
|
853
|
+
const replacement = this.replacementFor(refreshToken);
|
|
854
|
+
if (replacement) return replacement;
|
|
562
855
|
throw new SupersededRefreshError();
|
|
563
856
|
}
|
|
564
857
|
this.applyingRefresh = true;
|
|
@@ -586,12 +879,14 @@ var RefreshEnginePlugin = class {
|
|
|
586
879
|
if (error instanceof SupersededRefreshError) {
|
|
587
880
|
throw supersededError();
|
|
588
881
|
}
|
|
882
|
+
const replacement = this.replacementFor(refreshToken);
|
|
883
|
+
if (replacement) return replacement;
|
|
589
884
|
if (retryCount < this.maxRetries) {
|
|
590
885
|
const delay = this.retryDelay * Math.pow(2, retryCount);
|
|
591
886
|
await this.waitBackoff(Math.min(delay, MAX_TIMEOUT_DELAY));
|
|
592
887
|
return this.executeRefresh(retryCount + 1, refreshToken);
|
|
593
888
|
}
|
|
594
|
-
const authError = new
|
|
889
|
+
const authError = new chunkMONA6MGX_cjs.AuthError("REFRESH_FAILED", "Token refresh failed", {
|
|
595
890
|
cause: error instanceof Error ? error : new Error(String(error)),
|
|
596
891
|
context: { retryCount }
|
|
597
892
|
});
|
|
@@ -677,271 +972,25 @@ var RefreshEnginePlugin = class {
|
|
|
677
972
|
*/
|
|
678
973
|
uninstall() {
|
|
679
974
|
this.cancelScheduledRefresh();
|
|
975
|
+
this.coordinator = null;
|
|
680
976
|
this.uninstalled = true;
|
|
681
977
|
this.endBackoffs();
|
|
682
978
|
}
|
|
683
979
|
};
|
|
684
980
|
function supersededError() {
|
|
685
|
-
return new
|
|
981
|
+
return new chunkMONA6MGX_cjs.AuthError(
|
|
686
982
|
"REFRESH_FAILED",
|
|
687
983
|
"Session changed while refreshing (logout or new login); refresh result discarded",
|
|
688
984
|
{ context: { superseded: true } }
|
|
689
985
|
);
|
|
690
986
|
}
|
|
691
987
|
function isSupersededError(error) {
|
|
692
|
-
return error instanceof
|
|
988
|
+
return error instanceof chunkMONA6MGX_cjs.AuthError && error.context?.["superseded"] === true;
|
|
693
989
|
}
|
|
694
990
|
function createRefreshEnginePlugin(options) {
|
|
695
991
|
return new RefreshEnginePlugin(options);
|
|
696
992
|
}
|
|
697
993
|
|
|
698
|
-
// src/plugins/core/storage-memory.ts
|
|
699
|
-
var MemoryStorageAdapter = class {
|
|
700
|
-
constructor(options = {}) {
|
|
701
|
-
this.store = /* @__PURE__ */ new Map();
|
|
702
|
-
this.prefix = options.prefix || "authkeeper:";
|
|
703
|
-
}
|
|
704
|
-
/**
|
|
705
|
-
* Get value by key
|
|
706
|
-
*/
|
|
707
|
-
get(key) {
|
|
708
|
-
return this.store.get(this.prefix + key) || null;
|
|
709
|
-
}
|
|
710
|
-
/**
|
|
711
|
-
* Set value by key
|
|
712
|
-
*/
|
|
713
|
-
set(key, value) {
|
|
714
|
-
this.store.set(this.prefix + key, value);
|
|
715
|
-
}
|
|
716
|
-
/**
|
|
717
|
-
* Remove value by key
|
|
718
|
-
*/
|
|
719
|
-
remove(key) {
|
|
720
|
-
this.store.delete(this.prefix + key);
|
|
721
|
-
}
|
|
722
|
-
/**
|
|
723
|
-
* Clear all values
|
|
724
|
-
*/
|
|
725
|
-
clear() {
|
|
726
|
-
const keysToDelete = [];
|
|
727
|
-
for (const key of this.store.keys()) {
|
|
728
|
-
if (key.startsWith(this.prefix)) {
|
|
729
|
-
keysToDelete.push(key);
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
keysToDelete.forEach((key) => this.store.delete(key));
|
|
733
|
-
}
|
|
734
|
-
};
|
|
735
|
-
function createMemoryStorage(options) {
|
|
736
|
-
return new MemoryStorageAdapter(options);
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
// src/utils/cookie.ts
|
|
740
|
-
function parseCookies(cookieString) {
|
|
741
|
-
const cookies = {};
|
|
742
|
-
if (!cookieString) {
|
|
743
|
-
return cookies;
|
|
744
|
-
}
|
|
745
|
-
cookieString.split(";").forEach((cookie) => {
|
|
746
|
-
const [key, ...valueParts] = cookie.split("=");
|
|
747
|
-
const trimmedKey = key?.trim();
|
|
748
|
-
const value = valueParts.join("=").trim();
|
|
749
|
-
if (trimmedKey) {
|
|
750
|
-
cookies[safeDecode(trimmedKey)] = safeDecode(value);
|
|
751
|
-
}
|
|
752
|
-
});
|
|
753
|
-
return cookies;
|
|
754
|
-
}
|
|
755
|
-
function safeDecode(value) {
|
|
756
|
-
try {
|
|
757
|
-
return decodeURIComponent(value);
|
|
758
|
-
} catch {
|
|
759
|
-
return value;
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
var INVALID_ATTRIBUTE_VALUE = /[;\r\n\0]/;
|
|
763
|
-
function assertAttributeValue(name, value) {
|
|
764
|
-
if (INVALID_ATTRIBUTE_VALUE.test(value)) {
|
|
765
|
-
throw new TypeError(`Invalid cookie ${name}: must not contain ';', CR, LF or NUL`);
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
function getCookie(name) {
|
|
769
|
-
if (typeof document === "undefined") {
|
|
770
|
-
return null;
|
|
771
|
-
}
|
|
772
|
-
const cookies = parseCookies(document.cookie);
|
|
773
|
-
return cookies[name] || null;
|
|
774
|
-
}
|
|
775
|
-
function serializeCookie(name, value, attributes = {}) {
|
|
776
|
-
let cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
777
|
-
if (attributes.path) {
|
|
778
|
-
assertAttributeValue("path", attributes.path);
|
|
779
|
-
cookie += `; Path=${attributes.path}`;
|
|
780
|
-
}
|
|
781
|
-
if (attributes.domain) {
|
|
782
|
-
assertAttributeValue("domain", attributes.domain);
|
|
783
|
-
cookie += `; Domain=${attributes.domain}`;
|
|
784
|
-
}
|
|
785
|
-
if (attributes.maxAge !== void 0) {
|
|
786
|
-
cookie += `; Max-Age=${attributes.maxAge}`;
|
|
787
|
-
}
|
|
788
|
-
if (attributes.expires) {
|
|
789
|
-
cookie += `; Expires=${attributes.expires.toUTCString()}`;
|
|
790
|
-
}
|
|
791
|
-
if (attributes.secure || attributes.sameSite === "none") {
|
|
792
|
-
cookie += "; Secure";
|
|
793
|
-
}
|
|
794
|
-
if (attributes.sameSite) {
|
|
795
|
-
const sameSite = attributes.sameSite.charAt(0).toUpperCase() + attributes.sameSite.slice(1);
|
|
796
|
-
cookie += `; SameSite=${sameSite}`;
|
|
797
|
-
}
|
|
798
|
-
if (attributes.httpOnly) {
|
|
799
|
-
cookie += "; HttpOnly";
|
|
800
|
-
}
|
|
801
|
-
return cookie;
|
|
802
|
-
}
|
|
803
|
-
function setCookie(name, value, attributes = {}) {
|
|
804
|
-
if (typeof document === "undefined") {
|
|
805
|
-
return;
|
|
806
|
-
}
|
|
807
|
-
document.cookie = serializeCookie(name, value, attributes);
|
|
808
|
-
}
|
|
809
|
-
function deleteCookie(name, attributes = {}) {
|
|
810
|
-
if (typeof document === "undefined") {
|
|
811
|
-
return;
|
|
812
|
-
}
|
|
813
|
-
document.cookie = serializeCookie(name, "", {
|
|
814
|
-
...attributes,
|
|
815
|
-
expires: /* @__PURE__ */ new Date(0)
|
|
816
|
-
});
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
// src/utils/storage.ts
|
|
820
|
-
function isQuotaExceeded(error) {
|
|
821
|
-
if (typeof error !== "object" || error === null) return false;
|
|
822
|
-
const { name, code } = error;
|
|
823
|
-
return name === "QuotaExceededError" || code === 22;
|
|
824
|
-
}
|
|
825
|
-
function isStorageAvailable(storage) {
|
|
826
|
-
if (!storage) {
|
|
827
|
-
return false;
|
|
828
|
-
}
|
|
829
|
-
try {
|
|
830
|
-
const testKey = "__storage_test__";
|
|
831
|
-
storage.setItem(testKey, "test");
|
|
832
|
-
storage.removeItem(testKey);
|
|
833
|
-
return true;
|
|
834
|
-
} catch (error) {
|
|
835
|
-
return isQuotaExceeded(error) && storage.length > 0;
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
function safeGetItem(storage, key) {
|
|
839
|
-
try {
|
|
840
|
-
return storage.get(key);
|
|
841
|
-
} catch {
|
|
842
|
-
return null;
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
function safeSetItem(storage, key, value) {
|
|
846
|
-
try {
|
|
847
|
-
storage.set(key, value);
|
|
848
|
-
return true;
|
|
849
|
-
} catch {
|
|
850
|
-
return false;
|
|
851
|
-
}
|
|
852
|
-
}
|
|
853
|
-
function safeRemoveItem(storage, key) {
|
|
854
|
-
try {
|
|
855
|
-
storage.remove(key);
|
|
856
|
-
return true;
|
|
857
|
-
} catch {
|
|
858
|
-
return false;
|
|
859
|
-
}
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
// src/plugins/core/storage-web.ts
|
|
863
|
-
var WebStorageAdapter = class {
|
|
864
|
-
constructor(storage, options = {}) {
|
|
865
|
-
this.storage = storage;
|
|
866
|
-
this.prefix = options.prefix ?? "authkeeper:";
|
|
867
|
-
}
|
|
868
|
-
get(key) {
|
|
869
|
-
return this.storage.getItem(this.prefix + key);
|
|
870
|
-
}
|
|
871
|
-
set(key, value) {
|
|
872
|
-
this.storage.setItem(this.prefix + key, value);
|
|
873
|
-
}
|
|
874
|
-
remove(key) {
|
|
875
|
-
this.storage.removeItem(this.prefix + key);
|
|
876
|
-
}
|
|
877
|
-
clear() {
|
|
878
|
-
const keys = [];
|
|
879
|
-
for (let i = 0; i < this.storage.length; i++) {
|
|
880
|
-
const key = this.storage.key(i);
|
|
881
|
-
if (key !== null && key.startsWith(this.prefix)) {
|
|
882
|
-
keys.push(key);
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
keys.forEach((key) => this.storage.removeItem(key));
|
|
886
|
-
}
|
|
887
|
-
};
|
|
888
|
-
var CookieStorageAdapter = class {
|
|
889
|
-
constructor(options = {}) {
|
|
890
|
-
this.keys = /* @__PURE__ */ new Set();
|
|
891
|
-
this.prefix = options.prefix ?? "authkeeper_";
|
|
892
|
-
this.attributes = { path: "/", secure: true, sameSite: "strict", ...options.attributes };
|
|
893
|
-
}
|
|
894
|
-
get(key) {
|
|
895
|
-
return getCookie(this.prefix + key);
|
|
896
|
-
}
|
|
897
|
-
set(key, value) {
|
|
898
|
-
setCookie(this.prefix + key, value, this.attributes);
|
|
899
|
-
this.keys.add(key);
|
|
900
|
-
if (typeof document !== "undefined" && this.get(key) !== value) {
|
|
901
|
-
throw new chunkXGKVBZSF_cjs.AuthError(
|
|
902
|
-
"STORAGE_ERROR",
|
|
903
|
-
`Cookie "${this.prefix + key}" was not stored (too large or blocked)`
|
|
904
|
-
);
|
|
905
|
-
}
|
|
906
|
-
}
|
|
907
|
-
remove(key) {
|
|
908
|
-
const { path, domain, secure, sameSite } = this.attributes;
|
|
909
|
-
deleteCookie(this.prefix + key, { path, domain, secure, sameSite });
|
|
910
|
-
this.keys.delete(key);
|
|
911
|
-
}
|
|
912
|
-
clear() {
|
|
913
|
-
Array.from(this.keys).forEach((key) => this.remove(key));
|
|
914
|
-
}
|
|
915
|
-
};
|
|
916
|
-
function getWebStorage(type) {
|
|
917
|
-
try {
|
|
918
|
-
return typeof window !== "undefined" ? window[type] : void 0;
|
|
919
|
-
} catch {
|
|
920
|
-
return void 0;
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
function resolveStorage(storage) {
|
|
924
|
-
if (storage === void 0 || storage === "memory") {
|
|
925
|
-
return void 0;
|
|
926
|
-
}
|
|
927
|
-
if (typeof storage === "object" && storage !== null) {
|
|
928
|
-
return storage;
|
|
929
|
-
}
|
|
930
|
-
switch (storage) {
|
|
931
|
-
case "localStorage":
|
|
932
|
-
case "sessionStorage": {
|
|
933
|
-
const webStorage = getWebStorage(storage);
|
|
934
|
-
return webStorage && isStorageAvailable(webStorage) ? new WebStorageAdapter(webStorage) : createMemoryStorage();
|
|
935
|
-
}
|
|
936
|
-
case "cookie":
|
|
937
|
-
return typeof document !== "undefined" ? new CookieStorageAdapter() : createMemoryStorage();
|
|
938
|
-
default:
|
|
939
|
-
throw new chunkXGKVBZSF_cjs.AuthError("CONFIG_ERROR", `Unknown storage type: ${String(storage)}`, {
|
|
940
|
-
context: { storage }
|
|
941
|
-
});
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
|
|
945
994
|
// src/plugins/core/fetch-interceptor.ts
|
|
946
995
|
var FetchInterceptorPlugin = class {
|
|
947
996
|
constructor() {
|
|
@@ -1082,6 +1131,7 @@ exports.getCookie = getCookie;
|
|
|
1082
1131
|
exports.getTimeUntilExpiry = getTimeUntilExpiry;
|
|
1083
1132
|
exports.getTokenExpiry = getTokenExpiry;
|
|
1084
1133
|
exports.isStorageAvailable = isStorageAvailable;
|
|
1134
|
+
exports.isStoredTokens = isStoredTokens;
|
|
1085
1135
|
exports.isTokenExpired = isTokenExpired;
|
|
1086
1136
|
exports.isValidJwtFormat = isValidJwtFormat;
|
|
1087
1137
|
exports.parseCookies = parseCookies;
|
|
@@ -1092,5 +1142,5 @@ exports.safeSetItem = safeSetItem;
|
|
|
1092
1142
|
exports.serializeCookie = serializeCookie;
|
|
1093
1143
|
exports.setCookie = setCookie;
|
|
1094
1144
|
exports.stringToBase64Url = stringToBase64Url;
|
|
1095
|
-
//# sourceMappingURL=chunk-
|
|
1096
|
-
//# sourceMappingURL=chunk-
|
|
1145
|
+
//# sourceMappingURL=chunk-SI7KITBT.cjs.map
|
|
1146
|
+
//# sourceMappingURL=chunk-SI7KITBT.cjs.map
|