import { InfisicalSDK } from ".."; import { AuthApi } from "../api/endpoints/auth"; import { UniversalAuthLoginRequest } from "../api/types"; import { InfisicalSDKError, newInfisicalError } from "./errors"; type AuthenticatorFunction = (accessToken: string) => InfisicalSDK; export const renewToken = async (apiClient: AuthApi, token?: string) => { try { if (!token) { throw new InfisicalSDKError( "Unable to renew access token, no access token set." ); } const res = await apiClient.renewToken({ accessToken: token }); return res; } catch (err) { throw newInfisicalError(err); } }; export default class AuthClient { constructor( private sdkAuthenticator: AuthenticatorFunction, private apiClient: AuthApi, private _accessToken?: string ) {} universalAuth = { login: async (options: UniversalAuthLoginRequest) => { try { const res = await this.apiClient.universalAuthLogin(options); return this.sdkAuthenticator(res.accessToken); } catch (err) { throw newInfisicalError(err); } }, renew: async () => { try { const refreshedToken = await renewToken( this.apiClient, this._accessToken ); return this.sdkAuthenticator(refreshedToken.accessToken); } catch (err) { throw newInfisicalError(err); } }, }; /** * Gets the current access token that is set on the SDK instance * @returns The current access token or null if no access token is set. `null` is returned if the SDK is not authenticated. */ getAccessToken = () => this._accessToken || null; accessToken = (token: string) => { return this.sdkAuthenticator(token); }; }