Skip to content
Open
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
6 changes: 5 additions & 1 deletion packages/sdks/web-component/src/lib/helpers/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,11 @@ export const updateScreenFromScreenState = (
baseEle: HTMLElement,
screenState?: ScreenState,
) => {
replaceElementInputs(baseEle, screenState?.inputs);
const screenInputs = {
...screenState?.inputs,
...(screenState?.totp?.key ? { 'totp.key': screenState.totp.key } : {}),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see we are passing the other totp keys here,
so why do we need special treatment for this one?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we want to support the population of this one in a read-only input TOTP Key editor component.

};
replaceElementInputs(baseEle, screenInputs);
replaceElementInputs(baseEle, screenState?.form);
};

Expand Down
2 changes: 1 addition & 1 deletion packages/sdks/web-component/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface ScreenState {
inputs?: Record<string, string>; // Backward compatibility
lastAuth?: LastAuthState;
project?: Project;
totp?: { image?: string; provisionUrl?: string };
totp?: { image?: string; provisionUrl?: string; key?: string };
notp?: { image?: string; redirectUrl?: string };
selfProvisionDomains?: unknown;
user?: unknown;
Expand Down
39 changes: 39 additions & 0 deletions packages/sdks/web-component/test/helpers/templates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,45 @@ describe('templates', () => {
await waitFor(() => screen.getByShadowDisplayValue('email1'));
});

it('should forward totp.key from screenState to inputs', async () => {
document.body.innerHTML = `<div>
<input class="descope-input" name="totp.key">
</div>`;

updateScreenFromScreenState(document.body, {
totp: { key: 'my-totp-key' },
});
await waitFor(() => screen.getByShadowDisplayValue('my-totp-key'));
});

it('should forward totp.key alongside other inputs', async () => {
document.body.innerHTML = `<div>
<input class="descope-input" name="email">
<input class="descope-input" name="totp.key">
</div>`;

updateScreenFromScreenState(document.body, {
inputs: { email: 'email1' },
totp: { key: 'totp-key-123' },
});
await waitFor(() => screen.getByShadowDisplayValue('email1'));
await waitFor(() => screen.getByShadowDisplayValue('totp-key-123'));
});

it('should not set totp.key input when totp is missing', async () => {
document.body.innerHTML = `<div>
<input class="descope-input" name="totp.key">
</div>`;

updateScreenFromScreenState(document.body, {
inputs: { email: 'email1' },
});
const input = document.querySelector(
'input[name="totp.key"]',
) as HTMLInputElement;
expect(input.value).toBe('');
});

it('should handle descope form', async () => {
document.body.innerHTML = `<div>
<input class="descope-input" name="email"></input>
Expand Down
Loading