Standalone Auth Micro-Service
create_auth_app() deploys the Kirak authentication engine as a dedicated, standalone FastAPI microservice containing only /auth/* routes—with no CRUD engine, model-driven endpoints, or custom database tables exposed.
Use this pattern when you want a centralized identity and authentication service consumed by multiple downstream API services.
When to Use
Section titled “When to Use”| Architecture Pattern | Recommended Factory | Characteristics |
|---|---|---|
| Monolith / All-in-One API | create_kirak_app() |
Auth + CRUD + GraphQL in a single runtime |
| Dedicated Auth Microservice | create_auth_app() |
Only /auth/* endpoints; issues JWTs for your fleet |
| Custom FastAPI Integration | create_app() + Kirak() |
Mount Kirak auth and middleware into an existing app |
Architecture Overview
Section titled “Architecture Overview”In a microservice topology, the standalone Auth Service manages identity, credentials, and token issuance. Downstream API services verify JWT tokens locally in memory with zero network calls to the auth service:
flowchart TD subgraph Clients["Frontend Clients"] Mobile["Mobile App"] Web["Web / SPA"] end
subgraph AuthCluster["Auth Microservice (:8001)"] AuthApp["create_auth_app()<br/>(Handles /auth/*, Tokens, Users)"] AuthDB[("Auth Database<br/>(users, auth_tokens, auth_mfa)")] AuthApp --- AuthDB end
subgraph APICluster["Downstream API Services (:8000, :8002...)"] ServiceA["API Service 1 (CRUD Engine)"] ServiceB["API Service 2 (Custom Services)"] end
Clients -->|"1. POST /auth/login<br/>Obtains JWT"| AuthApp Clients -->|"2. API Calls<br/>Authorization: Bearer JWT"| ServiceA Clients -->|"2. API Calls<br/>Authorization: Bearer JWT"| ServiceB
ServiceA -.->|"Verifies JWT Locally (Zero Network I/O)<br/>Shared KIRAK_AUTH_JWT_SECRET_KEY"| ServiceA ServiceB -.->|"Verifies JWT Locally (Zero Network I/O)<br/>Shared KIRAK_AUTH_JWT_SECRET_KEY"| ServiceBBasic Setup
Section titled “Basic Setup”Create main_auth.py:
from dotenv import load_dotenvload_dotenv()
from kirak import create_auth_app
def on_auth_ready(auth): # 'auth' is the Auth module instance (not the full Kirak facade) @auth.hook("after_login") async def log_sign_in(result): if result.get("status") == "success": user_id = result["data"]["user"]["user_id"] print(f"[AUTH SERVICE] User {user_id} logged in") return result
app = create_auth_app( on_auth_ready=on_auth_ready,)Like any Kirak app, configuration is read from kirak.json:
routes.auth_prefix: Defaults to/auth(e.g./auth/login).cors: Allowed origins for your web apps.database: Dedicated database connection for auth tables.
Start the standalone service:
kirak run --app main_auth:app --port 8001Verifying Tokens from Downstream Services
Section titled “Verifying Tokens from Downstream Services”Downstream microservices can verify tokens in two ways:
Method 1: Local In-Memory Verification (Recommended — Zero Network Overhead)
Section titled “Method 1: Local In-Memory Verification (Recommended — Zero Network Overhead)”Because Kirak access tokens are cryptographically signed JWTs, downstream services can verify signatures locally without making HTTP calls back to the auth service:
from kirak.auth.utils.jwt import decode_tokenimport jwt
try: payload = decode_token(token) user_id = payload.get("sub") role = payload.get("role")except jwt.ExpiredSignatureError: # 401: Token has expired passexcept jwt.InvalidTokenError: # 401: Token is invalid or tampered passRequirement: Both the Auth service and downstream API services must share the same
KIRAK_AUTH_JWT_SECRET_KEY.
Method 2: Remote Profile Lookup (Network Call)
Section titled “Method 2: Remote Profile Lookup (Network Call)”If an API service needs live, fresh profile data from the database (such as updated profile fields or verifying whether the account was closed):
GET http://auth-service:8001/auth/meAuthorization: Bearer eyJhbGciOi...create_auth_app() Parameters
Section titled “create_auth_app() Parameters”| Parameter | Default | Description |
|---|---|---|
models_path |
Built-in auth_models.json |
Path to additional custom models if needed. Built-in auth tables (users, auth_tokens, etc.) are always included automatically. |
middlewares |
[] |
Additional ASGI middleware tuples (Class, kwargs). |
on_startup |
None |
async (app) hook executed before startup. |
on_auth_ready |
None |
async (auth) hook executed after the auth module initializes. Note: Receives the Auth module instance. |
on_shutdown |
None |
async (app) hook executed after shutdown. |
Production Deployment (Docker Compose)
Section titled “Production Deployment (Docker Compose)”A production deployment runs the Auth service and the API service as separate containers sharing the same JWT secret:
version: "3.8"
services: # Standalone Authentication Service auth-service: build: . command: uvicorn main_auth:app --host 0.0.0.0 --port 8001 ports: - "8001:8001" environment: - DB_PASSWORD=${DB_PASSWORD} - KIRAK_AUTH_JWT_SECRET_KEY=${SHARED_JWT_SECRET} - KIRAK_AUTH_VERIFICATION_KEY=${FERNET_KEY}
# Main Business Logic & CRUD API Service api-service: build: . command: uvicorn main:app --host 0.0.0.0 --port 8000 ports: - "8000:8000" environment: - DB_PASSWORD=${DB_PASSWORD} - KIRAK_AUTH_JWT_SECRET_KEY=${SHARED_JWT_SECRET} # Must match auth-service