Skip to content

Commit ef2ecbd

Browse files
committed
relax config type check on JWT_*_TOKEN_EXPIRES
Instead of requiring JWT_ACCESS_TOKEN_EXPIRES and JWT_REFRESH_TOKEN_EXPIRES to be of type `datetime.timedelta` or `False`, checks in config.py support any value that can be successfully added to a `datetime.datetime` object. Closes #214.
1 parent 3f37e1e commit ef2ecbd

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

docs/options.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ General Options:
2020
in a sequence or a set to check more then one location, such as:
2121
``('headers', 'cookies')``. Defaults to ``['headers']``
2222
``JWT_ACCESS_TOKEN_EXPIRES`` How long an access token should live before it expires. This
23-
takes a ``datetime.timedelta`` or an ``int`` (seconds), and defaults to 15 minutes.
23+
takes any value that can be safely added to a ``datetime.datetime`` object, including
24+
``datetime.timedelta``, `dateutil.relativedelta <https://dateutil.readthedocs.io/en/stable/relativedelta.html>`_,
25+
or an ``int`` (seconds), and defaults to 15 minutes.
2426
Can be set to ``False`` to disable expiration.
2527
``JWT_REFRESH_TOKEN_EXPIRES`` How long a refresh token should live before it expires. This
26-
takes a ``datetime.timedelta`` or ``int`` (seconds), and defaults to 30 days.
28+
takes any value that can be safely added to a ``datetime.datetime`` object, including
29+
``datetime.timedelta``, `dateutil.relativedelta <https://dateutil.readthedocs.io/en/stable/relativedelta.html>`_,
30+
or an ``int`` (seconds), and defaults to 30 days.
2731
Can be set to ``False`` to disable expiration.
2832
``JWT_ALGORITHM`` Which algorithm to sign the JWT with. `See here <https://pyjwt.readthedocs.io/en/latest/algorithms.html>`_
2933
for the options. Defaults to ``'HS256'``.

flask_jwt_extended/config.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,29 @@ def access_expires(self):
188188
delta = current_app.config['JWT_ACCESS_TOKEN_EXPIRES']
189189
if type(delta) is int:
190190
delta = datetime.timedelta(seconds=delta)
191-
if not isinstance(delta, datetime.timedelta) and delta is not False:
192-
err = 'JWT_ACCESS_TOKEN_EXPIRES must be a ' \
193-
'datetime.timedelta, int or False'
194-
raise RuntimeError(err)
191+
if delta is not False:
192+
try:
193+
delta + datetime.datetime.now()
194+
except TypeError as e:
195+
err = (
196+
"must be able to add JWT_ACCESS_TOKEN_EXPIRES to datetime.datetime"
197+
)
198+
raise RuntimeError(err) from e
195199
return delta
196200

197201
@property
198202
def refresh_expires(self):
199203
delta = current_app.config['JWT_REFRESH_TOKEN_EXPIRES']
200204
if type(delta) is int:
201205
delta = datetime.timedelta(seconds=delta)
202-
if not isinstance(delta, datetime.timedelta) and delta is not False:
203-
err = 'JWT_REFRESH_TOKEN_EXPIRES must be a ' \
204-
'datetime.timedelta, int or False'
205-
raise RuntimeError(err)
206+
if delta is not False:
207+
try:
208+
delta + datetime.datetime.now()
209+
except TypeError as e:
210+
err = (
211+
"must be able to add JWT_REFRESH_TOKEN_EXPIRES to datetime.datetime"
212+
)
213+
raise RuntimeError(err) from e
206214
return delta
207215

208216
@property

0 commit comments

Comments
 (0)