-
Notifications
You must be signed in to change notification settings - Fork 2
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
Draft
jeswr
wants to merge
4
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.
Draft
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
4 commits
Select commit
Hold shift + click to select a range
fa38fd9
feat: cache the DPoP session per issuer so repeat 401s reuse the token
jeswr c57f39c
test: make the fake AS honest about refresh tokens
jeswr 46e4362
feat: make session caching pluggable and its key configurable
jeswr e34467f
feat: persist sessions in IndexedDB or web storage
jeswr 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 @@ | ||
| export type GetSessionKeyCallback = (request: Request, issuer: URL) => Promise<string> |
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,56 @@ | ||
| import type { SessionCache } from "./SessionCache.js" | ||
|
|
||
| const defaultDatabaseName = "reactive-authentication" | ||
| const storeName = "sessions" | ||
|
|
||
| /** | ||
| * Persists sessions in IndexedDB, so they survive a reload or a browser restart. | ||
| * | ||
| * @remarks Preferred for DPoP. IndexedDB stores by structured clone, which keeps a non extractable {@link CryptoKey} intact, so the key outlives the page while remaining unreadable by script on the origin. {@link WebStorageSessionCache} cannot hold one at all. | ||
| */ | ||
| export class IndexedDbSessionCache<T> implements SessionCache<T> { | ||
| readonly #databaseName: string | ||
| #database?: Promise<IDBDatabase> | ||
|
|
||
| constructor(databaseName: string = defaultDatabaseName) { | ||
| this.#databaseName = databaseName | ||
| } | ||
|
|
||
| async get(key: string): Promise<T | undefined> { | ||
| return this.#run("readonly", store => store.get(key)) | ||
| } | ||
|
|
||
| async set(key: string, value: T): Promise<void> { | ||
| await this.#run("readwrite", store => store.put(value, key)) | ||
| } | ||
|
|
||
| async delete(key: string): Promise<void> { | ||
| await this.#run("readwrite", store => store.delete(key)) | ||
| } | ||
|
|
||
| async #run<R>(mode: IDBTransactionMode, work: (store: IDBObjectStore) => IDBRequest<R>): Promise<R> { | ||
| const database = await this.#open() | ||
|
|
||
| return settled(work(database.transaction(storeName, mode).objectStore(storeName))) | ||
| } | ||
|
|
||
| #open(): Promise<IDBDatabase> { | ||
| if (this.#database === undefined) { | ||
| const request = indexedDB.open(this.#databaseName) | ||
| request.onupgradeneeded = () => request.result.createObjectStore(storeName) | ||
|
|
||
| this.#database = settled(request) | ||
| } | ||
|
|
||
| return this.#database | ||
| } | ||
| } | ||
|
|
||
| function settled<R>(request: IDBRequest<R>): Promise<R> { | ||
| const {promise, resolve, reject} = Promise.withResolvers<R>() | ||
|
|
||
| request.onsuccess = () => resolve(request.result) | ||
| request.onerror = () => reject(request.error) | ||
|
|
||
| return promise | ||
| } |
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,18 @@ | ||
| import type { SessionCache } from "./SessionCache.js" | ||
|
|
||
| /** Keeps sessions for the lifetime of the provider. */ | ||
| export class MemorySessionCache<T> implements SessionCache<T> { | ||
| readonly #entries = new Map<string, T>() | ||
|
|
||
| async get(key: string): Promise<T | undefined> { | ||
| return this.#entries.get(key) | ||
| } | ||
|
|
||
| async set(key: string, value: T): Promise<void> { | ||
| this.#entries.set(key, value) | ||
| } | ||
|
|
||
| async delete(key: string): Promise<void> { | ||
| this.#entries.delete(key) | ||
| } | ||
| } |
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,13 @@ | ||
| /** | ||
| * Where a token provider keeps established sessions. | ||
| * | ||
| * @remarks Asynchronous so sessions can live wherever the host offers, such as | ||
| * IndexedDB in a browser or the secrets API in an editor extension. | ||
| */ | ||
| export interface SessionCache<T> { | ||
| get(key: string): Promise<T | undefined> | ||
|
|
||
| set(key: string, value: T): Promise<void> | ||
|
|
||
| delete(key: string): Promise<void> | ||
| } |
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,56 @@ | ||
| import type { SessionCache } from "./SessionCache.js" | ||
|
|
||
| const defaultPrefix = "reactive-authentication:" | ||
|
|
||
| /** | ||
| * Persists sessions as JSON in web storage: `localStorage` to survive a browser restart, or `sessionStorage` to last only as long as the tab. | ||
| * | ||
| * @remarks Suitable for sessions that are entirely JSON, such as a bare refresh token. A DPoP session is not, because {@link JSON.stringify} discards a {@link CryptoKey} without complaining; {@link set} throws rather than store one, and {@link IndexedDbSessionCache} handles that case. | ||
| * | ||
| * @remarks Anything kept here is readable by any script running on the origin, so store the least that will do. | ||
| */ | ||
| export class WebStorageSessionCache<T> implements SessionCache<T> { | ||
| readonly #storage: Storage | ||
| readonly #prefix: string | ||
|
|
||
| /** | ||
| * @param storage - Which store to use, normally `localStorage` or `sessionStorage`. | ||
| * @param prefix - Namespace for the keys, to keep them apart from the rest of the origin's data. | ||
| */ | ||
| constructor(storage: Storage, prefix: string = defaultPrefix) { | ||
| this.#storage = storage | ||
| this.#prefix = prefix | ||
| } | ||
|
|
||
| async get(key: string): Promise<T | undefined> { | ||
| const stored = this.#storage.getItem(this.#prefix + key) | ||
| if (stored === null) { | ||
| return undefined | ||
| } | ||
|
|
||
| try { | ||
| return JSON.parse(stored) as T | ||
| } catch { | ||
| // Left by an older version, or by something else on the origin. | ||
| await this.delete(key) | ||
|
|
||
| return undefined | ||
| } | ||
| } | ||
|
|
||
| async set(key: string, value: T): Promise<void> { | ||
| this.#storage.setItem(this.#prefix + key, JSON.stringify(value, rejectCryptoKey)) | ||
| } | ||
|
|
||
| async delete(key: string): Promise<void> { | ||
| this.#storage.removeItem(this.#prefix + key) | ||
| } | ||
| } | ||
|
|
||
| function rejectCryptoKey(_: string, value: unknown): unknown { | ||
| if (typeof CryptoKey !== "undefined" && value instanceof CryptoKey) { | ||
| throw new TypeError("A CryptoKey cannot be stored in web storage, because JSON.stringify would silently discard it. Use IndexedDbSessionCache instead.") | ||
| } | ||
|
|
||
| return value | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.