|
14 | 14 | from .utils import HTTP_METHOD_TO_AUTH_ACTION
|
15 | 15 |
|
16 | 16 |
|
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 |
| - |
31 | 17 | def authorized(
|
32 | 18 | action: Optional[Union[str, Callable]] = None,
|
33 | 19 | resource: Optional[str] = None,
|
@@ -74,17 +60,28 @@ def inner(self, *args, **kwargs):
|
74 | 60 | if not user:
|
75 | 61 | app_log.warning("Attempting to authorize request without authentication!")
|
76 | 62 | 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. |
79 | 65 | 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 | + ) |
84 | 77 | 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) |
88 | 85 |
|
89 | 86 | return inner
|
90 | 87 |
|
|
0 commit comments