|
1 |
| -import asyncio |
2 | 1 | import json
|
3 |
| -import functools |
4 | 2 | import flask
|
5 | 3 | import pytest
|
6 |
| -from tests.utils import test_async |
7 | 4 | from multiprocessing import Value
|
8 | 5 |
|
9 | 6 | from dash import Dash, Output, Input, html, dcc
|
10 | 7 | from dash.types import RendererHooks
|
11 |
| -from werkzeug.exceptions import HTTPException |
12 | 8 |
|
13 | 9 |
|
14 | 10 | def test_rdrh001_request_hooks(dash_duo):
|
@@ -257,40 +253,16 @@ def update_output(value):
|
257 | 253 | jwt_token = Value("i", 0)
|
258 | 254 |
|
259 | 255 | # test with an auth layer that requires a JWT with a certain length
|
260 |
| - def protect_route(func): |
261 |
| - @functools.wraps(func) |
262 |
| - def wrap(*args, **kwargs): |
263 |
| - try: |
264 |
| - |
265 |
| - if flask.request.method == "OPTIONS": |
266 |
| - return func(*args, **kwargs) |
267 |
| - token = flask.request.headers.environ.get("HTTP_AUTHORIZATION") |
268 |
| - if jwt_token.value and ( |
269 |
| - not token or len(token) != jwt_token.value + len("Bearer ") |
270 |
| - ): |
271 |
| - # Read the data to prevent bug with base http server. |
272 |
| - flask.request.get_json(silent=True) |
273 |
| - flask.abort(expiry_code, description="JWT Expired " + str(token)) |
274 |
| - except HTTPException as e: |
275 |
| - return e |
276 |
| - if asyncio.iscoroutinefunction(func): |
277 |
| - if test_async(): |
278 |
| - from asgiref.sync import ( |
279 |
| - async_to_sync, |
280 |
| - ) # pylint: disable=unused-import, # noqa: F401 |
281 |
| - |
282 |
| - return async_to_sync(func)(*args, **kwargs) |
283 |
| - return func(*args, **kwargs) |
284 |
| - |
285 |
| - return wrap |
286 |
| - |
287 |
| - # wrap all API calls with auth. |
288 |
| - for name, method in ( |
289 |
| - (x, app.server.view_functions[x]) |
290 |
| - for x in app.routes |
291 |
| - if x in app.server.view_functions |
292 |
| - ): |
293 |
| - app.server.view_functions[name] = protect_route(method) |
| 256 | + @app.server.before_request |
| 257 | + def add_auth(): |
| 258 | + if flask.request.method != "OPTIONS": |
| 259 | + token = flask.request.headers.environ.get("HTTP_AUTHORIZATION") |
| 260 | + if jwt_token.value and ( |
| 261 | + not token or len(token) != jwt_token.value + len("Bearer ") |
| 262 | + ): |
| 263 | + # Read the data to prevent bug with base http server. |
| 264 | + flask.request.get_json(silent=True) |
| 265 | + flask.abort(expiry_code, description="JWT Expired " + str(token)) |
294 | 266 |
|
295 | 267 | dash_duo.start_server(app)
|
296 | 268 |
|
|
0 commit comments