Principle:24kchengYe MemoMind Multi Tenant Authentication
| Knowledge Sources | |
|---|---|
| Domains | Security, Distributed_Systems |
| Last Updated | 2026-10-28 00:00 GMT |
Overview
Architecture pattern that isolates each tenant's data in a separate PostgreSQL schema while authenticating requests via pluggable extensions (API key, Supabase JWT, or revocable multi-key), enforced at the application layer.
Description
Multi-tenancy ensures that one tenant's data is never visible to another. MemoMind implements this via schema isolation: each tenant gets a separate PostgreSQL schema, and all SQL is schema-qualified through `fq_table()`.
Authentication is handled by pluggable tenant extensions:
- DefaultTenantExtension: no auth, single schema (local development)
- ApiKeyTenantExtension: validates a single API key against an env var
- SupabaseTenantExtension: validates Supabase JWTs, derives schema from user ID (one schema per user)
- MultiApiKeyTenantExtension: accepts an owner key plus comma-separated revocable client keys (production deployment)
All extensions implement the same `authenticate(context) -> TenantContext` interface, returning the schema name for the authenticated tenant. The schema name flows through to all database queries via `fq_table()`.
Usage
Use this principle when a single deployment serves multiple users or organizations that require data isolation. The extension system lets you choose the authentication mechanism without changing application code.
Theoretical Basis
# Abstract tenant auth (not real implementation)
class TenantExtension:
async def authenticate(self, context: RequestContext) -> TenantContext:
"""Validate credentials, return schema for this tenant."""
...
# All SQL uses schema-qualified table names:
def fq_table(table_name, schema=tenant_context.schema_name):
return f'"{schema}".{table_name}'
JWT verification (Supabase): uses JWKS for asymmetric keys (RS256), falls back to the `/auth/v1/user` endpoint for legacy HS256 projects. Schema name: `user_{uuid_with_underscores}`.
Revocable keys (MultiApiKey): uses `hmac.compare_digest` for constant-time comparison to prevent timing attacks. Client keys can be removed from the env var to revoke access without touching the owner key.