Skip to content

Commit

Permalink
Using utcnow() for current datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
mesemus committed May 17, 2024
1 parent e8be1a8 commit 1527c5e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
6 changes: 3 additions & 3 deletions invenio_oauthclient/handlers/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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


Expand Down
7 changes: 3 additions & 4 deletions invenio_oauthclient/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_refresh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down

0 comments on commit 1527c5e

Please sign in to comment.