MasterKale / SimpleWebAuthn

WebAuthn, Simplified. A collection of TypeScript-first libraries for simpler WebAuthn integration. Supports modern browsers, Node, Deno, and more.
https://simplewebauthn.dev
MIT License
1.63k stars 137 forks source link

Mock authenticator #300

Closed hersche closed 1 year ago

hersche commented 2 years ago

Hi there,

first, thanks for this nice lib and all the work around it, it works really well! :smile:

For my project, i would like to test the integration of your lib in my code, but a pipeline doesn't own a Yubikey.

There would be a solution with a headless chromebrowser and virtual keys - but this is slow and heavy.

Therefore: would it be possible to mock these parts? I tried to think about the flow and it's needs.

Take the register-optionsfrom the endpoint, solve challenge in mocked method. Send the challenge to register-endpoint. Expect success (or fail if issue at server) (Insert new mocked Key in DB with new method directly or take those over from previous registration) Take the login-option from the endpoint, solve challenge in mocked method. Send the challenge to login-endpoint. Expect success (or fail if issue at server)

If it's possible already, i've overseen it :sweat_smile:

MasterKale commented 1 year ago

Hello @hersche, it's been pretty straightforward to override the functionality of navigator.credentials.get in a testing environment and mock the return value. Check out the examples in @simplewebauthn/browser's startAuthentication() tests, hopefully that provides some inspiration to help you move forward with this:

https://github.com/MasterKale/SimpleWebAuthn/blob/master/packages/browser/src/methods/startAuthentication.test.ts

Search for "mockNavigatorGet" and see how I use it to mock good and bad WebAuthn API return values.

MasterKale commented 1 year ago

Hello @hersche, did my advice help? If I don't hear back in a couple of days I'm going to close out this ticket.

hersche commented 1 year ago

Hello @MasterKale, thanks a lot for the response. This looks pretty promising, will try to use it. I think the ticket can be closed as the places you point to show perfect examples :+1: