wauth package¶
Subpackages¶
Submodules¶
Module contents¶
WAuth - Local encrypted secret management library.
Provides a simple interface to store and retrieve secrets encrypted with Fernet (AES-256), backed by a SQLite database via wsqlite.
Example
>>> from wauth import WAuth
>>> auth = WAuth()
>>> auth.set("API_KEY", "secret-value")
>>> auth.get("API_KEY")
'secret-value'
- class wauth.WAuth(db_path='~/.wisrovi/wauth.db', custom_key=None, config_path=None, verbose=False)[source]¶
Bases:
objectMain interface for managing encrypted secrets.
Provides methods for storing, retrieving, deleting, rotating, backing up, and restoring encrypted secrets.
- Parameters:
db_path (str) – Path to the SQLite database file. Defaults to
~/.wisrovi/wauth.db.custom_key (str | None) – Optional custom encryption key string.
config_path (str | None) – Optional path to a TOML configuration file.
verbose (bool) – When
True, emit debug/info/warning logs via loguru. Defaults toFalse.
Example
>>> auth = WAuth() >>> auth.set("MY_KEY", "my-secret") >>> value = auth.get("MY_KEY")
- valid(key, value_to_check)[source]¶
Verify if a stored secret matches the provided value.
This method is more secure than get() when you only need to check a secret’s value, as the secret never leaves the library.
- Parameters:
- Returns:
True if the stored secret matches value_to_check.
- Return type:
Example
>>> auth = WAuth() >>> auth.set("API_KEY", "secret123") >>> auth.valid("API_KEY", "secret123") True >>> auth.valid("API_KEY", "wrong") False
- delete(key)[source]¶
Delete a secret from the vault.
- Parameters:
key (str) – Unique identifier for the secret to remove.
- Raises:
WAuthError – If the key does not exist.
- rotate_key(new_custom_key)[source]¶
Rotate the encryption key and re-encrypt all existing secrets.
Decrypts every secret with the current key, then re-encrypts and stores them with the new key. The internal engine is swapped only if all migrations succeed.
- backup(output_path)[source]¶
Export all secrets to an encrypted backup file.
Creates a JSON file containing all secret keys and their encrypted values (still encrypted — the backup itself is not decrypted).
- Parameters:
output_path (str) – Destination path for the backup file.
- Returns:
The absolute path of the created backup file.
- Raises:
BackupError – If the export fails.
- Return type:
- restore(backup_path)[source]¶
Restore secrets from an encrypted backup file.
Reads a backup file created by
backup()and re-stores all encrypted secrets into the current vault.- Parameters:
backup_path (str) – Path to the backup file.
- Returns:
The number of secrets restored.
- Raises:
RestoreError – If the file cannot be read or parsed.
- Return type:
- async async_set(key, value, ttl=None)[source]¶
Async variant of
set().Runs the synchronous
setin a thread pool to avoid blocking the event loop during I/O-bound database writes.
- async async_delete(key)[source]¶
Async variant of
delete().- Parameters:
key (str) – Unique identifier for the secret to remove.
- class wauth.CryptoEngine(custom_key=None)[source]¶
Bases:
objectHandles encryption and decryption using Fernet (AES-128-CBC).
The encryption key is derived from a machine-specific identifier, meaning data encrypted on one machine can only be decrypted on that same machine.
- Parameters:
custom_key (str | None) – Optional custom encryption key string. If provided, it is hashed with SHA-256 to produce a deterministic 32-byte key. If
None, the key is derived from the machine identifier.
- decrypt(token)[source]¶
Decrypt a Fernet token back to raw bytes.
- Parameters:
token (str) – Base64-encoded Fernet token.
- Returns:
Decrypted plaintext as bytes.
- Raises:
DecryptionError – If the token is invalid, tampered, or encrypted with a different key.
- Return type:
- class wauth.LocalDriver(custom_key=None)[source]¶
Bases:
objectDriver that stores secrets locally using Fernet encryption.
Combines the cryptographic engine with the SQLite-backed vault for persistent, machine-locked secret storage.
- Parameters:
custom_key (str | None) – Optional custom encryption key string.
- delete_secret(key)[source]¶
Delete a secret from the vault.
- Parameters:
key (str) – Unique identifier for the secret to remove.
- Raises:
KeyNotFoundError – If the key does not exist.
- rotate_key(new_custom_key, keys_to_migrate=None)[source]¶
Rotate the encryption key and re-encrypt existing secrets.
Creates a new CryptoEngine with the provided key, decrypts all existing secrets with the current engine, and re-encrypts them with the new engine.
- Parameters:
- Returns:
A dictionary mapping each key to a boolean indicating success (
True) or failure (False).- Return type:
- class wauth.SecretModel(*, key, value, type='text', created_at=0.0, updated_at=0.0, ttl=None)[source]¶
Bases:
BaseModelPydantic model representing a stored secret.
- Variables:
key (str) – Primary key — unique name of the secret.
value (str) – Encrypted ciphertext of the secret.
type (str) – Secret type, either
"text"or"file".created_at (float) – Unix timestamp when the secret was first stored.
updated_at (float) – Unix timestamp of the last modification.
ttl (float | None) – Optional time-to-live in seconds.
Nonemeans no expiration.
- model_config = {}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class wauth.Vault(db_path='~/.wisrovi/wauth.db')[source]¶
Bases:
objectEncrypted secret storage backed by a SQLite database.
- Parameters:
db_path (str) – Path to the SQLite database file. Defaults to
~/.wisrovi/wauth.db.
- db: WSQLite¶
- save(key, encrypted_value, val_type='text', ttl=None)[source]¶
Save or update an encrypted secret in the vault.
Uses
INSERT OR REPLACEsemantics to upsert the secret by key. Timestamps are automatically managed.
- delete(key)[source]¶
Delete a secret from the vault.
- Parameters:
key (str) – Unique identifier for the secret to remove.
- Raises:
KeyNotFoundError – If the key does not exist.
VaultError – If the delete operation fails.
- wauth.delete(key)[source]¶
Delete a secret (functional API).
- Parameters:
key (str) – Unique identifier for the secret to remove.
- Raises:
WAuthError – If the key does not exist.
- exception wauth.WAuthError[source]¶
Bases:
ExceptionBase exception for all WAuth-related errors.
All custom exceptions inherit from this class, allowing callers to catch any WAuth-specific error with a single
exceptclause.Example
>>> try: ... auth.get("NONEXISTENT") ... except WAuthError as exc: ... print(f"WAuth error: {exc}")