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

13 statements  

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

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

2 

3from dataclasses import dataclass 

4from typing import Self 

5 

6import bcrypt 

7 

8 

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

10class Password: 

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

12 

13 hashed_password: bytes 

14 

15 @classmethod 

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

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

18 return cls(bcrypt.hashpw(password.encode(), bcrypt.gensalt())) 

19 

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

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

22 return bcrypt.checkpw(password.encode(), self.hashed_password) 

23 

24 def __str__(self): 

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

26 return self.hashed_password.decode()