| 1 |
from http import HTTPStatus |
| 2 |
from unittest.mock import ANY |
| 3 |
|
| 4 |
from django.contrib.auth import get_user_model |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from allauth.account import app_settings |
| 9 |
from allauth.account.authentication import AUTHENTICATION_METHODS_SESSION_KEY |
| 10 |
from allauth.headless.constants import Flow |
| 11 |
from allauth.mfa.models import Authenticator |
| 12 |
|
| 13 |
|
| 14 |
def test_passkey_login( |
| 15 |
client, passkey, webauthn_authentication_bypass, headless_reverse |
| 16 |
): |
| 17 |
with webauthn_authentication_bypass(passkey) as credential: |
| 18 |
resp = client.get(headless_reverse("headless:mfa:login_webauthn")) |
| 19 |
assert "request_options" in resp.json()["data"] |
| 20 |
resp = client.post( |
| 21 |
headless_reverse("headless:mfa:login_webauthn"), |
| 22 |
data={"credential": credential}, |
| 23 |
content_type="application/json", |
| 24 |
) |
| 25 |
data = resp.json() |
| 26 |
assert data["data"]["user"]["id"] == passkey.user_id |
| 27 |
|
| 28 |
|
| 29 |
def test_passkey_login_get_options(client, headless_client, headless_reverse, db): |
| 30 |
resp = client.get(headless_reverse("headless:mfa:login_webauthn")) |
| 31 |
data = resp.json() |
| 32 |
meta = {} |
| 33 |
if headless_client == "app": |
| 34 |
meta = { |
| 35 |
"meta": {"session_token": ANY}, |
| 36 |
} |
| 37 |
assert data == { |
| 38 |
"status": HTTPStatus.OK, |
| 39 |
"data": {"request_options": {"publicKey": ANY}}, |
| 40 |
**meta, |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
def test_reauthenticate( |
| 45 |
auth_client, |
| 46 |
passkey, |
| 47 |
user_with_recovery_codes, |
| 48 |
webauthn_authentication_bypass, |
| 49 |
headless_reverse, |
| 50 |
): |
| 51 |
# View recovery codes, confirm webauthn reauthentication is an option |
| 52 |
resp = auth_client.get(headless_reverse("headless:mfa:manage_recovery_codes")) |
| 53 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 54 |
assert Flow.MFA_REAUTHENTICATE in [ |
| 55 |
flow["id"] for flow in resp.json()["data"]["flows"] |
| 56 |
] |
| 57 |
|
| 58 |
# Get request options |
| 59 |
with webauthn_authentication_bypass(passkey): |
| 60 |
resp = auth_client.get(headless_reverse("headless:mfa:reauthenticate_webauthn")) |
| 61 |
data = resp.json() |
| 62 |
assert data["status"] == HTTPStatus.OK |
| 63 |
assert data["data"]["request_options"] == ANY |
| 64 |
|
| 65 |
# Reauthenticate |
| 66 |
with webauthn_authentication_bypass(passkey) as credential: |
| 67 |
resp = auth_client.post( |
| 68 |
headless_reverse("headless:mfa:reauthenticate_webauthn"), |
| 69 |
data={"credential": credential}, |
| 70 |
content_type="application/json", |
| 71 |
) |
| 72 |
assert resp.status_code == HTTPStatus.OK |
| 73 |
resp = auth_client.get(headless_reverse("headless:mfa:manage_recovery_codes")) |
| 74 |
assert resp.status_code == HTTPStatus.OK |
| 75 |
|
| 76 |
|
| 77 |
def test_update_authenticator( |
| 78 |
auth_client, headless_reverse, passkey, reauthentication_bypass |
| 79 |
): |
| 80 |
data = {"id": passkey.pk, "name": "Renamed!"} |
| 81 |
resp = auth_client.put( |
| 82 |
headless_reverse("headless:mfa:manage_webauthn"), |
| 83 |
data=data, |
| 84 |
content_type="application/json", |
| 85 |
) |
| 86 |
# Reauthentication required |
| 87 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 88 |
with reauthentication_bypass(): |
| 89 |
resp = auth_client.put( |
| 90 |
headless_reverse("headless:mfa:manage_webauthn"), |
| 91 |
data=data, |
| 92 |
content_type="application/json", |
| 93 |
) |
| 94 |
assert resp.status_code == HTTPStatus.OK |
| 95 |
passkey.refresh_from_db() |
| 96 |
assert passkey.wrap().name == "Renamed!" |
| 97 |
|
| 98 |
|
| 99 |
def test_delete_authenticator( |
| 100 |
auth_client, headless_reverse, passkey, reauthentication_bypass |
| 101 |
): |
| 102 |
data = {"authenticators": [passkey.pk]} |
| 103 |
resp = auth_client.delete( |
| 104 |
headless_reverse("headless:mfa:manage_webauthn"), |
| 105 |
data=data, |
| 106 |
content_type="application/json", |
| 107 |
) |
| 108 |
# Reauthentication required |
| 109 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 110 |
with reauthentication_bypass(): |
| 111 |
resp = auth_client.delete( |
| 112 |
headless_reverse("headless:mfa:manage_webauthn"), |
| 113 |
data=data, |
| 114 |
content_type="application/json", |
| 115 |
) |
| 116 |
assert resp.status_code == HTTPStatus.OK |
| 117 |
assert not Authenticator.objects.filter(pk=passkey.pk).exists() |
| 118 |
|
| 119 |
|
| 120 |
@pytest.mark.parametrize("email_verified", [False, True]) |
| 121 |
def test_add_authenticator( |
| 122 |
user, |
| 123 |
auth_client, |
| 124 |
headless_reverse, |
| 125 |
webauthn_registration_bypass, |
| 126 |
reauthentication_bypass, |
| 127 |
email_verified, |
| 128 |
): |
| 129 |
resp = auth_client.get(headless_reverse("headless:mfa:manage_webauthn")) |
| 130 |
# Reauthentication required |
| 131 |
assert ( |
| 132 |
resp.status_code == HTTPStatus.UNAUTHORIZED |
| 133 |
if email_verified |
| 134 |
else HTTPStatus.CONFLICT |
| 135 |
) |
| 136 |
|
| 137 |
with reauthentication_bypass(): |
| 138 |
resp = auth_client.get(headless_reverse("headless:mfa:manage_webauthn")) |
| 139 |
if email_verified: |
| 140 |
assert resp.status_code == HTTPStatus.OK |
| 141 |
data = resp.json() |
| 142 |
assert data["data"]["creation_options"] == ANY |
| 143 |
else: |
| 144 |
assert resp.status_code == HTTPStatus.CONFLICT |
| 145 |
|
| 146 |
with webauthn_registration_bypass(user, False) as credential: |
| 147 |
resp = auth_client.post( |
| 148 |
headless_reverse("headless:mfa:manage_webauthn"), |
| 149 |
data={"credential": credential}, |
| 150 |
content_type="application/json", |
| 151 |
) |
| 152 |
webauthn_count = Authenticator.objects.filter( |
| 153 |
type=Authenticator.Type.WEBAUTHN, user=user |
| 154 |
).count() |
| 155 |
if email_verified: |
| 156 |
assert resp.status_code == HTTPStatus.OK |
| 157 |
assert webauthn_count == 1 |
| 158 |
else: |
| 159 |
assert resp.status_code == HTTPStatus.CONFLICT |
| 160 |
assert webauthn_count == 0 |
| 161 |
|
| 162 |
|
| 163 |
def test_2fa_login( |
| 164 |
client, |
| 165 |
user, |
| 166 |
user_password, |
| 167 |
passkey, |
| 168 |
webauthn_authentication_bypass, |
| 169 |
headless_reverse, |
| 170 |
): |
| 171 |
resp = client.post( |
| 172 |
headless_reverse("headless:account:login"), |
| 173 |
data={ |
| 174 |
"username": user.username, |
| 175 |
"password": user_password, |
| 176 |
}, |
| 177 |
content_type="application/json", |
| 178 |
) |
| 179 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 180 |
data = resp.json() |
| 181 |
pending_flows = [f for f in data["data"]["flows"] if f.get("is_pending")] |
| 182 |
assert len(pending_flows) == 1 |
| 183 |
pending_flow = pending_flows[0] |
| 184 |
assert pending_flow == { |
| 185 |
"id": "mfa_authenticate", |
| 186 |
"is_pending": True, |
| 187 |
"types": ["webauthn"], |
| 188 |
} |
| 189 |
with webauthn_authentication_bypass(passkey) as credential: |
| 190 |
resp = client.get(headless_reverse("headless:mfa:authenticate_webauthn")) |
| 191 |
assert "request_options" in resp.json()["data"] |
| 192 |
resp = client.post( |
| 193 |
headless_reverse("headless:mfa:authenticate_webauthn"), |
| 194 |
data={"credential": credential}, |
| 195 |
content_type="application/json", |
| 196 |
) |
| 197 |
data = resp.json() |
| 198 |
assert resp.status_code == HTTPStatus.OK |
| 199 |
assert data["data"]["user"]["id"] == passkey.user_id |
| 200 |
assert client.headless_session()[AUTHENTICATION_METHODS_SESSION_KEY] == [ |
| 201 |
{"method": "password", "at": ANY, "username": passkey.user.username}, |
| 202 |
{"method": "mfa", "at": ANY, "id": ANY, "type": Authenticator.Type.WEBAUTHN}, |
| 203 |
] |
| 204 |
|
| 205 |
|
| 206 |
@pytest.mark.parametrize("login_on_email_verification", [False, True]) |
| 207 |
def test_passkey_signup( |
| 208 |
client, |
| 209 |
db, |
| 210 |
webauthn_registration_bypass, |
| 211 |
headless_reverse, |
| 212 |
settings, |
| 213 |
get_last_email_verification_code, |
| 214 |
mailoutbox, |
| 215 |
login_on_email_verification, |
| 216 |
): |
| 217 |
settings.ACCOUNT_EMAIL_VERIFICATION = app_settings.EmailVerificationMethod.MANDATORY |
| 218 |
settings.ACCOUNT_EMAIL_VERIFICATION_BY_CODE_ENABLED = True |
| 219 |
# This setting should have no influence when verifying by code: |
| 220 |
settings.ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION = login_on_email_verification |
| 221 |
|
| 222 |
# Initiate passkey signup |
| 223 |
resp = client.post( |
| 224 |
headless_reverse("headless:mfa:signup_webauthn"), |
| 225 |
data={"email": "[email protected]", "username": "passkey"}, |
| 226 |
content_type="application/json", |
| 227 |
) |
| 228 |
|
| 229 |
# Email verification kicks in. |
| 230 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 231 |
pending_flows = [ |
| 232 |
flow for flow in resp.json()["data"]["flows"] if flow.get("is_pending") |
| 233 |
] |
| 234 |
assert len(pending_flows) == 1 |
| 235 |
flow = pending_flows[0] |
| 236 |
assert flow["id"] == Flow.VERIFY_EMAIL.value |
| 237 |
|
| 238 |
# Verify email. |
| 239 |
code = get_last_email_verification_code(client, mailoutbox) |
| 240 |
resp = client.post( |
| 241 |
headless_reverse("headless:account:verify_email"), |
| 242 |
data={"key": code}, |
| 243 |
content_type="application/json", |
| 244 |
) |
| 245 |
assert resp.status_code == HTTPStatus.UNAUTHORIZED |
| 246 |
|
| 247 |
# Now, the webauthn signup flow is pending. |
| 248 |
pending_flows = [ |
| 249 |
flow for flow in resp.json()["data"]["flows"] if flow.get("is_pending") |
| 250 |
] |
| 251 |
assert len(pending_flows) == 1 |
| 252 |
flow = pending_flows[0] |
| 253 |
assert flow["id"] == Flow.MFA_SIGNUP_WEBAUTHN.value |
| 254 |
|
| 255 |
# Fetch flow creation options. |
| 256 |
resp = client.get(headless_reverse("headless:mfa:signup_webauthn")) |
| 257 |
data = resp.json() |
| 258 |
assert "creation_options" in data["data"] |
| 259 |
|
| 260 |
# Create a passkey. |
| 261 |
user = get_user_model().objects.get(email="[email protected]") |
| 262 |
with webauthn_registration_bypass(user, True) as credential: |
| 263 |
resp = client.put( |
| 264 |
headless_reverse("headless:mfa:signup_webauthn"), |
| 265 |
data={"name": "Some key", "credential": credential}, |
| 266 |
content_type="application/json", |
| 267 |
) |
| 268 |
|
| 269 |
# Signed up successfully. |
| 270 |
data = resp.json() |
| 271 |
assert resp.status_code == HTTPStatus.OK |
| 272 |
assert data["meta"]["is_authenticated"] |
| 273 |
authenticator = Authenticator.objects.get(user=user) |
| 274 |
assert authenticator.wrap().name == "Some key" |
| 275 |
|
| 276 |
|
| 277 |
def test_get_login_webauthn_doesnt_crash_with_jwt( |
| 278 |
db, client, headless_reverse, settings |
| 279 |
): |
| 280 |
settings.HEADLESS_TOKEN_STRATEGY = ( |
| 281 |
"tests.apps.headless.tokens.test_jwttokenstrategy.CustomJWTTokenStrategy" |
| 282 |
) |
| 283 |
resp = client.get(headless_reverse("headless:mfa:login_webauthn")) |
| 284 |
assert resp.status_code == HTTPStatus.OK |