|
| 1 | +""" |
| 2 | +Test legacy login config via ServerApp.login_handler_class |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +from functools import wraps |
| 7 | +from urllib.parse import urlencode |
| 8 | + |
| 9 | +import pytest |
| 10 | +from tornado.httpclient import HTTPClientError |
| 11 | +from tornado.httputil import parse_cookie, url_concat |
| 12 | +from traitlets.config import Config |
| 13 | + |
| 14 | +from jupyter_server.auth.identity import LegacyIdentityProvider |
| 15 | +from jupyter_server.auth.login import LoginHandler |
| 16 | +from jupyter_server.auth.security import passwd |
| 17 | +from jupyter_server.serverapp import ServerApp |
| 18 | +from jupyter_server.utils import url_path_join |
| 19 | + |
| 20 | +# Don't raise on deprecation warnings in this module testing deprecated behavior |
| 21 | +pytestmark = pytest.mark.filterwarnings("ignore::DeprecationWarning") |
| 22 | + |
| 23 | + |
| 24 | +def record_calls(f): |
| 25 | + """Decorator to record call history""" |
| 26 | + f._calls = [] |
| 27 | + |
| 28 | + @wraps(f) |
| 29 | + def wrapped_f(*args, **kwargs): |
| 30 | + f._calls.append((args, kwargs)) |
| 31 | + return f(*args, **kwargs) |
| 32 | + |
| 33 | + return wrapped_f |
| 34 | + |
| 35 | + |
| 36 | +class CustomLoginHandler(LoginHandler): |
| 37 | + @classmethod |
| 38 | + @record_calls |
| 39 | + def get_user(cls, handler): |
| 40 | + header_user = handler.request.headers.get("test-user") |
| 41 | + if header_user: |
| 42 | + if header_user == "super": |
| 43 | + return super().get_user(handler) |
| 44 | + return header_user |
| 45 | + else: |
| 46 | + return None |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +def jp_server_config(): |
| 51 | + cfg = Config() |
| 52 | + cfg.ServerApp.login_handler_class = CustomLoginHandler |
| 53 | + return cfg |
| 54 | + |
| 55 | + |
| 56 | +def test_legacy_identity_config(jp_serverapp): |
| 57 | + # setting login_handler_class sets LegacyIdentityProvider |
| 58 | + app = ServerApp() |
| 59 | + idp = jp_serverapp.identity_provider |
| 60 | + assert type(idp) is LegacyIdentityProvider |
| 61 | + assert idp.login_available |
| 62 | + assert idp.auth_enabled |
| 63 | + assert idp.token |
| 64 | + assert idp.get_handlers() == [ |
| 65 | + ("/login", idp.login_handler_class), |
| 66 | + ("/logout", idp.logout_handler_class), |
| 67 | + ] |
| 68 | + |
| 69 | + |
| 70 | +async def test_legacy_identity_api(jp_serverapp, jp_fetch): |
| 71 | + response = await jp_fetch("/api/me", headers={"test-user": "pinecone"}) |
| 72 | + assert response.code == 200 |
| 73 | + model = json.loads(response.body.decode("utf8")) |
| 74 | + assert model["identity"]["username"] == "pinecone" |
| 75 | + |
| 76 | + |
| 77 | +async def test_legacy_base_class(jp_serverapp, jp_fetch): |
| 78 | + response = await jp_fetch("/api/me", headers={"test-user": "super"}) |
| 79 | + assert "Set-Cookie" in response.headers |
| 80 | + cookie = response.headers["Set-Cookie"] |
| 81 | + assert response.code == 200 |
| 82 | + model = json.loads(response.body.decode("utf8")) |
| 83 | + user_id = model["identity"]["username"] # a random uuid |
| 84 | + assert user_id |
| 85 | + |
| 86 | + response = await jp_fetch("/api/me", headers={"test-user": "super", "Cookie": cookie}) |
| 87 | + model2 = json.loads(response.body.decode("utf8")) |
| 88 | + # second request, should trigger cookie auth |
| 89 | + assert model2["identity"] == model["identity"] |
| 90 | + |
| 91 | + |
| 92 | +async def test_legacy_login(jp_serverapp, http_server_client, jp_base_url, jp_fetch): |
| 93 | + login_url = url_path_join(jp_base_url, "login") |
| 94 | + first = await http_server_client.fetch(login_url) |
| 95 | + cookie_header = first.headers["Set-Cookie"] |
| 96 | + xsrf = parse_cookie(cookie_header).get("_xsrf", "") |
| 97 | + new_password = "super-secret" |
| 98 | + |
| 99 | + async def login(form_fields): |
| 100 | + form = {"_xsrf": xsrf} |
| 101 | + form.update(form_fields) |
| 102 | + try: |
| 103 | + resp = await http_server_client.fetch( |
| 104 | + login_url, |
| 105 | + method="POST", |
| 106 | + body=urlencode(form), |
| 107 | + headers={"Cookie": cookie_header}, |
| 108 | + follow_redirects=False, |
| 109 | + ) |
| 110 | + except HTTPClientError as e: |
| 111 | + resp = e.response |
| 112 | + assert resp.code == 302, "Should have returned a redirect!" |
| 113 | + return resp |
| 114 | + |
| 115 | + resp = await login( |
| 116 | + dict(password=jp_serverapp.identity_provider.token, new_password=new_password) |
| 117 | + ) |
| 118 | + cookie = resp.headers["Set-Cookie"] |
| 119 | + id_resp = await jp_fetch("/api/me", headers={"test-user": "super", "Cookie": cookie}) |
| 120 | + assert id_resp.code == 200 |
| 121 | + model = json.loads(id_resp.body.decode("utf8")) |
| 122 | + user_id = model["identity"]["username"] # a random uuid |
| 123 | + |
| 124 | + # verify password change with second login |
| 125 | + resp2 = await login(dict(password=new_password)) |
| 126 | + cookie = resp.headers["Set-Cookie"] |
| 127 | + id_resp = await jp_fetch("/api/me", headers={"test-user": "super", "Cookie": cookie}) |
| 128 | + assert id_resp.code == 200 |
| 129 | + model = json.loads(id_resp.body.decode("utf8")) |
| 130 | + user_id2 = model["identity"]["username"] # a random uuid |
| 131 | + assert user_id2 == user_id |
| 132 | + |
| 133 | + |
| 134 | +def test_deprecated_config(): |
| 135 | + cfg = Config() |
| 136 | + cfg.ServerApp.token = token = "asdf" |
| 137 | + cfg.ServerApp.password = password = passwd("secrets") |
| 138 | + app = ServerApp(config=cfg) |
| 139 | + app.initialize([]) |
| 140 | + app.init_configurables() |
| 141 | + assert app.identity_provider.token == token |
| 142 | + assert app.token == token |
| 143 | + assert app.identity_provider.hashed_password == password |
| 144 | + assert app.password == password |
| 145 | + |
| 146 | + |
| 147 | +def test_deprecated_config_priority(): |
| 148 | + cfg = Config() |
| 149 | + cfg.ServerApp.token = "ignored" |
| 150 | + cfg.IdentityProvider.token = token = "idp_token" |
| 151 | + cfg.ServerApp.password = passwd("ignored") |
| 152 | + cfg.PasswordIdentityProvider.hashed_password = password = passwd("used") |
| 153 | + app = ServerApp(config=cfg) |
| 154 | + app.initialize([]) |
| 155 | + app.init_configurables() |
| 156 | + assert app.identity_provider.token == token |
| 157 | + assert app.identity_provider.hashed_password == password |
0 commit comments