Principle:ALwrity ALwrity OAuth Token Management
| Knowledge Sources | |
|---|---|
| Domains | Security, OAuth, Integration |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Mechanism that securely stores, encrypts, and migrates OAuth tokens for multiple providers in a multi-tenant system, ensuring tokens are encrypted at rest with a per-provider key and legacy plaintext tokens are migrated transparently.
Description
OAuth token management is the pattern of persisting the access and refresh tokens obtained from an OAuth flow so that subsequent API calls can use them without re-authorising. In a multi-tenant system, each user's tokens must be isolated (per-user database) and encrypted at rest (Fernet symmetric encryption). The management layer solves three problems: (1) secure storage — tokens are encrypted with a Fernet key resolved per provider, failing fast if the key is missing; (2) transparent decryption — tokens are decrypted on demand for API calls; (3) legacy migration — when a deployment adds encryption to a system that previously stored plaintext, a one-time migration re-encrypts existing rows without downtime, gated by a per-user-per-process set so it runs exactly once.
The fail-fast principle is critical: a missing encryption key is a configuration error that must surface at service construction, not lazily on the first token operation where the failure mode is a confusing Fernet is None.
Usage
Use this principle when:
- A platform integrates with multiple OAuth providers and needs a shared encryption pattern.
- Tokens must be encrypted at rest with a per-provider key and a shared fallback.
- Legacy plaintext tokens must be migrated to encrypted blobs without downtime.
- A missing encryption key must fail fast at construction, not at first use.
Theoretical Basis
Token management is a encrypt-store-decrypt cycle with a one-time migration:
# Abstract OAuth token management (NOT a real implementation)
def store_token(user_id, provider, access_token, refresh_token):
enc_access = fernet.encrypt(access_token)
enc_refresh = fernet.encrypt(refresh_token)
db.insert(user_id, provider, enc_access, enc_refresh)
def use_token(user_id, provider):
enc = db.select(user_id, provider)
return fernet.decrypt(enc) # plaintext for API call
def migrate(conn, user_id):
if user_id in migration_done: return
for row in conn.select_plaintext(user_id):
conn.update(encrypt(row))
migration_done.add(user_id)
The Fernet key is resolved by precedence: provider-specific env var → shared fallback → raise.
Practical Guide
- Fail fast.' A missing encryption key must raise at construction, not at first token operation.
- Per-provider keys.' Support provider-specific env vars with a shared fallback so each provider can have an independent key.
- One-time migration.' Gate migration by a per-user-per-process set so it runs exactly once and is idempotent.
- Per-tenant isolation.' Store tokens in the per-user database so one user's tokens are never visible to another.
Related Pages
Implemented By
- Implementation:ALwrity_ALwrity_OAuthProviderBase
- Implementation:ALwrity_ALwrity_WordPressPublisher_resolve_content_manager
- Implementation:ALwrity_ALwrity_OAuthTokenMonitoringService