authentik.stages.authenticator_webauthn.utils
webauthn utils
1"""webauthn utils""" 2 3from django.core.exceptions import ValidationError 4from django.http import HttpRequest 5from django.utils.translation import gettext_lazy as _ 6 7 8def validate_webauthn_origin(value: str) -> None: 9 """Reject ambiguous configuration; the WebAuthn library matches origins exactly.""" 10 if not value or value == "null" or "*" in value or any(char.isspace() for char in value): 11 raise ValidationError( 12 _( 13 "Enter a non-empty origin without whitespace or wildcards. " 14 "The value 'null' is not allowed." 15 ) 16 ) 17 18 19def get_rp_id(request: HttpRequest) -> str: 20 """Get hostname from http request, without port""" 21 host = request.get_host() 22 if ":" in host: 23 return host.split(":")[0] 24 return host 25 26 27def get_origin(request: HttpRequest) -> str: 28 """Return Origin by building an absolute URL and removing the 29 trailing slash""" 30 full_url = request.build_absolute_uri("/") 31 return full_url[:-1] 32 33 34def get_expected_origins(request: HttpRequest, additional_origins: list[str]) -> list[str]: 35 """Retain the request origin and add explicitly trusted ceremony origins.""" 36 return list(dict.fromkeys([get_origin(request), *additional_origins]))
def
validate_webauthn_origin(value: str) -> None:
9def validate_webauthn_origin(value: str) -> None: 10 """Reject ambiguous configuration; the WebAuthn library matches origins exactly.""" 11 if not value or value == "null" or "*" in value or any(char.isspace() for char in value): 12 raise ValidationError( 13 _( 14 "Enter a non-empty origin without whitespace or wildcards. " 15 "The value 'null' is not allowed." 16 ) 17 )
Reject ambiguous configuration; the WebAuthn library matches origins exactly.
def
get_rp_id(request: django.http.request.HttpRequest) -> str:
20def get_rp_id(request: HttpRequest) -> str: 21 """Get hostname from http request, without port""" 22 host = request.get_host() 23 if ":" in host: 24 return host.split(":")[0] 25 return host
Get hostname from http request, without port
def
get_origin(request: django.http.request.HttpRequest) -> str:
28def get_origin(request: HttpRequest) -> str: 29 """Return Origin by building an absolute URL and removing the 30 trailing slash""" 31 full_url = request.build_absolute_uri("/") 32 return full_url[:-1]
Return Origin by building an absolute URL and removing the trailing slash
def
get_expected_origins( request: django.http.request.HttpRequest, additional_origins: list[str]) -> list[str]:
35def get_expected_origins(request: HttpRequest, additional_origins: list[str]) -> list[str]: 36 """Retain the request origin and add explicitly trusted ceremony origins.""" 37 return list(dict.fromkeys([get_origin(request), *additional_origins]))
Retain the request origin and add explicitly trusted ceremony origins.