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

1"""Module that defines an access token entity.""" 

2 

3from dataclasses import dataclass, field, replace 

4from typing import ClassVar, Self, Type 

5 

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 

11 

12 

13class AccessTokenIdentifier(IntIdentifier): 

14 """Identifier for an access token.""" 

15 

16 

17@dataclass(kw_only=True, eq=False, slots=True, frozen=True) 

18class AccessTokenEntity(DataclassEntity): 

19 """An access token entity. 

20 

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 """ 

27 

28 ID: ClassVar[Type] = AccessTokenIdentifier 

29 

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) 

34 

35 @property 

36 def expired(self) -> bool: 

37 """Return true when the access token is expired.""" 

38 return self.expiration.is_past 

39 

40 def revoke(self) -> Self: 

41 """Revoke the access token. 

42 

43 Returns: 

44 A revoked access token. 

45 """ 

46 return replace( 

47 self, revoked=True, traceable_time=self.traceable_time.mark_for_update() 

48 )