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 ApiKeyTenantExtension authenticate

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

Overview

Concrete tool for authenticating requests via a single API key validated against an environment variable, returning the configured PostgreSQL schema for tenant isolation.

Description

The ApiKeyTenantExtension validates that the request's API key matches the configured HINDSIGHT_API_TENANT_API_KEY environment variable. On success, it returns a TenantContext with the configured schema (HINDSIGHT_API_DATABASE_SCHEMA, default 'public'). On failure, it raises AuthenticationError.

Features:

  • Single key validation: the expected API key is loaded once at initialization
  • MCP auth bypass: HINDSIGHT_API_TENANT_MCP_AUTH_DISABLED=true disables auth for MCP endpoints (backwards compatibility)
  • Single-tenant: all authenticated requests use the same schema

The DefaultTenantExtension (no auth) and MultiApiKeyTenantExtension (revocable multi-key) are related implementations sharing the same interface.

Usage

Configure this extension via HINDSIGHT_API_TENANT_EXTENSION when you need simple API key authentication for a single-tenant deployment.

Code Reference

Source Location

  • Repository: MemoMind
  • File: engine/hindsight_api/extensions/builtin/tenant.py
  • Lines: L44-90 (ApiKeyTenantExtension); L8-37 (DefaultTenantExtension)

Signature

class ApiKeyTenantExtension(TenantExtension):
    def __init__(self, config: dict[str, str]):
        ...

    async def authenticate(self, context: RequestContext) -> TenantContext:
        ...

    async def authenticate_mcp(self, context: RequestContext) -> TenantContext:
        ...

    async def list_tenants(self) -> list[Tenant]:
        ...

Import

from hindsight_api.extensions.builtin.tenant import ApiKeyTenantExtension, DefaultTenantExtension

I/O Contract

Inputs

Name Type Required Description
config dict[str, str] __init__ Config with "api_key" (required) and "mcp_auth_disabled" (optional)
context RequestContext authenticate Request context with api_key field

Outputs

Name Type Description
tenant_context TenantContext Schema name for the authenticated tenant

Usage Examples

Configuring API Key Auth

# In environment:
# HINDSIGHT_API_TENANT_EXTENSION=hindsight_api.extensions.builtin.tenant:ApiKeyTenantExtension
# HINDSIGHT_API_TENANT_API_KEY=your-secret-key
# HINDSIGHT_API_DATABASE_SCHEMA=my_schema

# The extension is loaded automatically by load_extension("TENANT", TenantExtension)
# All requests must include the API key in the Authorization header

Related Pages

Implements Principle

Requires Environment

Page Connections

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