Coverage for kwai/modules/identity/tokens/refresh_token.py: 100%

37 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-05 17:55 +0000

1"""Module for a refresh token entity.""" 

2from datetime import datetime 

3 

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.access_token import AccessTokenEntity 

8from kwai.modules.identity.tokens.token_identifier import TokenIdentifier 

9 

10RefreshTokenIdentifier = IntIdentifier 

11 

12 

13class RefreshTokenEntity(Entity[RefreshTokenIdentifier]): 

14 """A refresh token entity.""" 

15 

16 def __init__( 

17 self, 

18 *, 

19 id_: RefreshTokenIdentifier | None = None, 

20 access_token: AccessTokenEntity, 

21 identifier: TokenIdentifier | None = None, 

22 expiration: datetime | None, 

23 revoked: bool = False, 

24 traceable_time: TraceableTime | None = None, 

25 ): 

26 super().__init__(id_ or RefreshTokenIdentifier()) 

27 self._access_token = access_token 

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() 

32 

33 @property 

34 def access_token(self) -> AccessTokenEntity: 

35 """Return the associated access token.""" 

36 return self._access_token 

37 

38 @property 

39 def expiration(self) -> datetime: 

40 """Return the expiration timestamp of the refresh token.""" 

41 return self._expiration 

42 

43 @property 

44 def expired(self) -> bool: 

45 """Return True when the token is expired.""" 

46 return self._expiration < datetime.utcnow() 

47 

48 @property 

49 def identifier(self) -> TokenIdentifier: 

50 """Return the identifier of the refresh token.""" 

51 return self._identifier 

52 

53 def revoke(self) -> None: 

54 """Revoke the refresh token.""" 

55 self._revoked = True 

56 self._access_token.revoke() 

57 self._traceable_time = self._traceable_time.mark_for_update() 

58 

59 @property 

60 def revoked(self) -> bool: 

61 """Return True when the refresh token is revoked.""" 

62 return self._revoked 

63 

64 @property 

65 def traceable_time(self) -> TraceableTime: 

66 """Return the creation/modification timestamps.""" 

67 return self._traceable_time