Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:24kchengYe MemoMind TenantExtension

From Leeroopedia
Knowledge Sources
Domains Software_Architecture, Authentication, Multi_Tenancy
Last Updated 2026-10-28 00:00 GMT

Overview

Tenant extension base class for multi-tenancy — defines authentication, schema provisioning, and tenant config override hooks.

Description

The TenantExtension ABC is the core multi-tenancy abstraction. It defines `authenticate()` (validates API keys, returns `TenantContext` with schema_name), `get_tenant_config()` (returns tenant-level config overrides), and `provision_tenant()` (creates schema, runs migrations). The returned `schema_name` is used for fully-qualified table names in all database queries, enabling tenant isolation at the database level. Raises `AuthenticationError` on failure.

Usage

Subclass this when implementing a custom tenant authentication method. Built-in implementations include `ApiKeyTenantExtension` and `SupabaseTenantExtension`. Configure via `HINDSIGHT_API_TENANT_EXTENSION`.

Code Reference

Source Location

  • Repository: MemoMind
  • File: engine/hindsight_api/extensions/tenant.py
  • Lines: L1-158

Signature

class TenantExtension(Extension, ABC):
    @abstractmethod
    async def authenticate(self, context: RequestContext) -> TenantContext: ...

class AuthenticationError(Exception):
    def __init__(self, reason: str, headers: dict[str, str] | None = None): ...

@dataclass
class TenantContext:
    schema_name: str

@dataclass
class Tenant:
    schema: str

Import

from hindsight_api.extensions.tenant import TenantExtension, AuthenticationError, TenantContext

I/O Contract

Inputs

Name Type Required Description
context RequestContext Yes Contains api_key and auth data

Outputs

Name Type Description
authenticate() returns TenantContext Contains schema_name for DB isolation

Usage Examples

Implementing a Custom Tenant Extension

from hindsight_api.extensions.tenant import TenantExtension, TenantContext, AuthenticationError

class MyTenantExtension(TenantExtension):
    async def authenticate(self, context):
        api_key = context.api_key
        if not api_key or not self._validate_key(api_key):
            raise AuthenticationError("Invalid API key")
        tenant_id = self._get_tenant_id(api_key)
        return TenantContext(schema_name=f"tenant_{tenant_id}")

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment