@oxog/authkeeper 1.0.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 +154 -0
- package/LICENSE +21 -0
- package/README.md +410 -0
- package/dist/index-Dv1VLP97.d.cts +716 -0
- package/dist/index-Dv1VLP97.d.ts +716 -0
- package/dist/index.cjs +1389 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +560 -0
- package/dist/index.d.ts +560 -0
- package/dist/index.js +1350 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/index.cjs +632 -0
- package/dist/plugins/index.cjs.map +1 -0
- package/dist/plugins/index.d.cts +1 -0
- package/dist/plugins/index.d.ts +1 -0
- package/dist/plugins/index.js +621 -0
- package/dist/plugins/index.js.map +1 -0
- package/dist/react/index.cjs +8 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +9 -0
- package/dist/react/index.d.ts +9 -0
- package/dist/react/index.js +6 -0
- package/dist/react/index.js.map +1 -0
- package/dist/svelte/index.cjs +8 -0
- package/dist/svelte/index.cjs.map +1 -0
- package/dist/svelte/index.d.cts +9 -0
- package/dist/svelte/index.d.ts +9 -0
- package/dist/svelte/index.js +6 -0
- package/dist/svelte/index.js.map +1 -0
- package/dist/vue/index.cjs +8 -0
- package/dist/vue/index.cjs.map +1 -0
- package/dist/vue/index.d.cts +9 -0
- package/dist/vue/index.d.ts +9 -0
- package/dist/vue/index.js +6 -0
- package/dist/vue/index.js.map +1 -0
- package/package.json +114 -0
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/plugins/core/token-store.ts
|
|
4
|
+
var TokenStorePlugin = class {
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
this.name = "token-store";
|
|
7
|
+
this.version = "1.0.0";
|
|
8
|
+
this.type = "core";
|
|
9
|
+
this.tokens = null;
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
install(_kernel) {
|
|
13
|
+
return {
|
|
14
|
+
set: this.set.bind(this),
|
|
15
|
+
get: this.get.bind(this),
|
|
16
|
+
clear: this.clear.bind(this),
|
|
17
|
+
getAccessToken: () => this.tokens?.accessToken || null,
|
|
18
|
+
getRefreshToken: () => this.tokens?.refreshToken || null,
|
|
19
|
+
getTokenType: () => this.tokens?.tokenType || "Bearer",
|
|
20
|
+
getExpiresAt: () => {
|
|
21
|
+
if (!this.tokens?.expiresAt) return null;
|
|
22
|
+
return new Date(this.tokens.expiresAt);
|
|
23
|
+
},
|
|
24
|
+
getExpiresIn: () => {
|
|
25
|
+
if (!this.tokens?.expiresAt) return null;
|
|
26
|
+
const now = Date.now();
|
|
27
|
+
const timeUntil = this.tokens.expiresAt - now;
|
|
28
|
+
return Math.max(0, timeUntil);
|
|
29
|
+
},
|
|
30
|
+
isExpired: () => {
|
|
31
|
+
if (!this.tokens?.expiresAt) return false;
|
|
32
|
+
return Date.now() >= this.tokens.expiresAt;
|
|
33
|
+
},
|
|
34
|
+
getSetAt: () => {
|
|
35
|
+
if (!this.tokens?.setAt) return null;
|
|
36
|
+
return new Date(this.tokens.setAt);
|
|
37
|
+
},
|
|
38
|
+
getRefreshCount: () => this.tokens?.refreshCount || 0
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Set tokens
|
|
43
|
+
*/
|
|
44
|
+
set(tokens) {
|
|
45
|
+
let expiresAt = null;
|
|
46
|
+
if (tokens.expiresAt) {
|
|
47
|
+
expiresAt = tokens.expiresAt * 1e3;
|
|
48
|
+
} else if (tokens.expiresIn) {
|
|
49
|
+
expiresAt = Date.now() + tokens.expiresIn * 1e3;
|
|
50
|
+
}
|
|
51
|
+
const refreshCount = this.tokens ? this.tokens.refreshCount + 1 : 1;
|
|
52
|
+
this.tokens = {
|
|
53
|
+
accessToken: tokens.accessToken,
|
|
54
|
+
refreshToken: tokens.refreshToken || null,
|
|
55
|
+
tokenType: tokens.tokenType || "Bearer",
|
|
56
|
+
expiresAt,
|
|
57
|
+
setAt: Date.now(),
|
|
58
|
+
refreshCount
|
|
59
|
+
};
|
|
60
|
+
if (this.options.onSet) {
|
|
61
|
+
this.options.onSet(this.tokens);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get tokens
|
|
66
|
+
*/
|
|
67
|
+
get() {
|
|
68
|
+
return this.tokens;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Clear tokens
|
|
72
|
+
*/
|
|
73
|
+
clear() {
|
|
74
|
+
this.tokens = null;
|
|
75
|
+
if (this.options.onClear) {
|
|
76
|
+
this.options.onClear();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Uninstall plugin
|
|
81
|
+
*/
|
|
82
|
+
uninstall() {
|
|
83
|
+
this.clear();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
function createTokenStorePlugin(options) {
|
|
87
|
+
return new TokenStorePlugin(options);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/utils/base64.ts
|
|
91
|
+
function base64UrlDecode(str) {
|
|
92
|
+
let base64 = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
93
|
+
const pad = str.length % 4;
|
|
94
|
+
if (pad === 2) {
|
|
95
|
+
base64 += "==";
|
|
96
|
+
} else if (pad === 3) {
|
|
97
|
+
base64 += "=";
|
|
98
|
+
}
|
|
99
|
+
const binary = typeof window !== "undefined" ? atob(base64) : Buffer.from(base64, "base64").toString("binary");
|
|
100
|
+
return decodeURIComponent(
|
|
101
|
+
binary.split("").map((c) => {
|
|
102
|
+
return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
|
|
103
|
+
}).join("")
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/types.ts
|
|
108
|
+
var AuthError = class _AuthError extends Error {
|
|
109
|
+
constructor(code, message, options) {
|
|
110
|
+
super(message);
|
|
111
|
+
this.name = "AuthError";
|
|
112
|
+
this.code = code;
|
|
113
|
+
this.context = options?.context;
|
|
114
|
+
this.cause = options?.cause;
|
|
115
|
+
if (Error.captureStackTrace) {
|
|
116
|
+
Error.captureStackTrace(this, _AuthError);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// src/utils/jwt.ts
|
|
122
|
+
function decodeJwt(token) {
|
|
123
|
+
const parts = token.split(".");
|
|
124
|
+
if (parts.length !== 3) {
|
|
125
|
+
throw new AuthError("TOKEN_DECODE_FAILED", "Invalid JWT format: must have 3 parts", {
|
|
126
|
+
context: { parts: parts.length }
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
const headerJson = base64UrlDecode(parts[0]);
|
|
131
|
+
const header = JSON.parse(headerJson);
|
|
132
|
+
const payloadJson = base64UrlDecode(parts[1]);
|
|
133
|
+
const payload = JSON.parse(payloadJson);
|
|
134
|
+
return { header, payload };
|
|
135
|
+
} catch (error) {
|
|
136
|
+
throw new AuthError("TOKEN_DECODE_FAILED", "Failed to decode JWT", {
|
|
137
|
+
cause: error
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function decodePayload(token) {
|
|
142
|
+
return decodeJwt(token).payload;
|
|
143
|
+
}
|
|
144
|
+
function decodeHeader(token) {
|
|
145
|
+
return decodeJwt(token).header;
|
|
146
|
+
}
|
|
147
|
+
function getTokenExpiry(token) {
|
|
148
|
+
try {
|
|
149
|
+
const payload = decodePayload(token);
|
|
150
|
+
if (payload.exp) {
|
|
151
|
+
return new Date(payload.exp * 1e3);
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
} catch {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function isTokenExpired(token, bufferSeconds = 0) {
|
|
159
|
+
const expiry = getTokenExpiry(token);
|
|
160
|
+
if (!expiry) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
const now = Date.now();
|
|
164
|
+
const expiryTime = expiry.getTime() - bufferSeconds * 1e3;
|
|
165
|
+
return now >= expiryTime;
|
|
166
|
+
}
|
|
167
|
+
function getTimeUntilExpiry(token) {
|
|
168
|
+
const expiry = getTokenExpiry(token);
|
|
169
|
+
if (!expiry) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
const now = Date.now();
|
|
173
|
+
const timeUntil = expiry.getTime() - now;
|
|
174
|
+
return Math.max(0, timeUntil);
|
|
175
|
+
}
|
|
176
|
+
function getClaim(token, claim) {
|
|
177
|
+
try {
|
|
178
|
+
const payload = decodePayload(token);
|
|
179
|
+
return payload[claim];
|
|
180
|
+
} catch {
|
|
181
|
+
return void 0;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function getClaims(token, claims) {
|
|
185
|
+
try {
|
|
186
|
+
const payload = decodePayload(token);
|
|
187
|
+
const result = {};
|
|
188
|
+
for (const claim of claims) {
|
|
189
|
+
if (claim in payload) {
|
|
190
|
+
result[claim] = payload[claim];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
} catch {
|
|
195
|
+
return {};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// src/plugins/core/token-decoder.ts
|
|
200
|
+
var TokenDecoderPlugin = class {
|
|
201
|
+
constructor() {
|
|
202
|
+
this.name = "token-decoder";
|
|
203
|
+
this.version = "1.0.0";
|
|
204
|
+
this.type = "core";
|
|
205
|
+
}
|
|
206
|
+
install(kernel) {
|
|
207
|
+
this.kernel = kernel;
|
|
208
|
+
return {
|
|
209
|
+
decode: this.decode.bind(this),
|
|
210
|
+
getHeader: this.getHeader.bind(this),
|
|
211
|
+
getClaim: this.getClaimFromToken.bind(this),
|
|
212
|
+
getClaims: this.getClaimsFromToken.bind(this),
|
|
213
|
+
getExpiry: this.getExpiry.bind(this),
|
|
214
|
+
isExpired: this.isExpiredToken.bind(this),
|
|
215
|
+
getTokenInfo: this.getTokenInfo.bind(this)
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Decode token payload
|
|
220
|
+
*/
|
|
221
|
+
decode(token) {
|
|
222
|
+
const jwt = token || this.kernel.getAccessToken();
|
|
223
|
+
if (!jwt) {
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
try {
|
|
227
|
+
return decodePayload(jwt);
|
|
228
|
+
} catch {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Decode token header
|
|
234
|
+
*/
|
|
235
|
+
getHeader(token) {
|
|
236
|
+
const jwt = token || this.kernel.getAccessToken();
|
|
237
|
+
if (!jwt) {
|
|
238
|
+
return null;
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
return decodeHeader(jwt);
|
|
242
|
+
} catch {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Get specific claim from token
|
|
248
|
+
*/
|
|
249
|
+
getClaimFromToken(key, token) {
|
|
250
|
+
const jwt = token || this.kernel.getAccessToken();
|
|
251
|
+
if (!jwt) {
|
|
252
|
+
return void 0;
|
|
253
|
+
}
|
|
254
|
+
return getClaim(jwt, key);
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Get multiple claims from token
|
|
258
|
+
*/
|
|
259
|
+
getClaimsFromToken(keys, token) {
|
|
260
|
+
const jwt = token || this.kernel.getAccessToken();
|
|
261
|
+
if (!jwt) {
|
|
262
|
+
return {};
|
|
263
|
+
}
|
|
264
|
+
return getClaims(jwt, keys);
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Get token expiry
|
|
268
|
+
*/
|
|
269
|
+
getExpiry(token) {
|
|
270
|
+
const jwt = token || this.kernel.getAccessToken();
|
|
271
|
+
if (!jwt) {
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
return getTokenExpiry(jwt);
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Check if token is expired
|
|
278
|
+
*/
|
|
279
|
+
isExpiredToken(token) {
|
|
280
|
+
const jwt = token || this.kernel.getAccessToken();
|
|
281
|
+
if (!jwt) {
|
|
282
|
+
return true;
|
|
283
|
+
}
|
|
284
|
+
return isTokenExpired(jwt);
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Get complete token information
|
|
288
|
+
*/
|
|
289
|
+
getTokenInfo(token) {
|
|
290
|
+
const jwt = token || this.kernel.getAccessToken();
|
|
291
|
+
if (!jwt) {
|
|
292
|
+
return null;
|
|
293
|
+
}
|
|
294
|
+
try {
|
|
295
|
+
const { header, payload } = decodeJwt(jwt);
|
|
296
|
+
const expiresAt = getTokenExpiry(jwt);
|
|
297
|
+
const isExpired = isTokenExpired(jwt);
|
|
298
|
+
const expiresIn = getTimeUntilExpiry(jwt);
|
|
299
|
+
return {
|
|
300
|
+
raw: jwt,
|
|
301
|
+
header,
|
|
302
|
+
payload,
|
|
303
|
+
expiresAt,
|
|
304
|
+
isExpired,
|
|
305
|
+
expiresIn
|
|
306
|
+
};
|
|
307
|
+
} catch {
|
|
308
|
+
return null;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
uninstall() {
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
function createTokenDecoderPlugin() {
|
|
315
|
+
return new TokenDecoderPlugin();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// src/plugins/core/refresh-engine.ts
|
|
319
|
+
var RefreshEnginePlugin = class {
|
|
320
|
+
constructor(options) {
|
|
321
|
+
this.name = "refresh-engine";
|
|
322
|
+
this.version = "1.0.0";
|
|
323
|
+
this.type = "core";
|
|
324
|
+
this.isRefreshingFlag = false;
|
|
325
|
+
this.refreshPromise = null;
|
|
326
|
+
this.scheduledRefreshTimeout = null;
|
|
327
|
+
this.options = options;
|
|
328
|
+
this.refreshFn = options.refreshFn;
|
|
329
|
+
this.threshold = options.threshold || 60;
|
|
330
|
+
this.maxRetries = options.maxRetries || 3;
|
|
331
|
+
this.retryDelay = options.retryDelay || 1e3;
|
|
332
|
+
}
|
|
333
|
+
install(kernel) {
|
|
334
|
+
this.kernel = kernel;
|
|
335
|
+
return {
|
|
336
|
+
refresh: this.refresh.bind(this),
|
|
337
|
+
scheduleRefresh: this.scheduleRefresh.bind(this),
|
|
338
|
+
cancelScheduledRefresh: this.cancelScheduledRefresh.bind(this),
|
|
339
|
+
isRefreshing: () => this.isRefreshingFlag,
|
|
340
|
+
getNextRefreshAt: this.getNextRefreshAt.bind(this),
|
|
341
|
+
setRefreshFn: (fn) => {
|
|
342
|
+
this.refreshFn = fn;
|
|
343
|
+
},
|
|
344
|
+
setThreshold: (seconds) => {
|
|
345
|
+
this.threshold = seconds;
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Refresh tokens
|
|
351
|
+
*
|
|
352
|
+
* Queues concurrent requests - only one refresh executes at a time.
|
|
353
|
+
*/
|
|
354
|
+
async refresh() {
|
|
355
|
+
if (this.refreshPromise) {
|
|
356
|
+
return this.refreshPromise;
|
|
357
|
+
}
|
|
358
|
+
this.isRefreshingFlag = true;
|
|
359
|
+
this.refreshPromise = this.executeRefresh();
|
|
360
|
+
try {
|
|
361
|
+
const tokens = await this.refreshPromise;
|
|
362
|
+
return tokens;
|
|
363
|
+
} finally {
|
|
364
|
+
this.isRefreshingFlag = false;
|
|
365
|
+
this.refreshPromise = null;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Execute token refresh with retry logic
|
|
370
|
+
*/
|
|
371
|
+
async executeRefresh(retryCount = 0) {
|
|
372
|
+
if (this.options.onRefreshStart) {
|
|
373
|
+
this.options.onRefreshStart();
|
|
374
|
+
}
|
|
375
|
+
const refreshToken = this.kernel.getRefreshToken();
|
|
376
|
+
if (!refreshToken) {
|
|
377
|
+
const error = new Error("No refresh token available");
|
|
378
|
+
error.name = "AuthError";
|
|
379
|
+
error.code = "REFRESH_TOKEN_MISSING";
|
|
380
|
+
this.kernel.emit({
|
|
381
|
+
type: "error",
|
|
382
|
+
error,
|
|
383
|
+
context: "refresh",
|
|
384
|
+
timestamp: Date.now()
|
|
385
|
+
});
|
|
386
|
+
throw error;
|
|
387
|
+
}
|
|
388
|
+
try {
|
|
389
|
+
const previousExpiresAt = this.kernel.getExpiresAt();
|
|
390
|
+
const newTokens = await this.refreshFn(refreshToken);
|
|
391
|
+
this.kernel.setTokens(newTokens);
|
|
392
|
+
const newExpiresAt = this.kernel.getExpiresAt();
|
|
393
|
+
this.kernel.emit({
|
|
394
|
+
type: "refresh",
|
|
395
|
+
tokens: newTokens,
|
|
396
|
+
previousExpiresAt,
|
|
397
|
+
newExpiresAt,
|
|
398
|
+
timestamp: Date.now()
|
|
399
|
+
});
|
|
400
|
+
if (this.options.onRefreshSuccess) {
|
|
401
|
+
this.options.onRefreshSuccess(newTokens);
|
|
402
|
+
}
|
|
403
|
+
this.scheduleRefresh();
|
|
404
|
+
return newTokens;
|
|
405
|
+
} catch (error) {
|
|
406
|
+
if (retryCount < this.maxRetries) {
|
|
407
|
+
const delay = this.retryDelay * Math.pow(2, retryCount);
|
|
408
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
409
|
+
return this.executeRefresh(retryCount + 1);
|
|
410
|
+
}
|
|
411
|
+
const authError = new Error("Token refresh failed");
|
|
412
|
+
authError.name = "AuthError";
|
|
413
|
+
authError.code = "REFRESH_FAILED";
|
|
414
|
+
authError.cause = error;
|
|
415
|
+
authError.context = { retryCount };
|
|
416
|
+
this.kernel.emit({
|
|
417
|
+
type: "error",
|
|
418
|
+
error: authError,
|
|
419
|
+
context: "refresh",
|
|
420
|
+
timestamp: Date.now()
|
|
421
|
+
});
|
|
422
|
+
if (this.options.onRefreshError) {
|
|
423
|
+
this.options.onRefreshError(authError);
|
|
424
|
+
}
|
|
425
|
+
throw authError;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Schedule automatic refresh
|
|
430
|
+
*
|
|
431
|
+
* Schedules refresh before token expires (threshold seconds before).
|
|
432
|
+
*/
|
|
433
|
+
scheduleRefresh() {
|
|
434
|
+
this.cancelScheduledRefresh();
|
|
435
|
+
const expiresIn = this.kernel.getTimeUntilExpiry();
|
|
436
|
+
if (!expiresIn) {
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
const refreshIn = expiresIn - this.threshold * 1e3;
|
|
440
|
+
if (refreshIn > 0) {
|
|
441
|
+
this.scheduledRefreshTimeout = setTimeout(() => {
|
|
442
|
+
this.refresh().catch((error) => {
|
|
443
|
+
console.error("[AuthKeeper] Scheduled refresh failed:", error);
|
|
444
|
+
});
|
|
445
|
+
}, refreshIn);
|
|
446
|
+
} else {
|
|
447
|
+
this.refresh().catch((error) => {
|
|
448
|
+
console.error("[AuthKeeper] Immediate refresh failed:", error);
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Cancel scheduled refresh
|
|
454
|
+
*/
|
|
455
|
+
cancelScheduledRefresh() {
|
|
456
|
+
if (this.scheduledRefreshTimeout) {
|
|
457
|
+
clearTimeout(this.scheduledRefreshTimeout);
|
|
458
|
+
this.scheduledRefreshTimeout = null;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Get next scheduled refresh time
|
|
463
|
+
*/
|
|
464
|
+
getNextRefreshAt() {
|
|
465
|
+
const expiresAt = this.kernel.getExpiresAt();
|
|
466
|
+
if (!expiresAt) {
|
|
467
|
+
return null;
|
|
468
|
+
}
|
|
469
|
+
const refreshAt = new Date(expiresAt.getTime() - this.threshold * 1e3);
|
|
470
|
+
return refreshAt;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Uninstall plugin
|
|
474
|
+
*/
|
|
475
|
+
uninstall() {
|
|
476
|
+
this.cancelScheduledRefresh();
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
function createRefreshEnginePlugin(options) {
|
|
480
|
+
return new RefreshEnginePlugin(options);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
// src/plugins/core/storage-memory.ts
|
|
484
|
+
var MemoryStorageAdapter = class {
|
|
485
|
+
constructor(options = {}) {
|
|
486
|
+
this.store = /* @__PURE__ */ new Map();
|
|
487
|
+
this.prefix = options.prefix || "authkeeper:";
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Get value by key
|
|
491
|
+
*/
|
|
492
|
+
get(key) {
|
|
493
|
+
return this.store.get(this.prefix + key) || null;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Set value by key
|
|
497
|
+
*/
|
|
498
|
+
set(key, value) {
|
|
499
|
+
this.store.set(this.prefix + key, value);
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Remove value by key
|
|
503
|
+
*/
|
|
504
|
+
remove(key) {
|
|
505
|
+
this.store.delete(this.prefix + key);
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Clear all values
|
|
509
|
+
*/
|
|
510
|
+
clear() {
|
|
511
|
+
const keysToDelete = [];
|
|
512
|
+
for (const key of this.store.keys()) {
|
|
513
|
+
if (key.startsWith(this.prefix)) {
|
|
514
|
+
keysToDelete.push(key);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
keysToDelete.forEach((key) => this.store.delete(key));
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
function createMemoryStorage(options) {
|
|
521
|
+
return new MemoryStorageAdapter(options);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
// src/plugins/core/fetch-interceptor.ts
|
|
525
|
+
var FetchInterceptorPlugin = class {
|
|
526
|
+
constructor() {
|
|
527
|
+
this.name = "fetch-interceptor";
|
|
528
|
+
this.version = "1.0.0";
|
|
529
|
+
this.type = "core";
|
|
530
|
+
this.originalFetch = null;
|
|
531
|
+
}
|
|
532
|
+
install(kernel) {
|
|
533
|
+
this.kernel = kernel;
|
|
534
|
+
return {
|
|
535
|
+
createFetch: this.createFetch.bind(this),
|
|
536
|
+
wrapFetch: this.wrapFetch.bind(this),
|
|
537
|
+
unwrapFetch: this.unwrapFetch.bind(this)
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Create authenticated fetch function
|
|
542
|
+
*/
|
|
543
|
+
createFetch(options = {}) {
|
|
544
|
+
const {
|
|
545
|
+
headerName = "Authorization",
|
|
546
|
+
headerPrefix = "Bearer ",
|
|
547
|
+
includeUrls = [],
|
|
548
|
+
excludeUrls = [],
|
|
549
|
+
retry401 = true,
|
|
550
|
+
maxRetries = 1,
|
|
551
|
+
onUnauthorized
|
|
552
|
+
} = options;
|
|
553
|
+
return async (input, init) => {
|
|
554
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
|
|
555
|
+
const shouldInclude = includeUrls.length === 0 || includeUrls.some(
|
|
556
|
+
(pattern) => typeof pattern === "string" ? url.includes(pattern) : pattern.test(url)
|
|
557
|
+
);
|
|
558
|
+
const shouldExclude = excludeUrls.some(
|
|
559
|
+
(pattern) => typeof pattern === "string" ? url.includes(pattern) : pattern.test(url)
|
|
560
|
+
);
|
|
561
|
+
if (!shouldInclude || shouldExclude) {
|
|
562
|
+
return fetch(input, init);
|
|
563
|
+
}
|
|
564
|
+
const accessToken = this.kernel.getAccessToken();
|
|
565
|
+
const headers = new Headers(init?.headers);
|
|
566
|
+
if (accessToken) {
|
|
567
|
+
headers.set(headerName, headerPrefix + accessToken);
|
|
568
|
+
}
|
|
569
|
+
let response = await fetch(input, {
|
|
570
|
+
...init,
|
|
571
|
+
headers
|
|
572
|
+
});
|
|
573
|
+
if (response.status === 401 && retry401 && maxRetries > 0) {
|
|
574
|
+
if (onUnauthorized) {
|
|
575
|
+
onUnauthorized(response);
|
|
576
|
+
}
|
|
577
|
+
try {
|
|
578
|
+
await this.kernel.refresh();
|
|
579
|
+
const newToken = this.kernel.getAccessToken();
|
|
580
|
+
if (newToken) {
|
|
581
|
+
headers.set(headerName, headerPrefix + newToken);
|
|
582
|
+
response = await fetch(input, {
|
|
583
|
+
...init,
|
|
584
|
+
headers
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
} catch (error) {
|
|
588
|
+
console.error("[AuthKeeper] Token refresh failed during 401 retry:", error);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return response;
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
/**
|
|
595
|
+
* Wrap global fetch with auth
|
|
596
|
+
*/
|
|
597
|
+
wrapFetch(fetch2) {
|
|
598
|
+
if (!this.originalFetch) {
|
|
599
|
+
this.originalFetch = fetch2;
|
|
600
|
+
}
|
|
601
|
+
const authFetch = this.createFetch();
|
|
602
|
+
globalThis.fetch = authFetch;
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Restore original fetch
|
|
606
|
+
*/
|
|
607
|
+
unwrapFetch() {
|
|
608
|
+
if (this.originalFetch) {
|
|
609
|
+
globalThis.fetch = this.originalFetch;
|
|
610
|
+
this.originalFetch = null;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
uninstall() {
|
|
614
|
+
this.unwrapFetch();
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
function createFetchInterceptorPlugin() {
|
|
618
|
+
return new FetchInterceptorPlugin();
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
exports.FetchInterceptorPlugin = FetchInterceptorPlugin;
|
|
622
|
+
exports.MemoryStorageAdapter = MemoryStorageAdapter;
|
|
623
|
+
exports.RefreshEnginePlugin = RefreshEnginePlugin;
|
|
624
|
+
exports.TokenDecoderPlugin = TokenDecoderPlugin;
|
|
625
|
+
exports.TokenStorePlugin = TokenStorePlugin;
|
|
626
|
+
exports.createFetchInterceptorPlugin = createFetchInterceptorPlugin;
|
|
627
|
+
exports.createMemoryStorage = createMemoryStorage;
|
|
628
|
+
exports.createRefreshEnginePlugin = createRefreshEnginePlugin;
|
|
629
|
+
exports.createTokenDecoderPlugin = createTokenDecoderPlugin;
|
|
630
|
+
exports.createTokenStorePlugin = createTokenStorePlugin;
|
|
631
|
+
//# sourceMappingURL=index.cjs.map
|
|
632
|
+
//# sourceMappingURL=index.cjs.map
|