# Security Architecture & Best Practices

## 1. Password Hashing
- Algorithm: **Argon2id** (memory-hard, resistant to GPU/ASIC attacks).
- Implemented via `argon2-cffi`.

## 2. Token & Credential Encryption
- Telegram bot tokens and API secrets are never stored in plaintext.
- Encrypted using **AES-256-GCM** with authenticated 12-byte random nonce and 16-byte authentication tag.
- Encryption key is stored outside the database via environment variables / secret manager.
- Key versioning (`key_version`) is persisted to support automated key rotation.

## 3. JWT & Session Management
- Short-lived Access Tokens (default: 15 minutes).
- Refresh Tokens stored as **SHA-256 hashes** in database.
- **Refresh Token Rotation**: Old refresh tokens are revoked immediately upon use.

## 4. Tenant Isolation
- Every tenant database operation is scoped by `tenant_id`.
- Access between tenants is strictly blocked with `TenantAccessError` (HTTP 403).
- Redis cache keys are tenant-prefixed: `tenant:<tenant_id>:<key>`.

## 5. Secret Redaction
- Centralized structured logging strips sensitive fields (`password`, `token`, `secret`, `authorization`, `api_key`).
