-
Notifications
You must be signed in to change notification settings - Fork 4
refactor(pds-core): extract OAuth client-id resolution from CSS middleware #241
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
Merged
+171
−23
Merged
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
83 changes: 83 additions & 0 deletions
83
packages/pds-core/src/__tests__/oauth-request-context.test.ts
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,83 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { resolveOAuthClientIdFromQuery } from '../lib/oauth-request-context.js' | ||
|
|
||
| const CLIENT_ID = 'https://app.example/client.json' | ||
| const REQUEST_URI = 'urn:ietf:params:oauth:request_uri:req-123' | ||
|
|
||
| describe('resolveOAuthClientIdFromQuery', () => { | ||
| it('prefers an explicit client_id query parameter', async () => { | ||
| const resolver = vi.fn() | ||
|
|
||
| await expect( | ||
| resolveOAuthClientIdFromQuery({ client_id: CLIENT_ID }, resolver), | ||
| ).resolves.toBe(CLIENT_ID) | ||
| // The PAR round-trip is skipped entirely when the id is already present. | ||
| expect(resolver).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('prefers client_id even when a request_uri is also present', async () => { | ||
| const resolver = vi.fn().mockResolvedValue('https://other.example/c.json') | ||
|
|
||
| await expect( | ||
| resolveOAuthClientIdFromQuery( | ||
| { client_id: CLIENT_ID, request_uri: REQUEST_URI }, | ||
| resolver, | ||
| ), | ||
| ).resolves.toBe(CLIENT_ID) | ||
| expect(resolver).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('falls back to resolving the PAR request_uri', async () => { | ||
| const resolver = vi.fn().mockResolvedValue(CLIENT_ID) | ||
|
|
||
| await expect( | ||
| resolveOAuthClientIdFromQuery({ request_uri: REQUEST_URI }, resolver), | ||
| ).resolves.toBe(CLIENT_ID) | ||
| expect(resolver).toHaveBeenCalledWith(REQUEST_URI) | ||
| }) | ||
|
|
||
| it('returns undefined when the resolver finds no client for the request_uri', async () => { | ||
| const resolver = vi.fn().mockResolvedValue(undefined) | ||
|
|
||
| await expect( | ||
| resolveOAuthClientIdFromQuery({ request_uri: REQUEST_URI }, resolver), | ||
| ).resolves.toBeUndefined() | ||
| }) | ||
|
|
||
| it('returns undefined when no resolver is supplied', async () => { | ||
| await expect( | ||
| resolveOAuthClientIdFromQuery({ request_uri: REQUEST_URI }), | ||
| ).resolves.toBeUndefined() | ||
| }) | ||
|
|
||
| it('returns undefined for an empty query', async () => { | ||
| const resolver = vi.fn() | ||
|
|
||
| await expect( | ||
| resolveOAuthClientIdFromQuery({}, resolver), | ||
| ).resolves.toBeUndefined() | ||
| expect(resolver).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it.each([ | ||
| { name: 'non-string client_id', query: { client_id: 42 } }, | ||
| { name: 'non-string request_uri', query: { request_uri: 42 } }, | ||
| ])('ignores a $name', async ({ query }) => { | ||
| const resolver = vi.fn().mockResolvedValue(CLIENT_ID) | ||
|
|
||
| await expect( | ||
| resolveOAuthClientIdFromQuery(query, resolver), | ||
| ).resolves.toBeUndefined() | ||
| expect(resolver).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it('propagates resolver rejections to the caller', async () => { | ||
| // Deliberate: the module leaves error handling to each middleware so | ||
| // they can choose their own logging and fallback behaviour. | ||
| const resolver = vi.fn().mockRejectedValue(new Error('PAR lookup failed')) | ||
|
|
||
| await expect( | ||
| resolveOAuthClientIdFromQuery({ request_uri: REQUEST_URI }, resolver), | ||
| ).rejects.toThrow('PAR lookup failed') | ||
| }) | ||
| }) |
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,37 @@ | ||
| /** | ||
| * OAuth request-context helpers for pds-core response enrichment. | ||
| * | ||
| * OAuth authorize pages may receive either a direct `client_id` query | ||
| * parameter or only a PAR `request_uri`. This module owns the narrow | ||
| * resolution step from the current request query to the OAuth client id, | ||
| * while callers keep their feature-specific policy decisions local. | ||
| */ | ||
|
|
||
| export type OAuthRequestQuery = Record<string, unknown> | ||
|
|
||
| export type ResolveClientIdFromRequestUri = ( | ||
| requestUri: string, | ||
| ) => Promise<string | undefined> | ||
|
|
||
| /** | ||
| * Resolve the OAuth client id visible from the current request query. | ||
| * | ||
| * Prefer an explicit `client_id` query parameter. When it is absent, | ||
| * optionally resolve the PAR `request_uri` through the provider request | ||
| * manager supplied by the caller. Resolver errors are intentionally left | ||
| * to the caller so each middleware can choose its own logging and fallback | ||
| * behaviour. | ||
| */ | ||
| export async function resolveOAuthClientIdFromQuery( | ||
| query: OAuthRequestQuery, | ||
| resolveClientIdFromRequestUri?: ResolveClientIdFromRequestUri, | ||
| ): Promise<string | undefined> { | ||
| if (typeof query.client_id === 'string') return query.client_id | ||
| if (!resolveClientIdFromRequestUri) return undefined | ||
|
|
||
| const requestUri = | ||
| typeof query.request_uri === 'string' ? query.request_uri : undefined | ||
| if (!requestUri) return undefined | ||
|
|
||
| return resolveClientIdFromRequestUri(requestUri) | ||
| } |
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.