crypto.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from abc import ABC, abstractmethod
  2. from typing import Optional
  3. from .base import Provider, ProviderConfig
  4. class CryptoConfig(ProviderConfig):
  5. provider: Optional[str] = None
  6. @property
  7. def supported_providers(self) -> list[str]:
  8. return ["bcrypt"] # Add other crypto providers as needed
  9. def validate_config(self) -> None:
  10. if self.provider not in self.supported_providers:
  11. raise ValueError(f"Unsupported crypto provider: {self.provider}")
  12. class CryptoProvider(Provider, ABC):
  13. def __init__(self, config: CryptoConfig):
  14. if not isinstance(config, CryptoConfig):
  15. raise ValueError(
  16. "CryptoProvider must be initialized with a CryptoConfig"
  17. )
  18. super().__init__(config)
  19. @abstractmethod
  20. def get_password_hash(self, password: str) -> str:
  21. pass
  22. @abstractmethod
  23. def verify_password(
  24. self, plain_password: str, hashed_password: str
  25. ) -> bool:
  26. pass
  27. @abstractmethod
  28. def generate_verification_code(self, length: int = 32) -> str:
  29. pass