aboutsummaryrefslogtreecommitdiff
path: root/.venv/lib/python3.12/site-packages/core/base/providers/crypto.py
diff options
context:
space:
mode:
authorS. Solomon Darnell2025-03-28 21:52:21 -0500
committerS. Solomon Darnell2025-03-28 21:52:21 -0500
commit4a52a71956a8d46fcb7294ac71734504bb09bcc2 (patch)
treeee3dc5af3b6313e921cd920906356f5d4febc4ed /.venv/lib/python3.12/site-packages/core/base/providers/crypto.py
parentcc961e04ba734dd72309fb548a2f97d67d578813 (diff)
downloadgn-ai-master.tar.gz
two version of R2R are hereHEADmaster
Diffstat (limited to '.venv/lib/python3.12/site-packages/core/base/providers/crypto.py')
-rw-r--r--.venv/lib/python3.12/site-packages/core/base/providers/crypto.py120
1 files changed, 120 insertions, 0 deletions
diff --git a/.venv/lib/python3.12/site-packages/core/base/providers/crypto.py b/.venv/lib/python3.12/site-packages/core/base/providers/crypto.py
new file mode 100644
index 00000000..bdf794b0
--- /dev/null
+++ b/.venv/lib/python3.12/site-packages/core/base/providers/crypto.py
@@ -0,0 +1,120 @@
+from abc import ABC, abstractmethod
+from datetime import datetime
+from typing import Optional, Tuple
+
+from .base import Provider, ProviderConfig
+
+
+class CryptoConfig(ProviderConfig):
+ provider: Optional[str] = None
+
+ @property
+ def supported_providers(self) -> list[str]:
+ return ["bcrypt", "nacl"]
+
+ def validate_config(self) -> None:
+ if self.provider not in self.supported_providers:
+ raise ValueError(f"Unsupported crypto provider: {self.provider}")
+
+
+class CryptoProvider(Provider, ABC):
+ def __init__(self, config: CryptoConfig):
+ if not isinstance(config, CryptoConfig):
+ raise ValueError(
+ "CryptoProvider must be initialized with a CryptoConfig"
+ )
+ super().__init__(config)
+
+ @abstractmethod
+ def get_password_hash(self, password: str) -> str:
+ """Hash a plaintext password using a secure password hashing algorithm
+ (e.g., Argon2i)."""
+ pass
+
+ @abstractmethod
+ def verify_password(
+ self, plain_password: str, hashed_password: str
+ ) -> bool:
+ """Verify that a plaintext password matches the given hashed
+ password."""
+ pass
+
+ @abstractmethod
+ def generate_verification_code(self, length: int = 32) -> str:
+ """Generate a random code for email verification or reset tokens."""
+ pass
+
+ @abstractmethod
+ def generate_signing_keypair(self) -> Tuple[str, str, str]:
+ """Generate a new Ed25519 signing keypair for request signing.
+
+ Returns:
+ A tuple of (key_id, private_key, public_key).
+ - key_id: A unique identifier for this keypair.
+ - private_key: Base64 encoded Ed25519 private key.
+ - public_key: Base64 encoded Ed25519 public key.
+ """
+ pass
+
+ @abstractmethod
+ def sign_request(self, private_key: str, data: str) -> str:
+ """Sign request data with an Ed25519 private key, returning the
+ signature."""
+ pass
+
+ @abstractmethod
+ def verify_request_signature(
+ self, public_key: str, signature: str, data: str
+ ) -> bool:
+ """Verify a request signature using the corresponding Ed25519 public
+ key."""
+ pass
+
+ @abstractmethod
+ def generate_api_key(self) -> Tuple[str, str]:
+ """Generate a new API key for a user.
+
+ Returns:
+ A tuple (key_id, raw_api_key):
+ - key_id: A unique identifier for the API key.
+ - raw_api_key: The plaintext API key to provide to the user.
+ """
+ pass
+
+ @abstractmethod
+ def hash_api_key(self, raw_api_key: str) -> str:
+ """Hash a raw API key for secure storage in the database.
+
+ Use strong parameters suitable for long-term secrets.
+ """
+ pass
+
+ @abstractmethod
+ def verify_api_key(self, raw_api_key: str, hashed_key: str) -> bool:
+ """Verify that a provided API key matches the stored hashed version."""
+ pass
+
+ @abstractmethod
+ def generate_secure_token(self, data: dict, expiry: datetime) -> str:
+ """Generate a secure, signed token (e.g., JWT) embedding claims.
+
+ Args:
+ data: The claims to include in the token.
+ expiry: A datetime at which the token expires.
+
+ Returns:
+ A JWT string signed with a secret key.
+ """
+ pass
+
+ @abstractmethod
+ def verify_secure_token(self, token: str) -> Optional[dict]:
+ """Verify a secure token (e.g., JWT).
+
+ Args:
+ token: The token string to verify.
+
+ Returns:
+ The token payload if valid, otherwise None.
+ """
+ pass