Skip to content

Commit

Permalink
Get JupyterHub XSRF token from each redirect hop
Browse files Browse the repository at this point in the history
This is similar to what we're already doing for logging into JupyterLab.
Adam found this is necessary for logging into JupyterHub as well, see
https://rubinobs.atlassian.net/browse/DM-45523
  • Loading branch information
jonathansick committed Aug 2, 2024
1 parent 94f0cff commit d614e7d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
17 changes: 17 additions & 0 deletions changelog.d/20240802_135738_jsick_DM_45563.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- Delete the sections that don't apply -->

### Backwards-incompatible changes

-

### New features

-

### Bug fixes

- When logging into JupyterHub, a Noteburst now looks for XRSF tokens from each redirect.

### Other changes

-
28 changes: 21 additions & 7 deletions src/noteburst/jupyterclient/jupyterlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,16 +483,30 @@ def _extract_xsrf(self, response: Response) -> str | None:
async def log_into_hub(self) -> None:
"""Log into JupyterHub or raise a JupyterError."""
self.logger.debug("Logging into JupyterHub")
r = await self.http_client.get(self.url_for("hub/home"))
# JupyterHub returns a 302 redirect to the login page on success,
# but we don't want to follow that redirect. This request is just
# to set cookies.
if r.status_code >= 400:
raise JupyterError.from_response(self.user.username, r)
url = self.url_for("hub/home")
r = await self.http_client.get(url, follow_redirects=False)
while r.is_redirect:
xsrf = self._extract_xsrf(r)
if xsrf and xsrf != self._lab_xsrf:
self._hub_xsrf = xsrf
next_url = urljoin(url, r.headers["Location"])
r = await self.http_client.get(next_url, follow_redirects=False)
r.raise_for_status()
xsrf = self._extract_xsrf(r)
if xsrf:
if xsrf and xsrf != self._lab_xsrf:
self._hub_xsrf = xsrf

if not self._hub_xsrf:
raise JupyterError(
reason="No XSRF token found for JupyterHub",
url=url,
username=self.user.username,
status=r.status_code,
method="GET",
body=r.text,
)
self.logger.debug("Logged into JupyterHub with XSRF token")

async def log_into_lab(self) -> None:
"""Log into JupyterLab or raise a JupyterError."""
self.logger.debug("Logging into JupyterLab")
Expand Down
2 changes: 1 addition & 1 deletion src/noteburst/worker/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def create_message(message: str) -> SlackMessage:
)


async def shutdown(ctx: dict[Any, Any]) -> None: # noqa: PLR0912
async def shutdown(ctx: dict[Any, Any]) -> None:
"""Clean up the worker context on shutdown."""
if "logger" in ctx:
logger = ctx["logger"]
Expand Down

0 comments on commit d614e7d

Please sign in to comment.