-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mfa): Added support for TOTP code tolerances
This PR adds support for a TOTP tolerance range using a new `TOTP_TOLERANCE` MFA setting. It is very similar to https://django-otp-official.readthedocs.io/en/stable/overview.html#django_otp.plugins.otp_totp.models.TOTPDevice.tolerance. The idea is rather simple: if `TOTP_TOLERANCE` is non-zero we allow codes equal to the expected counter +/- TOTP_TOLERANCE. This should address #3954 Co-authored-by: Josiah Bruner <josiah.bruner@jellyfish.co> Reviewed-on: https://codeberg.org/allauth/django-allauth/pulls/4183 Reviewed-by: pennersr <pennersr@noreply.codeberg.org> Co-authored-by: josiah <josiah@noreply.codeberg.org> Co-committed-by: josiah <josiah@noreply.codeberg.org>
- Loading branch information
1 parent
db7e4bd
commit dff1b87
Showing
4 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from unittest import mock | ||
|
||
from allauth.mfa import app_settings | ||
from allauth.mfa.totp.internal.auth import ( | ||
format_hotp_value, | ||
generate_totp_secret, | ||
hotp_value, | ||
validate_totp_code, | ||
yield_hotp_counters_from_time, | ||
) | ||
|
||
|
||
@mock.patch("time.time", mock.MagicMock(return_value=1731948631)) | ||
def test_totp_counters_from_time(): | ||
app_settings.TOTP_TOLERANCE = 0 | ||
counters = list(yield_hotp_counters_from_time()) | ||
assert len(counters) == 1 | ||
|
||
|
||
@mock.patch("time.time", mock.MagicMock(return_value=1731948631)) | ||
def test_totp_counters_from_time_with_tolerance(): | ||
app_settings.TOTP_TOLERANCE = 1 | ||
counters = list(yield_hotp_counters_from_time()) | ||
assert len(counters) == 3 | ||
|
||
|
||
@mock.patch("time.time", mock.MagicMock(return_value=1731948631)) | ||
def test_validate_with_tolerance(): | ||
app_settings.TOTP_TOLERANCE = 1 | ||
test_secret = generate_totp_secret() | ||
expected_value = format_hotp_value(hotp_value(test_secret, 57731621)) | ||
assert validate_totp_code(test_secret, expected_value) | ||
|
||
before_value = format_hotp_value(hotp_value(test_secret, 57731620)) | ||
assert validate_totp_code(test_secret, before_value) | ||
|
||
after_value = format_hotp_value(hotp_value(test_secret, 57731622)) | ||
assert validate_totp_code(test_secret, after_value) | ||
|
||
two_before_value = format_hotp_value(hotp_value(test_secret, 57731619)) | ||
assert not validate_totp_code(test_secret, two_before_value) | ||
|
||
two_after_value = format_hotp_value(hotp_value(test_secret, 57731623)) | ||
assert not validate_totp_code(test_secret, two_after_value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters