Skip to content

Commit a3b4162

Browse files
authored
fix: Don't redirect user to the login page when logging in (ietf-tools#6570)
* fix: Don't redirect user to the login page when logging in (ietf-tools#5876) (Embrace and extend c4bf508.) * test: Add test case for login button * refactor: The template filter just strips off a path prefix, so rename/recode accordingly Also test with a non-trivial redirect target.
1 parent 1df7319 commit a3b4162

4 files changed

Lines changed: 40 additions & 11 deletions

File tree

ietf/doc/templatetags/ietf_filters.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright The IETF Trust 2007-2020, All Rights Reserved
1+
# Copyright The IETF Trust 2007-2023, All Rights Reserved
22
# -*- coding: utf-8 -*-
33

44

@@ -409,18 +409,18 @@ def startswith(x, y):
409409
return str(x).startswith(y)
410410

411411

412-
@register.filter(name='removesuffix', is_safe=False)
413-
def removesuffix(value, suffix):
414-
"""Remove an exact-match suffix
412+
@register.filter(name='removeprefix', is_safe=False)
413+
def removeprefix(value, prefix):
414+
"""Remove an exact-match prefix
415415
416416
The is_safe flag is False because indiscriminate use of this could result in non-safe output.
417417
See https://docs.djangoproject.com/en/2.2/howto/custom-template-tags/#filters-and-auto-escaping
418418
which describes the possibility that removing characters from an escaped string may introduce
419419
HTML-unsafe output.
420420
"""
421421
base = str(value)
422-
if base.endswith(suffix):
423-
return base[:-len(suffix)]
422+
if base.startswith(prefix):
423+
return base[len(prefix):]
424424
else:
425425
return base
426426

ietf/ietfauth/tests.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright The IETF Trust 2009-2022, All Rights Reserved
1+
# Copyright The IETF Trust 2009-2023, All Rights Reserved
22
# -*- coding: utf-8 -*-
33

44

@@ -111,6 +111,35 @@ def test_login_and_logout(self):
111111
self.assertEqual(r.status_code, 302)
112112
self.assertEqual(urlsplit(r["Location"])[2], "/foobar")
113113

114+
def test_login_button(self):
115+
PersonFactory(user__username='plain')
116+
117+
def _test_login(url):
118+
# try mashing the sign-in button repeatedly
119+
r = self.client.get(url)
120+
if r.status_code == 302:
121+
r = self.client.get(r["Location"])
122+
self.assertEqual(r.status_code, 200)
123+
q = PyQuery(r.content)
124+
login_url = q("a:Contains('Sign in')").attr("href")
125+
self.assertEqual(login_url, "/accounts/login/?next=" + url)
126+
r = self.client.get(login_url)
127+
self.assertEqual(r.status_code, 200)
128+
q = PyQuery(r.content)
129+
login_url = q("a:Contains('Sign in')").attr("href")
130+
self.assertEqual(login_url, "/accounts/login/?next=" + url)
131+
132+
# try logging in with the provided next
133+
r = self.client.post(login_url, {"username":"plain", "password":"plain+password"})
134+
self.assertEqual(r.status_code, 302)
135+
self.assertEqual(urlsplit(r["Location"])[2], url)
136+
self.client.logout()
137+
138+
# try with a trivial next
139+
_test_login("/")
140+
# try with a next that requires login
141+
_test_login(urlreverse(ietf.ietfauth.views.profile))
142+
114143
def test_login_with_different_email(self):
115144
person = PersonFactory(user__username='plain')
116145
email = EmailFactory(person=person)

ietf/templates/base.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{# Copyright The IETF Trust 2015-2022, All Rights Reserved #}
1+
{# Copyright The IETF Trust 2015-2023, All Rights Reserved #}
22
<!DOCTYPE html>
33
{% load analytical %}
44
{% load ietf_filters static %}
@@ -60,7 +60,7 @@
6060
{% if not user.is_authenticated %}
6161
<a class="btn me-1 {% if server_mode and server_mode == "production" %} btn-warning {% else %} btn-info {% endif %} d-none d-sm-block"
6262
rel="nofollow"
63-
href="{% url 'ietf.ietfauth.views.login' %}?next={{ request.get_full_path|removesuffix:'accounts/logout/'|urlencode }}">
63+
href="{% url 'ietf.ietfauth.views.login' %}?next={{ request.get_full_path|removeprefix:'/accounts/logout'|removeprefix:'/accounts/login/?next='|urlencode }}">
6464
Sign in
6565
</a>
6666
{% endif %}

ietf/templates/base/menu_user.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{# Copyright The IETF Trust 2015, All Rights Reserved #}
1+
{# Copyright The IETF Trust 2015-2023, All Rights Reserved #}
22
{% load origin %}
33
{% origin %}
44
{% load ietf_filters %}
@@ -87,7 +87,7 @@
8787
<li>
8888
<a class="dropdown-item {% if flavor != 'top' %} text-wrap{% endif %}"
8989
rel="nofollow"
90-
href="{% url 'ietf.ietfauth.views.login' %}?next={{ request.get_full_path|urlencode }}">
90+
href="{% url 'ietf.ietfauth.views.login' %}?next={{ request.get_full_path|removeprefix:'/accounts/login/?next='|urlencode }}">
9191
Sign in
9292
</a>
9393
</li>

0 commit comments

Comments
 (0)