| 1 |
from http import HTTPStatus |
| 2 |
from unittest.mock import ANY |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.contrib.auth import get_user_model |
| 6 |
from django.urls import reverse |
| 7 |
|
| 8 |
import pytest |
| 9 |
from pytest_django.asserts import assertTemplateUsed |
| 10 |
|
| 11 |
from allauth.account.authentication import AUTHENTICATION_METHODS_SESSION_KEY |
| 12 |
from allauth.mfa.models import Authenticator |
| 13 |
|
| 14 |
|
| 15 |
def test_passkey_login(client, passkey, webauthn_authentication_bypass): |
| 16 |
with webauthn_authentication_bypass(passkey) as credential: |
| 17 |
resp = client.get( |
| 18 |
reverse("mfa_login_webauthn"), HTTP_X_REQUESTED_WITH="XMLHttpRequest" |
| 19 |
) |
| 20 |
assert "request_options" in resp.json() |
| 21 |
resp = client.post( |
| 22 |
reverse("mfa_login_webauthn"), data={"credential": credential} |
| 23 |
) |
| 24 |
assert resp["location"] == settings.LOGIN_REDIRECT_URL |
| 25 |
assert client.session[AUTHENTICATION_METHODS_SESSION_KEY] == [ |
| 26 |
{ |
| 27 |
"at": ANY, |
| 28 |
"id": ANY, |
| 29 |
"method": "mfa", |
| 30 |
"passwordless": True, |
| 31 |
"type": "webauthn", |
| 32 |
} |
| 33 |
] |
| 34 |
|
| 35 |
|
| 36 |
def test_reauthenticate( |
| 37 |
auth_client, passkey, user_with_recovery_codes, webauthn_authentication_bypass |
| 38 |
): |
| 39 |
resp = auth_client.get(reverse("mfa_view_recovery_codes")) |
| 40 |
assert resp.status_code == HTTPStatus.FOUND |
| 41 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 42 |
resp = auth_client.get(reverse("mfa_reauthenticate")) |
| 43 |
assertTemplateUsed(resp, "mfa/reauthenticate.html") |
| 44 |
|
| 45 |
with webauthn_authentication_bypass(passkey) as credential: |
| 46 |
resp = auth_client.get( |
| 47 |
reverse("mfa_reauthenticate_webauthn"), |
| 48 |
) |
| 49 |
resp = auth_client.post( |
| 50 |
reverse("mfa_reauthenticate_webauthn"), |
| 51 |
data={"credential": credential, "next": "/redir"}, |
| 52 |
) |
| 53 |
assert resp["location"] == "/redir" |
| 54 |
|
| 55 |
|
| 56 |
def test_get_passkey_login_challenge_redirects_if_not_ajax(client): |
| 57 |
resp = client.get(reverse("mfa_login_webauthn")) |
| 58 |
assert resp["location"] == reverse("account_login") |
| 59 |
|
| 60 |
|
| 61 |
def test_get_passkey_login_challenge(client, db): |
| 62 |
resp = client.get( |
| 63 |
reverse("mfa_login_webauthn"), HTTP_X_REQUESTED_WITH="XMLHttpRequest" |
| 64 |
) |
| 65 |
assert resp.status_code == HTTPStatus.OK |
| 66 |
assert resp["content-type"] == "application/json" |
| 67 |
data = resp.json() |
| 68 |
assert data == { |
| 69 |
"request_options": { |
| 70 |
"publicKey": { |
| 71 |
"challenge": ANY, |
| 72 |
"rpId": "testserver", |
| 73 |
"allowCredentials": [], |
| 74 |
"userVerification": "preferred", |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
def test_invalid_passkey_login(client, passkey): |
| 81 |
resp = client.post(reverse("mfa_login_webauthn"), data={"credential": "{}"}) |
| 82 |
assert resp["location"] == reverse("account_login") |
| 83 |
|
| 84 |
|
| 85 |
def test_rename_key(auth_client, passkey, reauthentication_bypass): |
| 86 |
resp = auth_client.get(reverse("mfa_edit_webauthn", kwargs={"pk": passkey.pk})) |
| 87 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 88 |
with reauthentication_bypass(): |
| 89 |
resp = auth_client.get(reverse("mfa_edit_webauthn", kwargs={"pk": passkey.pk})) |
| 90 |
assertTemplateUsed(resp, "mfa/webauthn/edit_form.html") |
| 91 |
resp = auth_client.post( |
| 92 |
reverse("mfa_edit_webauthn", kwargs={"pk": passkey.pk}), |
| 93 |
data={"name": "Renamed"}, |
| 94 |
) |
| 95 |
assert resp["location"] == reverse("mfa_list_webauthn") |
| 96 |
passkey.refresh_from_db() |
| 97 |
assert passkey.data["name"] == "Renamed" |
| 98 |
assert str(passkey) == "Renamed" |
| 99 |
|
| 100 |
|
| 101 |
def test_remove_key(auth_client, passkey, reauthentication_bypass): |
| 102 |
resp = auth_client.get(reverse("mfa_remove_webauthn", kwargs={"pk": passkey.pk})) |
| 103 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 104 |
with reauthentication_bypass(): |
| 105 |
resp = auth_client.get( |
| 106 |
reverse("mfa_remove_webauthn", kwargs={"pk": passkey.pk}) |
| 107 |
) |
| 108 |
assertTemplateUsed(resp, "mfa/webauthn/authenticator_confirm_delete.html") |
| 109 |
resp = auth_client.post( |
| 110 |
reverse("mfa_remove_webauthn", kwargs={"pk": passkey.pk}) |
| 111 |
) |
| 112 |
assert resp["location"] == reverse("mfa_list_webauthn") |
| 113 |
|
| 114 |
|
| 115 |
@pytest.mark.parametrize("passwordless", [False, True]) |
| 116 |
def test_add_key( |
| 117 |
auth_client, |
| 118 |
user, |
| 119 |
webauthn_registration_bypass, |
| 120 |
reauthentication_bypass, |
| 121 |
passwordless, |
| 122 |
): |
| 123 |
with webauthn_registration_bypass(user, passwordless) as credential: |
| 124 |
resp = auth_client.post( |
| 125 |
reverse("mfa_add_webauthn"), data={"credential": credential} |
| 126 |
) |
| 127 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 128 |
with reauthentication_bypass(): |
| 129 |
resp = auth_client.get(reverse("mfa_add_webauthn")) |
| 130 |
assertTemplateUsed(resp, "mfa/webauthn/add_form.html") |
| 131 |
with webauthn_registration_bypass(user, passwordless) as credential: |
| 132 |
resp = auth_client.post( |
| 133 |
reverse("mfa_add_webauthn"), |
| 134 |
data={ |
| 135 |
"credential": credential, |
| 136 |
"passwordless": "on" if passwordless else "", |
| 137 |
}, |
| 138 |
) |
| 139 |
assert resp["location"].startswith(reverse("mfa_view_recovery_codes")) |
| 140 |
authenticator = Authenticator.objects.get( |
| 141 |
user=user, type=Authenticator.Type.WEBAUTHN |
| 142 |
) |
| 143 |
assert authenticator.wrap().is_passwordless == passwordless |
| 144 |
Authenticator.objects.filter( |
| 145 |
user=user, type=Authenticator.Type.RECOVERY_CODES |
| 146 |
).exists() |
| 147 |
|
| 148 |
|
| 149 |
def test_list_keys(auth_client): |
| 150 |
resp = auth_client.get(reverse("mfa_list_webauthn")) |
| 151 |
assertTemplateUsed(resp, "mfa/webauthn/authenticator_list.html") |
| 152 |
|
| 153 |
|
| 154 |
@pytest.mark.parametrize("email_verified", [False]) |
| 155 |
@pytest.mark.parametrize("method", ["get", "post"]) |
| 156 |
@pytest.mark.parametrize("allowed", [True, False]) |
| 157 |
def test_add_with_unverified_email( |
| 158 |
auth_client, |
| 159 |
user, |
| 160 |
webauthn_registration_bypass, |
| 161 |
reauthentication_bypass, |
| 162 |
method, |
| 163 |
allowed, |
| 164 |
settings, |
| 165 |
): |
| 166 |
settings.MFA_ALLOW_UNVERIFIED_EMAIL = allowed |
| 167 |
with webauthn_registration_bypass(user, False) as credential: |
| 168 |
if method == "get": |
| 169 |
resp = auth_client.get(reverse("mfa_add_webauthn")) |
| 170 |
else: |
| 171 |
resp = auth_client.post( |
| 172 |
reverse("mfa_add_webauthn"), data={"credential": credential} |
| 173 |
) |
| 174 |
if allowed: |
| 175 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 176 |
else: |
| 177 |
assert resp["location"] == reverse("mfa_index") |
| 178 |
|
| 179 |
|
| 180 |
def test_passkey_signup(client, db, webauthn_registration_bypass): |
| 181 |
resp = client.post( |
| 182 |
reverse("account_signup_by_passkey"), |
| 183 |
data={"email": "[email protected]", "username": "passkey"}, |
| 184 |
) |
| 185 |
assert resp["location"] == reverse("mfa_signup_webauthn") |
| 186 |
resp = client.post(resp["location"]) |
| 187 |
assert resp.status_code == HTTPStatus.OK |
| 188 |
user = get_user_model().objects.get(email="[email protected]") |
| 189 |
with webauthn_registration_bypass(user, True) as credential: |
| 190 |
resp = client.post( |
| 191 |
reverse("mfa_signup_webauthn"), data={"credential": credential} |
| 192 |
) |
| 193 |
assert resp["location"] == settings.LOGIN_REDIRECT_URL |
| 194 |
|
| 195 |
|
| 196 |
def test_login_webauthn_when_already_authenticated(auth_client, passkey): |
| 197 |
"""Visiting the WebAuthn login page as a logged-in user should redirect, |
| 198 |
not crash.""" |
| 199 |
resp = auth_client.get(reverse("mfa_login_webauthn")) |
| 200 |
assert resp.status_code == HTTPStatus.FOUND |
| 201 |
assert resp["location"] == settings.LOGIN_REDIRECT_URL |
| 202 |
|
| 203 |
|
| 204 |
def test_webauthn_login( |
| 205 |
client, user_with_passkey, passkey, user_password, webauthn_authentication_bypass |
| 206 |
): |
| 207 |
resp = client.post( |
| 208 |
reverse("account_login"), |
| 209 |
{"login": user_with_passkey.username, "password": user_password}, |
| 210 |
) |
| 211 |
assert resp.status_code == HTTPStatus.FOUND |
| 212 |
assert resp["location"] == reverse("mfa_authenticate") |
| 213 |
with webauthn_authentication_bypass(passkey) as credential: |
| 214 |
resp = client.get(reverse("mfa_authenticate")) |
| 215 |
assert resp.status_code == HTTPStatus.OK |
| 216 |
resp = client.post(reverse("mfa_authenticate"), {"credential": credential}) |
| 217 |
assert resp.status_code == HTTPStatus.FOUND |
| 218 |
assert resp["location"] == settings.LOGIN_REDIRECT_URL |
| 219 |
assert client.session[AUTHENTICATION_METHODS_SESSION_KEY] == [ |
| 220 |
{"method": "password", "at": ANY, "username": user_with_passkey.username}, |
| 221 |
{"method": "mfa", "at": ANY, "id": ANY, "type": Authenticator.Type.WEBAUTHN}, |
| 222 |
] |