Skip to content

Commit

Permalink
Merge pull request #25 from claroty/fix_timezone_change_issue
Browse files Browse the repository at this point in the history
unify all time handlings to use utc naive
  • Loading branch information
kobimic authored Jan 31, 2022
2 parents 027ed6f + 040324e commit e7ac02c
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 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.now().timestamp() >= self._jwt_exp
return datetime.utcnow().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.now() + timedelta(seconds=KEY_EXPIRY)
expires_at = datetime.utcnow() + timedelta(seconds=KEY_EXPIRY)
key_hash = sha512(key.encode()).hexdigest()

# If key already exists, update expiry date.
Expand Down
4 changes: 2 additions & 2 deletions jwthenticator/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class KeyInfo(Base):
__tablename__ = "keys"
id = Column(Integer, primary_key=True, autoincrement=True)
created = Column(DateTime, default=datetime.now)
created = Column(DateTime, default=datetime.utcnow())
expires_at = Column(DateTime)
key_hash = Column(String(256), unique=True)
identifier = Column(UUIDType(binary=False), nullable=False)
Expand All @@ -30,7 +30,7 @@ class KeyInfo(Base):
class RefreshTokenInfo(Base):
__tablename__ = "refresh_tokens"
id = Column(Integer, primary_key=True, autoincrement=True)
created = Column(DateTime, default=datetime.now)
created = Column(DateTime, default=datetime.utcnow())
expires_at = Column(DateTime)
token = Column(String(512))
key_id = Column(Integer, ForeignKey("keys.id"))
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.now()
return self.expires_at > datetime.utcnow()


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

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


# 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.now().timestamp()
return self.exp > datetime.utcnow().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.now() + timedelta(seconds=seconds)
return datetime.utcnow() + timedelta(seconds=seconds)


def backup_environment(func): # type: ignore
Expand Down
10 changes: 5 additions & 5 deletions jwthenticator/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ 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.now()
utc_now = datetime.utcnow()
payload = JWTPayloadData(
token_id=uuid4(),
identifier=identifier,
iat=int(now.timestamp()),
exp=int((now + timedelta(seconds=self.jwt_lease_time)).timestamp()),
iat=int(utc_now.timestamp()),
exp=int((utc_now + timedelta(seconds=self.jwt_lease_time)).timestamp()),
aud=self.jwt_audience
)
encoded_payload = self.jwt_payload_data_schema.dump(payload)
Expand All @@ -82,8 +82,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.now() + timedelta(seconds=REFRESH_TOKEN_EXPIRY)
if expires_at <= datetime.now():
expires_at = expires_at = datetime.utcnow() + timedelta(seconds=REFRESH_TOKEN_EXPIRY)
if expires_at <= datetime.utcnow():
raise Exception("Refresh token can't be created in the past")

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

0 comments on commit e7ac02c

Please sign in to comment.