@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,716 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AuthKeeper Type Definitions
|
|
3
|
+
*
|
|
4
|
+
* Complete type system for the AuthKeeper library.
|
|
5
|
+
* All interfaces and types are exported for use by consumers.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Token set returned from authentication/refresh operations
|
|
9
|
+
*/
|
|
10
|
+
interface TokenSet {
|
|
11
|
+
/** JWT access token */
|
|
12
|
+
accessToken: string;
|
|
13
|
+
/** Optional refresh token for obtaining new access tokens */
|
|
14
|
+
refreshToken?: string;
|
|
15
|
+
/** Token expiration in seconds from now */
|
|
16
|
+
expiresIn?: number;
|
|
17
|
+
/** Token expiration as Unix timestamp (seconds) */
|
|
18
|
+
expiresAt?: number;
|
|
19
|
+
/** Token type (default: 'Bearer') */
|
|
20
|
+
tokenType?: string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Standard JWT payload with common claims
|
|
24
|
+
*/
|
|
25
|
+
interface TokenPayload {
|
|
26
|
+
/** Subject - usually the user ID */
|
|
27
|
+
sub?: string;
|
|
28
|
+
/** Issuer - who created the token */
|
|
29
|
+
iss?: string;
|
|
30
|
+
/** Audience - who the token is intended for */
|
|
31
|
+
aud?: string | string[];
|
|
32
|
+
/** Expiration time (Unix timestamp in seconds) */
|
|
33
|
+
exp?: number;
|
|
34
|
+
/** Not before time (Unix timestamp in seconds) */
|
|
35
|
+
nbf?: number;
|
|
36
|
+
/** Issued at time (Unix timestamp in seconds) */
|
|
37
|
+
iat?: number;
|
|
38
|
+
/** JWT ID - unique identifier */
|
|
39
|
+
jti?: string;
|
|
40
|
+
/** User email address */
|
|
41
|
+
email?: string;
|
|
42
|
+
/** User display name */
|
|
43
|
+
name?: string;
|
|
44
|
+
/** User roles */
|
|
45
|
+
roles?: string[];
|
|
46
|
+
/** User permissions */
|
|
47
|
+
permissions?: string[];
|
|
48
|
+
/** OAuth2 scope */
|
|
49
|
+
scope?: string;
|
|
50
|
+
[key: string]: unknown;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* JWT header information
|
|
54
|
+
*/
|
|
55
|
+
interface TokenHeader {
|
|
56
|
+
/** Algorithm used to sign the token */
|
|
57
|
+
alg: string;
|
|
58
|
+
/** Token type (usually 'JWT') */
|
|
59
|
+
typ?: string;
|
|
60
|
+
/** Key ID used to sign the token */
|
|
61
|
+
kid?: string;
|
|
62
|
+
[key: string]: unknown;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Complete token information including decoded parts
|
|
66
|
+
*/
|
|
67
|
+
interface TokenInfo {
|
|
68
|
+
/** Raw JWT string */
|
|
69
|
+
raw: string;
|
|
70
|
+
/** Decoded payload */
|
|
71
|
+
payload: TokenPayload;
|
|
72
|
+
/** Decoded header */
|
|
73
|
+
header: TokenHeader;
|
|
74
|
+
/** When the token expires (null if no expiry) */
|
|
75
|
+
expiresAt: Date | null;
|
|
76
|
+
/** Whether the token is expired */
|
|
77
|
+
isExpired: boolean;
|
|
78
|
+
/** Time until expiry in milliseconds (null if no expiry) */
|
|
79
|
+
expiresIn: number | null;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Stored token data with metadata
|
|
83
|
+
*/
|
|
84
|
+
interface StoredTokens {
|
|
85
|
+
/** Access token */
|
|
86
|
+
accessToken: string;
|
|
87
|
+
/** Refresh token (null if not available) */
|
|
88
|
+
refreshToken: string | null;
|
|
89
|
+
/** Token type (e.g., 'Bearer') */
|
|
90
|
+
tokenType: string;
|
|
91
|
+
/** Expiration timestamp in milliseconds (null if no expiry) */
|
|
92
|
+
expiresAt: number | null;
|
|
93
|
+
/** When tokens were set (timestamp in milliseconds) */
|
|
94
|
+
setAt: number;
|
|
95
|
+
/** Number of times tokens have been refreshed */
|
|
96
|
+
refreshCount: number;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Error codes for authentication errors
|
|
100
|
+
*/
|
|
101
|
+
type AuthErrorCode = 'TOKEN_EXPIRED' | 'TOKEN_INVALID' | 'TOKEN_DECODE_FAILED' | 'REFRESH_FAILED' | 'REFRESH_TOKEN_MISSING' | 'STORAGE_ERROR' | 'NETWORK_ERROR' | 'UNAUTHORIZED' | 'FORBIDDEN' | 'OAUTH_ERROR' | 'PKCE_ERROR' | 'CALLBACK_ERROR' | 'CONFIG_ERROR';
|
|
102
|
+
/**
|
|
103
|
+
* Options for creating an AuthError
|
|
104
|
+
*/
|
|
105
|
+
interface AuthErrorOptions {
|
|
106
|
+
/** Additional context about the error */
|
|
107
|
+
context?: Record<string, unknown>;
|
|
108
|
+
/** Original error that caused this error */
|
|
109
|
+
cause?: Error;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Custom error class for authentication errors
|
|
113
|
+
*/
|
|
114
|
+
declare class AuthError extends Error {
|
|
115
|
+
/** Error code identifying the type of error */
|
|
116
|
+
code: AuthErrorCode;
|
|
117
|
+
/** Additional context about the error */
|
|
118
|
+
context?: Record<string, unknown>;
|
|
119
|
+
/** Original error that caused this error */
|
|
120
|
+
cause?: Error;
|
|
121
|
+
constructor(code: AuthErrorCode, message: string, options?: AuthErrorOptions);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* All possible event types
|
|
125
|
+
*/
|
|
126
|
+
type EventType = 'login' | 'logout' | 'refresh' | 'expired' | 'error' | 'storage-change' | 'tab-sync';
|
|
127
|
+
/**
|
|
128
|
+
* Login event - fired when user logs in
|
|
129
|
+
*/
|
|
130
|
+
interface LoginEvent {
|
|
131
|
+
type: 'login';
|
|
132
|
+
tokens: TokenSet;
|
|
133
|
+
timestamp: number;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Logout event - fired when user logs out
|
|
137
|
+
*/
|
|
138
|
+
interface LogoutEvent {
|
|
139
|
+
type: 'logout';
|
|
140
|
+
reason: 'manual' | 'expired' | 'error' | 'tab-sync';
|
|
141
|
+
timestamp: number;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Refresh event - fired when tokens are refreshed
|
|
145
|
+
*/
|
|
146
|
+
interface RefreshEvent {
|
|
147
|
+
type: 'refresh';
|
|
148
|
+
tokens: TokenSet;
|
|
149
|
+
previousExpiresAt: Date | null;
|
|
150
|
+
newExpiresAt: Date | null;
|
|
151
|
+
timestamp: number;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Expired event - fired when token expires
|
|
155
|
+
*/
|
|
156
|
+
interface ExpiredEvent {
|
|
157
|
+
type: 'expired';
|
|
158
|
+
expiredAt: Date;
|
|
159
|
+
timestamp: number;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Error event - fired when an error occurs
|
|
163
|
+
*/
|
|
164
|
+
interface ErrorEvent {
|
|
165
|
+
type: 'error';
|
|
166
|
+
error: AuthError;
|
|
167
|
+
context: 'refresh' | 'storage' | 'decode' | 'network';
|
|
168
|
+
timestamp: number;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Storage change event - fired when storage changes
|
|
172
|
+
*/
|
|
173
|
+
interface StorageChangeEvent {
|
|
174
|
+
type: 'storage-change';
|
|
175
|
+
key: string;
|
|
176
|
+
oldValue: string | null;
|
|
177
|
+
newValue: string | null;
|
|
178
|
+
timestamp: number;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Tab sync event - fired when syncing across tabs
|
|
182
|
+
*/
|
|
183
|
+
interface TabSyncEvent {
|
|
184
|
+
type: 'tab-sync';
|
|
185
|
+
action: 'login' | 'logout' | 'refresh';
|
|
186
|
+
sourceTabId: string;
|
|
187
|
+
timestamp: number;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Union of all event types
|
|
191
|
+
*/
|
|
192
|
+
type AuthEvent = LoginEvent | LogoutEvent | RefreshEvent | ExpiredEvent | ErrorEvent | StorageChangeEvent | TabSyncEvent;
|
|
193
|
+
/**
|
|
194
|
+
* Event handler function type
|
|
195
|
+
*/
|
|
196
|
+
type EventHandler<E extends EventType> = (event: Extract<AuthEvent, {
|
|
197
|
+
type: E;
|
|
198
|
+
}>) => void;
|
|
199
|
+
/**
|
|
200
|
+
* Unsubscribe function returned from event handlers
|
|
201
|
+
*/
|
|
202
|
+
type Unsubscribe = () => void;
|
|
203
|
+
/**
|
|
204
|
+
* Storage adapter interface for token persistence
|
|
205
|
+
*/
|
|
206
|
+
interface StorageAdapter {
|
|
207
|
+
/** Get value by key */
|
|
208
|
+
get(key: string): string | null;
|
|
209
|
+
/** Set value by key */
|
|
210
|
+
set(key: string, value: string): void;
|
|
211
|
+
/** Remove value by key */
|
|
212
|
+
remove(key: string): void;
|
|
213
|
+
/** Clear all values */
|
|
214
|
+
clear(): void;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Built-in storage types
|
|
218
|
+
*/
|
|
219
|
+
type StorageType = 'memory' | 'localStorage' | 'sessionStorage' | 'cookie';
|
|
220
|
+
/**
|
|
221
|
+
* Plugin interface for extending AuthKeeper
|
|
222
|
+
*/
|
|
223
|
+
interface Plugin {
|
|
224
|
+
/** Unique plugin name */
|
|
225
|
+
name: string;
|
|
226
|
+
/** Plugin version */
|
|
227
|
+
version: string;
|
|
228
|
+
/** Plugin type */
|
|
229
|
+
type: 'core' | 'optional';
|
|
230
|
+
/** Install plugin and return API */
|
|
231
|
+
install(kernel: AuthKeeper): any;
|
|
232
|
+
/** Uninstall plugin and cleanup */
|
|
233
|
+
uninstall?(): void | Promise<void>;
|
|
234
|
+
/** Optional lifecycle hooks */
|
|
235
|
+
hooks?: {
|
|
236
|
+
/** Called before tokens are set */
|
|
237
|
+
beforeSetTokens?: (tokens: TokenSet) => TokenSet | false;
|
|
238
|
+
/** Called after tokens are set */
|
|
239
|
+
afterSetTokens?: (tokens: TokenSet) => void;
|
|
240
|
+
/** Called before refresh */
|
|
241
|
+
beforeRefresh?: () => boolean;
|
|
242
|
+
/** Called after refresh */
|
|
243
|
+
afterRefresh?: (tokens: TokenSet) => void;
|
|
244
|
+
/** Called before logout */
|
|
245
|
+
beforeLogout?: () => boolean;
|
|
246
|
+
/** Called after logout */
|
|
247
|
+
afterLogout?: () => void;
|
|
248
|
+
/** Called when token expires */
|
|
249
|
+
onExpired?: () => void;
|
|
250
|
+
/** Called on error */
|
|
251
|
+
onError?: (error: AuthError) => void;
|
|
252
|
+
/** Called before request */
|
|
253
|
+
beforeRequest?: (request: Request) => Request;
|
|
254
|
+
/** Called after response */
|
|
255
|
+
afterResponse?: (response: Response) => Response;
|
|
256
|
+
};
|
|
257
|
+
/** Plugin API exposed to consumers */
|
|
258
|
+
api?: Record<string, unknown>;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Plugin information
|
|
262
|
+
*/
|
|
263
|
+
interface PluginInfo {
|
|
264
|
+
name: string;
|
|
265
|
+
version: string;
|
|
266
|
+
type: 'core' | 'optional';
|
|
267
|
+
enabled: boolean;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Refresh token function type
|
|
271
|
+
*/
|
|
272
|
+
type RefreshTokenFn = (refreshToken: string) => Promise<TokenSet>;
|
|
273
|
+
/**
|
|
274
|
+
* Kernel configuration options
|
|
275
|
+
*/
|
|
276
|
+
interface KernelOptions {
|
|
277
|
+
/** Storage adapter or type */
|
|
278
|
+
storage: StorageType | StorageAdapter;
|
|
279
|
+
/** Function to refresh tokens */
|
|
280
|
+
refreshToken: RefreshTokenFn;
|
|
281
|
+
/** Enable automatic token refresh (default: true) */
|
|
282
|
+
autoRefresh?: boolean;
|
|
283
|
+
/** Seconds before expiry to refresh (default: 60) */
|
|
284
|
+
refreshThreshold?: number;
|
|
285
|
+
/** Enable multi-tab sync (default: true) */
|
|
286
|
+
syncTabs?: boolean;
|
|
287
|
+
/** Error handler */
|
|
288
|
+
onError?: (error: AuthError) => void;
|
|
289
|
+
/** Additional plugins to install */
|
|
290
|
+
plugins?: Plugin[];
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Logout options
|
|
294
|
+
*/
|
|
295
|
+
interface LogoutOptions {
|
|
296
|
+
/** Logout from all tabs */
|
|
297
|
+
allTabs?: boolean;
|
|
298
|
+
/** Redirect URL after logout */
|
|
299
|
+
redirect?: string;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Authenticated fetch function
|
|
303
|
+
*/
|
|
304
|
+
type AuthFetch = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
305
|
+
/**
|
|
306
|
+
* Fetch interceptor options
|
|
307
|
+
*/
|
|
308
|
+
interface FetchInterceptorOptions {
|
|
309
|
+
/** Header name for auth token (default: 'Authorization') */
|
|
310
|
+
headerName?: string;
|
|
311
|
+
/** Header prefix (default: 'Bearer ') */
|
|
312
|
+
headerPrefix?: string;
|
|
313
|
+
/** Only include auth header for these URLs */
|
|
314
|
+
includeUrls?: (string | RegExp)[];
|
|
315
|
+
/** Exclude auth header for these URLs */
|
|
316
|
+
excludeUrls?: (string | RegExp)[];
|
|
317
|
+
/** Callback on 401 response */
|
|
318
|
+
onUnauthorized?: (response: Response) => void;
|
|
319
|
+
/** Retry on 401 after refresh (default: true) */
|
|
320
|
+
retry401?: boolean;
|
|
321
|
+
/** Max retries (default: 1) */
|
|
322
|
+
maxRetries?: number;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* OAuth2 login options
|
|
326
|
+
*/
|
|
327
|
+
interface LoginOptions {
|
|
328
|
+
/** OAuth2 scope */
|
|
329
|
+
scope?: string;
|
|
330
|
+
/** OAuth2 state parameter */
|
|
331
|
+
state?: string;
|
|
332
|
+
/** OAuth2 prompt parameter */
|
|
333
|
+
prompt?: 'none' | 'login' | 'consent' | 'select_account';
|
|
334
|
+
/** Login hint (email, etc.) */
|
|
335
|
+
loginHint?: string;
|
|
336
|
+
/** Additional OAuth2 parameters */
|
|
337
|
+
extraParams?: Record<string, string>;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* User info from OIDC userinfo endpoint
|
|
341
|
+
*/
|
|
342
|
+
interface UserInfo {
|
|
343
|
+
sub: string;
|
|
344
|
+
email?: string;
|
|
345
|
+
name?: string;
|
|
346
|
+
picture?: string;
|
|
347
|
+
[key: string]: unknown;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Main AuthKeeper interface - the kernel
|
|
351
|
+
*/
|
|
352
|
+
interface AuthKeeper {
|
|
353
|
+
/** Set authentication tokens */
|
|
354
|
+
setTokens(tokens: TokenSet): void;
|
|
355
|
+
/** Get access token */
|
|
356
|
+
getAccessToken(): string | null;
|
|
357
|
+
/** Get refresh token */
|
|
358
|
+
getRefreshToken(): string | null;
|
|
359
|
+
/** Clear all tokens */
|
|
360
|
+
clearTokens(): void;
|
|
361
|
+
/** Check if user is authenticated */
|
|
362
|
+
isAuthenticated(): boolean;
|
|
363
|
+
/** Check if token is expired */
|
|
364
|
+
isExpired(): boolean;
|
|
365
|
+
/** Get token expiration date */
|
|
366
|
+
getExpiresAt(): Date | null;
|
|
367
|
+
/** Get time until expiry in milliseconds */
|
|
368
|
+
getTimeUntilExpiry(): number | null;
|
|
369
|
+
/** Decode token payload */
|
|
370
|
+
decode<T = TokenPayload>(): T | null;
|
|
371
|
+
/** Get specific claim from token */
|
|
372
|
+
getClaim<K extends keyof TokenPayload>(key: K): TokenPayload[K] | undefined;
|
|
373
|
+
/** Get multiple claims from token */
|
|
374
|
+
getClaims<K extends keyof TokenPayload>(keys: K[]): Pick<TokenPayload, K>;
|
|
375
|
+
/** Manually refresh tokens */
|
|
376
|
+
refresh(): Promise<TokenSet>;
|
|
377
|
+
/** Schedule automatic refresh */
|
|
378
|
+
scheduleRefresh(): void;
|
|
379
|
+
/** Cancel scheduled refresh */
|
|
380
|
+
cancelScheduledRefresh(): void;
|
|
381
|
+
/** Logout user */
|
|
382
|
+
logout(options?: LogoutOptions): void;
|
|
383
|
+
/** Create authenticated fetch function */
|
|
384
|
+
createFetch(options?: FetchInterceptorOptions): AuthFetch;
|
|
385
|
+
/** Wrap global fetch with auth */
|
|
386
|
+
wrapFetch(fetch: typeof globalThis.fetch): void;
|
|
387
|
+
/** Register a plugin */
|
|
388
|
+
register(plugin: Plugin): void;
|
|
389
|
+
/** Unregister a plugin */
|
|
390
|
+
unregister(pluginName: string): void;
|
|
391
|
+
/** Get plugin by name */
|
|
392
|
+
getPlugin<P extends Plugin>(name: string): P | undefined;
|
|
393
|
+
/** List all plugins */
|
|
394
|
+
listPlugins(): PluginInfo[];
|
|
395
|
+
/** Emit an event */
|
|
396
|
+
emit(event: AuthEvent): void;
|
|
397
|
+
/** Subscribe to events */
|
|
398
|
+
on<E extends EventType>(eventType: E, handler: EventHandler<E>): Unsubscribe;
|
|
399
|
+
/** Unsubscribe from events */
|
|
400
|
+
off<E extends EventType>(eventType: E, handler: EventHandler<E>): void;
|
|
401
|
+
/** Initialize kernel */
|
|
402
|
+
init(): Promise<void>;
|
|
403
|
+
/** Destroy kernel and cleanup */
|
|
404
|
+
destroy(): void;
|
|
405
|
+
/** Update configuration */
|
|
406
|
+
configure(options: Partial<KernelOptions>): void;
|
|
407
|
+
/** Get current configuration */
|
|
408
|
+
getOptions(): KernelOptions;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Token Store Plugin
|
|
413
|
+
*
|
|
414
|
+
* Core plugin for token state management.
|
|
415
|
+
* Stores tokens in memory with metadata (setAt, refreshCount).
|
|
416
|
+
*/
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Token Store API
|
|
420
|
+
*/
|
|
421
|
+
interface TokenStoreAPI {
|
|
422
|
+
set(tokens: TokenSet): void;
|
|
423
|
+
get(): StoredTokens | null;
|
|
424
|
+
clear(): void;
|
|
425
|
+
getAccessToken(): string | null;
|
|
426
|
+
getRefreshToken(): string | null;
|
|
427
|
+
getTokenType(): string;
|
|
428
|
+
getExpiresAt(): Date | null;
|
|
429
|
+
getExpiresIn(): number | null;
|
|
430
|
+
isExpired(): boolean;
|
|
431
|
+
getSetAt(): Date | null;
|
|
432
|
+
getRefreshCount(): number;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Token Store Plugin Options
|
|
436
|
+
*/
|
|
437
|
+
interface TokenStoreOptions {
|
|
438
|
+
validateOnSet?: boolean;
|
|
439
|
+
onSet?: (tokens: StoredTokens) => void;
|
|
440
|
+
onClear?: () => void;
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* TokenStorePlugin class
|
|
444
|
+
*/
|
|
445
|
+
declare class TokenStorePlugin implements Plugin {
|
|
446
|
+
name: string;
|
|
447
|
+
version: string;
|
|
448
|
+
type: "core";
|
|
449
|
+
private tokens;
|
|
450
|
+
private options;
|
|
451
|
+
constructor(options?: TokenStoreOptions);
|
|
452
|
+
install(_kernel: AuthKeeper): TokenStoreAPI;
|
|
453
|
+
/**
|
|
454
|
+
* Set tokens
|
|
455
|
+
*/
|
|
456
|
+
private set;
|
|
457
|
+
/**
|
|
458
|
+
* Get tokens
|
|
459
|
+
*/
|
|
460
|
+
private get;
|
|
461
|
+
/**
|
|
462
|
+
* Clear tokens
|
|
463
|
+
*/
|
|
464
|
+
private clear;
|
|
465
|
+
/**
|
|
466
|
+
* Uninstall plugin
|
|
467
|
+
*/
|
|
468
|
+
uninstall(): void;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Create token-store plugin
|
|
472
|
+
*/
|
|
473
|
+
declare function createTokenStorePlugin(options?: TokenStoreOptions): TokenStorePlugin;
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Token Decoder Plugin
|
|
477
|
+
*
|
|
478
|
+
* Core plugin for JWT token decoding.
|
|
479
|
+
* Does NOT validate signatures (client-side doesn't need to).
|
|
480
|
+
*/
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Token Decoder API
|
|
484
|
+
*/
|
|
485
|
+
interface TokenDecoderAPI {
|
|
486
|
+
decode<T = TokenPayload>(token?: string): T | null;
|
|
487
|
+
getHeader(token?: string): TokenHeader | null;
|
|
488
|
+
getClaim<K extends keyof TokenPayload>(key: K, token?: string): TokenPayload[K] | undefined;
|
|
489
|
+
getClaims<K extends keyof TokenPayload>(keys: K[], token?: string): Pick<TokenPayload, K>;
|
|
490
|
+
getExpiry(token?: string): Date | null;
|
|
491
|
+
isExpired(token?: string): boolean;
|
|
492
|
+
getTokenInfo(token?: string): TokenInfo | null;
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* TokenDecoderPlugin class
|
|
496
|
+
*/
|
|
497
|
+
declare class TokenDecoderPlugin implements Plugin {
|
|
498
|
+
name: string;
|
|
499
|
+
version: string;
|
|
500
|
+
type: "core";
|
|
501
|
+
private kernel;
|
|
502
|
+
install(kernel: AuthKeeper): TokenDecoderAPI;
|
|
503
|
+
/**
|
|
504
|
+
* Decode token payload
|
|
505
|
+
*/
|
|
506
|
+
private decode;
|
|
507
|
+
/**
|
|
508
|
+
* Decode token header
|
|
509
|
+
*/
|
|
510
|
+
private getHeader;
|
|
511
|
+
/**
|
|
512
|
+
* Get specific claim from token
|
|
513
|
+
*/
|
|
514
|
+
private getClaimFromToken;
|
|
515
|
+
/**
|
|
516
|
+
* Get multiple claims from token
|
|
517
|
+
*/
|
|
518
|
+
private getClaimsFromToken;
|
|
519
|
+
/**
|
|
520
|
+
* Get token expiry
|
|
521
|
+
*/
|
|
522
|
+
private getExpiry;
|
|
523
|
+
/**
|
|
524
|
+
* Check if token is expired
|
|
525
|
+
*/
|
|
526
|
+
private isExpiredToken;
|
|
527
|
+
/**
|
|
528
|
+
* Get complete token information
|
|
529
|
+
*/
|
|
530
|
+
private getTokenInfo;
|
|
531
|
+
uninstall(): void;
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* Create token-decoder plugin
|
|
535
|
+
*/
|
|
536
|
+
declare function createTokenDecoderPlugin(): TokenDecoderPlugin;
|
|
537
|
+
|
|
538
|
+
/**
|
|
539
|
+
* Refresh Engine Plugin
|
|
540
|
+
*
|
|
541
|
+
* Core plugin for automatic token refresh with queue management.
|
|
542
|
+
* - Queues concurrent refresh requests
|
|
543
|
+
* - Schedules refresh before expiry
|
|
544
|
+
* - Retries with exponential backoff
|
|
545
|
+
*/
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* Refresh Engine API
|
|
549
|
+
*/
|
|
550
|
+
interface RefreshEngineAPI {
|
|
551
|
+
refresh(): Promise<TokenSet>;
|
|
552
|
+
scheduleRefresh(): void;
|
|
553
|
+
cancelScheduledRefresh(): void;
|
|
554
|
+
isRefreshing(): boolean;
|
|
555
|
+
getNextRefreshAt(): Date | null;
|
|
556
|
+
setRefreshFn(fn: RefreshTokenFn): void;
|
|
557
|
+
setThreshold(seconds: number): void;
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Refresh Engine Options
|
|
561
|
+
*/
|
|
562
|
+
interface RefreshEngineOptions {
|
|
563
|
+
refreshFn: RefreshTokenFn;
|
|
564
|
+
threshold?: number;
|
|
565
|
+
maxRetries?: number;
|
|
566
|
+
retryDelay?: number;
|
|
567
|
+
onRefreshStart?: () => void;
|
|
568
|
+
onRefreshSuccess?: (tokens: TokenSet) => void;
|
|
569
|
+
onRefreshError?: (error: Error) => void;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* RefreshEnginePlugin class
|
|
573
|
+
*/
|
|
574
|
+
declare class RefreshEnginePlugin implements Plugin {
|
|
575
|
+
name: string;
|
|
576
|
+
version: string;
|
|
577
|
+
type: "core";
|
|
578
|
+
private kernel;
|
|
579
|
+
private refreshFn;
|
|
580
|
+
private isRefreshingFlag;
|
|
581
|
+
private refreshPromise;
|
|
582
|
+
private scheduledRefreshTimeout;
|
|
583
|
+
private threshold;
|
|
584
|
+
private maxRetries;
|
|
585
|
+
private retryDelay;
|
|
586
|
+
private options;
|
|
587
|
+
constructor(options: RefreshEngineOptions);
|
|
588
|
+
install(kernel: AuthKeeper): RefreshEngineAPI;
|
|
589
|
+
/**
|
|
590
|
+
* Refresh tokens
|
|
591
|
+
*
|
|
592
|
+
* Queues concurrent requests - only one refresh executes at a time.
|
|
593
|
+
*/
|
|
594
|
+
private refresh;
|
|
595
|
+
/**
|
|
596
|
+
* Execute token refresh with retry logic
|
|
597
|
+
*/
|
|
598
|
+
private executeRefresh;
|
|
599
|
+
/**
|
|
600
|
+
* Schedule automatic refresh
|
|
601
|
+
*
|
|
602
|
+
* Schedules refresh before token expires (threshold seconds before).
|
|
603
|
+
*/
|
|
604
|
+
private scheduleRefresh;
|
|
605
|
+
/**
|
|
606
|
+
* Cancel scheduled refresh
|
|
607
|
+
*/
|
|
608
|
+
private cancelScheduledRefresh;
|
|
609
|
+
/**
|
|
610
|
+
* Get next scheduled refresh time
|
|
611
|
+
*/
|
|
612
|
+
private getNextRefreshAt;
|
|
613
|
+
/**
|
|
614
|
+
* Uninstall plugin
|
|
615
|
+
*/
|
|
616
|
+
uninstall(): void;
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Create refresh-engine plugin
|
|
620
|
+
*/
|
|
621
|
+
declare function createRefreshEnginePlugin(options: RefreshEngineOptions): RefreshEnginePlugin;
|
|
622
|
+
|
|
623
|
+
/**
|
|
624
|
+
* Memory Storage Plugin
|
|
625
|
+
*
|
|
626
|
+
* Core plugin for in-memory token storage.
|
|
627
|
+
* - Most secure (not accessible via XSS)
|
|
628
|
+
* - Lost on page refresh
|
|
629
|
+
* - Not shared across tabs
|
|
630
|
+
*/
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* Memory Storage Options
|
|
634
|
+
*/
|
|
635
|
+
interface MemoryStorageOptions {
|
|
636
|
+
prefix?: string;
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
639
|
+
* Memory Storage Adapter
|
|
640
|
+
*
|
|
641
|
+
* Stores data in a Map (in memory).
|
|
642
|
+
* Data is lost on page refresh.
|
|
643
|
+
*/
|
|
644
|
+
declare class MemoryStorageAdapter implements StorageAdapter {
|
|
645
|
+
private store;
|
|
646
|
+
private prefix;
|
|
647
|
+
constructor(options?: MemoryStorageOptions);
|
|
648
|
+
/**
|
|
649
|
+
* Get value by key
|
|
650
|
+
*/
|
|
651
|
+
get(key: string): string | null;
|
|
652
|
+
/**
|
|
653
|
+
* Set value by key
|
|
654
|
+
*/
|
|
655
|
+
set(key: string, value: string): void;
|
|
656
|
+
/**
|
|
657
|
+
* Remove value by key
|
|
658
|
+
*/
|
|
659
|
+
remove(key: string): void;
|
|
660
|
+
/**
|
|
661
|
+
* Clear all values
|
|
662
|
+
*/
|
|
663
|
+
clear(): void;
|
|
664
|
+
}
|
|
665
|
+
/**
|
|
666
|
+
* Create memory storage adapter
|
|
667
|
+
*/
|
|
668
|
+
declare function createMemoryStorage(options?: MemoryStorageOptions): MemoryStorageAdapter;
|
|
669
|
+
|
|
670
|
+
/**
|
|
671
|
+
* Fetch Interceptor Plugin
|
|
672
|
+
*
|
|
673
|
+
* Core plugin for fetch() wrapper with auth header injection and 401 retry.
|
|
674
|
+
* - Injects Authorization header
|
|
675
|
+
* - URL filtering (include/exclude)
|
|
676
|
+
* - 401 retry after token refresh
|
|
677
|
+
*/
|
|
678
|
+
|
|
679
|
+
/**
|
|
680
|
+
* Fetch Interceptor API
|
|
681
|
+
*/
|
|
682
|
+
interface FetchInterceptorAPI {
|
|
683
|
+
createFetch(options?: FetchInterceptorOptions): AuthFetch;
|
|
684
|
+
wrapFetch(fetch: typeof globalThis.fetch): void;
|
|
685
|
+
unwrapFetch(): void;
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* FetchInterceptorPlugin class
|
|
689
|
+
*/
|
|
690
|
+
declare class FetchInterceptorPlugin implements Plugin {
|
|
691
|
+
name: string;
|
|
692
|
+
version: string;
|
|
693
|
+
type: "core";
|
|
694
|
+
private kernel;
|
|
695
|
+
private originalFetch;
|
|
696
|
+
install(kernel: AuthKeeper): FetchInterceptorAPI;
|
|
697
|
+
/**
|
|
698
|
+
* Create authenticated fetch function
|
|
699
|
+
*/
|
|
700
|
+
private createFetch;
|
|
701
|
+
/**
|
|
702
|
+
* Wrap global fetch with auth
|
|
703
|
+
*/
|
|
704
|
+
private wrapFetch;
|
|
705
|
+
/**
|
|
706
|
+
* Restore original fetch
|
|
707
|
+
*/
|
|
708
|
+
private unwrapFetch;
|
|
709
|
+
uninstall(): void;
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Create fetch-interceptor plugin
|
|
713
|
+
*/
|
|
714
|
+
declare function createFetchInterceptorPlugin(): FetchInterceptorPlugin;
|
|
715
|
+
|
|
716
|
+
export { type AuthKeeper as A, createTokenDecoderPlugin as B, createRefreshEnginePlugin as C, createMemoryStorage as D, type EventType as E, type FetchInterceptorOptions as F, createFetchInterceptorPlugin as G, type TokenStoreAPI as H, type TokenStoreOptions as I, type TokenDecoderAPI as J, type KernelOptions as K, type LogoutOptions as L, MemoryStorageAdapter as M, type RefreshEngineAPI as N, type RefreshEngineOptions as O, type Plugin as P, type MemoryStorageOptions as Q, type RefreshEvent as R, type StoredTokens as S, type TokenSet as T, type Unsubscribe as U, type FetchInterceptorAPI as V, type EventHandler as a, type AuthEvent as b, type PluginInfo as c, type TokenPayload as d, type AuthFetch as e, type TokenHeader as f, type TokenInfo as g, type AuthErrorCode as h, type AuthErrorOptions as i, type LoginEvent as j, type LogoutEvent as k, type ExpiredEvent as l, type ErrorEvent as m, type StorageChangeEvent as n, type TabSyncEvent as o, type StorageAdapter as p, type StorageType as q, type LoginOptions as r, type UserInfo as s, type RefreshTokenFn as t, AuthError as u, TokenStorePlugin as v, TokenDecoderPlugin as w, RefreshEnginePlugin as x, FetchInterceptorPlugin as y, createTokenStorePlugin as z };
|