Skip to content

Commit a97edaa

Browse files
authored
raise no-authorization warning once and allow disabled authorization (#738)
1 parent 66ef12d commit a97edaa

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

jupyter_server/auth/decorator.py

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,6 @@
1414
from .utils import HTTP_METHOD_TO_AUTH_ACTION
1515

1616

17-
def raise_no_authorizer_warning():
18-
warnings.warn(
19-
"The Tornado web application does not have an 'authorizer' defined "
20-
"in its settings. In future releases of jupyter_server, this will "
21-
"be a required key for all subclasses of `JupyterHandler`. For an "
22-
"example, see the jupyter_server source code for how to "
23-
"add an authorizer to the tornado settings: "
24-
"https://github.com/jupyter-server/jupyter_server/blob/"
25-
"653740cbad7ce0c8a8752ce83e4d3c2c754b13cb/jupyter_server/serverapp.py"
26-
"#L234-L256",
27-
# stacklevel=2
28-
)
29-
30-
3117
def authorized(
3218
action: Optional[Union[str, Callable]] = None,
3319
resource: Optional[str] = None,
@@ -74,17 +60,28 @@ def inner(self, *args, **kwargs):
7460
if not user:
7561
app_log.warning("Attempting to authorize request without authentication!")
7662
raise HTTPError(status_code=403, log_message=message)
77-
# If the user is allowed to do this action,
78-
# call the method.
63+
64+
# Handle the case where an authorizer wasn't attached to the handler.
7965
if not self.authorizer:
80-
with warnings.catch_warnings():
81-
warnings.simplefilter("once")
82-
raise_no_authorizer_warning()
83-
elif self.authorizer.is_authorized(self, user, action, resource):
66+
warnings.warn(
67+
"The Tornado web application does not have an 'authorizer' defined "
68+
"in its settings. In future releases of jupyter_server, this will "
69+
"be a required key for all subclasses of `JupyterHandler`. For an "
70+
"example, see the jupyter_server source code for how to "
71+
"add an authorizer to the tornado settings: "
72+
"https://github.com/jupyter-server/jupyter_server/blob/"
73+
"653740cbad7ce0c8a8752ce83e4d3c2c754b13cb/jupyter_server/serverapp.py"
74+
"#L234-L256",
75+
FutureWarning,
76+
)
8477
return method(self, *args, **kwargs)
85-
# else raise an exception.
86-
else:
87-
raise HTTPError(status_code=403, log_message=message)
78+
79+
# Only return the method if the action is authorized.
80+
if self.authorizer.is_authorized(self, user, action, resource):
81+
return method(self, *args, **kwargs)
82+
83+
# Raise an exception if the method wasn't returned (i.e. not authorized)
84+
raise HTTPError(status_code=403, log_message=message)
8885

8986
return inner
9087

0 commit comments

Comments
 (0)