| 1 |
import time |
| 2 |
from http import HTTPStatus |
| 3 |
from unittest.mock import ANY, patch |
| 4 |
|
| 5 |
from django.conf import settings |
| 6 |
from django.core.cache import cache |
| 7 |
from django.test import Client |
| 8 |
from django.urls import reverse |
| 9 |
|
| 10 |
import pytest |
| 11 |
from pytest_django.asserts import assertTemplateUsed |
| 12 |
|
| 13 |
from allauth.account import app_settings |
| 14 |
from allauth.account.authentication import AUTHENTICATION_METHODS_SESSION_KEY |
| 15 |
from allauth.mfa.adapter import get_adapter |
| 16 |
from allauth.mfa.models import Authenticator |
| 17 |
from allauth.mfa.totp.internal.auth import SECRET_SESSION_KEY |
| 18 |
|
| 19 |
|
| 20 |
def test_activate_totp_with_incorrect_code(auth_client, reauthentication_bypass): |
| 21 |
with reauthentication_bypass(): |
| 22 |
resp = auth_client.get(reverse("mfa_activate_totp")) |
| 23 |
resp = auth_client.post( |
| 24 |
reverse("mfa_activate_totp"), |
| 25 |
{ |
| 26 |
"code": "123", |
| 27 |
}, |
| 28 |
) |
| 29 |
assert resp.context["form"].errors == { |
| 30 |
"code": [get_adapter().error_messages["incorrect_code"]] |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
@pytest.mark.parametrize("email_verified", [False]) |
| 35 |
@pytest.mark.parametrize("method", ["get", "post"]) |
| 36 |
def test_activate_totp_with_unverified_email( |
| 37 |
auth_client, user, totp_validation_bypass, reauthentication_bypass, method |
| 38 |
): |
| 39 |
with reauthentication_bypass(): |
| 40 |
if method == "get": |
| 41 |
resp = auth_client.get(reverse("mfa_activate_totp")) |
| 42 |
else: |
| 43 |
resp = auth_client.post(reverse("mfa_activate_totp"), {"code": "123"}) |
| 44 |
assert resp["location"] == reverse("mfa_index") |
| 45 |
|
| 46 |
|
| 47 |
def test_activate_totp_success( |
| 48 |
auth_client, |
| 49 |
totp_validation_bypass, |
| 50 |
user, |
| 51 |
reauthentication_bypass, |
| 52 |
settings, |
| 53 |
mailoutbox, |
| 54 |
): |
| 55 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 56 |
with reauthentication_bypass(): |
| 57 |
resp = auth_client.get(reverse("mfa_activate_totp")) |
| 58 |
with totp_validation_bypass(): |
| 59 |
resp = auth_client.post( |
| 60 |
reverse("mfa_activate_totp"), |
| 61 |
{ |
| 62 |
"code": "123", |
| 63 |
}, |
| 64 |
) |
| 65 |
assert resp["location"] == reverse("mfa_view_recovery_codes") |
| 66 |
assert Authenticator.objects.filter( |
| 67 |
user=user, type=Authenticator.Type.TOTP |
| 68 |
).exists() |
| 69 |
assert Authenticator.objects.filter( |
| 70 |
user=user, type=Authenticator.Type.RECOVERY_CODES |
| 71 |
).exists() |
| 72 |
assert len(mailoutbox) == 1 |
| 73 |
assert "Authenticator App Activated" in mailoutbox[0].subject |
| 74 |
assert "Authenticator app activated." in mailoutbox[0].body |
| 75 |
assert SECRET_SESSION_KEY not in auth_client.session |
| 76 |
|
| 77 |
|
| 78 |
def test_deactivate_totp_success( |
| 79 |
auth_client, user_with_totp, user_password, settings, mailoutbox |
| 80 |
): |
| 81 |
settings.ACCOUNT_EMAIL_NOTIFICATIONS = True |
| 82 |
resp = auth_client.get(reverse("mfa_deactivate_totp")) |
| 83 |
assert resp.status_code == HTTPStatus.FOUND |
| 84 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 85 |
resp = auth_client.post(resp["location"], {"password": user_password}) |
| 86 |
assert resp.status_code == HTTPStatus.FOUND |
| 87 |
resp = auth_client.post(reverse("mfa_deactivate_totp")) |
| 88 |
assert resp.status_code == HTTPStatus.FOUND |
| 89 |
assert resp["location"] == reverse("mfa_index") |
| 90 |
assert len(mailoutbox) == 1 |
| 91 |
assert "Authenticator App Deactivated" in mailoutbox[0].subject |
| 92 |
assert "Authenticator app deactivated." in mailoutbox[0].body |
| 93 |
|
| 94 |
|
| 95 |
def test_user_without_totp_deactivate_totp(auth_client): |
| 96 |
resp = auth_client.get(reverse("mfa_deactivate_totp")) |
| 97 |
assert resp.status_code == HTTPStatus.NOT_FOUND |
| 98 |
|
| 99 |
|
| 100 |
def test_user_with_totp_activate_totp( |
| 101 |
auth_client, user_with_totp, reauthentication_bypass |
| 102 |
): |
| 103 |
with reauthentication_bypass(): |
| 104 |
resp = auth_client.get(reverse("mfa_activate_totp")) |
| 105 |
assert resp.status_code == HTTPStatus.FOUND |
| 106 |
assert resp["location"] == reverse("mfa_deactivate_totp") |
| 107 |
|
| 108 |
|
| 109 |
def test_totp_login(client, user_with_totp, user_password, totp_validation_bypass): |
| 110 |
resp = client.post( |
| 111 |
reverse("account_login"), |
| 112 |
{"login": user_with_totp.username, "password": user_password}, |
| 113 |
) |
| 114 |
assert resp.status_code == HTTPStatus.FOUND |
| 115 |
assert resp["location"] == reverse("mfa_authenticate") |
| 116 |
resp = client.get(reverse("mfa_authenticate")) |
| 117 |
assert resp.context["request"].user.is_anonymous |
| 118 |
resp = client.post(reverse("mfa_authenticate"), {"code": "123"}) |
| 119 |
assert resp.context["form"].errors == { |
| 120 |
"code": [get_adapter().error_messages["incorrect_code"]] |
| 121 |
} |
| 122 |
with totp_validation_bypass(): |
| 123 |
resp = client.post( |
| 124 |
reverse("mfa_authenticate"), |
| 125 |
{"code": "123"}, |
| 126 |
) |
| 127 |
assert resp.status_code == HTTPStatus.FOUND |
| 128 |
assert resp["location"] == settings.LOGIN_REDIRECT_URL |
| 129 |
assert client.session[AUTHENTICATION_METHODS_SESSION_KEY] == [ |
| 130 |
{"method": "password", "at": ANY, "username": user_with_totp.username}, |
| 131 |
{"method": "mfa", "at": ANY, "id": ANY, "type": Authenticator.Type.TOTP}, |
| 132 |
] |
| 133 |
|
| 134 |
|
| 135 |
def test_totp_login_rate_limit( |
| 136 |
settings, enable_cache, user_with_totp, user_password, client |
| 137 |
): |
| 138 |
settings.ACCOUNT_LOGIN_ATTEMPTS_LIMIT = 3 |
| 139 |
resp = client.post( |
| 140 |
reverse("account_login"), |
| 141 |
{"login": user_with_totp.username, "password": user_password}, |
| 142 |
) |
| 143 |
assert resp.status_code == HTTPStatus.FOUND |
| 144 |
assert resp["location"] == reverse("mfa_authenticate") |
| 145 |
for i in range(5): |
| 146 |
is_locked = i >= 3 |
| 147 |
with patch("allauth.mfa.signals.authentication_failed") as auth_failed_signal: |
| 148 |
resp = client.post( |
| 149 |
reverse("mfa_authenticate"), |
| 150 |
{ |
| 151 |
"code": "wrong", |
| 152 |
}, |
| 153 |
) |
| 154 |
signal_called = not is_locked |
| 155 |
assert auth_failed_signal.send.called == signal_called |
| 156 |
if signal_called: |
| 157 |
signal_kwargs = auth_failed_signal.send.call_args[1] |
| 158 |
assert signal_kwargs["user"] == user_with_totp |
| 159 |
assert signal_kwargs["reauthentication"] is False |
| 160 |
assert resp.context["form"].errors == { |
| 161 |
"code": [ |
| 162 |
( |
| 163 |
"Too many failed login attempts. Try again later." |
| 164 |
if is_locked |
| 165 |
else "Incorrect code." |
| 166 |
) |
| 167 |
] |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
def test_cannot_deactivate_totp(auth_client, user_with_totp, user_password): |
| 172 |
with patch( |
| 173 |
"allauth.mfa.adapter.DefaultMFAAdapter.can_delete_authenticator" |
| 174 |
) as cda_mock: |
| 175 |
cda_mock.return_value = False |
| 176 |
resp = auth_client.get(reverse("mfa_deactivate_totp")) |
| 177 |
assert resp.status_code == HTTPStatus.FOUND |
| 178 |
assert resp["location"].startswith(reverse("account_reauthenticate")) |
| 179 |
resp = auth_client.post(resp["location"], {"password": user_password}) |
| 180 |
assert resp.status_code == HTTPStatus.FOUND |
| 181 |
resp = auth_client.get(reverse("mfa_deactivate_totp")) |
| 182 |
# When we GET, the form validation error is already on screen |
| 183 |
assert resp.context["form"].errors == { |
| 184 |
"__all__": [get_adapter().error_messages["cannot_delete_authenticator"]], |
| 185 |
} |
| 186 |
# And, when we POST anyway, it does not work |
| 187 |
resp = auth_client.post(reverse("mfa_deactivate_totp")) |
| 188 |
assert resp.status_code == HTTPStatus.OK |
| 189 |
assert resp.context["form"].errors == { |
| 190 |
"__all__": [get_adapter().error_messages["cannot_delete_authenticator"]], |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
def test_totp_code_reuse( |
| 195 |
user_with_totp, user_password, totp_validation_bypass, enable_cache |
| 196 |
): |
| 197 |
for code, time_lapse, expect_success in [ |
| 198 |
# First use of code, SUCCESS |
| 199 |
("123", False, True), |
| 200 |
# Second use, no time elapsed: FAIL |
| 201 |
("123", False, False), |
| 202 |
# Different code, no time elapsed: SUCCESS |
| 203 |
("456", False, True), |
| 204 |
# Again, previous code, no time elapsed: FAIL |
| 205 |
("123", False, False), |
| 206 |
# Previous code, but time elapsed: SUCCESS |
| 207 |
("123", True, True), |
| 208 |
]: |
| 209 |
if time_lapse: |
| 210 |
cache.clear() |
| 211 |
client = Client() |
| 212 |
resp = client.post( |
| 213 |
reverse("account_login"), |
| 214 |
{"login": user_with_totp.username, "password": user_password}, |
| 215 |
) |
| 216 |
assert resp.status_code == HTTPStatus.FOUND |
| 217 |
assert resp["location"] == reverse("mfa_authenticate") |
| 218 |
# Note that this bypass only bypasses the actual code check, not the |
| 219 |
# re-use check we're testing here. |
| 220 |
with totp_validation_bypass(): |
| 221 |
resp = client.post( |
| 222 |
reverse("mfa_authenticate"), |
| 223 |
{"code": code}, |
| 224 |
) |
| 225 |
if expect_success: |
| 226 |
assert resp.status_code == HTTPStatus.FOUND |
| 227 |
assert resp["location"] == settings.LOGIN_REDIRECT_URL |
| 228 |
else: |
| 229 |
assert resp.status_code == HTTPStatus.OK |
| 230 |
assert resp.context["form"].errors == { |
| 231 |
"code": [get_adapter().error_messages["incorrect_code"]] |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
def test_totp_stage_expires(client, user_with_totp, user_password): |
| 236 |
resp = client.post( |
| 237 |
reverse("account_login"), |
| 238 |
{"login": user_with_totp.username, "password": user_password}, |
| 239 |
) |
| 240 |
assert resp.status_code == HTTPStatus.FOUND |
| 241 |
assert resp["location"] == reverse("mfa_authenticate") |
| 242 |
resp = client.get(reverse("mfa_authenticate")) |
| 243 |
assert resp.status_code == HTTPStatus.OK |
| 244 |
assertTemplateUsed(resp, "mfa/authenticate.html") |
| 245 |
with patch( |
| 246 |
"allauth.account.internal.stagekit.time.time", |
| 247 |
return_value=time.time() + 1.1 * app_settings.LOGIN_TIMEOUT, |
| 248 |
): |
| 249 |
resp = client.get(reverse("mfa_authenticate")) |
| 250 |
assert resp.status_code == HTTPStatus.FOUND |
| 251 |
assert resp["location"] == reverse("account_login") |