# stackadnan/django-tenant-apikeys architecture

> Architecture diagram of the GitHub repository stackadnan/django-tenant-apikeys, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/stackadnan/django-tenant-apikeys
- Repository: https://github.com/stackadnan/django-tenant-apikeys

## Overview

django-tenant-apikeys is a Django library for issuing and enforcing tenant-scoped API keys. A project creates a key through the model or Django admin; a client presents it through the DRF or Ninja adapter, which delegates verification and shared policy checks to the core and attaches the tenant. Scope checks and optional per-key rate limits further gate access. Keys can later be rotated or revoked, with lifecycle signals available to projects. The graph reflects README capabilities and shown source references; unsampled implementation details are not assumed.

Main entry points: [`django_tenant_apikeys/models.py`](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/models.py) (Key model), [`django_tenant_apikeys/authentication.py`](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/authentication.py) (DRF authentication), [`django_tenant_apikeys/ninja.py`](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/ninja.py) (Ninja authentication), [`django_tenant_apikeys/core.py`](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/core.py) (Shared auth core)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_key_mgmt["Key management"]
  node_key_model["Key model<br/>[models.py]"]
  node_key_store[("Django database")]
  node_admin_ui["Admin interface<br/>[admin.py]"]
  node_lifecycle_cmds["Lifecycle commands"]
end

subgraph group_request_auth["Request authentication"]
  node_drf_auth["DRF authentication<br/>[authentication.py]"]
  node_ninja_auth["Ninja authentication<br/>[ninja.py]"]
  node_auth_core["Shared auth core<br/>[core.py]"]
  node_ip_resolution["Client IP resolution<br/>[ip.py]"]
end

subgraph group_policy["Access policies"]
  node_scope_checks["Scope permissions<br/>[permissions.py]"]
  node_rate_limit["Rate-limit engine<br/>[ratelimit.py]"]
  node_drf_throttle["DRF throttle<br/>[throttling.py]"]
  node_cache[("Django cache")]
end

subgraph group_operations["Operations and events"]
  node_lifecycle_signals["Lifecycle signals<br/>[signals.py]"]
end

node_developer(("Project developer"))
node_client(("API client"))

node_developer -->|"creates keys"| node_key_model
node_key_model -->|"persists keys"| node_key_store
node_developer -->|"manages keys"| node_admin_ui
node_admin_ui -->|"creates and revokes"| node_key_model
node_developer -->|"runs commands"| node_lifecycle_cmds
node_lifecycle_cmds -->|"rotates or revokes"| node_key_model
node_client -->|"sends API key"| node_drf_auth
node_client -->|"sends API key"| node_ninja_auth
node_drf_auth -->|"resolves client IP"| node_ip_resolution
node_drf_auth -->|"authenticates key"| node_auth_core
node_ninja_auth -->|"resolves client IP"| node_ip_resolution
node_ninja_auth -->|"authenticates key"| node_auth_core
node_auth_core -->|"looks up and verifies"| node_key_model
node_drf_auth -->|"resolves key model"| node_key_model
node_ninja_auth -->|"resolves key model"| node_key_model
node_drf_auth -.->|"provides authenticated key"| node_scope_checks
node_ninja_auth -->|"checks required scopes"| node_scope_checks
node_drf_auth -.->|"enables throttling"| node_drf_throttle
node_ninja_auth -->|"checks key limit"| node_rate_limit
node_drf_throttle -.->|"checks key limit"| node_rate_limit
node_rate_limit -->|"counts requests"| node_cache
node_key_model -->|"emits lifecycle events"| node_lifecycle_signals
node_auth_core -->|"emits rejection events"| node_lifecycle_signals
node_rate_limit -->|"emits limit events"| node_lifecycle_signals

