1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-01 16:36:06 +00:00

feat(accounts): an administrator can reset a user's OTP

A user's OTP secret could not be changed: "Lost OTP?" and the admin's
"Email OTP QR code" both send the same secret again, so a stolen phone
kept making valid codes. The user's profile page now has "Reset OTP",
which deletes the user's TOTP device; they sign in with the password
alone until a new QR code is mailed, which has a new secret.

The mailed link now names the device, not the user: a link mailed before
a reset shows nothing, not the next device's secret. A new signing salt
refuses the links that named a user, whose id may be another user's device.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kk4h2TcUiaWmU5MUszYSea
This commit is contained in:
catborise 2026-11-10 08:00:39 +03:00
parent fa0f7f79ac
commit 135839bd66
10 changed files with 992 additions and 888 deletions

View file

@ -93,7 +93,7 @@ class AccountsUtilsTestCase(TestCase):
@override_settings(OTP_ENABLED=True)
def test_the_mail_links_to_the_qr_code_and_holds_no_secret(self):
device = get_user_totp_device(self.user)
send_email_with_otp(RequestFactory().get("/"), self.user)
send_email_with_otp(RequestFactory().get("/"), device)
self.assertEqual(len(mail.outbox), 1)
message = mail.outbox[0]
self.assertEqual((message.subject, message.to, message.alternatives), ("OTP QR Code", ["user@example.com"], []))
@ -111,8 +111,7 @@ class AccountsUtilsTestCase(TestCase):
@override_settings(OTP_ENABLED=True)
def test_a_link_that_is_old_or_not_signed_shows_nothing(self):
get_user_totp_device(self.user)
token = OTP_LINK.sign(str(self.user.pk))
token = OTP_LINK.sign(str(get_user_totp_device(self.user).pk))
with patch("django.core.signing.time.time", return_value=time.time() + OTP_LINK_MAX_AGE + 1):
self.assertEqual(self.client.post(reverse("accounts:otp_qr"), {"token": token}).status_code, 404)
self.assertEqual(self.client.post(reverse("accounts:otp_qr"), {"token": f"{self.user.pk}:x:y"}).status_code, 404)