Repository navigation
feat(backend): expose the normalized act claim on verified OAuth access tokens #9929
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
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/backend': minor | ||
| --- | ||
|
|
||
| Verified OAuth access tokens now expose `act`, the RFC 8693 actor chain, when the token was issued by an OAuth 2.0 Token Exchange. It is available on the `IdPOAuthAccessToken` returned for both opaque and JWT access tokens. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,32 @@ | ||
| import type { JwtPayload } from '@clerk/shared/types'; | ||
|
|
||
| import type { IdPOAuthAccessTokenJSON } from './JSON'; | ||
| import type { IdPOAuthAccessTokenActorJSON, IdPOAuthAccessTokenJSON } from './JSON'; | ||
|
|
||
| type OAuthJwtPayload = JwtPayload & { | ||
| aud?: string | string[]; | ||
| jti?: string; | ||
| client_id?: string; | ||
| scope?: string; | ||
| scp?: string[]; | ||
| act?: unknown; | ||
| }; | ||
|
|
||
| function toActor(value: unknown, nestedLevels: number): IdPOAuthAccessTokenActorJSON | undefined { | ||
| if (typeof value !== 'object' || value === null) { | ||
| return undefined; | ||
| } | ||
| const { iss, sub, act } = value as Record<string, unknown>; | ||
| if (typeof sub !== 'string') { | ||
| return undefined; | ||
| } | ||
| const nested = nestedLevels > 0 ? toActor(act, nestedLevels - 1) : undefined; | ||
| return { | ||
| ...(typeof iss === 'string' ? { iss } : {}), | ||
| sub, | ||
| ...(nested ? { act: nested } : {}), | ||
| }; | ||
| } | ||
|
|
||
| export class IdPOAuthAccessToken { | ||
| constructor( | ||
| readonly id: string, | ||
|
|
@@ -28,6 +45,8 @@ export class IdPOAuthAccessToken { | |
| readonly updatedAt: number, | ||
| /** The intended audience for the access token. */ | ||
| readonly aud?: string[], | ||
| /** The actor chain of a token issued by an OAuth 2.0 Token Exchange (RFC 8693 section 4.1). */ | ||
| readonly act?: IdPOAuthAccessTokenActorJSON, | ||
| ) {} | ||
|
|
||
| static fromJSON(data: IdPOAuthAccessTokenJSON) { | ||
|
|
@@ -44,6 +63,7 @@ export class IdPOAuthAccessToken { | |
| data.created_at, | ||
| data.updated_at, | ||
| data.aud, | ||
| toActor(data.act, 1), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. under what conditions would
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it wouldn't! the value here is for tracking depth as it's nested
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay let's switch to a boolean instead then to make that clearer |
||
| ); | ||
| } | ||
|
|
||
|
|
@@ -69,6 +89,7 @@ export class IdPOAuthAccessToken { | |
| payload.iat * 1000, // milliseconds: createdAt, converted from JWT iat claim | ||
| payload.iat * 1000, // milliseconds: updatedAt, no JWT equivalent, defaults to iat | ||
| oauthPayload.aud === undefined ? undefined : [oauthPayload.aud].flat(), | ||
| toActor(oauthPayload.act, 1), | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -280,6 +280,69 @@ describe('tokens.verifyMachineAuthToken(token, options)', () => { | |
| expect(data.aud).toEqual(aud); | ||
| }); | ||
|
|
||
| describe.each(['opaque', 'at+jwt', 'application/at+jwt'] as const)('%s OAuth token actor', format => { | ||
| const chainedAct = { | ||
| iss: 'https://clerk.oauth.example.test', | ||
| sub: 'client_exchanging', | ||
| act: { iss: 'https://clerk.oauth.example.test', sub: 'client_subject' }, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.setSystemTime(new Date(mockOAuthAccessTokenJwtPayload.iat * 1000)); | ||
| }); | ||
|
|
||
| async function verifyWithAct(act: unknown) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value Declare the helper’s return type. Set the return type of 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| let token: string; | ||
| if (format === 'opaque') { | ||
| token = 'oat_8XOIucKvqHVr5tYP123456789abcdefghij'; | ||
| server.use( | ||
| http.post('https://api.clerk.test/oauth_applications/access_tokens/verify', () => | ||
| HttpResponse.json({ | ||
| object: 'clerk_idp_oauth_access_token', | ||
| ...mockVerificationResults.oauth_token, | ||
| ...(act === undefined ? {} : { act }), | ||
| }), | ||
| ), | ||
| ); | ||
| } else { | ||
| server.use(http.get('https://api.clerk.test/v1/jwks', () => HttpResponse.json(mockJwks))); | ||
| token = await createSignedOAuthJwt({ ...mockOAuthAccessTokenJwtPayload, act }, format); | ||
| } | ||
|
|
||
| const result = await verifyMachineAuthToken(token, { | ||
| apiUrl: 'https://api.clerk.test', | ||
| secretKey: 'a-valid-key', | ||
| }); | ||
|
|
||
| expect(result.errors).toBeUndefined(); | ||
| return (result.data as IdPOAuthAccessToken).act; | ||
| } | ||
|
|
||
| it.each([{ act: undefined }, { act: { sub: 'client_exchanging' } }, { act: chainedAct }])( | ||
| 'returns act=$act', | ||
| async ({ act }) => { | ||
| expect(await verifyWithAct(act)).toEqual(act); | ||
| }, | ||
| ); | ||
|
|
||
| it('keeps only iss, sub, and one nested actor', async () => { | ||
| const act = await verifyWithAct({ | ||
| ...chainedAct, | ||
| extra: 'dropped', | ||
| act: { ...chainedAct.act, extra: 'dropped', act: { sub: 'client_deeper' } }, | ||
| }); | ||
|
|
||
| expect(act).toStrictEqual(chainedAct); | ||
| }); | ||
|
|
||
| it.each([{ act: 'client_exchanging' }, { act: { iss: 'https://clerk.oauth.example.test' } }, { act: { sub: 42 } }])( | ||
| 'drops malformed act=$act', | ||
| async ({ act }) => { | ||
| expect(await verifyWithAct(act)).toBeUndefined(); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| describe.each(['opaque', 'at+jwt', 'application/at+jwt'] as const)('%s OAuth token audience verification', format => { | ||
| const audience = 'https://resource.example.com'; | ||
| const otherAudience = 'https://other.example.com'; | ||
|
|
||
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.
📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value
Declare the new public property explicitly.
Add
publicto theactparameter property. As per coding guidelines, “Usepublicexplicitly for clarity in public APIs.”🤖 Prompt for AI Agents
Source: Coding guidelines