@oxog/authkeeper 1.3.1 → 1.5.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.
Files changed (53) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +152 -2
  3. package/dist/auth-source-CXqrhLDO.d.ts +37 -0
  4. package/dist/auth-source-rjMWiP1Q.d.cts +37 -0
  5. package/dist/{chunk-NZPSW4M7.cjs → chunk-47JWRVTL.cjs} +21 -29
  6. package/dist/chunk-47JWRVTL.cjs.map +1 -0
  7. package/dist/{chunk-XUTTHBMF.js → chunk-ANGR5HRJ.js} +12 -19
  8. package/dist/chunk-ANGR5HRJ.js.map +1 -0
  9. package/dist/chunk-DLCD3SBV.js +17 -0
  10. package/dist/chunk-DLCD3SBV.js.map +1 -0
  11. package/dist/chunk-LGZGECYE.cjs +96 -0
  12. package/dist/chunk-LGZGECYE.cjs.map +1 -0
  13. package/dist/chunk-UUQDRHGF.js +90 -0
  14. package/dist/chunk-UUQDRHGF.js.map +1 -0
  15. package/dist/chunk-XGKVBZSF.cjs +19 -0
  16. package/dist/chunk-XGKVBZSF.cjs.map +1 -0
  17. package/dist/index-C4pGS-TI.d.cts +488 -0
  18. package/dist/index-CQwGO-Wk.d.ts +488 -0
  19. package/dist/index.cjs +71 -60
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +9 -3
  22. package/dist/index.d.ts +9 -3
  23. package/dist/index.js +24 -12
  24. package/dist/index.js.map +1 -1
  25. package/dist/plugins/index.cjs +15 -14
  26. package/dist/plugins/index.d.cts +2 -1
  27. package/dist/plugins/index.d.ts +2 -1
  28. package/dist/plugins/index.js +2 -1
  29. package/dist/react/index.cjs +124 -3
  30. package/dist/react/index.cjs.map +1 -1
  31. package/dist/react/index.d.cts +136 -5
  32. package/dist/react/index.d.ts +136 -5
  33. package/dist/react/index.js +118 -3
  34. package/dist/react/index.js.map +1 -1
  35. package/dist/svelte/index.cjs +53 -3
  36. package/dist/svelte/index.cjs.map +1 -1
  37. package/dist/svelte/index.d.cts +54 -5
  38. package/dist/svelte/index.d.ts +54 -5
  39. package/dist/svelte/index.js +51 -3
  40. package/dist/svelte/index.js.map +1 -1
  41. package/dist/types-DMPBl-Ym.d.cts +421 -0
  42. package/dist/types-DMPBl-Ym.d.ts +421 -0
  43. package/dist/vue/index.cjs +75 -3
  44. package/dist/vue/index.cjs.map +1 -1
  45. package/dist/vue/index.d.cts +93 -5
  46. package/dist/vue/index.d.ts +93 -5
  47. package/dist/vue/index.js +70 -3
  48. package/dist/vue/index.js.map +1 -1
  49. package/package.json +14 -8
  50. package/dist/chunk-NZPSW4M7.cjs.map +0 -1
  51. package/dist/chunk-XUTTHBMF.js.map +0 -1
  52. package/dist/index-BakvZ2rC.d.cts +0 -906
  53. package/dist/index-BakvZ2rC.d.ts +0 -906
