Coverage for kwai/core/domain/value_objects/password.py: 100%

13 statements  

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

1"""Module that defines a value object for a password.""" 

2from dataclasses import dataclass 

3 

4from passlib.context import CryptContext 

5 

6pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") 

7 

8 

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

10class Password: 

11 """A value object for a password.""" 

12 

13 hashed_password: str 

14 

15 @classmethod 

16 def create_from_string(cls, password: str) -> "Password": 

17 """Create a password from a string.""" 

18 return Password(hashed_password=pwd_context.hash(password)) 

19 

20 def verify(self, password: str) -> bool: 

21 """Verify this password against the given password.""" 

22 return pwd_context.verify(password, self.hashed_password) 

23 

24 def __str__(self): 

25 """Return a string representation (hashed password).""" 

26 return self.hashed_password