Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const captureRequestBody = (): Record<string, unknown> => {
return JSON.parse(requestInit.body as string) as Record<string, unknown>;
};

const captureRequestHeaders = (): Record<string, string> => {
const calls = (fetch as ReturnType<typeof vi.fn>).mock.calls;
const requestInit = calls[calls.length - 1][1] as RequestInit;
return requestInit.headers as Record<string, string>;
};

describe('executeEmbeddedSignInFlow', (): void => {
beforeEach((): void => {
vi.resetAllMocks();
Expand Down Expand Up @@ -134,6 +140,37 @@ describe('executeEmbeddedSignInFlow', (): void => {
});
});

describe('Flow-Secret header', (): void => {
it('sends the Flow Secret header on a new flow start', async (): Promise<void> => {
await executeEmbeddedSignInFlow({
flowSecret: 'flow-secret-123',
payload: {applicationId: 'app-1', flowType: 'AUTHENTICATION'},
url: URL,
});

expect(captureRequestHeaders()).toMatchObject({'Flow-Secret': 'flow-secret-123'});
});

it('does NOT send the Flow Secret header on a step submission', async (): Promise<void> => {
await executeEmbeddedSignInFlow({
flowSecret: 'flow-secret-123',
payload: {action: 'submit', executionId: 'exec-abc', inputs: {password: 'secret', username: 'user'}},
url: URL,
});

expect(captureRequestHeaders()).not.toHaveProperty('Flow-Secret');
});

it('does NOT send the Flow Secret header when no flowSecret is provided', async (): Promise<void> => {
await executeEmbeddedSignInFlow({
payload: {applicationId: 'app-1', flowType: 'AUTHENTICATION'},
url: URL,
});

expect(captureRequestHeaders()).not.toHaveProperty('Flow-Secret');
});
});

it('throws when payload is missing', async (): Promise<void> => {
await expect(executeEmbeddedSignInFlow({url: URL})).rejects.toThrow('Authorization payload is required');
});
Expand Down
4 changes: 4 additions & 0 deletions packages/javascript/src/api/executeEmbeddedSignInFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const executeEmbeddedSignInFlow = async ({
baseUrl,
payload,
authId,
flowSecret,
...requestConfig
}: EmbeddedFlowExecuteRequestConfig): Promise<EmbeddedSignInFlowResponse> => {
if (!payload) {
Expand Down Expand Up @@ -74,6 +75,9 @@ const executeEmbeddedSignInFlow = async ({
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
// A backend/server-side application presents its Flow Secret to initiate a new flow. It is
// only relevant on initiation, so it is omitted from continuation requests.
...(isNewFlowStart && flowSecret ? {'Flow-Secret': flowSecret} : {}),
...requestConfig.headers,
} as HeadersInit,
method: requestConfig.method || 'POST',
Expand Down
7 changes: 7 additions & 0 deletions packages/javascript/src/models/embedded-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ export enum EmbeddedFlowResponseType {
*/
export interface EmbeddedFlowExecuteRequestConfigBase<T = any> extends Partial<Request> {
baseUrl?: string;
/**
* Flow Secret authenticating a backend/server-side application when it initiates a new native
* flow directly. When set, it is sent in the `Flow-Secret` request header, and only on a new
* flow initiation (a payload carrying `applicationId` and `flowType`) — never on continuation
* requests. Not applicable to public clients or redirect-based applications.
*/
flowSecret?: string;
payload?: T;
url?: string;
}
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/src/ThunderIDNextClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ class ThunderIDNextClient<T extends ThunderIDNextConfig = ThunderIDNextConfig> e

return executeEmbeddedSignInFlow({
baseUrl: configData?.baseUrl,
flowSecret: arg2?.flowSecret,
payload: arg1,
url: arg2?.url,
}) as unknown as Promise<EmbeddedSignInFlowResponse>;
Expand Down
Loading