-
Notifications
You must be signed in to change notification settings - Fork 25
fix: apply expiry buffer before reusing cached tokens #331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
c52539c
890b4c1
26a7d7b
fbc241b
4014b7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import * as jose from "jose"; | |
| import { Credentials, CredentialsMethod, DEFAULT_TOKEN_ENDPOINT_PATH } from "../credentials"; | ||
| import { AuthCredentialsConfig } from "../credentials/types"; | ||
| import { TelemetryConfiguration } from "../telemetry/configuration"; | ||
| import SdkConstants from "../constants"; | ||
| import { | ||
| OPENFGA_API_AUDIENCE, | ||
| OPENFGA_CLIENT_ASSERTION_SIGNING_KEY, | ||
|
|
@@ -537,5 +538,53 @@ describe("Credentials", () => { | |
|
|
||
| expect(scope.isDone()).toBe(true); | ||
| }); | ||
|
|
||
| test("should refresh cached token when it is close to expiration", async () => { | ||
| const apiTokenIssuer = "issuer.fga.example"; | ||
| const expectedBaseUrl = "https://issuer.fga.example"; | ||
| const expectedPath = `/${DEFAULT_TOKEN_ENDPOINT_PATH}`; | ||
| const randomSpy = jest.spyOn(Math, "random").mockReturnValue(0); | ||
| const shortLivedTokenInSec = Math.max( | ||
| 1, | ||
| SdkConstants.TokenExpiryThresholdBufferInSec - 1 | ||
| ); | ||
|
|
||
| const scope = nock(expectedBaseUrl) | ||
| .post(expectedPath) | ||
| .reply(200, { | ||
| access_token: "short-lived-token", | ||
| expires_in: shortLivedTokenInSec, | ||
| }) | ||
|
Comment on lines
554
to
557
|
||
| .post(expectedPath) | ||
| .reply(200, { | ||
| access_token: "refreshed-token", | ||
| expires_in: 3600, | ||
| }); | ||
|
|
||
| const credentials = new Credentials( | ||
| { | ||
| method: CredentialsMethod.ClientCredentials, | ||
| config: { | ||
| apiTokenIssuer, | ||
| apiAudience: OPENFGA_API_AUDIENCE, | ||
| clientId: OPENFGA_CLIENT_ID, | ||
| clientSecret: OPENFGA_CLIENT_SECRET, | ||
| }, | ||
| } as AuthCredentialsConfig, | ||
| undefined, | ||
| mockTelemetryConfig, | ||
| ); | ||
|
|
||
| try { | ||
| const header1 = await credentials.getAccessTokenHeader(); | ||
| const header2 = await credentials.getAccessTokenHeader(); | ||
|
|
||
| expect(header1?.value).toBe("Bearer short-lived-token"); | ||
| expect(header2?.value).toBe("Bearer refreshed-token"); | ||
| expect(scope.isDone()).toBe(true); | ||
| } finally { | ||
| randomSpy.mockRestore(); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file header states it is auto-generated (“DO NOT EDIT”), but this change introduces a new import. If this file is regenerated as part of the build/release process, these edits may be overwritten; consider updating the generator template/source or adjusting the generation settings/header so the change persists reliably.