Skip to content

Commit e53bb8c

Browse files
committed
Allow both 400 and 401 for JWT expiry responses
1 parent c124fdf commit e53bb8c

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

dash/dash-renderer/src/actions/api.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export default function apiThunk(endpoint, method, store, id, body) {
6565
return;
6666
}
6767

68-
if (res.status === STATUS.UNAUTHORIZED) {
68+
if (
69+
res.status === STATUS.UNAUTHORIZED ||
70+
res.status === STATUS.BAD_REQUEST
71+
) {
6972
if (hooks.request_refresh_jwt) {
7073
const body = await res.text();
7174
if (body.includes(JWT_EXPIRED_MESSAGE)) {

dash/dash-renderer/src/actions/callbacks.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ export function executeCallback(
536536
lastError = res;
537537
if (
538538
retry <= MAX_AUTH_RETRIES &&
539-
res.status === STATUS.UNAUTHORIZED
539+
(res.status === STATUS.UNAUTHORIZED ||
540+
res.status === STATUS.BAD_REQUEST)
540541
) {
541542
const body = await res.text();
542543

dash/dash-renderer/src/constants/constants.js

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ export const JWT_EXPIRED_MESSAGE = 'JWT Expired';
55
export const STATUS = {
66
OK: 200,
77
PREVENT_UPDATE: 204,
8+
// We accept both 400 and 401 for JWT token expiry responses.
9+
// Some servers use code 400 for expired tokens, because
10+
// they reserve 401 for cases that require user action
11+
BAD_REQUEST: 400,
812
UNAUTHORIZED: 401,
913
CLIENTSIDE_ERROR: 'CLIENTSIDE_ERROR',
1014
NO_RESPONSE: 'NO_RESPONSE'

dash/development/base_component.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def _set_random_id(self):
196196
"""
197197
)
198198

199-
v = str(uuid.UUID(int=rd.randint(0, 2 ** 128)))
199+
v = str(uuid.UUID(int=rd.randint(0, 2**128)))
200200
setattr(self, "id", v)
201201
return v
202202

tests/integration/renderer/test_request_hooks.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import functools
33
import flask
4+
import pytest
45

56
from dash import Dash, Output, Input, html, dcc
67
from werkzeug.exceptions import HTTPException
@@ -195,7 +196,8 @@ def update_output(value):
195196
assert dash_duo.get_logs() == []
196197

197198

198-
def test_rdrh003_refresh_jwt(dash_duo):
199+
@pytest.mark.parametrize("expiry_code", [401, 400])
200+
def test_rdrh003_refresh_jwt(expiry_code, dash_duo):
199201

200202
app = Dash(__name__)
201203

@@ -260,7 +262,7 @@ def wrap(*args, **kwargs):
260262
):
261263
# Read the data to prevent bug with base http server.
262264
flask.request.get_json(silent=True)
263-
flask.abort(401, description="JWT Expired " + str(token))
265+
flask.abort(expiry_code, description="JWT Expired " + str(token))
264266
except HTTPException as e:
265267
return e
266268
return func(*args, **kwargs)

0 commit comments

Comments
 (0)