-
Notifications
You must be signed in to change notification settings - Fork 406
feat(backend): handle handshake nonce payload #5865
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
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8181f6b
feat(backend): handle handshake nonce payload
jacekradko f8fba57
move HandshakePayload under clients/
jacekradko 08aae86
wip
jacekradko 85e630d
cleanup
jacekradko b720296
use existing api client
jacekradko 0a68ba8
Merge branch 'main' into feat/handle-handshake-payload
jacekradko ee3936b
changeset
jacekradko 4c9e76a
add client api test
jacekradko 39d237c
wip
jacekradko e99dd78
Merge branch 'main' into feat/handle-handshake-payload
jacekradko 94f234e
small refactor for testability
jacekradko 4d75597
fix: do not catch token verification errors
jacekradko 5a4f25e
rename getHandshakePayload to getCookiesFromHandshake
jacekradko b09f649
Merge branch 'main' into feat/handle-handshake-payload
jacekradko 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@clerk/clerk-js': minor | ||
| '@clerk/backend': minor | ||
| --- | ||
|
|
||
| Add handling of new Handshake nonce flow when authenticating requests |
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,98 @@ | ||
| import { http, HttpResponse } from 'msw'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { server, validateHeaders } from '../../mock-server'; | ||
| import { createBackendApiClient } from '../factory'; | ||
|
|
||
| describe('ClientAPI', () => { | ||
| const apiClient = createBackendApiClient({ | ||
| apiUrl: 'https://api.clerk.test', | ||
| secretKey: 'deadbeef', | ||
| }); | ||
|
|
||
| describe('getHandshakePayload', () => { | ||
| it('successfully fetches the handshake payload with a valid nonce', async () => { | ||
| const mockHandshakePayload = { | ||
| directives: ['directive1', 'directive2'], | ||
| }; | ||
|
|
||
| server.use( | ||
| http.get( | ||
| 'https://api.clerk.test/v1/clients/handshake_payload', | ||
| validateHeaders(({ request }) => { | ||
| const url = new URL(request.url); | ||
| expect(url.searchParams.get('nonce')).toBe('test-nonce-123'); | ||
| return HttpResponse.json(mockHandshakePayload); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| const response = await apiClient.clients.getHandshakePayload({ | ||
| nonce: 'test-nonce-123', | ||
| }); | ||
|
|
||
| expect(response.directives).toEqual(['directive1', 'directive2']); | ||
| expect(response.directives.length).toBe(2); | ||
| }); | ||
|
|
||
| it('handles error responses correctly', async () => { | ||
| const mockErrorPayload = { | ||
| code: 'invalid_nonce', | ||
| message: 'Invalid nonce provided', | ||
| long_message: 'The nonce provided is invalid or has expired', | ||
| meta: { param_name: 'nonce' }, | ||
| }; | ||
| const traceId = 'trace_id_handshake'; | ||
|
|
||
| server.use( | ||
| http.get( | ||
| 'https://api.clerk.test/v1/clients/handshake_payload', | ||
| validateHeaders(() => { | ||
| return HttpResponse.json( | ||
| { errors: [mockErrorPayload], clerk_trace_id: traceId }, | ||
| { status: 400, headers: { 'cf-ray': traceId } }, | ||
| ); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| const errResponse = await apiClient.clients.getHandshakePayload({ nonce: 'invalid-nonce' }).catch(err => err); | ||
|
|
||
| expect(errResponse.clerkTraceId).toBe(traceId); | ||
| expect(errResponse.status).toBe(400); | ||
| expect(errResponse.errors[0].code).toBe('invalid_nonce'); | ||
| expect(errResponse.errors[0].message).toBe('Invalid nonce provided'); | ||
| expect(errResponse.errors[0].longMessage).toBe('The nonce provided is invalid or has expired'); | ||
| expect(errResponse.errors[0].meta.paramName).toBe('nonce'); | ||
| }); | ||
|
|
||
| it('requires a nonce parameter', async () => { | ||
| server.use( | ||
| http.get( | ||
| 'https://api.clerk.test/v1/clients/handshake_payload', | ||
| validateHeaders(() => { | ||
| return HttpResponse.json( | ||
| { | ||
| errors: [ | ||
| { | ||
| code: 'missing_parameter', | ||
| message: 'Missing required parameter', | ||
| long_message: 'The nonce parameter is required', | ||
| meta: { param_name: 'nonce' }, | ||
| }, | ||
| ], | ||
| }, | ||
| { status: 400 }, | ||
| ); | ||
| }), | ||
| ), | ||
| ); | ||
|
|
||
| // @ts-expect-error Testing invalid input | ||
| const errResponse = await apiClient.clients.getHandshakePayload({}).catch(err => err); | ||
|
|
||
| expect(errResponse.status).toBe(400); | ||
| expect(errResponse.errors[0].code).toBe('missing_parameter'); | ||
| }); | ||
| }); | ||
| }); | ||
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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,15 +1,11 @@ | ||
| export type HandshakePayloadJSON = { | ||
| nonce: string; | ||
| payload: string; | ||
| directives: string[]; | ||
| }; | ||
|
|
||
| export class HandshakePayload { | ||
| constructor( | ||
| readonly nonce: string, | ||
| readonly payload: string, | ||
| ) {} | ||
| constructor(readonly directives: string[]) {} | ||
|
|
||
| static fromJSON(data: HandshakePayloadJSON): HandshakePayload { | ||
| return new HandshakePayload(data.nonce, data.payload); | ||
| return new HandshakePayload(data.directives); | ||
| } | ||
| } |
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
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.
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.