diff --git a/README.md b/README.md index f76c1869..2011291c 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,14 @@ You can get more data on the Fireblocks error using the following fields: - `error.response.data.message`: Explanation of the Fireblocks error - `error.response.headers['x-request-id']`: The request ID correlated to the API request, should be provided on support tickets / Github issues +#### Auth Provider +You can supply an async auth provider instance that implements the following interface: +```ts +export interface IAuthProvider { + signJwt(path: string, bodyJson?: any): string | Promise; + getApiKey(): string | Promise; +} +``` - +Methods can be async. \ No newline at end of file diff --git a/src/api-client.ts b/src/api-client.ts index c32608a5..90e3c9ed 100644 --- a/src/api-client.ts +++ b/src/api-client.ts @@ -14,10 +14,12 @@ export class ApiClient { baseURL: this.apiBaseUrl, proxy: this.options?.proxy, timeout: this.options?.timeoutInMs, - headers: { - "X-API-Key": this.authProvider.getApiKey(), - "User-Agent": this.getUserAgent() - } + }); + + this.axiosInstance.interceptors.request.use(async (config: any) => { + config.headers.common["X-API-Key"] = await this.authProvider.getApiKey(); + config.headers.common["User-Agent"] = this.getUserAgent(); + return config; }); if (options.customAxiosOptions?.interceptors?.response) { @@ -37,7 +39,7 @@ export class ApiClient { } public async issueGetRequestForTransactionPages(path: string): Promise { - const token = this.authProvider.signJwt(path); + const token = await this.authProvider.signJwt(path); const res = await this.axiosInstance.get(path, { headers: {"Authorization": `Bearer ${token}`} }); @@ -51,7 +53,7 @@ export class ApiClient { } public async issueGetRequest(path: string): Promise { - const token = this.authProvider.signJwt(path); + const token = await this.authProvider.signJwt(path); const res = await this.axiosInstance.get(path, { headers: {"Authorization": `Bearer ${token}`} }); @@ -59,7 +61,7 @@ export class ApiClient { } public async issuePostRequest(path: string, body: any, requestOptions?: RequestOptions): Promise { - const token = this.authProvider.signJwt(path, body); + const token = await this.authProvider.signJwt(path, body); const headers: any = {"Authorization": `Bearer ${token}`}; const idempotencyKey = requestOptions?.idempotencyKey; if (idempotencyKey) { @@ -70,7 +72,7 @@ export class ApiClient { } public async issuePutRequest(path: string, body: any): Promise { - const token = this.authProvider.signJwt(path, body); + const token = await this.authProvider.signJwt(path, body); const res = (await this.axiosInstance.put(path, body, { headers: {"Authorization": `Bearer ${token}`} })); @@ -78,7 +80,7 @@ export class ApiClient { } public async issuePatchRequest(path: string, body: any): Promise { - const token = this.authProvider.signJwt(path, body); + const token = await this.authProvider.signJwt(path, body); const res = (await this.axiosInstance.patch(path, body, { headers: {"Authorization": `Bearer ${token}`} })); @@ -86,7 +88,7 @@ export class ApiClient { } public async issueDeleteRequest(path: string): Promise { - const token = this.authProvider.signJwt(path); + const token = await this.authProvider.signJwt(path); const res = (await this.axiosInstance.delete(path, { headers: {"Authorization": `Bearer ${token}`} })); diff --git a/src/fireblocks-sdk.ts b/src/fireblocks-sdk.ts index ee65965f..e2dd1594 100644 --- a/src/fireblocks-sdk.ts +++ b/src/fireblocks-sdk.ts @@ -67,6 +67,8 @@ import { AxiosInterceptorOptions, AxiosProxyConfig, AxiosResponse } from "axios" export * from "./types"; +export * from "./iauth-provider"; + export interface SDKOptions { /** HTTP request timeout */ timeoutInMs?: number; diff --git a/src/iauth-provider.ts b/src/iauth-provider.ts index afbfa7e2..93540de2 100644 --- a/src/iauth-provider.ts +++ b/src/iauth-provider.ts @@ -1,5 +1,5 @@ export interface IAuthProvider { - signJwt(path: string, bodyJson?: any): string; + signJwt(path: string, bodyJson?: any): string | Promise; - getApiKey(): string; + getApiKey(): string | Promise; }