import { type AccessTokens, type IHttpOpts } from "./interface.ts"; /** * This is an internal module. See {@link MatrixHttpApi} for the public class. */ export declare const enum TokenRefreshOutcome { Success = "success", Failure = "failure", Logout = "logout" } type Opts = Pick; /** * This class is responsible for managing the access token and refresh token for authenticated requests. * It will automatically refresh the tokens when the access token is about to expire and can handle * Unknown Token errors (via {@link TokenManager.handleUnknownToken}) * * It will update the {@link Opts} object with new tokens as they are refreshed, and also call the {@link IHttpOpts.onTokenRefresh} callback if provided. */ export declare class TokenManager { private readonly opts; private readonly getAbortSignal?; /** * @param opts - the HTTP options, which are updated in place as tokens are refreshed. * @param getAbortSignal - optional callback returning the signal which, when aborted, should abort any * in-flight request. Called at the start of each such request. */ constructor(opts: Opts, getAbortSignal?: (() => AbortSignal) | undefined); /** * Promise used to block authenticated requests during a token refresh to avoid repeated expected errors. * @private */ private tokenRefreshPromise?; private latestTokenRefreshExpiry?; private tokenRefresher?; private tokenRefresherPromise?; /** * This function is called before every request to ensure that the access token is valid. * @returns a snapshot containing the access token and other properties which must be passed to the handleUnknownToken * handler if an M_UNKNOWN_TOKEN error is encountered. */ prepareForRequest(): Promise; private refreshIfNeeded; /** * This function is called when an M_UNKNOWN_TOKEN error is encountered. * It will attempt to refresh the access token if it is unknown, and will return a TokenRefreshOutcome. * @param snapshot - the snapshot returned by prepareForRequest * @param attempt - the number of attempts made for this request so far * @returns a TokenRefreshOutcome indicating the result of the refresh attempt */ handleUnknownToken(snapshot: AccessTokens, attempt: number): Promise; private _handleUnknownToken; /** * Attempt to refresh access tokens. * On success, sets new access and refresh tokens in opts. * @returns Promise that resolves to a boolean - true when token was refreshed successfully */ private doTokenRefresh; /** * Attempt to revoke the current access and refresh tokens with the OAuth2 authorization server, e.g. as * part of logging out. Does nothing if this is not an OAuth2-native session (ie. no `oauth2ClientConfig` * was supplied). * @throws when discovery of the auth server metadata, or revocation of either token, fails */ revokeTokens(): Promise; /** * Lazily constructs (and caches) the {@link TokenRefresher} used to talk to the OAuth2 authorization * server, discovering its metadata first if this is the first time it's needed. * @returns the token refresher, or undefined if no `oauth2ClientConfig` was supplied * @throws when discovery of the auth server metadata fails in a way that isn't worth retrying */ private ensureTokenRefresher; /** * Discovers the OAuth2 auth server metadata and constructs the OAuth2 client and token refresher from it. * Throws if the request fails. */ private discoverTokenRefresher; } export {}; //# sourceMappingURL=refresh.d.ts.map