Skip to content

Commit

Permalink
Merge pull request #16 from Ph0tonic/fix_redirect_issue
Browse files Browse the repository at this point in the history
Fix missing next redirect handling
  • Loading branch information
sgaist authored Nov 20, 2023
2 parents 076e6c0 + dc01223 commit 3c36f26
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions multiauthenticator/multiauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ def check_blocked_users(self, username, authentication=None):
self._authenticators.append(authenticator)

def get_custom_html(self, base_url):
"""Re-implementation generating one login button per configured authenticator"""
"""Re-implementation generating one login button per configured authenticator
Note: the html generated in this method will be passed through Jinja's template
rendering, see the login implementation in JupyterHub's sources.
"""

html = []
for authenticator in self._authenticators:
Expand All @@ -137,7 +141,7 @@ def get_custom_html(self, base_url):
html.append(
f"""
<div class="service-login">
<a role="button" class='btn btn-jupyter btn-lg' href='{url}'>
<a role="button" class='btn btn-jupyter btn-lg' href='{url}{{% if next is defined and next|length %}}?next={{{{next}}}}{{% endif %}}'>
Sign in with {login_service}
</a>
</div>
Expand Down
25 changes: 25 additions & 0 deletions multiauthenticator/tests/test_multiauthenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Test module for the MultiAuthenticator class"""
import pytest

from jinja2 import Template
from jupyterhub.auth import DummyAuthenticator
from jupyterhub.auth import PAMAuthenticator
from oauthenticator import OAuthenticator
Expand Down Expand Up @@ -297,3 +298,27 @@ class MyAuthenticator(OAuthenticator):
MultiAuthenticator()

assert f"Login service cannot contain {PREFIX_SEPARATOR}" in str(excinfo.value)


def test_next_handling():
MultiAuthenticator.authenticators = [
(
PAMAuthenticator,
"/pam",
{"service_name": "test-service", "allowed_users": {"test"}},
),
]

multi_authenticator = MultiAuthenticator()
html = multi_authenticator.get_custom_html("")

template = Template(html)

with_next = template.render({"next": "/next-destination"})
assert "href='pam/login?next=/next-destination'" in with_next

without_next = template.render()
assert "href='pam/login'" in without_next

with_empty_next = template.render({"next": ""})
assert "href='pam/login'" in with_empty_next

0 comments on commit 3c36f26

Please sign in to comment.