Skip to content

Commit 0d78fb0

Browse files
committed
feat(auth): add TokenManager for automated auth token lifecycle management
Introduces TokenManager and supporting classes to handle token acquisition, automatic refresh, and updates via identity providers. This foundation enables consistent authentication token management across different identity provider implementations. Key additions: - Add TokenManager to obtain and maintain auth tokens from identity providers with automated refresh scheduling based on TTL and configurable thresholds - Add IdentityProvider interface for token acquisition from auth providers - Implement Token class for managing token state and TTL tracking - Include configurable retry mechanism with exponential backoff and jitter - Add comprehensive test suite covering refresh cycles and error handling - Add Clock abstraction and FakeClock for testing timing scenarios This change establishes the core infrastructure needed for reliable token lifecycle management across different authentication providers.
1 parent a0c324b commit 0d78fb0

File tree

9 files changed

+971
-7
lines changed

9 files changed

+971
-7
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* A clock that provides the current time in milliseconds since the epoch.
3+
*/
4+
export interface Clock {
5+
// returns milliseconds since epoch
6+
now(): number;
7+
}
8+
9+
/**
10+
* A clock that uses the system time ( Date.now() ) to provide the current time.
11+
*/
12+
export class SystemClock implements Clock {
13+
now(): number {
14+
return Date.now();
15+
}
16+
}
17+
18+
/**
19+
* A fake clock that allows the time to be manually advanced.
20+
*/
21+
export class FakeClock implements Clock {
22+
constructor(private timeMs: number) {}
23+
24+
now(): number {
25+
return this.timeMs;
26+
}
27+
28+
advance(ms: number): void {
29+
this.timeMs += ms;
30+
}
31+
}
32+
33+
export const SYSTEM_CLOCK = new SystemClock();
34+

packages/client/lib/client/authx/credentials-provider.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {Disposable} from './types';
2+
13
/**
24
* Provides credentials asynchronously.
35
*/
@@ -66,12 +68,6 @@ export type StreamingCredentialsListener<T> = {
6668
onError: (e: Error) => void;
6769
}
6870

69-
/**
70-
* Disposable is an interface for objects that hold resources that should be released when they are no longer needed.
71-
*/
72-
export type Disposable = {
73-
dispose: () => void;
74-
}
7571

7672
/**
7773
* Providers that can supply authentication credentials
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* An identity provider is responsible for providing a token that can be used to authenticate with a service.
3+
*/
4+
5+
/**
6+
* The response from an identity provider when requesting a token.
7+
*
8+
* note: "native" refers to the type of the token that the actual identity provider library is using.
9+
*
10+
* @type T The type of the native idp token.
11+
* @property token The token.
12+
* @property ttlMs The time-to-live of the token in epoch milliseconds extracted from the native token in local time.
13+
*/
14+
export type TokenResponse<T> = { token: T, ttlMs: number };
15+
16+
export interface IdentityProvider<T> {
17+
/**
18+
* Request a token from the identity provider.
19+
* @returns A promise that resolves to an object containing the token and the time-to-live in epoch milliseconds.
20+
*/
21+
requestToken(): Promise<TokenResponse<T>>;
22+
}

packages/client/lib/client/authx/index.ts

Whitespace-only changes.

0 commit comments

Comments
 (0)