| 1 |
""" |
| 2 |
flask_security.webauthn_util |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
Utility class providing methods controlling various aspects of webauthn. |
| 6 |
|
| 7 |
:copyright: (c) 2020-2026 by J. Christopher Wagner (jwag). |
| 8 |
:license: MIT, see LICENSE for more details. |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
from __future__ import annotations |
| 13 |
|
| 14 |
import secrets |
| 15 |
import typing as t |
| 16 |
|
| 17 |
from flask import current_app, request |
| 18 |
|
| 19 |
from .utils import _config_value as cv, input_svn, get_message |
| 20 |
|
| 21 |
try: |
| 22 |
# noinspection PyUnresolvedReferences |
| 23 |
from webauthn.helpers.structs import ( |
| 24 |
AuthenticatorAttachment, |
| 25 |
AuthenticatorSelectionCriteria, |
| 26 |
ResidentKeyRequirement, |
| 27 |
UserVerificationRequirement, |
| 28 |
) |
| 29 |
except ImportError: # pragma: no cover |
| 30 |
pass |
| 31 |
|
| 32 |
|
| 33 |
if t.TYPE_CHECKING: # pragma: no cover |
| 34 |
import flask |
| 35 |
from flask_security import UserMixin |
| 36 |
|
| 37 |
|
| 38 |
class WebauthnUtil: |
| 39 |
""" |
| 40 |
Utility class allowing an application to fine-tune various Relying Party |
| 41 |
attributes. |
| 42 |
|
| 43 |
To provide your own implementation, pass in the class as ``webauthn_util_cls`` |
| 44 |
at init time. Your class will be instantiated once as part of app initialization. |
| 45 |
|
| 46 |
.. versionadded:: 5.0.0 |
| 47 |
""" |
| 48 |
|
| 49 |
def __init__(self, app: flask.Flask): |
| 50 |
"""Instantiate class. |
| 51 |
|
| 52 |
:param app: The Flask application being initialized. |
| 53 |
""" |
| 54 |
pass |
| 55 |
|
| 56 |
def generate_challenge(self, nbytes: int | None = None) -> str: |
| 57 |
# Mostly override this for testing, so we can have a 'constant' challenge. |
| 58 |
return secrets.token_urlsafe(nbytes) |
| 59 |
|
| 60 |
def origin(self) -> str: |
| 61 |
# Return the RP origin - normally this is just the URL of the application. |
| 62 |
return request.host_url.rstrip("/") |
| 63 |
|
| 64 |
def registration_options( |
| 65 |
self, user: UserMixin, usage: str, existing_options: dict[str, t.Any] |
| 66 |
) -> dict[str, t.Any]: |
| 67 |
""" |
| 68 |
:param user: User object - could be used to configure on a per-user basis. |
| 69 |
:param usage: Either "first" or "secondary" (webauthn is being used as a second |
| 70 |
factor for authentication) |
| 71 |
:param existing_options: Currently filled in registration options. |
| 72 |
|
| 73 |
Return a dict that will be sent in to py-webauthn generate_registration_options |
| 74 |
""" |
| 75 |
existing_options["authenticator_selection"] = self.authenticator_selection( |
| 76 |
user, usage |
| 77 |
) |
| 78 |
return existing_options |
| 79 |
|
| 80 |
def authenticator_selection( |
| 81 |
self, user: UserMixin, usage: str |
| 82 |
) -> AuthenticatorSelectionCriteria: |
| 83 |
""" |
| 84 |
:param user: User object - could be used to configure on a per-user basis. |
| 85 |
:param usage: Either "first" or "secondary" (webauthn is being used as a second |
| 86 |
factor for authentication |
| 87 |
|
| 88 |
Part of the registration ceremony is providing information about what kind |
| 89 |
of authenticators the app is interested in. |
| 90 |
See: https://www.w3.org/TR/2021/REC-webauthn-2-20210408/#dictionary-authenticatorSelection |
| 91 |
|
| 92 |
The main options are: |
| 93 |
- whether you want a ResidentKey (discoverable) |
| 94 |
- Attachment - platform or cross-platform |
| 95 |
- Does the key have to provide user-verification |
| 96 |
|
| 97 |
:note:: |
| 98 |
If the key isn't resident then it isn't discoverable which means that |
| 99 |
the user won't be able to use that key unless they identify themselves |
| 100 |
(use the key as a second factor OR type in their identity). If they are forced |
| 101 |
to type in their identity PRIOR to being authenticated, then there is the |
| 102 |
possibility that the app will leak username information. |
| 103 |
""" # noqa: E501 |
| 104 |
|
| 105 |
select_criteria = AuthenticatorSelectionCriteria() |
| 106 |
# TODO: look at #sctn-usecase-new-device-registration to see a reason |
| 107 |
# to allow multiple keys as "first" - only one would need to be cross-platform |
| 108 |
if usage == "first": |
| 109 |
select_criteria.authenticator_attachment = ( |
| 110 |
AuthenticatorAttachment.CROSS_PLATFORM |
| 111 |
) |
| 112 |
select_criteria.user_verification = UserVerificationRequirement.PREFERRED |
| 113 |
else: |
| 114 |
# For second factor minimize user-interaction by not asking for UV |
| 115 |
select_criteria.user_verification = UserVerificationRequirement.DISCOURAGED |
| 116 |
|
| 117 |
if not current_app.config.get("SECURITY_WAN_ALLOW_USER_HINTS"): |
| 118 |
select_criteria.resident_key = ResidentKeyRequirement.REQUIRED |
| 119 |
else: |
| 120 |
select_criteria.resident_key = ResidentKeyRequirement.PREFERRED |
| 121 |
return select_criteria |
| 122 |
|
| 123 |
def authentication_options( |
| 124 |
self, |
| 125 |
user: UserMixin | None, |
| 126 |
usage: list[str], |
| 127 |
existing_options: dict[str, t.Any], |
| 128 |
) -> dict[str, t.Any]: |
| 129 |
""" |
| 130 |
:param user: User object - could be used to configure on a per-user basis. |
| 131 |
However, this can be null. |
| 132 |
:param usage: Either "first" or "secondary" (webauthn is being used as a second |
| 133 |
factor for authentication) |
| 134 |
:param existing_options: Currently filled in authentication options. |
| 135 |
|
| 136 |
Return a dict that will be sent in to |
| 137 |
py-webauthn generate_authentication_options |
| 138 |
""" |
| 139 |
existing_options["user_verification"] = self.user_verification(user, usage) |
| 140 |
return existing_options |
| 141 |
|
| 142 |
def user_verification( |
| 143 |
self, user: UserMixin | None, usage: list[str] |
| 144 |
) -> UserVerificationRequirement: |
| 145 |
""" |
| 146 |
As part of signin - do we want/need user verification. |
| 147 |
This is called from /wan-signin and /wan-verify |
| 148 |
|
| 149 |
:param user: User object - could be used to configure on a per-user basis. |
| 150 |
Note that this may not be set on initial wan-signin. |
| 151 |
:param usage: List of "first", "secondary" (webauthn is being used as a second |
| 152 |
factor for authentication). Note that in the ``verify``/``reauthentication`` |
| 153 |
case this list is derived from :py:data:`SECURITY_WAN_ALLOW_AS_VERIFY` |
| 154 |
|
| 155 |
""" |
| 156 |
if "secondary" in usage: |
| 157 |
return UserVerificationRequirement.DISCOURAGED |
| 158 |
if current_app.config.get("SECURITY_WAN_ALLOW_AS_MULTI_FACTOR"): |
| 159 |
return UserVerificationRequirement.PREFERRED |
| 160 |
return UserVerificationRequirement.PREFERRED |
| 161 |
|
| 162 |
def name_svn(self, value: str) -> tuple[str | None, str | None]: |
| 163 |
""" |
| 164 |
Sanitize, validate, and normalize form input. |
| 165 |
|
| 166 |
Return value is a tuple (msg, clean_value). msg will be None if |
| 167 |
properly validated. |
| 168 |
The config variable :py:data:`SECURITY_WAN_NAME_ALLOWED_CHARS` specifies |
| 169 |
which unicode character classes are allowed (if None then all are allowed). |
| 170 |
|
| 171 |
The config variable :py:data:`SECURITY_INPUT_NORMALIZE_FORM` specifies |
| 172 |
the input to unicode normalization (if None then no normalization is performed). |
| 173 |
|
| 174 |
For webauthn names, allow letters, numbers, and some punctuation. |
| 175 |
""" |
| 176 |
reason, clean_input = input_svn( |
| 177 |
value, cv("WAN_NAME_ALLOWED_CHARS"), cv("INPUT_NORMALIZE_FORM") |
| 178 |
) |
| 179 |
if clean_input is None: |
| 180 |
msg = ( |
| 181 |
"WEBAUTHN_NAME_DISALLOWED_CHARACTERS" |
| 182 |
if reason == "unallowed" |
| 183 |
else "INVALID_INPUT" |
| 184 |
) |
| 185 |
return get_message(msg)[0], None |
| 186 |
if len(clean_input) > cv("WAN_NAME_MAX_LENGTH"): |
| 187 |
msg = "INVALID_INPUT_LENGTH" |
| 188 |
return ( |
| 189 |
get_message(msg, length=cv("WAN_NAME_MAX_LENGTH"))[0], |
| 190 |
None, |
| 191 |
) |
| 192 |
return None, clean_input |