Stormbase / django-otp-webauthn

Passkey support for Django. Currently in early stages of development and not ready for production use!
BSD 3-Clause "New" or "Revised" License
10 stars 2 forks source link

Add django authentication backend and resolve http 500 error logging in when multiple authentication backends are enabled. #8

Closed jmichalicek closed 1 month ago

jmichalicek commented 2 months ago

For a more general overview of what happens and how this resolves it, here's how the django auth and login systems work.

This bug was happening if multiple auth backends were configured, which is common, because if this was the primary/only authentication method then no backend was set, so the check for a backend arg and user.backend failed and then the check for only a single auth backend configured failed.

Additionally, once an auth backend is set, which is stored as part of the session, AuthenticationMiddlware looks at the session to see which backend was used, and then calls the get_user() method on that backend, passing in the user id from the session. I kept this simple and just look up the user directly in that method. I initially considered going through the credential model from get_credential_model() but at this point all we have is a user id, so while that could verify that there is some webauthn credential we wouldn't know which one was used and only that one exists now, so just going straight to the user seems just as useful. Arguably going through the credential model at least ensures that some webauthn credential exists and so is probably the one which was used but the views already do enough work around that.

I do feel like maybe more of the logic should actually live in this auth backend, but since we only want to use this backend if the user is not authenticated via a different method and the rest of the auth code is relevant even when using this as 2fa for an already authenticated user, sorting that logic out felt out of scope for this if it is even reasonably possible.

jmichalicek commented 2 months ago

I jumped the gun on this PR, but fortunately caught a bug this morning before it had been merged and fixed it up along with a minor tweak for clarity and intent of some code.

jmichalicek commented 2 months ago

Thanks again for the helpful PR @jmichalicek, great explanation about authentication backends too 👍

Would you please update the README instructions too to include adding this backend to the settings?

Ack. Yes, I meant to update the documentation. Good catch there.