Debian Codegraph @

docs/webauthn.rst

raw
1 .. _webauthn_topic:
2
3 WebAuthn
4 =========
5
6 WebAuthn/FIDO2 is a `W3C standard`_ that defines a cryptographic protocol between a
7 relying party (your Flask application) and an authenticator. In simple terms this allows connecting
8 your Flask application to a variety of authenticators including dedicated hardware (e.g. YubiKey) and
9 devices that have cryptographic capabilities (e.g. a mobile phone with fingerprint or face id).
10
11 This protocol is supported by all major browsers, and gradually (2025) many major web applications actually
12 support it. Note that those that do normally just support using a WebAuthn credential/passkey as an additional, optional, second factor.
13
14 Note that a WebAuthn credential/passkey can possibly satisfy a complete 2-factor authentication requirement - something you have
15 and something you are (think a mobile device with face-Id). Flask-Security supports this use case.
16
17 Flask-Security uses the term WebAuthn and WebAuthn credential internally and in its JSON API. For the non-developer community, the term 'passkey'
18 has become standard, so all templates and messages refer to WebAuthn credentials as passkeys.
19
20 .. _W3C standard: https://www.w3.org/TR/webauthn-2/
21
22 Getting Started
23 +++++++++++++++
24 To add WebAuthn support to your application you will need:
25
26 - Install the `webauthn`_ package (the Flask-Security pip 'extra' [mfa] will do this).
27 - Add/modify your DB models as described :ref:`here <webauthn_model>`.
28
29 .. _webauthn: https://pypi.org/project/webauthn/
30
31 Key Concepts
32 +++++++++++++
33
34 While the spec is quite complex - there are 2 important concepts to know. WebAuthn keys
35 can be classified as 'platform'/'cross-platform' and 'resident'/or not. If a key is a ``platform`` key
36 that means it is tied to a particular device. If you set up a second factor WebAuthn key that is a ``platform``
37 key you will ONLY BE ABLE TO AUTHENTICATE using that device. For that reason it is a best practice to make sure
38 there is at least one second-factor authentication method setup that is NOT platform specific (such as SMS, an authenticator app, etc.).
39
40 Flask-Security requires that when registering a WebAuthn key, the user must specify whether the key
41 will be used for first/primary authentication or for multi-factor/second authentication.
42
43 It should be noted the the current spec REQUIRES JavaScript to communicate from your front-end to the browser.
44 Flask-Security ships with the basic required JS (static/{webauthn.js,base64.js}).
45 An application should be able to simply wire those into their templates or JavaScript.
46
47
48 Configuration
49 ++++++++++++++
50
51 As with many features in Flask-Security, configuration is a combination of config variables,
52 constructor parameters, and a sub-classable utility class. The WebAuthn spec offers a lot of
53 flexibility in supporting a wide range of authenticators. The default configuration is:
54
55 - Allow a WebAuthn credential/passkey to be used for first/primary authentication (:py:data:`SECURITY_WAN_ALLOW_AS_FIRST_FACTOR` = ``True``)
56 - Allow a WebAuthn credential/passkey to be used as a multi-factor (both first and secondary) if
57 the key supports it (:py:data:`SECURITY_WAN_ALLOW_AS_MULTI_FACTOR` = ``True``)
58 - Allow both 'first' and 'secondary' WebAuthn credentials/passkeys to be used for 'freshness' verification
59 (:py:data:`SECURITY_WAN_ALLOW_AS_VERIFY` = ``True``)
60 - Allow returning WebAuthn credential/passkey names to un-authenticated users (:py:data:`SECURITY_WAN_ALLOW_USER_HINTS` = ``True``)
61 Please see `this`_ portion of the WebAuthn spec for security implications.
62
63
64 The bundled :class:`.WebauthnUtil` class implements the following defaults:
65
66 - The ``AuthenticatorSelectionCriteria`` is set to ``CROSS_PLATFORM`` for webauthn credentials/passkeys being
67 registered for first/primary authentication.
68 - The ``UserVerificationRequirement`` is set to ``DISCOURAGED`` for credentials/passkeys used for secondary
69 authentication, and ``PREFERRED`` for credentials/passkeys used for first/primary or multi-factor.
70
71 .. _this: https://www.w3.org/TR/webauthn-2/#sctn-unprotected-account-detection