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

16 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module that defines value objects used with token entities.""" 

2 

3import ipaddress 

4 

5from dataclasses import dataclass 

6from ipaddress import IPv4Address, IPv6Address 

7from typing import Self 

8 

9 

10@dataclass(kw_only=True, frozen=True, slots=True) 

11class IpAddress: 

12 """An IP address.""" 

13 

14 ip: IPv4Address | IPv6Address 

15 

16 def __str__(self) -> str: 

17 """Return string representation of an IP address.""" 

18 return str(self.ip) 

19 

20 @classmethod 

21 def create(cls, ip: str) -> Self: 

22 """Create an IpAddress instance from a string. 

23 

24 A ValueError will be raised if the ip address is invalid. 

25 """ 

26 return cls(ip=ipaddress.ip_address(ip)) 

27 

28 

29@dataclass(kw_only=True, frozen=True, slots=True) 

30class OpenId: 

31 """OpenID information. 

32 

33 sub: The subject identifier of the OpenID object. 

34 provider: The provider of the OpenID object. 

35 """ 

36 

37 sub: str = "" 

38 provider: str = ""