import { GuestTokenProvider, TokenData, type TokenKind, UserTokenProvider } from './token-providers/index.js'; import { MisconfiguredTokenProviderError } from './errors.js'; import { type AxiosError, type AxiosInstance, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios'; import type { AxiosAuthenticationTokenManagerOptions } from './types/TokenManagerOptions.types.js'; import type { ITokenData } from './token-providers/types/TokenData.types.js'; import type { RequestConfig } from './types/AuthenticationTokenManager.types.js'; import type { TokenContext } from './token-providers/types/TokenContext.types.js'; import type { UserToken } from './types/index.js'; type TokenDataChangedListener = (activeToken: { kind: TokenKind; data: ITokenData | null; } | null) => void; type UserSessionTerminatedEventListener = (value: UserToken | null) => void; /** * Class responsible for installing an axios interceptor which will manage * authentication of any requests that require it with the correct access token * type (user or guest). */ declare class AuthenticationTokenManager { activeTokenDataChangedListener: TokenDataChangedListener | null; authorizationHeaderFormatter: AxiosAuthenticationTokenManagerOptions['authorizationHeaderFormatter']; axiosInstance: AxiosInstance; currentTokenProvider: UserTokenProvider | GuestTokenProvider | null; guestTokenProvider: GuestTokenProvider; isLoaded: boolean; isLoading: boolean; loadError: MisconfiguredTokenProviderError | null; loadPromise: Promise | null; requestInterceptor: number; responseInterceptor: number; userSessionTerminatedEventListener: UserSessionTerminatedEventListener | null; userTokenProvider: UserTokenProvider; /** * @param client - The axios instance to apply the interceptors to. * @param options - Options to configure this instance. */ constructor(client: AxiosInstance, options: AxiosAuthenticationTokenManagerOptions); /** * Initializes the instance with the passed in options by validating them first and * then applying them. * * @param client - The axios instance to apply the interceptors to. * @param options - The options object passed to the constructor. */ initialize(client: AxiosInstance, options: AxiosAuthenticationTokenManagerOptions): void; /** * Validates the passed options. * * @throws * Will throw an error if an option does not contain a valid value. * * @param client - The axios instance to apply the interceptors to. * @param options - The options object passed to the constructor. */ validateOptions(client: AxiosInstance, options: AxiosAuthenticationTokenManagerOptions): void; /** * Applies the options and makes the call to the function that will install the * axios interceptors to the axios instance. * * @param client - The axios instance to apply the interceptors to. * @param options - The options object passed to the constructor. */ applyOptions(client: AxiosInstance, options: AxiosAuthenticationTokenManagerOptions): void; /** * Installs the interceptors to the axios instance. */ installInterceptors(): void; /** * Ejects the added intersectors from the axios instance. */ ejectInterceptors(): void; /** * Clears token data from memory and storage if provided. * * @returns Promise that will finish when both the guest and user tokens data are cleared. */ clearData(): Promise; /** * Select the guest token provider as the current token provider. Used when * performing a logout. */ selectGuestTokenProvider(): void; /** * Switches the current token provider to the new one. This method is used * internally to switch the current token provider context from guest to * authenticated users and vice-versa. Make sure you know what you are doing before * calling this method yourself. * * @param newTokenProvider - The token provider instance to select. */ selectTokenProvider(newTokenProvider: UserTokenProvider | GuestTokenProvider): void; /** * Gets the token from the current token provider. * * @returns Returns the token data from the current token provider or null if there is not a current * token provider. */ getActiveToken(): UserToken | null; /** * Raises on active token data changed event. */ raiseOnActiveTokenDataChangedEvent(): void; /** * Sets the context for guest tokens retrieval. * * @param newContext - Properties to set on the guest token context. */ setGuestTokensContext(newContext: TokenContext): void; /** * Resets the guest tokens context to the default one. */ resetGuestTokensContext(): void; /** * Retrieves the current guest tokens context. * * @returns The current guest tokens context. */ getCurrentGuestTokensContext(): TokenContext; /** * Returns an access token from the current token provider. If useCache is false, a * new create access token request will be made. * * @param useCache - Returns an access token from the cache if available, if not a new create access * token request will be made. * * @returns Promise that will resolve with a renewed or cached access token from the current provider. * If the renew request fails, the promise will reject with the error. */ getAccessToken(useCache?: boolean): Promise; /** * Listener for guest token data changes. Will raise an active token data changed * event if it is the current token provider. */ guestTokenChangesListener: () => void; /** * Listener for user token data changes. Will raise an active token data changed * event if it is the current token provider. */ userTokenChangesListener: () => void; /** * Sets the active token data changed events listener. * * @param listener - The new listener to apply. */ setActiveTokenDataChangedEventListener(listener: TokenDataChangedListener | null): void; /** * Sets the user forced logout events listener. * * @param listener - The new listener to apply. */ setUserSessionTerminatedEventListener(listener: UserSessionTerminatedEventListener | null): void; /** * Sets the user info to the correct token. The token data received by invoking an * endpoint that creates tokens do not return a userId but we need a userId to * associate the tokens with in order to persist sessions. This function must be * called after the user data is retrieved for an access token. * * @param userData - The user data obtained from the get profile endpoint. * * @returns Promise that will be resolved when the user info is set on the appropriate token provider * instance. */ setUserInfo(userData: { id: number; isGuest: boolean; } | null): Promise; /** * Loads all token providers and sets the current provider to the either the user * token provider if it can retrieve tokens (i.e. It has a refresh token field) or * the client credentials token provider if not. * * @throws * MisconfiguredTokenProviderError. * * @returns Promise that will be resolved when the load method completes. */ load(): Promise | null; /** * Checks if a request needs an access token from the axios request config object. * It does it by checking if: 1 - The config object contains an access token * property. If it does, then that access token will be used. 2 - The config object * contains a no authentication property. If it does, no access token will be added * to the request. This is necessary on certain requests that do not need an access * token. 3 - The config object already contains an authorization header. * * @param config - Axios request config object. * * @returns If the request needs an access token or not. */ requestNeedsAccessTokenMatcher(config: RequestConfig): boolean; /** * Installed request fulfilled axios interceptor which will be called before every * request that is dispatched. Will add an access token if the request is flagged * as needing it. * * @param config - Axios request config object. * * @returns Promise that will be resolved with the final config object. */ onBeforeRequestInterceptor(config: InternalAxiosRequestConfig): Promise>; /** * Called when the currently logged in user's refresh token expires. Will set the * guest token provider as the current and raise the on user forced logout event. */ forceLogout(): void; /** * Raises the user forced logout event. * * @param expiredUserToken - The expired user token data. */ raiseOnUserSessionTerminatedEvent(expiredUserToken: UserToken | null): void; onRequestSuccessfulInterceptor(result: AxiosResponse): Promise>; /** * Response rejected axios interceptor which will be called after a request has * failed. This method will look for 401 errors and retry the original request one * more time with a new access token. If after that the method still fails, the * original error will be returned to the caller. * * @param error - Axios error object. * * @returns Promise that will be rejected with the original error if the retry for the 401 error was * not successful or resolved with the data from the request if the retry is successful. */ onRequestFailedInterceptor(error: AxiosError): Promise; /** * Sets if the user token provider can persist user tokens on the storage. Note: * Guest tokens are always preserved, ignoring this value. * * @param rememberMe - The new remember me value. */ setRememberMe(rememberMe: boolean): void; /** * Sets user token data and optionally switches the current token provider to the * user token provider. * * @param tokenData - The new user token data. * @param forceSwitch - If 'true' will switch the current token provider to the user token provider * after setting the user token data. * * @returns Promise that will be resolved when the token data is successfully applied to the user token * provider and the switch to it is performed if forceSwitch parameter is true. */ setUserTokenData(tokenData: TokenData, forceSwitch: boolean): Promise; } export default AuthenticationTokenManager;