-
Notifications
You must be signed in to change notification settings - Fork 2
IS-11302: Auto-start WebAuthn ceremonies with a single action #158
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
Open
aleixsuau
wants to merge
1
commit into
feature/IS-11252/webauthn-client-operations
Choose a base branch
from
feature/IS-11302/webauthn-single-option-auto-start
base: feature/IS-11252/webauthn-client-operations
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
51 changes: 51 additions & 0 deletions
51
...-app/src/haapi-stepper/feature/actions/client-operation/operations/webauthn/auto-start.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,51 @@ | ||
| /* | ||
| * Copyright (C) 2025 Curity AB. All rights reserved. | ||
| * | ||
| * The contents of this file are the property of Curity AB. | ||
| * You may not copy or use this file, in either source code | ||
| * or executable form, except in compliance with terms | ||
| * set by Curity AB. | ||
| * | ||
| * For further information, please contact Curity AB. | ||
| */ | ||
|
|
||
| import type { HaapiStepperNextStep, HaapiStepperStep } from '../../../../stepper/haapi-stepper.types'; | ||
| import { | ||
| isPlatformOnlyAnyDeviceWebAuthnRegistrationAction, | ||
| isWebAuthnApiSupported, | ||
| isWebAuthnClientOperationAction, | ||
| isWebAuthnPlatformAuthenticatorAvailable, | ||
| requiresWebAuthnUserInteraction, | ||
| } from './utils'; | ||
|
|
||
| export const manageWebAuthnAutoStart = async ( | ||
| step: HaapiStepperStep, | ||
| nextStep: HaapiStepperNextStep | ||
| ): Promise<void> => { | ||
| if (!isWebAuthnApiSupported() || requiresWebAuthnUserInteraction()) { | ||
| return; | ||
| } | ||
|
|
||
| const clientOperationActions = step.dataHelpers.actions?.clientOperation ?? []; | ||
| const webAuthnActions = clientOperationActions.filter(isWebAuthnClientOperationAction); | ||
|
|
||
| /* | ||
| * Multi-option registration steps (any-device with both platform + cross-platform) | ||
| * are split into two actions by `splitWebAuthnRegistrationAction` upstream, so they | ||
| * won't trigger auto-start. | ||
| */ | ||
| if (webAuthnActions.length !== 1) { | ||
| return; | ||
| } | ||
|
|
||
| const action = webAuthnActions[0]; | ||
|
|
||
| if (isPlatformOnlyAnyDeviceWebAuthnRegistrationAction(action)) { | ||
| const webAuthnPlatformAuthenticatorAvailable = await isWebAuthnPlatformAuthenticatorAvailable(); | ||
| if (!webAuthnPlatformAuthenticatorAvailable) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| nextStep(action); | ||
| }; |
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
184 changes: 184 additions & 0 deletions
184
...p/src/haapi-stepper/feature/actions/client-operation/operations/webauthn/webauthn.spec.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,184 @@ | ||
| /* | ||
| * Copyright (C) 2025 Curity AB. All rights reserved. | ||
| * | ||
| * The contents of this file are the property of Curity AB. | ||
| * You may not copy or use this file, in either source code | ||
| * or executable form, except in compliance with terms | ||
| * set by Curity AB. | ||
| * | ||
| * For further information, please contact Curity AB. | ||
| */ | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { runWebAuthnAuthentication, runWebAuthnRegistration } from './webauthn'; | ||
| import { | ||
| createMockWebAuthnAuthenticationAction, | ||
| createMockWebAuthnCrossPlatformOnlyAnyDeviceAction, | ||
| createMockWebAuthnPlatformOnlyAnyDeviceAction, | ||
| createMockWebAuthnRegistrationAction, | ||
| } from '../../../../../util/tests/mocks'; | ||
|
|
||
| describe('webauthn', () => { | ||
| const abortSignal = new AbortController().signal; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.stubGlobal('PublicKeyCredential', stubPublicKeyCredential()); | ||
| installNavigatorCredentials(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| restoreNavigatorCredentials(); | ||
| }); | ||
|
|
||
| describe('runWebAuthnRegistration', () => { | ||
| it('throws when WebAuthn API is not supported', async () => { | ||
| vi.unstubAllGlobals(); | ||
| const action = createMockWebAuthnRegistrationAction(); | ||
|
|
||
| await expect(runWebAuthnRegistration(action, abortSignal)).rejects.toMatchObject({ | ||
| message: 'WebAuthn API is not supported in this browser', | ||
| }); | ||
| }); | ||
|
|
||
| it('throws when navigator.credentials.create returns null', async () => { | ||
| mockCredentialsCreate.mockResolvedValue(null); | ||
| const action = createMockWebAuthnRegistrationAction(); | ||
|
|
||
| await expect(runWebAuthnRegistration(action, abortSignal)).rejects.toMatchObject({ | ||
| message: 'Could not create credential', | ||
| }); | ||
| }); | ||
|
|
||
| describe('passkey', () => { | ||
| it('parses credentialCreationOptions, creates a credential, and returns a continuation under "credential"', async () => { | ||
| const parsedOptions = { challenge: 'parsed' }; | ||
| const credentialJSON = { id: 'passkey-cred', type: 'public-key' }; | ||
|
|
||
| mockParseCreationOptionsFromJSON.mockReturnValue(parsedOptions); | ||
| mockCredentialsCreate.mockResolvedValue(mockCredential(credentialJSON)); | ||
|
|
||
| const action = createMockWebAuthnRegistrationAction(); | ||
| const result = await runWebAuthnRegistration(action, abortSignal); | ||
|
|
||
| expect(mockParseCreationOptionsFromJSON).toHaveBeenCalledWith( | ||
| action.model.arguments.credentialCreationOptions.publicKey | ||
| ); | ||
| expect(mockCredentialsCreate).toHaveBeenCalledWith({ publicKey: parsedOptions, signal: abortSignal }); | ||
| expect(result).toEqual({ | ||
| action: action.model.continueActions[0], | ||
| payload: { credential: credentialJSON }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('any-device', () => { | ||
| it('platform-only: parses platformCredentialCreationOptions, creates a credential, and returns a continuation under "platformCredential"', async () => { | ||
| const parsedOptions = { challenge: 'platform' }; | ||
| const credentialJSON = { id: 'platform-cred', type: 'public-key' }; | ||
|
|
||
| mockParseCreationOptionsFromJSON.mockReturnValue(parsedOptions); | ||
| mockCredentialsCreate.mockResolvedValue(mockCredential(credentialJSON)); | ||
|
|
||
| const action = createMockWebAuthnPlatformOnlyAnyDeviceAction(); | ||
| const result = await runWebAuthnRegistration(action, abortSignal); | ||
|
|
||
| expect(mockParseCreationOptionsFromJSON).toHaveBeenCalledWith( | ||
| action.model.arguments.platformCredentialCreationOptions?.publicKey | ||
| ); | ||
| expect(mockCredentialsCreate).toHaveBeenCalledWith({ publicKey: parsedOptions, signal: abortSignal }); | ||
| expect(result).toEqual({ | ||
| action: action.model.continueActions[0], | ||
| payload: { platformCredential: credentialJSON }, | ||
| }); | ||
| }); | ||
|
|
||
| it('cross-platform-only: parses crossPlatformCredentialCreationOptions, creates a credential, and returns a continuation under "crossPlatformCredential"', async () => { | ||
| const parsedOptions = { challenge: 'cross-platform' }; | ||
| const credentialJSON = { id: 'cross-platform-cred', type: 'public-key' }; | ||
|
|
||
| mockParseCreationOptionsFromJSON.mockReturnValue(parsedOptions); | ||
| mockCredentialsCreate.mockResolvedValue(mockCredential(credentialJSON)); | ||
|
|
||
| const action = createMockWebAuthnCrossPlatformOnlyAnyDeviceAction(); | ||
| const result = await runWebAuthnRegistration(action, abortSignal); | ||
|
|
||
| expect(mockParseCreationOptionsFromJSON).toHaveBeenCalledWith( | ||
| action.model.arguments.crossPlatformCredentialCreationOptions?.publicKey | ||
| ); | ||
| expect(mockCredentialsCreate).toHaveBeenCalledWith({ publicKey: parsedOptions, signal: abortSignal }); | ||
| expect(result).toEqual({ | ||
| action: action.model.continueActions[0], | ||
| payload: { crossPlatformCredential: credentialJSON }, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('runWebAuthnAuthentication', () => { | ||
| it('throws when WebAuthn API is not supported', async () => { | ||
| vi.unstubAllGlobals(); | ||
| const action = createMockWebAuthnAuthenticationAction(); | ||
|
|
||
| await expect(runWebAuthnAuthentication(action, abortSignal)).rejects.toMatchObject({ | ||
| message: 'WebAuthn API is not supported in this browser', | ||
| }); | ||
| }); | ||
|
|
||
| it('throws when navigator.credentials.get returns null', async () => { | ||
| mockCredentialsGet.mockResolvedValue(null); | ||
| const action = createMockWebAuthnAuthenticationAction(); | ||
|
|
||
| await expect(runWebAuthnAuthentication(action, abortSignal)).rejects.toMatchObject({ | ||
| message: 'Could not get credential', | ||
| }); | ||
| }); | ||
|
|
||
| it('parses credentialRequestOptions, gets a credential, and returns a continuation under "credential"', async () => { | ||
| const parsedOptions = { challenge: 'auth' }; | ||
| const credentialJSON = { id: 'auth-cred', type: 'public-key' }; | ||
|
|
||
| mockParseRequestOptionsFromJSON.mockReturnValue(parsedOptions); | ||
| mockCredentialsGet.mockResolvedValue(mockCredential(credentialJSON)); | ||
|
|
||
| const action = createMockWebAuthnAuthenticationAction(); | ||
| const result = await runWebAuthnAuthentication(action, abortSignal); | ||
|
|
||
| expect(mockParseRequestOptionsFromJSON).toHaveBeenCalledWith( | ||
| action.model.arguments.credentialRequestOptions.publicKey | ||
| ); | ||
| expect(mockCredentialsGet).toHaveBeenCalledWith({ publicKey: parsedOptions, signal: abortSignal }); | ||
| expect(result).toEqual({ | ||
| action: action.model.continueActions[0], | ||
| payload: { credential: credentialJSON }, | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| const mockParseCreationOptionsFromJSON = vi.fn(); | ||
| const mockParseRequestOptionsFromJSON = vi.fn(); | ||
| const mockCredentialsCreate = vi.fn(); | ||
| const mockCredentialsGet = vi.fn(); | ||
|
|
||
| const stubPublicKeyCredential = () => | ||
| Object.assign(vi.fn(), { | ||
| parseCreationOptionsFromJSON: mockParseCreationOptionsFromJSON, | ||
| parseRequestOptionsFromJSON: mockParseRequestOptionsFromJSON, | ||
| }); | ||
|
|
||
| const installNavigatorCredentials = () => { | ||
| Object.defineProperty(navigator, 'credentials', { | ||
| configurable: true, | ||
| value: { create: mockCredentialsCreate, get: mockCredentialsGet }, | ||
| }); | ||
| }; | ||
|
|
||
| const restoreNavigatorCredentials = () => { | ||
| Reflect.deleteProperty(navigator, 'credentials'); | ||
| }; | ||
|
|
||
| const mockCredential = (toJSONResult: unknown = { id: 'cred-id', type: 'public-key' }) => ({ | ||
| toJSON: vi.fn(() => toJSONResult), | ||
| }); |
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.
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.
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.
Ah, right. This was removed in server version 11.0. Since the LWA will released after that, I suggest removing this mention here. The HAAPI docs will be updated at some point.