Skip to content

Commit a5a425f

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 a5a425f

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-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: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
from warnings import warn
3+
from six import raise_from
34

45
# In Python 2.7 collections.abc is a part of the collections module.
56
try:
@@ -188,21 +189,29 @@ def access_expires(self):
188189
delta = current_app.config['JWT_ACCESS_TOKEN_EXPIRES']
189190
if type(delta) is int:
190191
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)
192+
if delta is not False:
193+
try:
194+
delta + datetime.datetime.now()
195+
except TypeError as e:
196+
err = (
197+
"must be able to add JWT_ACCESS_TOKEN_EXPIRES to datetime.datetime"
198+
)
199+
raise_from(RuntimeError(err), e)
195200
return delta
196201

197202
@property
198203
def refresh_expires(self):
199204
delta = current_app.config['JWT_REFRESH_TOKEN_EXPIRES']
200205
if type(delta) is int:
201206
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)
207+
if delta is not False:
208+
try:
209+
delta + datetime.datetime.now()
210+
except TypeError as e:
211+
err = (
212+
"must be able to add JWT_REFRESH_TOKEN_EXPIRES to datetime.datetime"
213+
)
214+
raise_from(RuntimeError(err), e)
206215
return delta
207216

208217
@property

0 commit comments

Comments
 (0)