Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

8.6 KB
Raw Blame
import { browserSupportsWebAuthnAutofill, startAuthentication } from '@simplewebauthn/browser'
import { expectLogic } from 'kea-test-utils'

import { apiStatusLogic } from 'lib/logic/apiStatusLogic'
import { loginLogic } from 'scenes/authentication/auth/loginLogic'
import { passkeyLogic } from 'scenes/authentication/shared/passkeyLogic'

import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'

jest.mock('@simplewebauthn/browser', () => ({
    startAuthentication: jest.fn(),
    browserSupportsWebAuthnAutofill: jest.fn(),
}))

// isWebKitBrowser() reads navigator.vendor: "Apple Computer, Inc." on WebKit, "Google Inc." on Chromium.
const WEBKIT_VENDOR = 'Apple Computer, Inc.'
const CHROMIUM_VENDOR = 'Google Inc.'

function setVendor(vendor: string): void {
    Object.defineProperty(window.navigator, 'vendor', { value: vendor, configurable: true })
}

describe('passkeyLogic', () => {
    describe('startConditionalPasskeyLogin (WebKit-only passkey autofill)', () => {
        let logic: ReturnType<typeof passkeyLogic.build>
        let beginHandler: jest.Mock
        const originalVendor = window.navigator.vendor

        beforeEach(() => {
            setVendor(WEBKIT_VENDOR)
            ;(browserSupportsWebAuthnAutofill as jest.Mock).mockResolvedValue(true)
            // Resolve the passkey prompt as a cancellation so it settles without a page reload.
            ;(startAuthentication as jest.Mock).mockRejectedValue(
                Object.assign(new Error('cancelled'), { name: 'AbortError' })
            )
            beginHandler = jest.fn(() => [
                200,
                {
                    challenge: 'abc',
                    timeout: 60000,
                    rpId: 'localhost',
                    allowCredentials: [{ id: 'cred-1', type: 'public-key' }],
                    userVerification: 'required',
                },
            ])
            useMocks({
                get: { '/api/users/@me/': () => [200, {}] },
                post: { '/api/webauthn/login/begin/': beginHandler },
            })
            initKeaTests()
            logic = passkeyLogic()
            logic.mount()
        })

        afterEach(() => {
            logic.unmount()
            setVendor(originalVendor)
            jest.clearAllMocks()
        })

        it('on WebKit, requests a passkey via browser autofill with no credential constraint', async () => {
            logic.actions.startConditionalPasskeyLogin()
            await expectLogic(logic).toFinishAllListeners()

            expect(beginHandler).toHaveBeenCalledTimes(1)
            const options = (startAuthentication as jest.Mock).mock.calls[0][0]
            expect(options.useBrowserAutofill).toBe(true)
            // Conditional UI must not constrain credentials — the browser offers all discoverable passkeys.
            expect(options.optionsJSON.allowCredentials).toEqual([])
        })

        it('does nothing on a non-WebKit browser (those use the auto-modal instead)', async () => {
            setVendor(CHROMIUM_VENDOR)

            logic.actions.startConditionalPasskeyLogin()
            await expectLogic(logic).toFinishAllListeners()

            expect(beginHandler).not.toHaveBeenCalled()
            expect(startAuthentication).not.toHaveBeenCalled()
        })

        it('does nothing when the browser does not support autofill', async () => {
            ;(browserSupportsWebAuthnAutofill as jest.Mock).mockResolvedValue(false)

            logic.actions.startConditionalPasskeyLogin()
            await expectLogic(logic).toFinishAllListeners()

            expect(beginHandler).not.toHaveBeenCalled()
            expect(startAuthentication).not.toHaveBeenCalled()
        })

        it('starts only one passkey request when triggered repeatedly', async () => {
            logic.actions.startConditionalPasskeyLogin()
            logic.actions.startConditionalPasskeyLogin()
            await expectLogic(logic).toFinishAllListeners()

            expect(beginHandler).toHaveBeenCalledTimes(1)
        })
    })

    describe('re-authentication', () => {
        let logic: ReturnType<typeof passkeyLogic.build>

        beforeEach(() => {
            ;(startAuthentication as jest.Mock).mockResolvedValue({ id: 'cred-1', response: {} })
            useMocks({
                get: { '/api/users/@me/': () => [200, {}] },
                post: {
                    '/api/webauthn/login/begin/': () => [
                        200,
                        {
                            challenge: 'abc',
                            timeout: 60000,
                            rpId: 'localhost',
                            allowCredentials: [{ id: 'cred-1', type: 'public-key' }],
                            userVerification: 'required',
                        },
                    ],
                    '/api/webauthn/login/complete/': () => [200, {}],
                },
            })
            initKeaTests()
            logic = passkeyLogic()
            logic.mount()
        })

        afterEach(() => {
            logic.unmount()
            jest.clearAllMocks()
        })

        it('signals the sensitive action waiting on re-authentication', async () => {
            const onSuccess = jest.fn()
            const onFailure = jest.fn()
            // A sensitive action (e.g. inviting members) parks this pair while it awaits re-auth.
            // Without the signal it stays pending forever and its button spins.
            apiStatusLogic.actions.setTimeSensitiveAuthenticationRequired([onSuccess, onFailure])

            logic.actions.beginPasskeyLogin(undefined, { reauth: 'true' })
            await expectLogic(logic).toFinishAllListeners()

            expect(onSuccess).toHaveBeenCalledTimes(1)
            expect(onFailure).not.toHaveBeenCalled()
            expect(apiStatusLogic.values.timeSensitiveAuthenticationRequired).toBe(false)
        })

        it('clears the gate even when the sensitive action throws', async () => {
            const onSuccess = jest.fn(() => {
                throw new Error('sensitive action failed')
            })
            apiStatusLogic.actions.setTimeSensitiveAuthenticationRequired([onSuccess, jest.fn()])

            logic.actions.beginPasskeyLogin(undefined, { reauth: 'true' })
            await expectLogic(logic).toFinishAllListeners()

            expect(onSuccess).toHaveBeenCalledTimes(1)
            expect(apiStatusLogic.values.timeSensitiveAuthenticationRequired).toBe(false)
            expect(logic.values.isReauth).toBe(false)
        })
    })

    describe('SSO-enforced account', () => {
        let logic: ReturnType<typeof passkeyLogic.build>

        beforeEach(() => {
            ;(startAuthentication as jest.Mock).mockResolvedValue({ id: 'cred-1', response: {} })
            useMocks({
                get: { '/api/users/@me/': () => [200, {}] },
                post: {
                    '/api/webauthn/login/begin/': () => [
                        200,
                        {
                            challenge: 'abc',
                            timeout: 60000,
                            rpId: 'localhost',
                            allowCredentials: [],
                            userVerification: 'required',
                        },
                    ],
                    '/api/webauthn/login/complete/': () => [
                        400,
                        {
                            error: 'You can only login with SSO for this account.',
                            code: 'sso_enforced',
                            sso_enforcement: 'google-oauth2',
                            email: '[email protected]',
                        },
                    ],
                    '/api/login/precheck': () => [200, { sso_enforcement: 'google-oauth2', saml_available: false }],
                },
            })
            initKeaTests()
            logic = passkeyLogic()
            logic.mount()
        })

        afterEach(() => {
            logic.unmount()
            jest.clearAllMocks()
        })

        it('names the required provider and runs the precheck for the returned email', async () => {
            logic.actions.beginPasskeyLogin()
            await expectLogic(loginLogic).toDispatchActions(['precheckSuccess'])

            expect(loginLogic.values.generalError).toEqual({
                code: 'passkey_error',
                detail: 'Your organization requires you to log in with Google.',
            })
            expect(loginLogic.values.login.email).toBe('[email protected]')
            expect(loginLogic.values.precheckResponse).toMatchObject({
                email: '[email protected]',
                sso_enforcement: 'google-oauth2',
            })
        })
    })
})