Skip to content

Commit

Permalink
feat(auth): auth extensions
Browse files Browse the repository at this point in the history
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

This change establishes the core infrastructure needed for reliable token
lifecycle management across different authentication providers.
  • Loading branch information
bobymicroby committed Dec 13, 2024
1 parent a0c324b commit e7d6f5b
Show file tree
Hide file tree
Showing 13 changed files with 1,156 additions and 35 deletions.
150 changes: 145 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions packages/authx/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export { TokenManager, TokenManagerConfig, TokenStreamListener, RetryPolicy, IDPError } from './lib/token-manager';
export {
CredentialsProvider,
StreamingCredentialsProvider,
UnableToObtainNewCredentialsError,
CredentialsError,
StreamingCredentialsListener,
AsyncCredentialsProvider,
ReAuthenticationError,
BasicAuth
} from './lib/credentials-provider';
export { Token } from './lib/token';
export { IdentityProvider, TokenResponse } from './lib/identity-provider';
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/**
* Provides credentials asynchronously.
*/
Expand Down Expand Up @@ -66,12 +67,6 @@ export type StreamingCredentialsListener<T> = {
onError: (e: Error) => void;
}

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

/**
* Providers that can supply authentication credentials
Expand Down
22 changes: 22 additions & 0 deletions packages/authx/lib/identity-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* An identity provider is responsible for providing a token that can be used to authenticate with a service.
*/

/**
* The response from an identity provider when requesting a token.
*
* note: "native" refers to the type of the token that the actual identity provider library is using.
*
* @type T The type of the native idp token.
* @property token The token.
* @property ttlMs The time-to-live of the token in epoch milliseconds extracted from the native token in local time.
*/
export type TokenResponse<T> = { token: T, ttlMs: number };

export interface IdentityProvider<T> {
/**
* Request a token from the identity provider.
* @returns A promise that resolves to an object containing the token and the time-to-live in epoch milliseconds.
*/
requestToken(): Promise<TokenResponse<T>>;
}
Loading

0 comments on commit e7d6f5b

Please sign in to comment.