From 1527c5e6d70fcd4e6423273bd1a94f6662fd9b4d Mon Sep 17 00:00:00 2001 From: Mirek Simek Date: Fri, 17 May 2024 10:26:46 +0200 Subject: [PATCH] Using utcnow() for current datetime --- invenio_oauthclient/handlers/token.py | 6 +++--- invenio_oauthclient/models.py | 7 +++---- tests/test_refresh.py | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/invenio_oauthclient/handlers/token.py b/invenio_oauthclient/handlers/token.py index 2fa6dd9..e020fac 100644 --- a/invenio_oauthclient/handlers/token.py +++ b/invenio_oauthclient/handlers/token.py @@ -7,7 +7,7 @@ # under the terms of the MIT License; see LICENSE file for more details. """Funcs to manage tokens.""" -import datetime +from datetime import datetime, timedelta from functools import partial from flask import current_app, session @@ -124,7 +124,7 @@ def make_expiration_time(expires_in): """ if expires_in is None: return None - return datetime.datetime.now() + datetime.timedelta(seconds=expires_in) + return datetime.utcnow() + timedelta(seconds=expires_in) def token_setter( @@ -216,7 +216,7 @@ def token_getter(remote, token=""): return ret[0], ret[1], None, None if ret[3] is not None: # refresh token and expiration time - return ret[0], ret[1], ret[2], datetime.datetime.fromisoformat(ret[3]) + return ret[0], ret[1], ret[2], datetime.fromisoformat(ret[3]) return ret diff --git a/invenio_oauthclient/models.py b/invenio_oauthclient/models.py index 1b2f1b0..e1f6b10 100644 --- a/invenio_oauthclient/models.py +++ b/invenio_oauthclient/models.py @@ -8,7 +8,7 @@ """Models for storing access tokens and links between users and remote apps.""" -import datetime +from datetime import datetime, timedelta from flask import current_app @@ -147,9 +147,8 @@ def is_expired(self): return False leeway = current_app.config.get("OAUTHCLIENT_TOKEN_EXPIRES_LEEWAY", 10) - expiration_with_leeway = self.expires_at - datetime.timedelta(seconds=leeway) - - return expiration_with_leeway < datetime.datetime.now() + expiration_with_leeway = self.expires_at - timedelta(seconds=leeway) + return expiration_with_leeway < datetime.utcnow() def __repr__(self): """String representation for model.""" diff --git a/tests/test_refresh.py b/tests/test_refresh.py index fae8ba3..ae76b50 100644 --- a/tests/test_refresh.py +++ b/tests/test_refresh.py @@ -7,8 +7,8 @@ # under the terms of the MIT License; see LICENSE file for more details. """Test handlers.""" -import datetime import json +from datetime import datetime from helpers import mock_remote_http_request @@ -27,7 +27,7 @@ def test_refresh(models_fixture, app): "mytoken", "mysecret", refresh_token="myrefreshtoken", - expires_at=datetime.datetime.now(), + expires_at=datetime.utcnow(), ) assert rt.is_expired is True