@@ -0,0 +1,488 @@
1
+ import { T as TokenSet, r as StoredTokens, S as StorageAdapter, P as Plugin, b as AuthKeeper, a as TokenPayload, g as TokenHeader, t as TokenInfo, o as RefreshTokenFn, q as StorageType, F as FetchInterceptorOptions, A as AuthFetch } from './types-DMPBl-Ym.cjs';
2
+
3
+ /**
4
+ * Token Store Plugin
5
+ *
6
+ * Core plugin for token state management.
7
+ * Stores tokens in memory with metadata (setAt, refreshCount).
8
+ */
9
+
10
+ /**
11
+ * Token Store API
12
+ */
13
+ interface TokenStoreAPI {
14
+ set(tokens: TokenSet): void;
15
+ get(): StoredTokens | null;
16
+ clear(): void;
17
+ getAccessToken(): string | null;
18
+ getRefreshToken(): string | null;
19
+ getTokenType(): string;
20
+ getExpiresAt(): Date | null;
21
+ getExpiresIn(): number | null;
22
+ isExpired(): boolean;
23
+ getSetAt(): Date | null;
24
+ getRefreshCount(): number;
25
+ }
26
+ /**
27
+ * Token Store Plugin Options
28
+ */
29
+ interface TokenStoreOptions {
30
+ /** Reject access tokens that are not structurally valid JWTs (default: false) */
31
+ validateOnSet?: boolean;
32
+ /**
33
+ * Persist tokens in this adapter (default: none, tokens live only in memory).
34
+ * Tokens are restored from it when the plugin is installed.
35
+ */
36
+ storage?: StorageAdapter;
37
+ /** Key used in `storage` (default: 'tokens') */
38
+ storageKey?: string;
39
+ onSet?: (tokens: StoredTokens) => void;
40
+ onClear?: () => void;
41
+ }
42
+ /**
43
+ * TokenStorePlugin class
44
+ */
45
+ declare class TokenStorePlugin implements Plugin {
46
+ name: string;
47
+ version: string;
48
+ type: "core";
49
+ private tokens;
50
+ private options;
51
+ private kernel;
52
+ constructor(options?: TokenStoreOptions);
53
+ install(kernel: AuthKeeper): TokenStoreAPI;
54
+ /**
55
+ * Set tokens
56
+ */
57
+ private set;
58
+ /**
59
+ * Get tokens
60
+ */
61
+ private get;
62
+ /**
63
+ * Clear tokens
64
+ */
65
+ private clear;
66
+ /**
67
+ * Uninstall plugin
68
+ *
69
+ * Drops the in-memory copy only: destroying an instance (e.g. on component
70
+ * unmount) must not log the user out of the persisted session.
71
+ */
72
+ uninstall(): void;
73
+ private get storageKey();
74
+ /**
75
+ * Write tokens to the storage adapter (null removes them).
76
+ * Failures (quota exceeded, storage disabled, ...) are reported as an
77
+ * 'error' event with context 'storage' instead of breaking setTokens().
78
+ */
79
+ private persist;
80
+ /**
81
+ * Load tokens from the storage adapter. Corrupt or malformed entries are
82
+ * removed rather than trusted.
83
+ */
84
+ private restore;
85
+ private reportStorageError;
86
+ }
87
+ /**
88
+ * Create token-store plugin
89
+ */
90
+ declare function createTokenStorePlugin(options?: TokenStoreOptions): TokenStorePlugin;
91
+
92
+ /**
93
+ * Token Decoder Plugin
94
+ *
95
+ * Core plugin for JWT token decoding.
96
+ * Does NOT validate signatures (client-side doesn't need to).
97
+ */
98
+
99
+ /**
100
+ * Token Decoder API
101
+ */
102
+ interface TokenDecoderAPI {
103
+ decode<T = TokenPayload>(token?: string): T | null;
104
+ getHeader(token?: string): TokenHeader | null;
105
+ getClaim<K extends keyof TokenPayload>(key: K, token?: string): TokenPayload[K] | undefined;
106
+ getClaims<K extends keyof TokenPayload>(keys: K[], token?: string): Pick<TokenPayload, K>;
107
+ getExpiry(token?: string): Date | null;
108
+ isExpired(token?: string): boolean;
109
+ getTokenInfo(token?: string): TokenInfo | null;
110
+ }
111
+ /**
112
+ * TokenDecoderPlugin class
113
+ */
114
+ declare class TokenDecoderPlugin implements Plugin {
115
+ name: string;
116
+ version: string;
117
+ type: "core";
118
+ private kernel;
119
+ install(kernel: AuthKeeper): TokenDecoderAPI;
120
+ /**
121
+ * Decode token payload
122
+ */
123
+ private decode;
124
+ /**
125
+ * Decode token header
126
+ */
127
+ private getHeader;
128
+ /**
129
+ * Get specific claim from token
130
+ */
131
+ private getClaimFromToken;
132
+ /**
133
+ * Get multiple claims from token
134
+ */
135
+ private getClaimsFromToken;
136
+ /**
137
+ * Get token expiry
138
+ */
139
+ private getExpiry;
140
+ /**
141
+ * Check if token is expired
142
+ */
143
+ private isExpiredToken;
144
+ /**
145
+ * Get complete token information
146
+ */
147
+ private getTokenInfo;
148
+ uninstall(): void;
149
+ }
150
+ /**
151
+ * Create token-decoder plugin
152
+ */
153
+ declare function createTokenDecoderPlugin(): TokenDecoderPlugin;
154
+
155
+ /**
156
+ * Refresh Engine Plugin
157
+ *
158
+ * Core plugin for automatic token refresh with queue management.
159
+ * - Queues concurrent refresh requests
160
+ * - Schedules refresh before expiry
161
+ * - Retries with exponential backoff
162
+ */
163
+
164
+ /**
165
+ * Refresh Engine API
166
+ */
167
+ interface RefreshEngineAPI {
168
+ refresh(): Promise<TokenSet>;
169
+ scheduleRefresh(): void;
170
+ cancelScheduledRefresh(): void;
171
+ isRefreshing(): boolean;
172
+ /** True only while a refresh result is being stored via kernel.setTokens() */
173
+ isApplyingRefresh?(): boolean;
174
+ getNextRefreshAt(): Date | null;
175
+ setRefreshFn(fn: RefreshTokenFn): void;
176
+ setThreshold(seconds: number): void;
177
+ }
178
+ /**
179
+ * Refresh Engine Options
180
+ */
181
+ interface RefreshEngineOptions {
182
+ refreshFn: RefreshTokenFn;
183
+ threshold?: number;
184
+ maxRetries?: number;
185
+ retryDelay?: number;
186
+ onRefreshStart?: () => void;
187
+ onRefreshSuccess?: (tokens: TokenSet) => void;
188
+ onRefreshError?: (error: Error) => void;
189
+ }
190
+ /**
191
+ * RefreshEnginePlugin class
192
+ */
193
+ declare class RefreshEnginePlugin implements Plugin {
194
+ name: string;
195
+ version: string;
196
+ type: "core";
197
+ private kernel;
198
+ private refreshFn;
199
+ private isRefreshingFlag;
200
+ private applyingRefresh;
201
+ private refreshPromise;
202
+ /** Refresh token of the session the in-flight refresh belongs to */
203
+ private refreshPromiseToken;
204
+ /** Pending retry backoff waits (ended early on session change / uninstall) */
205
+ private backoffs;
206
+ private uninstalled;
207
+ private scheduledRefreshTimeout;
208
+ private threshold;
209
+ private maxRetries;
210
+ private retryDelay;
211
+ private options;
212
+ constructor(options: RefreshEngineOptions);
213
+ install(kernel: AuthKeeper): RefreshEngineAPI;
214
+ /**
215
+ * Refresh tokens
216
+ *
217
+ * Queues concurrent requests - only one refresh executes at a time.
218
+ */
219
+ private refresh;
220
+ /** Wait before a retry; endBackoffs() cuts the wait short */
221
+ private waitBackoff;
222
+ private endBackoffs;
223
+ /**
224
+ * Execute token refresh with retry logic
225
+ */
226
+ private executeRefresh;
227
+ /**
228
+ * Schedule automatic refresh
229
+ *
230
+ * Schedules refresh before token expires (threshold seconds before).
231
+ */
232
+ private scheduleRefresh;
233
+ /**
234
+ * Whether the kernel has automatic refresh enabled (default: true)
235
+ */
236
+ private isAutoRefreshEnabled;
237
+ /**
238
+ * Cancel scheduled refresh
239
+ */
240
+ private cancelScheduledRefresh;
241
+ /**
242
+ * Get next scheduled refresh time
243
+ */
244
+ private getNextRefreshAt;
245
+ /**
246
+ * Uninstall plugin
247
+ */
248
+ uninstall(): void;
249
+ }
250
+ /**
251
+ * Create refresh-engine plugin
252
+ */
253
+ declare function createRefreshEnginePlugin(options: RefreshEngineOptions): RefreshEnginePlugin;
254
+
255
+ /**
256
+ * Memory Storage Plugin
257
+ *
258
+ * Core plugin for in-memory token storage.
259
+ * - Most secure (not accessible via XSS)
260
+ * - Lost on page refresh
261
+ * - Not shared across tabs
262
+ */
263
+
264
+ /**
265
+ * Memory Storage Options
266
+ */
267
+ interface MemoryStorageOptions {
268
+ prefix?: string;
269
+ }
270
+ /**
271
+ * Memory Storage Adapter
272
+ *
273
+ * Stores data in a Map (in memory).
274
+ * Data is lost on page refresh.
275
+ */
276
+ declare class MemoryStorageAdapter implements StorageAdapter {
277
+ private store;
278
+ private prefix;
279
+ constructor(options?: MemoryStorageOptions);
280
+ /**
281
+ * Get value by key
282
+ */
283
+ get(key: string): string | null;
284
+ /**
285
+ * Set value by key
286
+ */
287
+ set(key: string, value: string): void;
288
+ /**
289
+ * Remove value by key
290
+ */
291
+ remove(key: string): void;
292
+ /**
293
+ * Clear all values
294
+ */
295
+ clear(): void;
296
+ }
297
+ /**
298
+ * Create memory storage adapter
299
+ */
300
+ declare function createMemoryStorage(options?: MemoryStorageOptions): MemoryStorageAdapter;
301
+
302
+ /**
303
+ * Cookie utilities
304
+ *
305
+ * Helper functions for parsing and serializing cookies.
306
+ */
307
+ /**
308
+ * Cookie attributes for serialization
309
+ */
310
+ interface CookieAttributes {
311
+ /** Path where cookie is valid (default: '/') */
312
+ path?: string;
313
+ /** Domain where cookie is valid */
314
+ domain?: string;
315
+ /** Max age in seconds */
316
+ maxAge?: number;
317
+ /** Expiration date */
318
+ expires?: Date;
319
+ /** Secure flag (HTTPS only) */
320
+ secure?: boolean;
321
+ /** SameSite attribute */
322
+ sameSite?: 'strict' | 'lax' | 'none';
323
+ /** HttpOnly flag (not accessible via JavaScript) */
324
+ httpOnly?: boolean;
325
+ }
326
+ /**
327
+ * Parse document.cookie string into key-value pairs
328
+ *
329
+ * @param cookieString - Cookie string from document.cookie
330
+ * @returns Object with cookie key-value pairs
331
+ */
332
+ declare function parseCookies(cookieString: string): Record<string, string>;
333
+ /**
334
+ * Get cookie value by name
335
+ *
336
+ * @param name - Cookie name
337
+ * @returns Cookie value or null if not found
338
+ */
339
+ declare function getCookie(name: string): string | null;
340
+ /**
341
+ * Serialize cookie with attributes to Set-Cookie format
342
+ *
343
+ * @param name - Cookie name
344
+ * @param value - Cookie value
345
+ * @param attributes - Cookie attributes
346
+ * @returns Set-Cookie header value
347
+ */
348
+ declare function serializeCookie(name: string, value: string, attributes?: CookieAttributes): string;
349
+ /**
350
+ * Set cookie in document
351
+ *
352
+ * @param name - Cookie name
353
+ * @param value - Cookie value
354
+ * @param attributes - Cookie attributes
355
+ */
356
+ declare function setCookie(name: string, value: string, attributes?: CookieAttributes): void;
357
+ /**
358
+ * Delete cookie by name
359
+ *
360
+ * @param name - Cookie name
361
+ * @param attributes - Cookie attributes (path and domain should match original)
362
+ */
363
+ declare function deleteCookie(name: string, attributes?: Omit<CookieAttributes, 'maxAge' | 'expires'>): void;
364
+
365
+ /**
366
+ * Browser Storage Adapters
367
+ *
368
+ * StorageAdapter implementations backed by Web Storage (localStorage /
369
+ * sessionStorage) and document.cookie.
370
+ *
371
+ * Security note: anything stored here is readable by any script running on
372
+ * the page (XSS). In-memory storage (the default) is the safest choice; use
373
+ * these only when tokens must survive a page reload.
374
+ */
375
+
376
+ /**
377
+ * Web Storage adapter options
378
+ */
379
+ interface WebStorageOptions {
380
+ /** Key prefix (default: 'authkeeper:') */
381
+ prefix?: string;
382
+ }
383
+ /**
384
+ * StorageAdapter backed by a Web Storage object (localStorage / sessionStorage)
385
+ */
386
+ declare class WebStorageAdapter implements StorageAdapter {
387
+ private storage;
388
+ private prefix;
389
+ constructor(storage: Storage, options?: WebStorageOptions);
390
+ get(key: string): string | null;
391
+ set(key: string, value: string): void;
392
+ remove(key: string): void;
393
+ clear(): void;
394
+ }
395
+ /**
396
+ * Cookie adapter options
397
+ */
398
+ interface CookieStorageOptions {
399
+ /** Cookie name prefix (default: 'authkeeper_') */
400
+ prefix?: string;
401
+ /**
402
+ * Cookie attributes. Defaults: `path: '/'`, `secure: true`,
403
+ * `sameSite: 'strict'`. HttpOnly cannot be set from JavaScript.
404
+ */
405
+ attributes?: Omit<CookieAttributes, 'httpOnly'>;
406
+ }
407
+ /**
408
+ * StorageAdapter backed by document.cookie
409
+ */
410
+ declare class CookieStorageAdapter implements StorageAdapter {
411
+ private prefix;
412
+ private attributes;
413
+ private keys;
414
+ constructor(options?: CookieStorageOptions);
415
+ get(key: string): string | null;
416
+ set(key: string, value: string): void;
417
+ remove(key: string): void;
418
+ clear(): void;
419
+ }
420
+ /**
421
+ * Resolve the `storage` kernel option to a StorageAdapter.
422
+ *
423
+ * - `undefined` / `'memory'`: no persistence (returns `undefined`); tokens
424
+ * live only in the token store's memory.
425
+ * - `'localStorage'` / `'sessionStorage'`: Web Storage, falling back to an
426
+ * in-memory adapter where it is unavailable (SSR, privacy mode).
427
+ * - `'cookie'`: document.cookie, falling back to memory outside browsers.
428
+ * - a StorageAdapter object: used as-is.
429
+ *
430
+ * @throws {AuthError} CONFIG_ERROR for an unknown storage type
431
+ */
432
+ declare function resolveStorage(storage: StorageType | StorageAdapter | undefined): StorageAdapter | undefined;
433
+
434
+ /**
435
+ * Fetch Interceptor Plugin
436
+ *
437
+ * Core plugin for fetch() wrapper with auth header injection and 401 retry.
438
+ * - Injects Authorization header
439
+ * - URL filtering (include/exclude)
440
+ * - 401 retry after token refresh
441
+ */
442
+
443
+ /**
444
+ * Fetch Interceptor API
445
+ */
446
+ interface FetchInterceptorAPI {
447
+ createFetch(options?: FetchInterceptorOptions): AuthFetch;
448
+ wrapFetch(fetch: typeof globalThis.fetch): void;
449
+ unwrapFetch(): void;
450
+ }
451
+ /**
452
+ * FetchInterceptorPlugin class
453
+ */
454
+ declare class FetchInterceptorPlugin implements Plugin {
455
+ name: string;
456
+ version: string;
457
+ type: "core";
458
+ private kernel;
459
+ private originalFetch;
460
+ /**
461
+ * The fetch implementation used to perform requests.
462
+ *
463
+ * While the global fetch is wrapped, this is the original (unwrapped)
464
+ * fetch; calling `globalThis.fetch` from inside the wrapper would recurse
465
+ * into the wrapper itself.
466
+ */
467
+ private baseFetch;
468
+ install(kernel: AuthKeeper): FetchInterceptorAPI;
469
+ /**
470
+ * Create authenticated fetch function
471
+ */
472
+ private createFetch;
473
+ /**
474
+ * Wrap global fetch with auth
475
+ */
476
+ private wrapFetch;
477
+ /**
478
+ * Restore original fetch
479
+ */
480
+ private unwrapFetch;
481
+ uninstall(): void;
482
+ }
483
+ /**
484
+ * Create fetch-interceptor plugin
485
+ */
486
+ declare function createFetchInterceptorPlugin(): FetchInterceptorPlugin;
487
+
488
+ export { type CookieAttributes as C, type FetchInterceptorAPI as F, MemoryStorageAdapter as M, type RefreshEngineAPI as R, type TokenDecoderAPI as T, WebStorageAdapter as W, CookieStorageAdapter as a, type CookieStorageOptions as b, FetchInterceptorPlugin as c, type MemoryStorageOptions as d, type RefreshEngineOptions as e, RefreshEnginePlugin as f, TokenDecoderPlugin as g, type TokenStoreAPI as h, type TokenStoreOptions as i, TokenStorePlugin as j, type WebStorageOptions as k, createFetchInterceptorPlugin as l, createMemoryStorage as m, createRefreshEnginePlugin as n, createTokenDecoderPlugin as o, createTokenStorePlugin as p, deleteCookie as q, getCookie as r, parseCookies as s, serializeCookie as t, setCookie as u, resolveStorage as v };