Skip to content

Commit 0095557

Browse files
evangilovimalloc
authored andcommitted
adds ability to use int (seconds) for expires options
adds the ability to use int values (seconds) for JWT_ACCESS_TOKEN_EXPIRES and JWT_REFRESH_TOKEN_EXPIRES configs. Refs #226
1 parent af99063 commit 0095557

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

docs/options.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ 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``, and defaults to 15 minutes.
23+
takes a ``datetime.timedelta`` or an ``int`` (seconds), and defaults to 15 minutes.
2424
Can be set to ``False`` to disable expiration.
2525
``JWT_REFRESH_TOKEN_EXPIRES`` How long a refresh token should live before it expires. This
26-
takes a ``datetime.timedelta``, and defaults to 30 days.
26+
takes a ``datetime.timedelta`` or ``int`` (seconds), and defaults to 30 days.
2727
Can be set to ``False`` to disable expiration.
2828
``JWT_ALGORITHM`` Which algorithm to sign the JWT with. `See here <https://pyjwt.readthedocs.io/en/latest/algorithms.html>`_
2929
for the options. Defaults to ``'HS256'``.

flask_jwt_extended/config.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,22 @@ def refresh_csrf_header_name(self):
186186
@property
187187
def access_expires(self):
188188
delta = current_app.config['JWT_ACCESS_TOKEN_EXPIRES']
189+
if type(delta) is int:
190+
delta = datetime.timedelta(seconds=delta)
189191
if not isinstance(delta, datetime.timedelta) and delta is not False:
190-
err = 'JWT_ACCESS_TOKEN_EXPIRES must be a datetime.timedelta or False'
192+
err = 'JWT_ACCESS_TOKEN_EXPIRES must be a ' \
193+
'datetime.timedelta, int or False'
191194
raise RuntimeError(err)
192195
return delta
193196

194197
@property
195198
def refresh_expires(self):
196199
delta = current_app.config['JWT_REFRESH_TOKEN_EXPIRES']
200+
if type(delta) is int:
201+
delta = datetime.timedelta(seconds=delta)
197202
if not isinstance(delta, datetime.timedelta) and delta is not False:
198-
err = 'JWT_REFRESH_TOKEN_EXPIRES must be a datetime.timedelta or False'
203+
err = 'JWT_REFRESH_TOKEN_EXPIRES must be a ' \
204+
'datetime.timedelta, int or False'
199205
raise RuntimeError(err)
200206
return delta
201207

tests/test_config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,15 @@ def test_tokens_never_expire(app):
180180
assert config.refresh_expires is False
181181

182182

183+
def test_tokens_with_int_values(app):
184+
app.config['JWT_ACCESS_TOKEN_EXPIRES'] = 300
185+
app.config['JWT_REFRESH_TOKEN_EXPIRES'] = 432000
186+
187+
with app.test_request_context():
188+
assert config.access_expires == timedelta(minutes=5)
189+
assert config.refresh_expires == timedelta(days=5)
190+
191+
183192
# noinspection PyStatementEffect
184193
def test_symmetric_secret_key(app):
185194
with app.test_request_context():

0 commit comments

Comments
 (0)