Skip to content

Commit 6791cf9

Browse files
authored
(backport) ensure authorizer is defined (#815)
1 parent 514760a commit 6791cf9

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

jupyter_server/auth/decorator.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from tornado.log import app_log
99
from tornado.web import HTTPError
1010

11-
from .utils import HTTP_METHOD_TO_AUTH_ACTION, warn_disabled_authorization
11+
from .utils import HTTP_METHOD_TO_AUTH_ACTION
1212

1313

1414
def authorized(
@@ -57,18 +57,13 @@ def inner(self, *args, **kwargs):
5757
if not user:
5858
app_log.warning("Attempting to authorize request without authentication!")
5959
raise HTTPError(status_code=403, log_message=message)
60-
61-
# Handle the case where an authorizer wasn't attached to the handler.
62-
if not self.authorizer:
63-
warn_disabled_authorization()
64-
return method(self, *args, **kwargs)
65-
66-
# Only return the method if the action is authorized.
60+
# If the user is allowed to do this action,
61+
# call the method.
6762
if self.authorizer.is_authorized(self, user, action, resource):
6863
return method(self, *args, **kwargs)
69-
70-
# Raise an exception if the method wasn't returned (i.e. not authorized)
71-
raise HTTPError(status_code=403, log_message=message)
64+
# else raise an exception.
65+
else:
66+
raise HTTPError(status_code=403, log_message=message)
7267

7368
return inner
7469

jupyter_server/auth/utils.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@
88

99

1010
def warn_disabled_authorization():
11+
"""DEPRECATED, does nothing"""
1112
warnings.warn(
12-
"The Tornado web application does not have an 'authorizer' defined "
13-
"in its settings. In future releases of jupyter_server, this will "
14-
"be a required key for all subclasses of `JupyterHandler`. For an "
15-
"example, see the jupyter_server source code for how to "
16-
"add an authorizer to the tornado settings: "
17-
"https://github.com/jupyter-server/jupyter_server/blob/"
18-
"653740cbad7ce0c8a8752ce83e4d3c2c754b13cb/jupyter_server/serverapp.py"
19-
"#L234-L256",
20-
FutureWarning,
13+
"jupyter_server.auth.utils.warn_disabled_authorization is deprecated",
14+
DeprecationWarning,
15+
stacklevel=2,
2116
)
2217

2318

jupyter_server/base/handlers.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,23 @@ def login_available(self):
193193

194194
@property
195195
def authorizer(self):
196-
return self.settings.get("authorizer")
196+
if "authorizer" not in self.settings:
197+
warnings.warn(
198+
"The Tornado web application does not have an 'authorizer' defined "
199+
"in its settings. In future releases of jupyter_server, this will "
200+
"be a required key for all subclasses of `JupyterHandler`. For an "
201+
"example, see the jupyter_server source code for how to "
202+
"add an authorizer to the tornado settings: "
203+
"https://github.com/jupyter-server/jupyter_server/blob/"
204+
"653740cbad7ce0c8a8752ce83e4d3c2c754b13cb/jupyter_server/serverapp.py"
205+
"#L234-L256",
206+
)
207+
from jupyter_server.auth import AllowAllAuthorizer
208+
209+
self.settings["authorizer"] = AllowAllAuthorizer(
210+
config=self.settings.get("config", None)
211+
)
212+
return self.settings["authorizer"]
197213

198214

199215
class JupyterHandler(AuthenticatedHandler):

jupyter_server/base/zmqhandlers.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
from tornado import ioloop, web
2020
from tornado.websocket import WebSocketHandler
2121

22-
from jupyter_server.auth.utils import warn_disabled_authorization
23-
2422
from .handlers import JupyterHandler
2523

2624

@@ -321,10 +319,7 @@ def pre_get(self):
321319
raise web.HTTPError(403)
322320

323321
# authorize the user.
324-
if not self.authorizer:
325-
# Warn if there is not authorizer.
326-
warn_disabled_authorization()
327-
elif not self.authorizer.is_authorized(self, user, "execute", "kernels"):
322+
if not self.authorizer.is_authorized(self, user, "execute", "kernels"):
328323
raise web.HTTPError(403)
329324

330325
if self.get_argument("session_id", False):

jupyter_server/terminal/handlers.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from tornado import web
66

77
from jupyter_server._tz import utcnow
8-
from jupyter_server.auth.utils import warn_disabled_authorization
98

109
from ..base.handlers import JupyterHandler
1110
from ..base.zmqhandlers import WebSocketMixin
@@ -30,10 +29,7 @@ def get(self, *args, **kwargs):
3029
raise web.HTTPError(403)
3130

3231
# authorize the user.
33-
if not self.authorizer:
34-
# Warn if there is not authorizer.
35-
warn_disabled_authorization()
36-
elif not self.authorizer.is_authorized(self, user, "execute", self.auth_resource):
32+
if not self.authorizer.is_authorized(self, user, "execute", self.auth_resource):
3733
raise web.HTTPError(403)
3834

3935
if not args[0] in self.term_manager.terminals:

0 commit comments

Comments
 (0)