-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an option to have authentication enabled for all endpoints by default #1392
Merged
+632
−26
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3f65147
Add `allow_unauthenticated_access` traitlet and `@allow_unauthenticated`
krassowski a9cff16
Tests, correct coroutine handling, add decorators where needed,
krassowski 646739e
Align ordering of test cases, improve wording of comments
krassowski 4cbb504
Implement auth and tests for websockets
krassowski faee488
Use `allow_unauthenticated` in `FilesRedirectHandler` for now
krassowski 204d29f
Do not touch coroutines
krassowski dccc423
Add runtime check to ensure handlers have auth decorators
krassowski 8e13727
Do not ever raise for extension endpoints, and only
krassowski e6a8d9a
Add test for extension handler warning, correct type
krassowski 2882516
Fix test for authentication enforcement for extensions
krassowski 4e1d664
Add test for GET redirect in handler tests
krassowski cd84175
Add `JUPYTER_SERVER_ALLOW_UNAUTHENTICATED_ACCESS` env variable
krassowski 5f6bc16
Better heuristic for `@tornado.web.authenticated`
krassowski 0facfec
Improve docstring/fix typo
krassowski 90d7044
Improve and test tornado.web.authenticated heuristic
krassowski 54c2ea2
Merge branch 'main' into auth-by-default
krassowski 319a4d1
Call `super.prepare()` in all code paths, test it
krassowski 5e7615d
Add test for identity provider being used
krassowski 4265f4e
Ensure that identity provider is used for auth,
krassowski c3f5d8f
Update minimum `pytest-jupyter`; we need 0.7 as it
krassowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Ensure that identity provider is used for auth,
even in websockets (but only if those inherit from JupyterHandler, and if they do not fallback to previous implementation and warn).
commit 4265f4e6df6dcf60164a6bfcf158cdb03910e819
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -589,7 +589,7 @@ def check_host(self) -> bool: | |
) | ||
return allow | ||
|
||
async def prepare(self) -> Awaitable[None] | None: # type:ignore[override] | ||
async def prepare(self, *, _redirect_to_login=True) -> Awaitable[None] | None: # type:ignore[override] | ||
"""Prepare a response.""" | ||
# Set the current Jupyter Handler context variable. | ||
CallContext.set(CallContext.JUPYTER_HANDLER, self) | ||
|
@@ -636,9 +636,18 @@ async def prepare(self) -> Awaitable[None] | None: # type:ignore[override] | |
raise HTTPError(403) | ||
method = getattr(self, self.request.method.lower()) | ||
if not getattr(method, "__allow_unauthenticated", False): | ||
# reuse `web.authenticated` logic, which redirects to the login | ||
# page on GET and HEAD and otherwise raises 403 | ||
return web.authenticated(lambda _: super().prepare)(self) | ||
if _redirect_to_login: | ||
# reuse `web.authenticated` logic, which redirects to the login | ||
# page on GET and HEAD and otherwise raises 403 | ||
return web.authenticated(lambda _: super().prepare())(self) | ||
else: | ||
# raise 403 if user is not known without redirecting to login page | ||
user = self.current_user | ||
if user is None: | ||
self.log.warning( | ||
f"Couldn't authenticate {self.__class__.__name__} connection" | ||
) | ||
raise web.HTTPError(403) | ||
|
||
return super().prepare() | ||
|
||
|
@@ -736,7 +745,7 @@ def write_error(self, status_code: int, **kwargs: Any) -> None: | |
class APIHandler(JupyterHandler): | ||
"""Base class for API handlers""" | ||
|
||
async def prepare(self) -> None: | ||
async def prepare(self) -> None: # type:ignore[override] | ||
"""Prepare an API response.""" | ||
await super().prepare() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something for later - API handlers should probably 403, not redirect. Now that we have logic for this. |
||
if not self.check_origin(): | ||
|
@@ -848,7 +857,7 @@ def options(self, *args: Any, **kwargs: Any) -> None: | |
class Template404(JupyterHandler): | ||
"""Render our 404 template""" | ||
|
||
async def prepare(self) -> None: | ||
async def prepare(self) -> None: # type:ignore[override] | ||
"""Prepare a 404 response.""" | ||
await super().prepare() | ||
raise web.HTTPError(404) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private keyword arg makes this hard for subclasses, e.g. extensions with websockets, but I suppose they will inherit from our own websocket classes, too?
I wonder if this should be a Handler class attribute, rather than a private argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a check for inheritance from
JupyterHandler
in the weboscket mixin class:jupyter_server/jupyter_server/base/websocket.py
Lines 110 to 121 in c3f5d8f
A Handler class attribute is certainly a reasonable alternative that I briefly considered. My thinking was along: if you pass
_redirect_to_login
argument but are inheriting from wrong class you will get an error. If you set a class attribute but are inheriting from a wrong class you would not get anything. The downside of passing argument is that mypy typing might complain downstream.