@opensourcekd/ng-common-libs 2.0.9 → 2.0.10

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/dist/index.d.ts CHANGED
@@ -198,6 +198,10 @@ declare function setStorageItem(key: string, value: string, storageType?: 'local
198
198
  */
199
199
  declare function removeStorageItem(key: string, storageType?: 'localStorage' | 'sessionStorage'): void;
200
200
 
201
+ /**
202
+ * Type definitions for AuthService
203
+ * Framework-agnostic interfaces for authentication state management
204
+ */
201
205
  /**
202
206
  * User information from ID token
203
207
  * Standard OIDC claims compatible with Auth0
@@ -305,19 +309,88 @@ interface StorageKeys {
305
309
  USER_INFO: string;
306
310
  DECODED_TOKEN: string;
307
311
  }
312
+
313
+ /**
314
+ * Decode a JWT access token and store its payload in storage
315
+ *
316
+ * Note: This only decodes the JWT structure without verifying the signature.
317
+ * The token signature is already validated by the Auth0 SDK when obtained.
318
+ * The stored payload is for informational use only (e.g. checking expiration,
319
+ * reading scopes). Do NOT use it for authorization decisions — always validate
320
+ * on the backend.
321
+ *
322
+ * @param token - Raw JWT access token string
323
+ * @param storageKeys - Storage key names configuration
324
+ * @param storageConfig - Storage type (localStorage / sessionStorage) configuration
325
+ */
326
+ declare function decodeAndStoreToken(token: string, storageKeys: StorageKeys, storageConfig: StorageConfig): void;
327
+ /**
328
+ * Retrieve and parse the decoded token payload from storage
329
+ *
330
+ * Note: This data is for informational purposes only (e.g. checking expiration,
331
+ * viewing scopes). Do NOT use it for authorization decisions — always validate
332
+ * permissions on the backend.
333
+ *
334
+ * @param storageKeys - Storage key names configuration
335
+ * @param storageConfig - Storage type (localStorage / sessionStorage) configuration
336
+ * @returns Decoded {@link TokenPayload} or `null` if not present or unparseable
337
+ */
338
+ declare function getDecodedToken(storageKeys: StorageKeys, storageConfig: StorageConfig): TokenPayload | null;
339
+
340
+ /**
341
+ * User data extraction utilities
342
+ * Pure functions for parsing user claims and constructing user data objects
343
+ */
344
+
345
+ /**
346
+ * Standard OIDC and JWT claims excluded from custom/namespaced claim detection
347
+ */
348
+ declare const STANDARD_JWT_CLAIMS: readonly string[];
349
+ /**
350
+ * Determine whether a claim key is a namespaced Auth0 custom claim
351
+ * @param key - Claim key to inspect
352
+ * @returns `true` if the key starts with `http://` or `https://`
353
+ */
354
+ declare function isNamespacedClaim(key: string): boolean;
355
+ /**
356
+ * Extract namespaced (Auth0 custom) claim keys from a user info object
357
+ * @param user - {@link UserInfo} object to inspect
358
+ * @returns Array of claim keys that are URL-namespaced
359
+ */
360
+ declare function getCustomClaims(user: UserInfo): string[];
361
+ /**
362
+ * Resolve the first matching primitive claim value from a {@link UserInfo} object
363
+ *
364
+ * Checks direct claims first, then falls back to namespaced (Auth0 custom) claims
365
+ * whose keys contain the requested name as a substring.
366
+ *
367
+ * @param userInfo - The user info object to search
368
+ * @param claimNames - Single claim name or ordered array of names to check
369
+ * @param defaultValue - Value to return when no matching claim is found
370
+ * @returns Resolved string value or `defaultValue`
371
+ * @example
372
+ * const role = extractClaimValue(userInfo, ['role', 'user_role'], 'user');
373
+ */
374
+ declare function extractClaimValue(userInfo: UserInfo, claimNames: string | string[], defaultValue: string): string;
375
+ /**
376
+ * Build a simplified {@link UserData} object from a full {@link UserInfo} object
377
+ * @param userInfo - Full user info from the ID token
378
+ * @returns Simplified user data with id, name, email, role, and org
379
+ */
380
+ declare function buildUserData(userInfo: UserInfo): UserData;
381
+
308
382
  /**
309
383
  * Pure TypeScript Authentication Service for Auth0 integration
310
- * Framework-agnostic - works with any JavaScript framework (Angular, React, Vue, etc.)
384
+ * Framework-agnostic works with any JavaScript framework (Angular, React, Vue, etc.)
311
385
  *
312
- * Handles login, logout, token management, and user session
313
- * Uses configurable storage (sessionStorage/localStorage) for sensitive data
314
- * Emits authentication events via EventBus for cross-application communication
386
+ * Handles login, logout, token management, and user session.
387
+ * Uses configurable storage (sessionStorage/localStorage) for sensitive data.
388
+ * Emits authentication events via EventBus for cross-application communication.
315
389
  *
316
390
  * @example
317
391
  * ```typescript
318
392
  * import { AuthService, EventBus } from '@opensourcekd/ng-common-libs';
319
393
  *
320
- * // Create instances
321
394
  * const eventBus = new EventBus();
322
395
  * const authConfig = {
323
396
  * domain: 'your-domain.auth0.com',
@@ -328,25 +401,22 @@ interface StorageKeys {
328
401
  * };
329
402
  * const authService = new AuthService(authConfig, eventBus);
330
403
  *
331
- * // Or create with an identifier
404
+ * // With an identifier for MFE scenarios
332
405
  * const authService = new AuthService(authConfig, eventBus, undefined, undefined, { id: 'MFE' });
333
406
  *
334
- * // Use the service
335
407
  * await authService.login();
336
408
  * const user = authService.getUser();
337
409
  * const token = await authService.getToken();
338
- *
339
- * // Get the identifier
340
410
  * const id = authService.getId(); // 'MFE' or undefined
341
411
  * ```
342
412
  */
343
413
  declare class AuthService {
344
- private readonly STANDARD_JWT_CLAIMS;
345
414
  private auth0Client;
346
415
  private initializationPromise;
347
416
  private callbackHandled;
348
417
  private callbackPromise;
349
418
  private userSubject;
419
+ /** Observable stream of the currently authenticated user */
350
420
  user$: Observable<UserInfo | null>;
351
421
  private config;
352
422
  private storageConfig;
@@ -355,145 +425,149 @@ declare class AuthService {
355
425
  private id?;
356
426
  /**
357
427
  * Create a new AuthService instance
358
- * @param config - Auth0 configuration
428
+ * @param config - Auth0 configuration (domain, clientId, redirectUri, etc.)
359
429
  * @param eventBus - EventBus instance for emitting auth events
360
- * @param storageConfig - Storage configuration (optional, defaults to sessionStorage)
361
- * @param storageKeys - Storage keys (optional, defaults to standard keys)
362
- * @param options - Optional configuration with an id to identify the instance
430
+ * @param storageConfig - Storage configuration (defaults to sessionStorage for both token and user)
431
+ * @param storageKeys - Storage key names (defaults to standard auth0_* keys)
432
+ * @param options - Optional settings; supply `id` to label this instance in MFE scenarios
363
433
  */
364
434
  constructor(config: Auth0Config, eventBus: EventBus, storageConfig?: StorageConfig, storageKeys?: StorageKeys, options?: AuthServiceOptions);
365
435
  /**
366
436
  * Get the identifier of this AuthService instance
367
- * @returns The id if provided during initialization, undefined otherwise
437
+ * @returns The id supplied via options during construction, or `undefined`
368
438
  */
369
439
  getId(): string | undefined;
370
440
  /**
371
- * Get effective audience value (with fallback to defaultAudience)
372
- * @private
441
+ * Resolve the effective audience value, falling back to defaultAudience when audience is unset
442
+ * @returns The audience string, or `undefined` if neither field is set
373
443
  */
374
444
  private getEffectiveAudience;
375
445
  /**
376
- * Initialize Auth0 client
446
+ * Initialize the Auth0 SPA client
447
+ * @throws {Error} When required config fields (domain, clientId) are missing
377
448
  */
378
449
  private initializeAuth0;
379
450
  /**
380
- * Ensure Auth0 client is initialized before use
451
+ * Ensure the Auth0 client is initialized before use
452
+ * Lazy-initializes on the first call and auto-handles OAuth callbacks when detected
453
+ * @throws {Error} When the Auth0 client fails to initialize
381
454
  */
382
455
  private ensureInitialized;
383
456
  /**
384
- * Check for OAuth callback parameters in URL and auto-handle if present
385
- * Note: The Auth0 SDK's handleRedirectCallback() validates the state parameter
386
- * to prevent CSRF attacks. This method only checks for the presence of callback
387
- * parameters before delegating to the Auth0 SDK for secure processing.
457
+ * Check for OAuth callback parameters in the URL and auto-handle them
458
+ *
459
+ * The Auth0 SDK's `handleRedirectCallback` validates the `state` parameter
460
+ * to prevent CSRF attacks. This method only detects presence of callback
461
+ * params before delegating securely to the SDK.
388
462
  */
389
463
  private checkAndHandleCallback;
390
464
  /**
391
- * Login with Auth0
465
+ * Redirect the user to Auth0 Universal Login
466
+ * @param user - Optional username hint (for logging/debugging only)
467
+ * @param options - Optional invitation or organization parameters
468
+ * @throws {Error} When the Auth0 redirect fails
392
469
  */
393
470
  login(user?: string, options?: {
394
471
  invitation?: string;
395
472
  organization?: string;
396
473
  }): Promise<void>;
397
474
  /**
398
- * Handle OAuth2 callback after successful authorization
475
+ * Handle the OAuth2 redirect callback after successful authorization
476
+ * Stores the user info and access token, then cleans up the callback URL
477
+ * @returns {@link CallbackResult} with `success` flag and optional `appState`
399
478
  */
400
479
  handleCallback(): Promise<CallbackResult>;
401
480
  /**
402
- * Log all user claims for debugging
403
- */
404
- private logUserClaims;
405
- private logStandardClaims;
406
- private logClaims;
407
- private getCustomClaims;
408
- private getAdditionalClaims;
409
- private isNamespacedClaim;
410
- /**
411
- * Logout user and clear authentication state
481
+ * Log the user out, clear all stored auth data, and redirect to the logout URI
412
482
  */
413
483
  logout(): Promise<void>;
414
484
  /**
415
- * Get current access token
485
+ * Get the current access token asynchronously
486
+ * Returns from storage first; falls back to a silent Auth0 token refresh
487
+ * @returns The access token string, or `null` on failure
416
488
  */
417
489
  getToken(): Promise<string | null>;
418
490
  /**
419
- * Get current access token synchronously from storage only
491
+ * Get the current access token synchronously from storage only
492
+ * @returns The stored token string, or `null` if not present
420
493
  */
421
494
  getTokenSync(): string | null;
422
495
  /**
423
- * Set access token in storage and emit event
424
- */
425
- private setToken;
426
- /**
427
- * Decode JWT token and store its payload
428
- * Note: This only decodes the JWT structure without verifying the signature.
429
- * The token signature is already validated by Auth0 SDK when obtained.
430
- * This decoded data is for informational purposes (e.g., checking expiration, viewing scopes).
431
- * Do NOT use decoded token data for authorization decisions - always validate on the backend.
432
- */
433
- private decodeAndStoreToken;
434
- /**
435
- * Get decoded token payload from storage
436
- * Note: This data is for informational purposes only (checking expiration, viewing scopes, etc.).
437
- * Do NOT use this for authorization decisions - always validate permissions on the backend.
438
- * The token signature is validated by Auth0 SDK when the token is obtained.
496
+ * Get the decoded access token payload from storage
497
+ *
498
+ * Note: For informational use only (checking expiration, viewing scopes, etc.).
499
+ * Do NOT use for authorization decisions — always validate on the backend.
500
+ *
501
+ * @returns Decoded {@link TokenPayload} or `null` if not present
439
502
  */
440
503
  getDecodedToken(): TokenPayload | null;
441
504
  /**
442
- * Check if user is authenticated
505
+ * Check whether the user is authenticated via the Auth0 SDK
506
+ * @returns `true` if the Auth0 session is valid; falls back to storage check on error
443
507
  */
444
508
  isAuthenticated(): Promise<boolean>;
445
509
  /**
446
- * Check if user is authenticated synchronously
510
+ * Check whether the user is authenticated synchronously based on stored token presence
511
+ * @returns `true` when an access token exists in storage
447
512
  */
448
513
  isAuthenticatedSync(): boolean;
449
514
  /**
450
- * Get current user information
515
+ * Get the current authenticated user's info
516
+ * @returns {@link UserInfo} object or `null` if not authenticated
451
517
  */
452
518
  getUser(): UserInfo | null;
453
519
  /**
454
- * Get simplified user data from token
520
+ * Get a simplified view of the current user's data
521
+ * @returns {@link UserData} with id, name, email, role, and org — or `null` if not authenticated
455
522
  */
456
523
  getUserData(): UserData | null;
457
- private extractClaimValue;
458
524
  /**
459
- * Get user information from storage
525
+ * Read and parse user info from storage on initialization
526
+ * @returns Stored {@link UserInfo} or `null` if absent
460
527
  */
461
528
  private getUserInfoFromStorage;
462
529
  /**
463
- * Set user information in storage and update observable
530
+ * Persist user info to storage and update the user observable
531
+ * @param userInfo - The {@link UserInfo} object to store
464
532
  */
465
533
  private setUserInfo;
466
534
  /**
467
- * Emit authentication event for cross-application communication
535
+ * Persist the access token to storage, decode and cache its payload
536
+ * @param token - Raw JWT access token string
537
+ */
538
+ private setToken;
539
+ /**
540
+ * Emit an authentication event via the shared EventBus
541
+ * @param eventType - Short event type suffix (e.g. `'login_success'`, `'logout'`)
542
+ * @param payload - Arbitrary metadata payload, or `null`
468
543
  */
469
544
  private emitAuthEvent;
470
545
  /**
471
- * Clean up OAuth callback parameters from URL after successful authentication
472
- * Removes 'code' and 'state' parameters while preserving other query parameters
546
+ * Remove OAuth callback parameters (`code`, `state`) from the browser URL
547
+ * while preserving all other query parameters and the hash fragment.
548
+ * Uses `history.replaceState` to avoid adding an entry to browser history.
473
549
  */
474
550
  private cleanupCallbackUrl;
475
551
  }
476
552
  /**
477
- * Create AuthService instance using AUTH0_CONFIG
478
- * Helper function for creating AuthService with default configuration from AUTH0_CONFIG
553
+ * Create an {@link AuthService} instance pre-configured from the shared {@link AUTH0_CONFIG}
479
554
  *
480
- * Note: Make sure to call configureAuth0() before using this helper
555
+ * Note: Call {@link configureAuth0} before using this helper so that `AUTH0_CONFIG`
556
+ * is populated with the correct values for your environment.
481
557
  *
482
558
  * @param eventBus - EventBus instance for auth events
483
- * @returns Configured AuthService instance
559
+ * @returns Fully configured {@link AuthService} instance
484
560
  *
485
561
  * @example
486
562
  * ```typescript
487
563
  * import { createAuthService, EventBus, configureAuth0, APP_CONFIG } from '@opensourcekd/ng-common-libs';
488
564
  *
489
- * // Configure Auth0 first
490
565
  * configureAuth0({
491
566
  * domain: APP_CONFIG.auth0Domain,
492
567
  * clientId: APP_CONFIG.auth0ClientId,
493
568
  * audience: APP_CONFIG.apiUrl,
494
569
  * });
495
570
  *
496
- * // Create instances
497
571
  * const eventBus = new EventBus();
498
572
  * const authService = createAuthService(eventBus);
499
573
  * ```
@@ -699,5 +773,5 @@ declare class Logger {
699
773
  }): void;
700
774
  }
701
775
 
702
- export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, LogSeverity, Logger, STORAGE_CONFIG, STORAGE_KEYS, configureAuth0, createAuthService, getStorageItem, removeStorageItem, resetAuth0Config, setStorageItem };
776
+ export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, LogSeverity, Logger, STANDARD_JWT_CLAIMS, STORAGE_CONFIG, STORAGE_KEYS, buildUserData, configureAuth0, createAuthService, decodeAndStoreToken, extractClaimValue, getCustomClaims, getDecodedToken, getStorageItem, isNamespacedClaim, removeStorageItem, resetAuth0Config, setStorageItem };
703
777
  export type { AppState, Auth0Config, Auth0ConfigOptions, AuthServiceOptions, AuthorizationParams, CallbackResult, EventBusOptions, EventPayload, LogAttributes, LogRecord, LoggerOptions, StorageConfig, StorageKeys, TokenPayload, UserData, UserInfo };