-
Notifications
You must be signed in to change notification settings - Fork 1
feat: per-issuer session cache — stop re-running the full auth flow (popup included) on every 401 #11
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
Open
jeswr
wants to merge
2
commits into
main
Choose a base branch
from
feat/dpop-session-cache
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+400
−15
Open
feat: per-issuer session cache — stop re-running the full auth flow (popup included) on every 401 #11
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" | ||
| import { DPoPTokenProvider } from "../src/DPoPTokenProvider.js" | ||
| import { createFakeAuthorizationServer, type FakeAuthorizationServer } from "./fakeAuthorizationServer.js" | ||
|
|
||
| const callbackUri = "https://app.test/callback.html" | ||
|
|
||
| let as: FakeAuthorizationServer | ||
|
|
||
| function makeProvider(getCode = vi.fn((url: URL) => as.authorize(url))) { | ||
| const provider = new DPoPTokenProvider(callbackUri, getCode, async () => new URL(as.issuer)) | ||
| return {provider, getCode} | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals() | ||
| vi.useRealTimers() | ||
| }) | ||
|
|
||
| describe("DPoPTokenProvider session cache", () => { | ||
| beforeEach(async () => { | ||
| as = await createFakeAuthorizationServer() | ||
| vi.stubGlobal("fetch", as.fetch) | ||
| }) | ||
|
|
||
| it("attaches a DPoP-bound access token to the upgraded request", async () => { | ||
| const {provider} = makeProvider() | ||
|
|
||
| const upgraded = await provider.upgrade(new Request("https://pod.test/private")) | ||
|
|
||
| expect(upgraded.headers.get("Authorization")).toMatch(/^DPoP at-\d+$/) | ||
| expect(upgraded.headers.get("DPoP")).toBeTruthy() | ||
| }) | ||
|
|
||
| it("runs the authorization flow once for concurrent upgrades (single-flight)", async () => { | ||
| const {provider, getCode} = makeProvider() | ||
|
|
||
| await Promise.all([ | ||
| provider.upgrade(new Request("https://pod.test/a")), | ||
| provider.upgrade(new Request("https://pod.test/b")), | ||
| provider.upgrade(new Request("https://pod.test/c")), | ||
| ]) | ||
|
|
||
| expect(getCode).toHaveBeenCalledTimes(1) | ||
| expect(as.registrations).toHaveLength(1) | ||
| }) | ||
|
|
||
| it("reuses the established session for later upgrades instead of re-prompting", async () => { | ||
| const {provider, getCode} = makeProvider() | ||
|
|
||
| const first = await provider.upgrade(new Request("https://pod.test/a")) | ||
| const second = await provider.upgrade(new Request("https://pod.test/b")) | ||
|
|
||
| expect(getCode).toHaveBeenCalledTimes(1) | ||
| expect(second.headers.get("Authorization")).toBe(first.headers.get("Authorization")) | ||
| }) | ||
|
|
||
| it("signs a fresh DPoP proof per request while reusing the access token", async () => { | ||
| const {provider} = makeProvider() | ||
|
|
||
| const first = await provider.upgrade(new Request("https://pod.test/a")) | ||
| const second = await provider.upgrade(new Request("https://pod.test/b")) | ||
|
|
||
| expect(second.headers.get("DPoP")).not.toBe(first.headers.get("DPoP")) | ||
| }) | ||
|
|
||
| it("re-authenticates once the access token has expired", async () => { | ||
| const {provider, getCode} = makeProvider() | ||
|
|
||
| const first = await provider.upgrade(new Request("https://pod.test/a")) | ||
|
|
||
| // Step past the reported expiry (minus the skew allowance). | ||
| vi.useFakeTimers() | ||
| vi.setSystemTime(Date.now() + 3601 * 1000) | ||
|
|
||
| const second = await provider.upgrade(new Request("https://pod.test/b")) | ||
|
|
||
| expect(getCode).toHaveBeenCalledTimes(2) | ||
| expect(second.headers.get("Authorization")).not.toBe(first.headers.get("Authorization")) | ||
| }) | ||
|
|
||
| it("does not cache a failed flow: the next upgrade retries", async () => { | ||
| const getCode = vi.fn((url: URL) => as.authorize(url)) | ||
| getCode.mockRejectedValueOnce(new Error("user closed the popup")) | ||
| const {provider} = makeProvider(getCode) | ||
|
|
||
| await expect(provider.upgrade(new Request("https://pod.test/a"))).rejects.toThrow("user closed the popup") | ||
|
|
||
| const second = await provider.upgrade(new Request("https://pod.test/b")) | ||
|
|
||
| expect(second.headers.get("Authorization")).toMatch(/^DPoP at-\d+$/) | ||
| expect(getCode).toHaveBeenCalledTimes(2) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Agreed this needed a strategy. Fixed in #14 (stacked follow-up):
TokenProvidergains an optionalinvalidate(), andReactiveFetchManagerretries exactly once with renewed credentials when an upgraded request is still rejected — covering both the missing-expires_incase and early revocation, without guessing a max age.