@oxog/authkeeper 1.0.0 → 1.2.1

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.
@@ -164,7 +164,7 @@ interface ExpiredEvent {
164
164
  interface ErrorEvent {
165
165
  type: 'error';
166
166
  error: AuthError;
167
- context: 'refresh' | 'storage' | 'decode' | 'network';
167
+ context: 'refresh' | 'storage' | 'decode' | 'network' | 'plugin';
168
168
  timestamp: number;
169
169
  }
170
170
  /**
@@ -231,7 +231,7 @@ interface Plugin {
231
231
  install(kernel: AuthKeeper): any;
232
232
  /** Uninstall plugin and cleanup */
233
233
  uninstall?(): void | Promise<void>;
234
- /** Optional lifecycle hooks */
234
+ /** Reserved lifecycle hooks; not invoked by the kernel yet */
235
235
  hooks?: {
236
236
  /** Called before tokens are set */
237
237
  beforeSetTokens?: (tokens: TokenSet) => TokenSet | false;
@@ -274,15 +274,22 @@ type RefreshTokenFn = (refreshToken: string) => Promise<TokenSet>;
274
274
  * Kernel configuration options
275
275
  */
276
276
  interface KernelOptions {
277
- /** Storage adapter or type */
278
- storage: StorageType | StorageAdapter;
277
+ /**
278
+ * Where tokens are persisted (default: 'memory', i.e. not persisted).
279
+ * 'localStorage' / 'sessionStorage' / 'cookie' fall back to memory where
280
+ * unavailable. Persistent storage is readable by any script on the page.
281
+ */
282
+ storage?: StorageType | StorageAdapter;
279
283
  /** Function to refresh tokens */
280
284
  refreshToken: RefreshTokenFn;
281
285
  /** Enable automatic token refresh (default: true) */
282
286
  autoRefresh?: boolean;
283
287
  /** Seconds before expiry to refresh (default: 60) */
284
288
  refreshThreshold?: number;
285
- /** Enable multi-tab sync (default: true) */
289
+ /**
290
+ * Reserved for multi-tab sync; not implemented yet (currently a no-op).
291
+ * `getOptions().syncTabs` always reports `false`.
292
+ */
286
293
  syncTabs?: boolean;
287
294
  /** Error handler */
288
295
  onError?: (error: AuthError) => void;
@@ -293,7 +300,7 @@ interface KernelOptions {
293
300
  * Logout options
294
301
  */
295
302
  interface LogoutOptions {
296
- /** Logout from all tabs */
303
+ /** Reserved for multi-tab logout; not implemented yet (currently a no-op) */
297
304
  allTabs?: boolean;
298
305
  /** Redirect URL after logout */
299
306
  redirect?: string;
@@ -384,10 +391,13 @@ interface AuthKeeper {
384
391
  createFetch(options?: FetchInterceptorOptions): AuthFetch;
385
392
  /** Wrap global fetch with auth */
386
393
  wrapFetch(fetch: typeof globalThis.fetch): void;
387
- /** Register a plugin */
388
- register(plugin: Plugin): void;
394
+ /**
395
+ * Register and install a plugin. Rejects if installation fails; the failure
396
+ * is also emitted as an 'error' event with context 'plugin'.
397
+ */
398
+ register(plugin: Plugin): Promise<void>;
389
399
  /** Unregister a plugin */
390
- unregister(pluginName: string): void;
400
+ unregister(pluginName: string): Promise<void>;
391
401
  /** Get plugin by name */
392
402
  getPlugin<P extends Plugin>(name: string): P | undefined;
393
403
  /** List all plugins */
@@ -435,7 +445,15 @@ interface TokenStoreAPI {
435
445
  * Token Store Plugin Options
436
446
  */
437
447
  interface TokenStoreOptions {
448
+ /** Reject access tokens that are not structurally valid JWTs (default: false) */
438
449
  validateOnSet?: boolean;
450
+ /**
451
+ * Persist tokens in this adapter (default: none, tokens live only in memory).
452
+ * Tokens are restored from it when the plugin is installed.
453
+ */
454
+ storage?: StorageAdapter;
455
+ /** Key used in `storage` (default: 'tokens') */
456
+ storageKey?: string;
439
457
  onSet?: (tokens: StoredTokens) => void;
440
458
  onClear?: () => void;
441
459
  }
@@ -448,8 +466,9 @@ declare class TokenStorePlugin implements Plugin {
448
466
  type: "core";
449
467
  private tokens;
450
468
  private options;
469
+ private kernel;
451
470
  constructor(options?: TokenStoreOptions);
452
- install(_kernel: AuthKeeper): TokenStoreAPI;
471
+ install(kernel: AuthKeeper): TokenStoreAPI;
453
472
  /**
454
473
  * Set tokens
455
474
  */
@@ -464,8 +483,24 @@ declare class TokenStorePlugin implements Plugin {
464
483
  private clear;
465
484
  /**
466
485
  * Uninstall plugin
486
+ *
487
+ * Drops the in-memory copy only: destroying an instance (e.g. on component
488
+ * unmount) must not log the user out of the persisted session.
467
489
  */
468
490
  uninstall(): void;
491
+ private get storageKey();
492
+ /**
493
+ * Write tokens to the storage adapter (null removes them).
494
+ * Failures (quota exceeded, storage disabled, ...) are reported as an
495
+ * 'error' event with context 'storage' instead of breaking setTokens().
496
+ */
497
+ private persist;
498
+ /**
499
+ * Load tokens from the storage adapter. Corrupt or malformed entries are
500
+ * removed rather than trusted.
501
+ */
502
+ private restore;
503
+ private reportStorageError;
469
504
  }
470
505
  /**
471
506
  * Create token-store plugin
@@ -552,6 +587,8 @@ interface RefreshEngineAPI {
552
587
  scheduleRefresh(): void;
553
588
  cancelScheduledRefresh(): void;
554
589
  isRefreshing(): boolean;
590
+ /** True only while a refresh result is being stored via kernel.setTokens() */
591
+ isApplyingRefresh?(): boolean;
555
592
  getNextRefreshAt(): Date | null;
556
593
  setRefreshFn(fn: RefreshTokenFn): void;
557
594
  setThreshold(seconds: number): void;
@@ -578,6 +615,7 @@ declare class RefreshEnginePlugin implements Plugin {
578
615
  private kernel;
579
616
  private refreshFn;
580
617
  private isRefreshingFlag;
618
+ private applyingRefresh;
581
619
  private refreshPromise;
582
620
  private scheduledRefreshTimeout;
583
621
  private threshold;
@@ -602,6 +640,10 @@ declare class RefreshEnginePlugin implements Plugin {
602
640
  * Schedules refresh before token expires (threshold seconds before).
603
641
  */
604
642
  private scheduleRefresh;
643
+ /**
644
+ * Whether the kernel has automatic refresh enabled (default: true)
645
+ */
646
+ private isAutoRefreshEnabled;
605
647
  /**
606
648
  * Cancel scheduled refresh
607
649
  */
@@ -667,6 +709,138 @@ declare class MemoryStorageAdapter implements StorageAdapter {
667
709
  */
668
710
  declare function createMemoryStorage(options?: MemoryStorageOptions): MemoryStorageAdapter;
669
711
 
712
+ /**
713
+ * Cookie utilities
714
+ *
715
+ * Helper functions for parsing and serializing cookies.
716
+ */
717
+ /**
718
+ * Cookie attributes for serialization
719
+ */
720
+ interface CookieAttributes {
721
+ /** Path where cookie is valid (default: '/') */
722
+ path?: string;
723
+ /** Domain where cookie is valid */
724
+ domain?: string;
725
+ /** Max age in seconds */
726
+ maxAge?: number;
727
+ /** Expiration date */
728
+ expires?: Date;
729
+ /** Secure flag (HTTPS only) */
730
+ secure?: boolean;
731
+ /** SameSite attribute */
732
+ sameSite?: 'strict' | 'lax' | 'none';
733
+ /** HttpOnly flag (not accessible via JavaScript) */
734
+ httpOnly?: boolean;
735
+ }
736
+ /**
737
+ * Parse document.cookie string into key-value pairs
738
+ *
739
+ * @param cookieString - Cookie string from document.cookie
740
+ * @returns Object with cookie key-value pairs
741
+ */
742
+ declare function parseCookies(cookieString: string): Record<string, string>;
743
+ /**
744
+ * Get cookie value by name
745
+ *
746
+ * @param name - Cookie name
747
+ * @returns Cookie value or null if not found
748
+ */
749
+ declare function getCookie(name: string): string | null;
750
+ /**
751
+ * Serialize cookie with attributes to Set-Cookie format
752
+ *
753
+ * @param name - Cookie name
754
+ * @param value - Cookie value
755
+ * @param attributes - Cookie attributes
756
+ * @returns Set-Cookie header value
757
+ */
758
+ declare function serializeCookie(name: string, value: string, attributes?: CookieAttributes): string;
759
+ /**
760
+ * Set cookie in document
761
+ *
762
+ * @param name - Cookie name
763
+ * @param value - Cookie value
764
+ * @param attributes - Cookie attributes
765
+ */
766
+ declare function setCookie(name: string, value: string, attributes?: CookieAttributes): void;
767
+ /**
768
+ * Delete cookie by name
769
+ *
770
+ * @param name - Cookie name
771
+ * @param attributes - Cookie attributes (path and domain should match original)
772
+ */
773
+ declare function deleteCookie(name: string, attributes?: Omit<CookieAttributes, 'maxAge' | 'expires'>): void;
774
+
775
+ /**
776
+ * Browser Storage Adapters
777
+ *
778
+ * StorageAdapter implementations backed by Web Storage (localStorage /
779
+ * sessionStorage) and document.cookie.
780
+ *
781
+ * Security note: anything stored here is readable by any script running on
782
+ * the page (XSS). In-memory storage (the default) is the safest choice; use
783
+ * these only when tokens must survive a page reload.
784
+ */
785
+
786
+ /**
787
+ * Web Storage adapter options
788
+ */
789
+ interface WebStorageOptions {
790
+ /** Key prefix (default: 'authkeeper:') */
791
+ prefix?: string;
792
+ }
793
+ /**
794
+ * StorageAdapter backed by a Web Storage object (localStorage / sessionStorage)
795
+ */
796
+ declare class WebStorageAdapter implements StorageAdapter {
797
+ private storage;
798
+ private prefix;
799
+ constructor(storage: Storage, options?: WebStorageOptions);
800
+ get(key: string): string | null;
801
+ set(key: string, value: string): void;
802
+ remove(key: string): void;
803
+ clear(): void;
804
+ }
805
+ /**
806
+ * Cookie adapter options
807
+ */
808
+ interface CookieStorageOptions {
809
+ /** Cookie name prefix (default: 'authkeeper_') */
810
+ prefix?: string;
811
+ /**
812
+ * Cookie attributes. Defaults: `path: '/'`, `secure: true`,
813
+ * `sameSite: 'strict'`. HttpOnly cannot be set from JavaScript.
814
+ */
815
+ attributes?: Omit<CookieAttributes, 'httpOnly'>;
816
+ }
817
+ /**
818
+ * StorageAdapter backed by document.cookie
819
+ */
820
+ declare class CookieStorageAdapter implements StorageAdapter {
821
+ private prefix;
822
+ private attributes;
823
+ private keys;
824
+ constructor(options?: CookieStorageOptions);
825
+ get(key: string): string | null;
826
+ set(key: string, value: string): void;
827
+ remove(key: string): void;
828
+ clear(): void;
829
+ }
830
+ /**
831
+ * Resolve the `storage` kernel option to a StorageAdapter.
832
+ *
833
+ * - `undefined` / `'memory'`: no persistence (returns `undefined`); tokens
834
+ * live only in the token store's memory.
835
+ * - `'localStorage'` / `'sessionStorage'`: Web Storage, falling back to an
836
+ * in-memory adapter where it is unavailable (SSR, privacy mode).
837
+ * - `'cookie'`: document.cookie, falling back to memory outside browsers.
838
+ * - a StorageAdapter object: used as-is.
839
+ *
840
+ * @throws {AuthError} CONFIG_ERROR for an unknown storage type
841
+ */
842
+ declare function resolveStorage(storage: StorageType | StorageAdapter | undefined): StorageAdapter | undefined;
843
+
670
844
  /**
671
845
  * Fetch Interceptor Plugin
672
846
  *
@@ -693,6 +867,14 @@ declare class FetchInterceptorPlugin implements Plugin {
693
867
  type: "core";
694
868
  private kernel;
695
869
  private originalFetch;
870
+ /**
871
+ * The fetch implementation used to perform requests.
872
+ *
873
+ * While the global fetch is wrapped, this is the original (unwrapped)
874
+ * fetch; calling `globalThis.fetch` from inside the wrapper would recurse
875
+ * into the wrapper itself.
876
+ */
877
+ private baseFetch;
696
878
  install(kernel: AuthKeeper): FetchInterceptorAPI;
697
879
  /**
698
880
  * Create authenticated fetch function
@@ -713,4 +895,4 @@ declare class FetchInterceptorPlugin implements Plugin {
713
895
  */
714
896
  declare function createFetchInterceptorPlugin(): FetchInterceptorPlugin;
715
897
 
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 };
898
+ export { getCookie as $, type AuthKeeper as A, createTokenDecoderPlugin as B, CookieStorageAdapter as C, createRefreshEnginePlugin as D, type EventType as E, type FetchInterceptorOptions as F, createMemoryStorage as G, createFetchInterceptorPlugin as H, type TokenStoreAPI as I, type TokenStoreOptions as J, type KernelOptions as K, type LogoutOptions as L, MemoryStorageAdapter as M, type TokenDecoderAPI as N, type RefreshEngineAPI as O, type Plugin as P, type RefreshEngineOptions as Q, type RefreshEvent as R, type StorageAdapter as S, type TokenSet as T, type Unsubscribe as U, type MemoryStorageOptions as V, WebStorageAdapter as W, type WebStorageOptions as X, type CookieStorageOptions as Y, type FetchInterceptorAPI as Z, parseCookies as _, type EventHandler as a, serializeCookie as a0, setCookie as a1, deleteCookie as a2, type CookieAttributes as a3, resolveStorage as a4, 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 StoredTokens as h, type AuthErrorCode as i, type AuthErrorOptions as j, type LoginEvent as k, type LogoutEvent as l, type ExpiredEvent as m, type ErrorEvent as n, type StorageChangeEvent as o, type TabSyncEvent 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 };