-
Notifications
You must be signed in to change notification settings - Fork 389
feat: signal handshake nonce support for all flows #5908
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
Closed
Closed
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
c8379a0
feat: oauth hadnshake nonce support
jacekradko 6df50f4
Merge branch 'main' into feat/signal-handshake-nonce-support-oauth
jacekradko 583f41c
Merge branch 'main' into feat/signal-handshake-nonce-support-oauth
jacekradko f3517a1
Merge branch 'main' into feat/signal-handshake-nonce-support-oauth
jacekradko 5e80be1
add comment
jacekradko 1946bc8
format
jacekradko 2608c89
set cookie on fapi domain
jacekradko 0d7aae9
wip
jacekradko e37ecd7
remove clerk.
jacekradko 09ee066
wip
jacekradko 07d25d4
Merge branch 'main' into feat/signal-handshake-nonce-support-oauth
jacekradko 06fbe9d
wip
jacekradko 0723b0e
fix build
jacekradko f51b719
wip
jacekradko 88a4e31
wip
jacekradko 7615018
Merge branch 'main' into feat/signal-handshake-nonce-support-oauth
jacekradko 3764d2e
wip
jacekradko d48ea78
wip
jacekradko 335245c
wip
jacekradko 962c95a
wip
jacekradko eb4c138
wip
jacekradko 7249bb9
wip
jacekradko 08e0e8e
wip
jacekradko 4ca3450
wip
jacekradko e533c8f
wip
jacekradko fd5207d
Merge branch 'main' into feat/signal-handshake-nonce-support-oauth
jacekradko 280a5cb
wip
jacekradko 864809c
Merge branch 'main' into feat/signal-handshake-nonce-support-oauth
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
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
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
88 changes: 88 additions & 0 deletions
88
packages/backend/src/util/__tests__/handshakeUtils.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,88 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { constants, SUPPORTED_HANDSHAKE_FORMAT } from '../../constants'; | ||
import type { AuthenticateContext } from '../../tokens/authenticateContext'; | ||
import { | ||
appendHandshakeFormatToOAuthCallback, | ||
createHandshakeFormatHeaders, | ||
getHandshakeFormatCookie, | ||
} from '../handshakeUtils'; | ||
|
||
describe('handshakeUtils', () => { | ||
const mockAuthenticateContextWithNonce = { | ||
canHandleNonceHandshake: () => true, | ||
getHandshakeFormat: () => 'nonce' as const, | ||
} as AuthenticateContext; | ||
|
||
const mockAuthenticateContextWithToken = { | ||
canHandleNonceHandshake: () => false, | ||
getHandshakeFormat: () => 'token' as const, | ||
} as AuthenticateContext; | ||
|
||
describe('appendHandshakeFormatToOAuthCallback', () => { | ||
it('should append handshake format parameter when nonce is supported', () => { | ||
const originalUrl = 'https://example.com/oauth/callback'; | ||
const modifiedUrl = appendHandshakeFormatToOAuthCallback(originalUrl, mockAuthenticateContextWithNonce); | ||
|
||
const url = new URL(modifiedUrl); | ||
expect(url.searchParams.get(constants.QueryParameters.HandshakeFormat)).toBe(SUPPORTED_HANDSHAKE_FORMAT); | ||
}); | ||
|
||
it('should not append handshake format parameter when token is used', () => { | ||
const originalUrl = 'https://example.com/oauth/callback'; | ||
const modifiedUrl = appendHandshakeFormatToOAuthCallback(originalUrl, mockAuthenticateContextWithToken); | ||
|
||
const url = new URL(modifiedUrl); | ||
expect(url.searchParams.get(constants.QueryParameters.HandshakeFormat)).toBeNull(); | ||
}); | ||
|
||
it('should preserve existing query parameters', () => { | ||
const originalUrl = 'https://example.com/oauth/callback?existing=param&other=value'; | ||
const modifiedUrl = appendHandshakeFormatToOAuthCallback(originalUrl, mockAuthenticateContextWithNonce); | ||
|
||
const url = new URL(modifiedUrl); | ||
expect(url.searchParams.get('existing')).toBe('param'); | ||
expect(url.searchParams.get('other')).toBe('value'); | ||
expect(url.searchParams.get(constants.QueryParameters.HandshakeFormat)).toBe(SUPPORTED_HANDSHAKE_FORMAT); | ||
}); | ||
}); | ||
|
||
describe('getHandshakeFormatCookie', () => { | ||
it('should return cookie string when nonce is supported', () => { | ||
const cookie = getHandshakeFormatCookie(mockAuthenticateContextWithNonce); | ||
|
||
expect(cookie).toBe( | ||
`${constants.Cookies.HandshakeFormat}=${SUPPORTED_HANDSHAKE_FORMAT}; Path=/; SameSite=None; Secure`, | ||
); | ||
}); | ||
|
||
it('should return null when token is used', () => { | ||
const cookie = getHandshakeFormatCookie(mockAuthenticateContextWithToken); | ||
|
||
expect(cookie).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe('createHandshakeFormatHeaders', () => { | ||
it('should create headers with Set-Cookie when nonce is supported', () => { | ||
const headers = createHandshakeFormatHeaders(mockAuthenticateContextWithNonce); | ||
|
||
const setCookieHeader = headers.get('Set-Cookie'); | ||
expect(setCookieHeader).toBe( | ||
`${constants.Cookies.HandshakeFormat}=${SUPPORTED_HANDSHAKE_FORMAT}; Path=/; SameSite=None; Secure`, | ||
); | ||
}); | ||
|
||
it('should create empty headers when token is used', () => { | ||
const headers = createHandshakeFormatHeaders(mockAuthenticateContextWithToken); | ||
|
||
const setCookieHeader = headers.get('Set-Cookie'); | ||
expect(setCookieHeader).toBeNull(); | ||
}); | ||
|
||
it('should return Headers instance', () => { | ||
const headers = createHandshakeFormatHeaders(mockAuthenticateContextWithNonce); | ||
|
||
expect(headers).toBeInstanceOf(Headers); | ||
}); | ||
}); | ||
}); |
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,51 @@ | ||
import { constants, SUPPORTED_HANDSHAKE_FORMAT, SUPPORTS_HANDSHAKE_NONCE } from '../constants'; | ||
import type { AuthenticateContext } from '../tokens/authenticateContext'; | ||
|
||
/** | ||
* Appends handshake format query parameter to an OAuth callback URL | ||
* so that FAPI knows the backend can handle nonce-based handshakes | ||
* | ||
* @param url - The OAuth callback URL to modify | ||
* @param authenticateContext - The authentication context containing handshake format info | ||
* @returns The modified URL with handshake format parameter | ||
*/ | ||
export function appendHandshakeFormatToOAuthCallback(url: string, authenticateContext: AuthenticateContext): string { | ||
const callbackUrl = new URL(url); | ||
|
||
// If the backend can handle nonce handshakes, add the format parameter | ||
if (authenticateContext.canHandleNonceHandshake()) { | ||
callbackUrl.searchParams.set(constants.QueryParameters.HandshakeFormat, SUPPORTED_HANDSHAKE_FORMAT); | ||
} | ||
|
||
return callbackUrl.toString(); | ||
} | ||
|
||
/** | ||
* Gets the handshake format cookie value that indicates nonce capability | ||
* | ||
* @param authenticateContext - The authentication context | ||
* @returns Cookie string if nonce handshakes are supported, null otherwise | ||
*/ | ||
export function getHandshakeFormatCookie(authenticateContext: AuthenticateContext): string | null { | ||
if (authenticateContext.canHandleNonceHandshake()) { | ||
return `${constants.Cookies.HandshakeFormat}=${SUPPORTED_HANDSHAKE_FORMAT}; Path=/; SameSite=None; Secure`; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Creates headers with handshake format cookie for OAuth flows | ||
* | ||
* @param authenticateContext - The authentication context | ||
* @returns Headers object with Set-Cookie header if nonce is supported | ||
*/ | ||
export function createHandshakeFormatHeaders(authenticateContext: AuthenticateContext): Headers { | ||
const headers = new Headers(); | ||
const cookie = getHandshakeFormatCookie(authenticateContext); | ||
|
||
if (cookie) { | ||
headers.append('Set-Cookie', cookie); | ||
} | ||
|
||
return headers; | ||
} |
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.