| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.urls import URLPattern, URLResolver, include, path |
| 4 |
|
| 5 |
from allauth.headless.constants import Client |
| 6 |
from allauth.headless.mfa import views |
| 7 |
from allauth.mfa import app_settings as mfa_settings |
| 8 |
|
| 9 |
|
| 10 |
def build_urlpatterns(client: Client) -> list[URLPattern | URLResolver]: |
| 11 |
auth_patterns = [ |
| 12 |
path( |
| 13 |
"2fa/authenticate", |
| 14 |
views.AuthenticateView.as_api_view(client=client), |
| 15 |
name="authenticate", |
| 16 |
), |
| 17 |
path( |
| 18 |
"2fa/reauthenticate", |
| 19 |
views.ReauthenticateView.as_api_view(client=client), |
| 20 |
name="reauthenticate", |
| 21 |
), |
| 22 |
] |
| 23 |
if mfa_settings.TRUST_ENABLED and client == Client.BROWSER: |
| 24 |
auth_patterns.append( |
| 25 |
path( |
| 26 |
"2fa/trust", |
| 27 |
views.TrustView.as_api_view(client=client), |
| 28 |
name="trust", |
| 29 |
) |
| 30 |
) |
| 31 |
|
| 32 |
authenticators = [] |
| 33 |
if "totp" in mfa_settings.SUPPORTED_TYPES: |
| 34 |
authenticators.append( |
| 35 |
path( |
| 36 |
"totp", |
| 37 |
views.ManageTOTPView.as_api_view(client=client), |
| 38 |
name="manage_totp", |
| 39 |
) |
| 40 |
) |
| 41 |
if "recovery_codes" in mfa_settings.SUPPORTED_TYPES: |
| 42 |
authenticators.append( |
| 43 |
path( |
| 44 |
"recovery-codes", |
| 45 |
views.ManageRecoveryCodesView.as_api_view(client=client), |
| 46 |
name="manage_recovery_codes", |
| 47 |
) |
| 48 |
) |
| 49 |
if "webauthn" in mfa_settings.SUPPORTED_TYPES: |
| 50 |
authenticators.extend( |
| 51 |
[ |
| 52 |
path( |
| 53 |
"webauthn", |
| 54 |
views.ManageWebAuthnView.as_api_view(client=client), |
| 55 |
name="manage_webauthn", |
| 56 |
), |
| 57 |
] |
| 58 |
) |
| 59 |
auth_patterns.extend( |
| 60 |
[ |
| 61 |
path( |
| 62 |
"webauthn/authenticate", |
| 63 |
views.AuthenticateWebAuthnView.as_api_view(client=client), |
| 64 |
name="authenticate_webauthn", |
| 65 |
), |
| 66 |
path( |
| 67 |
"webauthn/reauthenticate", |
| 68 |
views.ReauthenticateWebAuthnView.as_api_view(client=client), |
| 69 |
name="reauthenticate_webauthn", |
| 70 |
), |
| 71 |
] |
| 72 |
) |
| 73 |
if mfa_settings.PASSKEY_LOGIN_ENABLED: |
| 74 |
auth_patterns.append( |
| 75 |
path( |
| 76 |
"webauthn/login", |
| 77 |
views.LoginWebAuthnView.as_api_view(client=client), |
| 78 |
name="login_webauthn", |
| 79 |
) |
| 80 |
) |
| 81 |
if mfa_settings.PASSKEY_SIGNUP_ENABLED: |
| 82 |
auth_patterns.append( |
| 83 |
path( |
| 84 |
"webauthn/signup", |
| 85 |
views.SignupWebAuthnView.as_api_view(client=client), |
| 86 |
name="signup_webauthn", |
| 87 |
) |
| 88 |
) |
| 89 |
|
| 90 |
return [ |
| 91 |
path( |
| 92 |
"auth/", |
| 93 |
include(auth_patterns), |
| 94 |
), |
| 95 |
path( |
| 96 |
"account/", |
| 97 |
include( |
| 98 |
[ |
| 99 |
path( |
| 100 |
"authenticators", |
| 101 |
views.AuthenticatorsView.as_api_view(client=client), |
| 102 |
name="authenticators", |
| 103 |
), |
| 104 |
path( |
| 105 |
"authenticators/", |
| 106 |
include(authenticators), |
| 107 |
), |
| 108 |
] |
| 109 |
), |
| 110 |
), |
| 111 |
] |