Environment:ALwrity ALwrity Fernet Requirements
| Knowledge Sources | |
|---|---|
| Domains | Security, Encryption, OAuth |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Python cryptography environment using Fernet symmetric encryption for at-rest OAuth token encryption across Bing, Wix, WordPress, YouTube, and LinkedIn providers.
Description
This environment layer provides the Fernet symmetric encryption stack for OAuth token security. It uses the cryptography package's Fernet implementation to encrypt OAuth access and refresh tokens before storing them in per-user SQLite databases. Each provider can use a per-provider encryption key (`BING_TOKEN_ENCRYPTION_KEY`, `WIX_TOKEN_ENCRYPTION_KEY`, `WORDPRESS_TOKEN_ENCRYPTION_KEY`, `YOUTUBE_TOKEN_ENCRYPTION_KEY`, `LINKEDIN_TOKEN_ENCRYPTION_KEY`) with a shared fallback to `OAUTH_TOKEN_ENCRYPTION_KEY`.
The `OAuthProviderBase` class centralizes the encryption, decryption, and one-time plaintext-to-encrypted migration logic. The fail-fast design means a missing or invalid key raises `ValueError` at service construction — not lazily on the first token operation.
Usage
Use this environment for:
- OAuth token encryption at rest — all OAuth providers (Bing, Wix, WordPress, YouTube, LinkedIn)
- Plaintext token migration — one-time re-encryption of legacy plaintext rows during rollout
- Token monitoring — decrypting tokens for status checks and refresh flows
System Requirements
| Category | Requirement | Notes |
|---|---|---|
| OS | Any (Python 3.11 compatible) | No OS-specific requirements |
| Hardware | Minimal | Encryption is CPU-light |
| Disk | N/A | Keys are in environment, not disk |
Dependencies
Python Packages
- `cryptography` >= 41.0.0
Credentials
The following environment variables must be set (generate keys with `python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"`):
Per-provider keys (override shared key):
- `BING_TOKEN_ENCRYPTION_KEY`: Fernet key for Bing OAuth tokens
- `WIX_TOKEN_ENCRYPTION_KEY`: Fernet key for Wix OAuth tokens
- `WORDPRESS_TOKEN_ENCRYPTION_KEY`: Fernet key for WordPress OAuth tokens
- `YOUTUBE_TOKEN_ENCRYPTION_KEY`: Fernet key for YouTube OAuth tokens
- `LINKEDIN_TOKEN_ENCRYPTION_KEY`: Fernet key for LinkedIn OAuth tokens
Shared fallback:
- `OAUTH_TOKEN_ENCRYPTION_KEY`: Shared Fernet key used when per-provider key is not set
Quick Install
# Install cryptography package
pip install cryptography>=41.0.0
# Generate a Fernet key
python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"
# Copy the output to OAUTH_TOKEN_ENCRYPTION_KEY in .env
Code Evidence
Fail-fast key validation from `backend/services/integrations/oauth_provider_base.py:99-117`:
if not self.token_encryption_key:
env_vars = " / ".join(sorted(PROVIDER_ENV_VARS.values()))
raise ValueError(
f"{type(self).__name__} token encryption key is not configured. "
f"Set the appropriate env var ({env_vars}) or the shared "
f"OAUTH_TOKEN_ENCRYPTION_KEY fallback. "
f"Generate a key: python -c \"from cryptography.fernet import Fernet; "
f"print(Fernet.generate_key().decode())\""
)
try:
return Fernet(self.token_encryption_key.encode("utf-8"))
except Exception as e:
raise ValueError(
f"{type(self).__name__} token encryption key is invalid: {e}"
)
Encrypted blob detection for migration from `backend/services/integrations/oauth_provider_base.py:133-134`:
def _is_likely_encrypted_blob(self, value: Optional[str]) -> bool:
return bool(value and value.startswith("gAAAAA"))
Key resolution with per-provider + fallback from `backend/services/integrations/oauth_provider_base.py:182-195`:
def resolve_encryption_key(provider_name: str) -> Optional[str]:
provider_var = PROVIDER_ENV_VARS.get(provider_name.lower())
if provider_var:
specific = os.getenv(provider_var)
if specific:
return specific
return os.getenv("OAUTH_TOKEN_ENCRYPTION_KEY")
Common Errors
| Error Message | Cause | Solution |
|---|---|---|
| `OAuthProviderBase token encryption key is not configured` | No Fernet key in env vars | Generate and set `OAUTH_TOKEN_ENCRYPTION_KEY` in `.env` |
| `token encryption key is invalid: ...` | Key is not a valid Fernet key | Regenerate with `Fernet.generate_key()` |
| `cryptography.fernet.InvalidToken` during decrypt | Token was encrypted with a different key | Ensure the same key is used across restarts; check for key rotation without re-encryption |
Compatibility Notes
- Key rotation: Changing the encryption key invalidates all previously encrypted tokens. A migration script must re-encrypt existing tokens before key rotation.
- Per-provider isolation: Each provider can use a separate key for isolation, with `OAUTH_TOKEN_ENCRYPTION_KEY` as the shared fallback.
- Migration gate: The `_migration_done` set is per-user-per-process; the migration runs at most once per process restart per user.