Skip to content

Commit

Permalink
Merge pull request #22 from tomer322/fixture/timezone
Browse files Browse the repository at this point in the history
Fixed timezone offset when verifying jwt expiration
  • Loading branch information
tomer322 authored Sep 22, 2021
2 parents c6b77c3 + 826e6d3 commit 9d77054
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion jwthenticator/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def jwt(self, value: str) -> None:
def is_jwt_expired(self) -> bool:
if self._jwt_exp is None:
return True
return datetime.utcnow().timestamp() >= self._jwt_exp
return datetime.now().timestamp() >= self._jwt_exp

@property
def refresh_token(self) -> Optional[str]:
Expand Down
2 changes: 1 addition & 1 deletion jwthenticator/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def create_key(self, key: str, identifier: UUID, expires_at: Optional[date
:return: Returns True if successfull, raises exception otherwise.
"""
if expires_at is None:
expires_at = datetime.utcnow() + timedelta(seconds=KEY_EXPIRY)
expires_at = datetime.now() + timedelta(seconds=KEY_EXPIRY)
key_hash = sha512(key.encode()).hexdigest()

# If key already exists, update expiry date.
Expand Down
6 changes: 3 additions & 3 deletions jwthenticator/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class KeyData:
key: Optional[str] = field(default=None, repr=False, metadata=dict(load_only=True))

async def is_valid(self) -> bool:
return self.expires_at > datetime.utcnow()
return self.expires_at > datetime.now()


@dataclass
Expand All @@ -53,7 +53,7 @@ class RefreshTokenData:
key_id: int

async def is_valid(self) -> bool:
return self.expires_at > datetime.utcnow()
return self.expires_at > datetime.now()


# Skipping None values on dump since 'aud' is optional and can't be None/empty
Expand All @@ -68,7 +68,7 @@ class JWTPayloadData:
aud: Optional[List[str]] = None # JWT Audience

async def is_valid(self) -> bool:
return self.exp > datetime.utcnow().timestamp()
return self.exp > datetime.now().timestamp()


# Request dataclasses
Expand Down
2 changes: 1 addition & 1 deletion jwthenticator/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async def hash_key(key: str) -> str:


async def future_datetime(seconds: int = 0) -> datetime:
return datetime.utcnow() + timedelta(seconds=seconds)
return datetime.now() + timedelta(seconds=seconds)


def backup_environment(func): # type: ignore
Expand Down
8 changes: 3 additions & 5 deletions jwthenticator/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def create_access_token(self, identifier: UUID) -> str:
"""
if self.private_key is None:
raise Exception("Private key required for JWT token creation")
now = datetime.utcnow()
now = datetime.now()
payload = JWTPayloadData(
token_id=uuid4(),
identifier=identifier,
Expand All @@ -68,8 +68,6 @@ async def load_access_token(self, token_string: str) -> JWTPayloadData:
"""
Load + parse an existing JWT token.
Raises exception if the token is incorrectly signed.
Exp verification is disabled since it checks againt datetime.now(), and we want to
ignore the machine's timezone by using `datetime.utcnow()` everywhere.
"""
if not token_string:
raise MissingJWTError
Expand All @@ -85,8 +83,8 @@ async def create_refresh_token(self, key_id: int, expires_at: Optional[datetime]
:return: The refresh token created.
"""
if expires_at is None:
expires_at = expires_at = datetime.utcnow() + timedelta(seconds=REFRESH_TOKEN_EXPIRY)
if expires_at <= datetime.utcnow():
expires_at = expires_at = datetime.now() + timedelta(seconds=REFRESH_TOKEN_EXPIRY)
if expires_at <= datetime.now():
raise Exception("Refresh token can't be created in the past")

refresh_token_str = sha512(uuid4().bytes).hexdigest()
Expand Down

0 comments on commit 9d77054

Please sign in to comment.