Skip to content

Commit e3b189b

Browse files
committed
create a wrapper function for instantiating a company auth'd client with token refresh
1 parent 05d935f commit e3b189b

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { TokenRefreshOptions, withTokenRefresh } from "./companyAuth.js";
2+
import { GustoEmbeddedCore } from "./core.js";
3+
import { SDKOptions, ServerDemo, ServerList } from "./lib/config.js";
4+
5+
type ClientArguments = {
6+
clientId: string;
7+
clientSecret: string;
8+
accessToken: string;
9+
refreshToken: string;
10+
expiresIn: number;
11+
options: TokenRefreshOptions & SDKOptions;
12+
};
13+
14+
export function CompanyAuthenticatedClient({
15+
clientId,
16+
clientSecret,
17+
accessToken,
18+
refreshToken,
19+
expiresIn,
20+
options,
21+
}: ClientArguments) {
22+
const authUrl = constructAuthUrl(options);
23+
24+
return new GustoEmbeddedCore({
25+
...options,
26+
companyAccessAuth: withTokenRefresh(
27+
clientId,
28+
clientSecret,
29+
accessToken,
30+
refreshToken,
31+
expiresIn,
32+
{
33+
...options,
34+
url: authUrl,
35+
}
36+
),
37+
});
38+
}
39+
40+
function constructAuthUrl(
41+
options: TokenRefreshOptions & Pick<SDKOptions, "server" | "serverURL">
42+
) {
43+
const { server, serverURL, url } = options;
44+
45+
if (server) {
46+
const baseUrl = ServerList[server] || "";
47+
return `${baseUrl}/oauth/token`;
48+
}
49+
50+
if (serverURL) {
51+
return `${serverURL}/oauth/token`;
52+
}
53+
54+
if (url) {
55+
return url;
56+
}
57+
58+
return `${ServerList[ServerDemo]}/oauth/token`;
59+
}

gusto_embedded/src/companyAuth.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,31 @@ const tokenResponseSchema = z.object({
1515
// and slow or unreliable networks.
1616
const tolerance = 5 * 60 * 1000;
1717

18+
export type TokenRefreshOptions = { tokenStore?: TokenStore; url?: string };
19+
1820
/**
1921
* A callback function that can be used to obtain an OAuth access token for use
2022
* with SDKs that require OAuth security. A new token is requested from the
2123
* OAuth provider when the current token has expired.
2224
*/
2325
export function withTokenRefresh(
24-
clientID: string,
26+
clientId: string,
2527
clientSecret: string,
2628
accessToken: string,
2729
refreshToken: string,
28-
options: { tokenStore?: TokenStore; url?: string } = {}
30+
expiresIn: number,
31+
options: TokenRefreshOptions = {}
2932
) {
3033
const {
3134
tokenStore = new InMemoryTokenStore(),
32-
// Replace this with your default OAuth provider's access token endpoint.
3335
url = "https://api.gusto-demo.com/oauth/token",
3436
} = options;
3537

36-
tokenStore.set({ token: accessToken, refreshToken, expires: 10 });
38+
tokenStore.set({
39+
token: accessToken,
40+
refreshToken,
41+
expires: Date.now() + expiresIn * 1000 - tolerance,
42+
});
3743

3844
return async (): Promise<string> => {
3945
const session = await tokenStore.get();
@@ -53,7 +59,7 @@ export function withTokenRefresh(
5359
"user-agent": SDK_METADATA.userAgent,
5460
},
5561
body: new URLSearchParams({
56-
client_id: clientID,
62+
client_id: clientId,
5763
client_secret: clientSecret,
5864
grant_type: "refresh_token",
5965
refresh_token: refreshToken,

0 commit comments

Comments
 (0)