Coverage for kwai/modules/identity/tokens/access_token.py: 100%
36 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
1"""Module that defines an access token entity."""
2from datetime import datetime
4from kwai.core.domain.entity import Entity
5from kwai.core.domain.value_objects.identifier import IntIdentifier
6from kwai.core.domain.value_objects.traceable_time import TraceableTime
7from kwai.modules.identity.tokens.token_identifier import TokenIdentifier
8from kwai.modules.identity.users.user_account import UserAccountEntity
10AccessTokenIdentifier = IntIdentifier
13class AccessTokenEntity(Entity[AccessTokenIdentifier]):
14 """An access token entity."""
16 def __init__(
17 self,
18 *,
19 id_: AccessTokenIdentifier | None = None,
20 user_account: UserAccountEntity,
21 identifier: TokenIdentifier | None = None,
22 expiration: datetime | None = None,
23 revoked: bool = False,
24 traceable_time: TraceableTime | None = None,
25 ):
26 super().__init__(id_ or AccessTokenIdentifier())
27 self._user_account = user_account
28 self._identifier = identifier or TokenIdentifier.generate()
29 self._expiration = expiration or datetime.utcnow()
30 self._revoked = revoked
31 self._traceable_time = traceable_time or TraceableTime()
33 @property
34 def expiration(self) -> datetime:
35 """Return the expiration timestamp of the access token."""
36 return self._expiration
38 @property
39 def expired(self) -> bool:
40 """Return true when the access token is expired."""
41 return self._expiration < datetime.utcnow()
43 @property
44 def identifier(self) -> TokenIdentifier:
45 """Return the identifier of the access token."""
46 return self._identifier
48 def revoke(self) -> None:
49 """Revoke the access token."""
50 self._revoked = True
51 self._traceable_time = self._traceable_time.mark_for_update()
53 @property
54 def revoked(self) -> bool:
55 """Return True when the access token is revoked."""
56 return self._revoked
58 @property
59 def traceable_time(self) -> TraceableTime:
60 """Return the creation/modification timestamps."""
61 return self._traceable_time
63 @property
64 def user_account(self) -> UserAccountEntity:
65 """Return the user of this access token."""
66 return self._user_account