Coverage for src/django_otp_webauthn/templatetags/otp_webauthn.py: 0%

22 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-06-23 20:15 +0000

1from django import template 

2from django.conf import settings 

3from django.urls import reverse 

4 

5from django_otp_webauthn.settings import app_settings 

6from django_otp_webauthn.utils import get_credential_model 

7 

8register = template.Library() 

9 

10WebAuthnCredential = get_credential_model() 

11 

12 

13def get_configuration(extra_options: dict = {}) -> dict: 

14 configuration = { 

15 "autocompleteLoginFieldSelector": None, 

16 "csrfCookieName": settings.CSRF_COOKIE_NAME, 

17 "beginAuthenticationUrl": reverse("otp_webauthn:credential-authentication-begin"), 

18 "completeAuthenticationUrl": reverse("otp_webauthn:credential-authentication-complete"), 

19 "beginRegistrationUrl": reverse("otp_webauthn:credential-registration-begin"), 

20 "completeRegistrationUrl": reverse("otp_webauthn:credential-registration-complete"), 

21 } 

22 configuration.update(extra_options) 

23 

24 return configuration 

25 

26 

27@register.inclusion_tag("django_otp_webauthn/auth_scripts.html", takes_context=True) 

28def render_otp_webauthn_auth_scripts(context, username_field_selector=None): 

29 extra_options = {} 

30 # If passwordless login is allowed, tell the client-side script what the username field selector is 

31 # so the field can be marked with the autocomplete="webauthn" attribute to indicate passwordless login is available. 

32 if app_settings.OTP_WEBAUTHN_ALLOW_PASSWORDLESS_LOGIN: 

33 extra_options["autocompleteLoginFieldSelector"] = username_field_selector 

34 context["configuration"] = get_configuration(extra_options) 

35 

36 return context 

37 

38 

39@register.inclusion_tag("django_otp_webauthn/register_scripts.html", takes_context=True) 

40def render_otp_webauthn_register_scripts(context): 

41 context["configuration"] = get_configuration() 

42 return context