Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/DPoPTokenProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import type { GetCodeCallback } from "./GetCodeCallback.js"
import type { TokenProvider } from "./TokenProvider.js"
import type { GetIssuerCallback } from "./GetIssuerCallback.js"

type CacheEntry = { created: number, tokenResult: oauth.TokenEndpointResponse, dpopKey: CryptoKeyPair }

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locally represents what we cache.
Will move out of here once cache also moves out.


export class DPoPTokenProvider implements TokenProvider {
readonly #getCode: GetCodeCallback
readonly #callbackUri: string
readonly #getIssuer: GetIssuerCallback
readonly #cache = new Map<string, CacheEntry> // TODO: Take cache from caller

constructor(callbackUri: string, getCodeCallback: GetCodeCallback, getIssuerCallback: GetIssuerCallback) {
this.#getCode = getCodeCallback
Expand All @@ -20,6 +23,23 @@ export class DPoPTokenProvider implements TokenProvider {
}

async upgrade(request: Request): Promise<Request> {
// TODO: More robust key via callback to support complex caching scenarios
let tokenData = this.#cache.get(request.url)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The request url is not necessarily the right cache key. I'd suggest a callback to map from request to key.

For instance, we will often want this token to be shareable across all URLs in one or multiple storages.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// TODO: Support actively refreshing the token
if (tokenData === undefined || isExpired(tokenData)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (tokenData === undefined || isExpired(tokenData)) {
// TODO: Support proactive refreshing as well
if (tokenData === undefined || isExpired(tokenData)) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tokenData = await this.obtainToken(request)
this.#cache.set(request.url, tokenData)
}

const headers = new Headers(request.headers)

headers.set("DPoP", await DPoP.generateProof(tokenData.dpopKey, request.url, request.method, undefined, tokenData.tokenResult.access_token))
headers.set("Authorization", ["DPoP", tokenData.tokenResult.access_token].join(" "))

return new Request(request, {headers})
}

private async obtainToken(request: Request): Promise<CacheEntry> {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All upgrade logic except caching is unchanged in this extracted method.

const issuer = await this.#getIssuer(request)

const discoveryResponse = await oauth.discoveryRequest(issuer, {signal: request.signal})
Expand Down Expand Up @@ -83,12 +103,7 @@ export class DPoPTokenProvider implements TokenProvider {

const tokenResult = await oauth.processAuthorizationCodeResponse(authorizationServer, clientRegistration, tokenResponse, {expectedNonce: this.nonceVerificationOverride(authorizationServer.issuer, nonce)})

const headers = new Headers(request.headers)

headers.set("DPoP", await DPoP.generateProof(dpopKey, request.url, request.method, undefined, tokenResult.access_token))
headers.set("Authorization", ["DPoP", tokenResult.access_token].join(" "))

return new Request(request, {headers})
return {created: Date.now(), tokenResult, dpopKey}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the newly extracted method we don't actually upgrade the request.
That has moved to 32-37 above.

}

private getClientAuth(issuer: string, client: oauth.OmitSymbolProperties<oauth.Client>): oauth.ClientAuth {
Expand Down Expand Up @@ -144,3 +159,9 @@ function clientSecretBasicFor(issuer: string): (clientSecret: string) => oauth.C

return oauth.ClientSecretBasic
}

function isExpired(tokenData: CacheEntry) {
// TODO: Add some headroom (expire a bit before limit)
// TODO: What to do when `expires_in` is Missing? (optional in https://datatracker.ietf.org/doc/html/rfc6749#section-4.2.2)
return Date.now() - tokenData.created > tokenData.tokenResult.expires_in! * 1_000;
}