diff --git a/docs/options.rst b/docs/options.rst index bebb71d4..754769cb 100644 --- a/docs/options.rst +++ b/docs/options.rst @@ -20,10 +20,10 @@ General Options: in a sequence or a set to check more then one location, such as: ``('headers', 'cookies')``. Defaults to ``['headers']`` ``JWT_ACCESS_TOKEN_EXPIRES`` How long an access token should live before it expires. This - takes a ``datetime.timedelta``, and defaults to 15 minutes. + takes a ``datetime.timedelta`` or an ``int`` (seconds), and defaults to 15 minutes. Can be set to ``False`` to disable expiration. ``JWT_REFRESH_TOKEN_EXPIRES`` How long a refresh token should live before it expires. This - takes a ``datetime.timedelta``, and defaults to 30 days. + takes a ``datetime.timedelta`` or ``int`` (seconds), and defaults to 30 days. Can be set to ``False`` to disable expiration. ``JWT_ALGORITHM`` Which algorithm to sign the JWT with. `See here `_ for the options. Defaults to ``'HS256'``. diff --git a/flask_jwt_extended/config.py b/flask_jwt_extended/config.py index 2fbdc78f..2b2d36b6 100644 --- a/flask_jwt_extended/config.py +++ b/flask_jwt_extended/config.py @@ -186,16 +186,22 @@ def refresh_csrf_header_name(self): @property def access_expires(self): delta = current_app.config['JWT_ACCESS_TOKEN_EXPIRES'] + if type(delta) is int: + delta = datetime.timedelta(seconds=delta) if not isinstance(delta, datetime.timedelta) and delta is not False: - err = 'JWT_ACCESS_TOKEN_EXPIRES must be a datetime.timedelta or False' + err = 'JWT_ACCESS_TOKEN_EXPIRES must be a ' \ + 'datetime.timedelta, int or False' raise RuntimeError(err) return delta @property def refresh_expires(self): delta = current_app.config['JWT_REFRESH_TOKEN_EXPIRES'] + if type(delta) is int: + delta = datetime.timedelta(seconds=delta) if not isinstance(delta, datetime.timedelta) and delta is not False: - err = 'JWT_REFRESH_TOKEN_EXPIRES must be a datetime.timedelta or False' + err = 'JWT_REFRESH_TOKEN_EXPIRES must be a ' \ + 'datetime.timedelta, int or False' raise RuntimeError(err) return delta diff --git a/tests/test_config.py b/tests/test_config.py index a2b5c04c..f680b8c3 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -180,6 +180,15 @@ def test_tokens_never_expire(app): assert config.refresh_expires is False +def test_tokens_with_int_values(app): + app.config['JWT_ACCESS_TOKEN_EXPIRES'] = 300 + app.config['JWT_REFRESH_TOKEN_EXPIRES'] = 432000 + + with app.test_request_context(): + assert config.access_expires == timedelta(minutes=5) + assert config.refresh_expires == timedelta(days=5) + + # noinspection PyStatementEffect def test_symmetric_secret_key(app): with app.test_request_context():