@opensourcekd/ng-common-libs 2.0.8 → 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/README.md +12 -5
- package/dist/index.cjs +362 -268
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +173 -51
- package/dist/index.mjs +353 -266
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -111,6 +111,7 @@ interface Auth0ConfigOptions {
|
|
|
111
111
|
redirectUri?: string;
|
|
112
112
|
logoutUri?: string;
|
|
113
113
|
audience?: string;
|
|
114
|
+
defaultAudience?: string;
|
|
114
115
|
scope?: string;
|
|
115
116
|
connection?: string;
|
|
116
117
|
}
|
|
@@ -127,6 +128,7 @@ declare const AUTH0_CONFIG: {
|
|
|
127
128
|
redirectUri: string;
|
|
128
129
|
logoutUri: string;
|
|
129
130
|
audience: string;
|
|
131
|
+
defaultAudience: string;
|
|
130
132
|
scope: string;
|
|
131
133
|
connection: string | undefined;
|
|
132
134
|
};
|
|
@@ -163,6 +165,7 @@ declare const STORAGE_CONFIG: {
|
|
|
163
165
|
declare const STORAGE_KEYS: {
|
|
164
166
|
ACCESS_TOKEN: string;
|
|
165
167
|
USER_INFO: string;
|
|
168
|
+
DECODED_TOKEN: string;
|
|
166
169
|
};
|
|
167
170
|
/**
|
|
168
171
|
* Reset AUTH0_CONFIG to default values
|
|
@@ -195,6 +198,10 @@ declare function setStorageItem(key: string, value: string, storageType?: 'local
|
|
|
195
198
|
*/
|
|
196
199
|
declare function removeStorageItem(key: string, storageType?: 'localStorage' | 'sessionStorage'): void;
|
|
197
200
|
|
|
201
|
+
/**
|
|
202
|
+
* Type definitions for AuthService
|
|
203
|
+
* Framework-agnostic interfaces for authentication state management
|
|
204
|
+
*/
|
|
198
205
|
/**
|
|
199
206
|
* User information from ID token
|
|
200
207
|
* Standard OIDC claims compatible with Auth0
|
|
@@ -218,6 +225,20 @@ interface UserInfo {
|
|
|
218
225
|
organization?: string;
|
|
219
226
|
[key: string]: unknown;
|
|
220
227
|
}
|
|
228
|
+
/**
|
|
229
|
+
* Decoded access token payload
|
|
230
|
+
* Standard JWT claims plus any custom claims
|
|
231
|
+
*/
|
|
232
|
+
interface TokenPayload {
|
|
233
|
+
iss?: string;
|
|
234
|
+
sub?: string;
|
|
235
|
+
aud?: string | string[];
|
|
236
|
+
exp?: number;
|
|
237
|
+
iat?: number;
|
|
238
|
+
scope?: string;
|
|
239
|
+
permissions?: string[];
|
|
240
|
+
[key: string]: unknown;
|
|
241
|
+
}
|
|
221
242
|
/**
|
|
222
243
|
* Simplified user data extracted from token
|
|
223
244
|
*/
|
|
@@ -263,6 +284,7 @@ interface Auth0Config {
|
|
|
263
284
|
redirectUri: string;
|
|
264
285
|
logoutUri: string;
|
|
265
286
|
audience?: string;
|
|
287
|
+
defaultAudience?: string;
|
|
266
288
|
scope: string;
|
|
267
289
|
connection?: string;
|
|
268
290
|
}
|
|
@@ -285,20 +307,90 @@ interface StorageConfig {
|
|
|
285
307
|
interface StorageKeys {
|
|
286
308
|
ACCESS_TOKEN: string;
|
|
287
309
|
USER_INFO: string;
|
|
310
|
+
DECODED_TOKEN: string;
|
|
288
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
|
+
|
|
289
382
|
/**
|
|
290
383
|
* Pure TypeScript Authentication Service for Auth0 integration
|
|
291
|
-
* Framework-agnostic
|
|
384
|
+
* Framework-agnostic — works with any JavaScript framework (Angular, React, Vue, etc.)
|
|
292
385
|
*
|
|
293
|
-
* Handles login, logout, token management, and user session
|
|
294
|
-
* Uses configurable storage (sessionStorage/localStorage) for sensitive data
|
|
295
|
-
* 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.
|
|
296
389
|
*
|
|
297
390
|
* @example
|
|
298
391
|
* ```typescript
|
|
299
392
|
* import { AuthService, EventBus } from '@opensourcekd/ng-common-libs';
|
|
300
393
|
*
|
|
301
|
-
* // Create instances
|
|
302
394
|
* const eventBus = new EventBus();
|
|
303
395
|
* const authConfig = {
|
|
304
396
|
* domain: 'your-domain.auth0.com',
|
|
@@ -309,23 +401,22 @@ interface StorageKeys {
|
|
|
309
401
|
* };
|
|
310
402
|
* const authService = new AuthService(authConfig, eventBus);
|
|
311
403
|
*
|
|
312
|
-
* //
|
|
404
|
+
* // With an identifier for MFE scenarios
|
|
313
405
|
* const authService = new AuthService(authConfig, eventBus, undefined, undefined, { id: 'MFE' });
|
|
314
406
|
*
|
|
315
|
-
* // Use the service
|
|
316
407
|
* await authService.login();
|
|
317
408
|
* const user = authService.getUser();
|
|
318
409
|
* const token = await authService.getToken();
|
|
319
|
-
*
|
|
320
|
-
* // Get the identifier
|
|
321
410
|
* const id = authService.getId(); // 'MFE' or undefined
|
|
322
411
|
* ```
|
|
323
412
|
*/
|
|
324
413
|
declare class AuthService {
|
|
325
|
-
private readonly STANDARD_JWT_CLAIMS;
|
|
326
414
|
private auth0Client;
|
|
327
415
|
private initializationPromise;
|
|
416
|
+
private callbackHandled;
|
|
417
|
+
private callbackPromise;
|
|
328
418
|
private userSubject;
|
|
419
|
+
/** Observable stream of the currently authenticated user */
|
|
329
420
|
user$: Observable<UserInfo | null>;
|
|
330
421
|
private config;
|
|
331
422
|
private storageConfig;
|
|
@@ -334,118 +425,149 @@ declare class AuthService {
|
|
|
334
425
|
private id?;
|
|
335
426
|
/**
|
|
336
427
|
* Create a new AuthService instance
|
|
337
|
-
* @param config - Auth0 configuration
|
|
428
|
+
* @param config - Auth0 configuration (domain, clientId, redirectUri, etc.)
|
|
338
429
|
* @param eventBus - EventBus instance for emitting auth events
|
|
339
|
-
* @param storageConfig - Storage configuration (
|
|
340
|
-
* @param storageKeys - Storage
|
|
341
|
-
* @param options - Optional
|
|
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
|
|
342
433
|
*/
|
|
343
434
|
constructor(config: Auth0Config, eventBus: EventBus, storageConfig?: StorageConfig, storageKeys?: StorageKeys, options?: AuthServiceOptions);
|
|
344
435
|
/**
|
|
345
436
|
* Get the identifier of this AuthService instance
|
|
346
|
-
* @returns The id
|
|
437
|
+
* @returns The id supplied via options during construction, or `undefined`
|
|
347
438
|
*/
|
|
348
439
|
getId(): string | undefined;
|
|
349
440
|
/**
|
|
350
|
-
*
|
|
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
|
|
443
|
+
*/
|
|
444
|
+
private getEffectiveAudience;
|
|
445
|
+
/**
|
|
446
|
+
* Initialize the Auth0 SPA client
|
|
447
|
+
* @throws {Error} When required config fields (domain, clientId) are missing
|
|
351
448
|
*/
|
|
352
449
|
private initializeAuth0;
|
|
353
450
|
/**
|
|
354
|
-
* 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
|
|
355
454
|
*/
|
|
356
455
|
private ensureInitialized;
|
|
357
456
|
/**
|
|
358
|
-
*
|
|
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.
|
|
462
|
+
*/
|
|
463
|
+
private checkAndHandleCallback;
|
|
464
|
+
/**
|
|
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
|
|
359
469
|
*/
|
|
360
470
|
login(user?: string, options?: {
|
|
361
471
|
invitation?: string;
|
|
362
472
|
organization?: string;
|
|
363
473
|
}): Promise<void>;
|
|
364
474
|
/**
|
|
365
|
-
* 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`
|
|
366
478
|
*/
|
|
367
479
|
handleCallback(): Promise<CallbackResult>;
|
|
368
480
|
/**
|
|
369
|
-
* Log
|
|
370
|
-
*/
|
|
371
|
-
private logUserClaims;
|
|
372
|
-
private logStandardClaims;
|
|
373
|
-
private logClaims;
|
|
374
|
-
private getCustomClaims;
|
|
375
|
-
private getAdditionalClaims;
|
|
376
|
-
private isNamespacedClaim;
|
|
377
|
-
/**
|
|
378
|
-
* Logout user and clear authentication state
|
|
481
|
+
* Log the user out, clear all stored auth data, and redirect to the logout URI
|
|
379
482
|
*/
|
|
380
483
|
logout(): Promise<void>;
|
|
381
484
|
/**
|
|
382
|
-
* 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
|
|
383
488
|
*/
|
|
384
489
|
getToken(): Promise<string | null>;
|
|
385
490
|
/**
|
|
386
|
-
* 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
|
|
387
493
|
*/
|
|
388
494
|
getTokenSync(): string | null;
|
|
389
495
|
/**
|
|
390
|
-
*
|
|
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
|
|
391
502
|
*/
|
|
392
|
-
|
|
503
|
+
getDecodedToken(): TokenPayload | null;
|
|
393
504
|
/**
|
|
394
|
-
* Check
|
|
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
|
|
395
507
|
*/
|
|
396
508
|
isAuthenticated(): Promise<boolean>;
|
|
397
509
|
/**
|
|
398
|
-
* Check
|
|
510
|
+
* Check whether the user is authenticated synchronously based on stored token presence
|
|
511
|
+
* @returns `true` when an access token exists in storage
|
|
399
512
|
*/
|
|
400
513
|
isAuthenticatedSync(): boolean;
|
|
401
514
|
/**
|
|
402
|
-
* Get current user
|
|
515
|
+
* Get the current authenticated user's info
|
|
516
|
+
* @returns {@link UserInfo} object or `null` if not authenticated
|
|
403
517
|
*/
|
|
404
518
|
getUser(): UserInfo | null;
|
|
405
519
|
/**
|
|
406
|
-
* Get simplified user data
|
|
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
|
|
407
522
|
*/
|
|
408
523
|
getUserData(): UserData | null;
|
|
409
|
-
private extractClaimValue;
|
|
410
524
|
/**
|
|
411
|
-
*
|
|
525
|
+
* Read and parse user info from storage on initialization
|
|
526
|
+
* @returns Stored {@link UserInfo} or `null` if absent
|
|
412
527
|
*/
|
|
413
528
|
private getUserInfoFromStorage;
|
|
414
529
|
/**
|
|
415
|
-
*
|
|
530
|
+
* Persist user info to storage and update the user observable
|
|
531
|
+
* @param userInfo - The {@link UserInfo} object to store
|
|
416
532
|
*/
|
|
417
533
|
private setUserInfo;
|
|
418
534
|
/**
|
|
419
|
-
*
|
|
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`
|
|
420
543
|
*/
|
|
421
544
|
private emitAuthEvent;
|
|
422
545
|
/**
|
|
423
|
-
*
|
|
424
|
-
*
|
|
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.
|
|
425
549
|
*/
|
|
426
550
|
private cleanupCallbackUrl;
|
|
427
551
|
}
|
|
428
552
|
/**
|
|
429
|
-
* Create AuthService instance
|
|
430
|
-
* 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}
|
|
431
554
|
*
|
|
432
|
-
* Note:
|
|
555
|
+
* Note: Call {@link configureAuth0} before using this helper so that `AUTH0_CONFIG`
|
|
556
|
+
* is populated with the correct values for your environment.
|
|
433
557
|
*
|
|
434
558
|
* @param eventBus - EventBus instance for auth events
|
|
435
|
-
* @returns
|
|
559
|
+
* @returns Fully configured {@link AuthService} instance
|
|
436
560
|
*
|
|
437
561
|
* @example
|
|
438
562
|
* ```typescript
|
|
439
563
|
* import { createAuthService, EventBus, configureAuth0, APP_CONFIG } from '@opensourcekd/ng-common-libs';
|
|
440
564
|
*
|
|
441
|
-
* // Configure Auth0 first
|
|
442
565
|
* configureAuth0({
|
|
443
566
|
* domain: APP_CONFIG.auth0Domain,
|
|
444
567
|
* clientId: APP_CONFIG.auth0ClientId,
|
|
445
568
|
* audience: APP_CONFIG.apiUrl,
|
|
446
569
|
* });
|
|
447
570
|
*
|
|
448
|
-
* // Create instances
|
|
449
571
|
* const eventBus = new EventBus();
|
|
450
572
|
* const authService = createAuthService(eventBus);
|
|
451
573
|
* ```
|
|
@@ -651,5 +773,5 @@ declare class Logger {
|
|
|
651
773
|
}): void;
|
|
652
774
|
}
|
|
653
775
|
|
|
654
|
-
export { APP_CONFIG, AUTH0_CONFIG, AuthService, EventBus, LogSeverity, Logger, STORAGE_CONFIG, STORAGE_KEYS, configureAuth0, createAuthService, getStorageItem, removeStorageItem, resetAuth0Config, setStorageItem };
|
|
655
|
-
export type { AppState, Auth0Config, Auth0ConfigOptions, AuthServiceOptions, AuthorizationParams, CallbackResult, EventBusOptions, EventPayload, LogAttributes, LogRecord, LoggerOptions, StorageConfig, StorageKeys, UserData, UserInfo };
|
|
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 };
|
|
777
|
+
export type { AppState, Auth0Config, Auth0ConfigOptions, AuthServiceOptions, AuthorizationParams, CallbackResult, EventBusOptions, EventPayload, LogAttributes, LogRecord, LoggerOptions, StorageConfig, StorageKeys, TokenPayload, UserData, UserInfo };
|