Skip to content

Commit

Permalink
🌿 introduce OAuth configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-api[bot] committed Jul 26, 2024
1 parent 13ff57d commit 271fa4f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .fernignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# Specify files that shouldn't be modified by Fern
src/wrapper/
src/core/auth/OAuthTokenProvider.ts
src/index.ts
57 changes: 57 additions & 0 deletions src/core/auth/OAuthTokenProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

import * as core from "../../core";
import { Auth } from "../../api/resources/auth/client/Client";

/**
* The OAuthTokenProvider retrieves an OAuth access token, refreshing it as needed.
* The access token is then used as the bearer token in every authenticated request.
*/
export class OAuthTokenProvider {
private readonly BUFFER_IN_MINUTES = 2;
private readonly _clientId: core.Supplier<string>;
private readonly _clientSecret: core.Supplier<string>;
private readonly _authClient: Auth;
private _accessToken: string | undefined;
private _expiresAt: Date;

constructor({
clientId,
clientSecret,
authClient,
}: {
clientId: core.Supplier<string>;
clientSecret: core.Supplier<string>;
authClient: Auth;
}) {
this._clientId = clientId;
this._clientSecret = clientSecret;
this._authClient = authClient;
this._expiresAt = new Date();
}

public async getToken(): Promise<string> {
if (this._accessToken && this._expiresAt > new Date()) {
return this._accessToken;
}
return this.refresh();
}

private async refresh(): Promise<string> {
const tokenResponse = await this._authClient.getToken({
clientId: await core.Supplier.get(this._clientId),
clientSecret: await core.Supplier.get(this._clientSecret),
});

this._accessToken = tokenResponse.accessToken;
this._expiresAt = this.getExpiresAt(tokenResponse.expiresIn, this.BUFFER_IN_MINUTES);
return this._accessToken;
}

private getExpiresAt(expiresInSeconds: number, bufferInMinutes: number): Date {
const now = new Date();
return new Date(now.getTime() + expiresInSeconds * 1000 - bufferInMinutes * 60 * 1000);
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * as Chariot from "./api";
export { ChariotClient } from "./Client";
export { ChariotEnvironment } from "./environments";
export { ChariotError, ChariotTimeoutError } from "./errors";
export { ChariotClient } from "./wrapper/ChariotClient";
30 changes: 30 additions & 0 deletions src/wrapper/ChariotClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Auth } from "api/resources/auth/client/Client";
import { ChariotClient as FernClient } from "../Client";
import * as core from "../core";
import * as environments from "../environments";
import { OAuthTokenProvider } from "core/auth/OAuthTokenProvider";

export declare namespace ChariotClient {

interface Options {
environment?: core.Supplier<environments.ChariotEnvironment | string>;
clientId: core.Supplier<string>;
clientSecret: core.Supplier<string>;
}
}

export class ChariotClient extends FernClient {
constructor(protected readonly _options: ChariotClient.Options) {
const _oauthTokenProvider = new OAuthTokenProvider({
clientId: _options.clientId,
clientSecret: _options.clientSecret,
authClient: new Auth({
environment: _options.environment,
}),
});
super({
..._options,
token: async () => await _oauthTokenProvider.getToken(),
})
}
}

0 comments on commit 271fa4f

Please sign in to comment.