diff --git a/accounts/templates/accounts/otp_qr.html b/accounts/templates/accounts/otp_qr.html index 82697f70..2ce7af43 100644 --- a/accounts/templates/accounts/otp_qr.html +++ b/accounts/templates/accounts/otp_qr.html @@ -1,5 +1,5 @@ {% extends "base.html" %} -{% load i18n %} +{% load i18n static %} {% block title %}{% trans "OTP QR Code" %}{% endblock %} @@ -8,8 +8,17 @@ {% block content %}
+ {% if qr_svg %}

{% blocktrans %}Scan this QR code to get OTP for account '{{ user }}'{% endblocktrans %}

{{ qr_svg }} + {% else %} +
{% csrf_token %}
+

{% trans "Open the link from the mail to see the OTP QR code." %}

+ {% endif %}
{% endblock content %} + +{% block script %} +{% if not qr_svg %}{% endif %} +{% endblock script %} diff --git a/accounts/tests/test_utils.py b/accounts/tests/test_utils.py index bd84ec03..dd25e5e0 100644 --- a/accounts/tests/test_utils.py +++ b/accounts/tests/test_utils.py @@ -99,18 +99,24 @@ class AccountsUtilsTestCase(TestCase): self.assertEqual((message.subject, message.to, message.alternatives), ("OTP QR Code", ["user@example.com"], [])) self.assertNotIn(device.bin_key.hex(), message.body) self.assertNotIn("otpauth", message.body) - link = re.search(r"http://testserver(/\S+)", message.body).group(1) - page = self.client.get(link) - self.assertEqual(page["Cache-Control"], "no-store") - self.assertEqual(len(html.fromstring(page.content).xpath("//svg")), 1) + # the token is the fragment, which no request (no access log) carries + path, token = re.search(r"http://testserver(/\S+)#(\S+)", message.body).groups() + self.assertEqual(path, reverse("accounts:otp_qr")) + page = html.fromstring(self.client.get(path).content) + self.assertEqual((page.xpath("//svg"), page.xpath("//form[@id='otp-qr-form']/input[@name='token']/@type")), ([], ["hidden"])) + self.assertEqual(page.xpath("//script/@src")[-1], "/static/js/otp_qr.js") + shown = self.client.post(path, {"token": token}) + self.assertEqual(shown["Cache-Control"], "no-store") + self.assertEqual(len(html.fromstring(shown.content).xpath("//svg")), 1) @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)) with patch("django.core.signing.time.time", return_value=time.time() + OTP_LINK_MAX_AGE + 1): - self.assertEqual(self.client.get(reverse("accounts:otp_qr", args=[token])).status_code, 404) - self.assertEqual(self.client.get(reverse("accounts:otp_qr", args=[f"{self.user.pk}:x:y"])).status_code, 404) + 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) + self.assertEqual(self.client.post(reverse("accounts:otp_qr")).status_code, 404) def test_the_qr_code_is_what_django_qr_code_drew(self): # sha256 of the SVG django-qr-code 4.2.0 made for this URL diff --git a/accounts/urls.py b/accounts/urls.py index 1179d066..182f7665 100644 --- a/accounts/urls.py +++ b/accounts/urls.py @@ -38,7 +38,7 @@ urlpatterns = [ path("api_token/created/", views.api_token_created, name="api_token_created"), path("api_token//delete/", views.api_token_delete, name="api_token_delete"), path("email_otp/", views.email_otp, name="email_otp"), - path("otp_qr//", views.otp_qr, name="otp_qr"), + path("otp_qr/", views.otp_qr, name="otp_qr"), path( "admin_email_otp//", views.admin_email_otp, diff --git a/accounts/utils.py b/accounts/utils.py index a65ae96c..57f886f9 100644 --- a/accounts/utils.py +++ b/accounts/utils.py @@ -68,8 +68,9 @@ OTP_LINK_MAX_AGE = 3600 def send_email_with_otp(request, user): """Mail the user a link to the page of their OTP QR code, good for - OTP_LINK_MAX_AGE: the QR code's secret is never in a mailbox.""" - url = request.build_absolute_uri(reverse("accounts:otp_qr", args=[OTP_LINK.sign(str(user.pk))])) + OTP_LINK_MAX_AGE: the QR code's secret is never in a mailbox. The token + is the URL's fragment, which no request carries: it is in no access log.""" + url = request.build_absolute_uri(reverse("accounts:otp_qr")) + "#" + OTP_LINK.sign(str(user.pk)) send_mail( _("OTP QR Code"), _("Open this link within an hour to see the OTP QR code of account '%(user)s':\n\n%(url)s") diff --git a/accounts/views.py b/accounts/views.py index 5fadd73c..72105fbb 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -353,12 +353,15 @@ def email_otp(request): @login_not_required -def otp_qr(request, token): - """The OTP QR code of the user a mailed link (send_email_with_otp) names.""" +def otp_qr(request): + """The OTP QR code of the user a mailed link (send_email_with_otp) names. + The link's token is in its fragment: the page posts it (otp_qr.js).""" if not settings.OTP_ENABLED: raise Http404 + if request.method != "POST": + return render(request, "accounts/otp_qr.html") try: - user_id = OTP_LINK.unsign(token, max_age=OTP_LINK_MAX_AGE) + user_id = OTP_LINK.unsign(request.POST.get("token", ""), max_age=OTP_LINK_MAX_AGE) except BadSignature: raise Http404 user = get_object_or_404(get_user_model(), pk=user_id, is_active=True) diff --git a/locale/tr/LC_MESSAGES/django.mo b/locale/tr/LC_MESSAGES/django.mo index fa18f4ab..f548c02c 100644 Binary files a/locale/tr/LC_MESSAGES/django.mo and b/locale/tr/LC_MESSAGES/django.mo differ diff --git a/locale/tr/LC_MESSAGES/django.po b/locale/tr/LC_MESSAGES/django.po index 000e60cc..30404194 100644 --- a/locale/tr/LC_MESSAGES/django.po +++ b/locale/tr/LC_MESSAGES/django.po @@ -399,11 +399,15 @@ msgstr "2FA Koruması Etkin" msgid "OTP QR Code" msgstr "OTP QR Kodu" -#: accounts/templates/accounts/otp_qr.html:11 +#: accounts/templates/accounts/otp_qr.html:12 #, python-format msgid "Scan this QR code to get OTP for account '%(user)s'" msgstr "'%(user)s' hesabının OTP kodlarını almak için bu QR kodunu tarayın" +#: accounts/templates/accounts/otp_qr.html:16 +msgid "Open the link from the mail to see the OTP QR code." +msgstr "OTP QR kodunu görmek için e-postadaki bağlantıyı açın." + #: accounts/templates/api_token_created.html:4 #: accounts/templates/api_token_created.html:5 msgid "API token created" diff --git a/locale/zh/LC_MESSAGES/django.mo b/locale/zh/LC_MESSAGES/django.mo index 6c785cd2..f7034050 100644 Binary files a/locale/zh/LC_MESSAGES/django.mo and b/locale/zh/LC_MESSAGES/django.mo differ diff --git a/locale/zh/LC_MESSAGES/django.po b/locale/zh/LC_MESSAGES/django.po index b104851e..0aa71ae7 100644 --- a/locale/zh/LC_MESSAGES/django.po +++ b/locale/zh/LC_MESSAGES/django.po @@ -396,11 +396,15 @@ msgstr "双因素保护已启用" msgid "OTP QR Code" msgstr "OTP 二维码" -#: accounts/templates/accounts/otp_qr.html:11 +#: accounts/templates/accounts/otp_qr.html:12 #, python-format msgid "Scan this QR code to get OTP for account '%(user)s'" msgstr "扫描此二维码以获取账户“%(user)s”的 OTP" +#: accounts/templates/accounts/otp_qr.html:16 +msgid "Open the link from the mail to see the OTP QR code." +msgstr "请打开邮件中的链接以查看 OTP 二维码。" + #: accounts/templates/api_token_created.html:4 #: accounts/templates/api_token_created.html:5 msgid "API token created" diff --git a/static/js/otp_qr.js b/static/js/otp_qr.js new file mode 100644 index 00000000..41c243ec --- /dev/null +++ b/static/js/otp_qr.js @@ -0,0 +1,8 @@ +// The mailed OTP link carries its token after "#", which no request sends, +// so it is in no access log: the page posts it to get the QR code. +var otpQrForm = document.getElementById("otp-qr-form"); +if (otpQrForm && location.hash.length > 1) { + otpQrForm.elements.token.value = location.hash.slice(1); + history.replaceState(null, "", location.pathname); + otpQrForm.submit(); +}