Coverage for src/kwai/modules/identity/tokens/access_token.py: 95%
20 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module that defines an access token entity."""
3from dataclasses import dataclass, field, replace
4from typing import ClassVar, Self, Type
6from kwai.core.domain.entity import DataclassEntity
7from kwai.core.domain.value_objects.identifier import IntIdentifier
8from kwai.core.domain.value_objects.timestamp import Timestamp
9from kwai.modules.identity.tokens.token_identifier import TokenIdentifier
10from kwai.modules.identity.users.user_account import UserAccountEntity
13class AccessTokenIdentifier(IntIdentifier):
14 """Identifier for an access token."""
17@dataclass(kw_only=True, eq=False, slots=True, frozen=True)
18class AccessTokenEntity(DataclassEntity):
19 """An access token entity.
21 Attributes:
22 user_account: The user account associated with this token.
23 identifier: The actual token.
24 expiration: The expiration timestamp of the token.
25 revoked: Whether the token has been revoked.
26 """
28 ID: ClassVar[Type] = AccessTokenIdentifier
30 user_account: UserAccountEntity
31 identifier: TokenIdentifier = field(default_factory=TokenIdentifier.generate)
32 expiration: Timestamp = field(default_factory=Timestamp.create_now)
33 revoked: bool = field(default=False)
35 @property
36 def expired(self) -> bool:
37 """Return true when the access token is expired."""
38 return self.expiration.is_past
40 def revoke(self) -> Self:
41 """Revoke the access token.
43 Returns:
44 A revoked access token.
45 """
46 return replace(
47 self, revoked=True, traceable_time=self.traceable_time.mark_for_update()
48 )