Skip to main content

API Key Authentication System Documentation

1. Introduction

1.1 Overview

Secure authentication system for programmatic API access using tenant-specific API keys with role-based permission inheritance.


2. System Architecture

2.1 Database Schema

2.2 Core Components

ComponentDescription
API Key ServiceHandles CRUD operations, role mapping, and transaction management
Authentication MiddlewareValidates keys, extracts user context, and enforces permissions
API Key ControllerManages REST endpoints and request/response handling

3. Authentication Workflow


4. API Key Management

4.1 Management Endpoints

EndpointMethodDescription
/tenants/{tenantID}/apikeysGETList all API keys
/tenants/{tenantID}/apikeysPOSTCreate new API key
/tenants/{tenantID}/apikeys/{apiKeyID}GETGet key details
/tenants/{tenantID}/apikeys/{apiKeyID}PUTUpdate key configuration
/tenants/{tenantID}/apikeys/{apiKeyID}DELETEDisable API key

4.2 Key Lifecycle Management

  1. Creation

    • Generate cryptographically secure key (256-bit)
    • Store key hash (PBKDF2 with HMAC-SHA256)
    • Set default 90-day expiration
  2. Rotation

    • Auto-generate new keys before expiration
    • Maintain legacy key validity for 7 days
  3. Revocation

    • Immediate disablement via DELETE endpoint
    • Global invalidation within 60 seconds

5. Usage Guidelines

5.1 Authentication Header

GET /api/v1/resources HTTP/1.1
Host: api.example.com
Authorization: api_key sk_live_12ab34cd56ef78gh90ij12kl34mn56op

5.2 Permission Inheritance

// Example role mapping
{
"apiKeyID": "key_12345",
"roles": [
"tenant.admin",
"global.monitoring"
]
}

6. Security Implementation

6.1 Key Generation

const generateAPIKey = () => {
const buffer = crypto.randomBytes(32);
return buffer.toString('base64url');
};

6.2 Security Features

FeatureImplementation Details
Tenant IsolationMandatory tenantID in all requests
Rate Limiting1000 requests/minute/key
Audit LoggingFull request metadata capture
Key HashingArgon2id with 64MB memory cost

7. Error Handling

7.1 Common Error Codes

CodeScenarioResolution
401Invalid/missing API keyVerify key header format
403Insufficient permissionsCheck role assignments
429Rate limit exceededImplement exponential backoff
500Database connection failureRetry with jittered delay