click node_key_model "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/models.py"
click node_admin_ui "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/admin.py"
click node_lifecycle_cmds "https://github.com/stackadnan/django-tenant-apikeys/tree/main/django_tenant_apikeys/management/commands"
click node_drf_auth "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/authentication.py"
click node_ninja_auth "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/ninja.py"
click node_auth_core "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/core.py"
click node_ip_resolution "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/ip.py"
click node_scope_checks "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/permissions.py"
click node_rate_limit "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/ratelimit.py"
click node_drf_throttle "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/throttling.py"
click node_lifecycle_signals "https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/signals.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_key_model,node_key_store,node_admin_ui,node_lifecycle_cmds,node_client toneBlue
class node_drf_auth,node_ninja_auth,node_auth_core,node_ip_resolution toneAmber
class node_scope_checks,node_rate_limit,node_drf_throttle,node_cache toneMint
class node_lifecycle_signals toneRose
class node_developer toneIndigo
```

## Components

### Key management

- [Key model](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/models.py): `django_tenant_apikeys/models.py`
- **Django database**
- [Admin interface](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/admin.py): `django_tenant_apikeys/admin.py`
- [Lifecycle commands](https://github.com/stackadnan/django-tenant-apikeys/tree/main/django_tenant_apikeys/management/commands): `django_tenant_apikeys/management/commands`

### Request authentication

- [DRF authentication](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/authentication.py): `django_tenant_apikeys/authentication.py`
- [Ninja authentication](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/ninja.py): `django_tenant_apikeys/ninja.py`
- [Shared auth core](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/core.py): `django_tenant_apikeys/core.py`
- [Client IP resolution](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/ip.py): `django_tenant_apikeys/ip.py`

### Access policies

- [Scope permissions](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/permissions.py): `django_tenant_apikeys/permissions.py`
- [Rate-limit engine](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/ratelimit.py): `django_tenant_apikeys/ratelimit.py`
- [DRF throttle](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/throttling.py): `django_tenant_apikeys/throttling.py`
- **Django cache**

### Operations and events

- [Lifecycle signals](https://github.com/stackadnan/django-tenant-apikeys/blob/main/django_tenant_apikeys/signals.py): `django_tenant_apikeys/signals.py`

### Other components

- **Project developer**
- **API client**

## Connections

- Project developer → Key model: creates keys (evidence: `README.md`)
- Key model → Django database: persists keys (evidence: `README.md`)
- Project developer → Admin interface: manages keys (evidence: `README.md`)
- Admin interface → Key model: creates and revokes (evidence: `django_tenant_apikeys/admin.py`)
- Project developer → Lifecycle commands: runs commands (evidence: `README.md`)
- Lifecycle commands → Key model: rotates or revokes (evidence: `django_tenant_apikeys/management/commands/tenant_api_key_rotate.py`)
- API client → DRF authentication: sends API key (evidence: `README.md`)
- API client → Ninja authentication: sends API key (evidence: `README.md`)
- DRF authentication → Client IP resolution: resolves client IP (evidence: `django_tenant_apikeys/authentication.py`)
- DRF authentication → Shared auth core: authenticates key (evidence: `django_tenant_apikeys/authentication.py`)
- Ninja authentication → Client IP resolution: resolves client IP (evidence: `django_tenant_apikeys/ninja.py`)
- Ninja authentication → Shared auth core: authenticates key (evidence: `django_tenant_apikeys/ninja.py`)
- Shared auth core → Key model: looks up and verifies (evidence: `django_tenant_apikeys/core.py`)
- DRF authentication → Key model: resolves key model (evidence: `django_tenant_apikeys/authentication.py`)
- Ninja authentication → Key model: resolves key model (evidence: `django_tenant_apikeys/ninja.py`)
- DRF authentication → Scope permissions: provides authenticated key (evidence: `README.md`)
- Ninja authentication → Scope permissions: checks required scopes (evidence: `django_tenant_apikeys/ninja.py`)
- DRF authentication → DRF throttle: enables throttling (evidence: `README.md`)
- Ninja authentication → Rate-limit engine: checks key limit (evidence: `django_tenant_apikeys/ninja.py`)
- DRF throttle → Rate-limit engine: checks key limit (evidence: `django_tenant_apikeys/throttling.py`)
- Rate-limit engine → Django cache: counts requests (evidence: `django_tenant_apikeys/ratelimit.py`)
- Key model → Lifecycle signals: emits lifecycle events (evidence: `django_tenant_apikeys/models.py`)
- Shared auth core → Lifecycle signals: emits rejection events (evidence: `django_tenant_apikeys/signals.py`)
- Rate-limit engine → Lifecycle signals: emits limit events (evidence: `django_tenant_apikeys/ratelimit.py`)
