A reusable FastAPI router for API key management
Project description
API Key Management Library
A library for API key management and JWT validation, designed to be integrated into services that need to handle API key operations and authentication.
Overview
This library provides:
- API key model and persistence
- API key generation and validation
- JWT validation
- Key management endpoints
- Database access layer
Installation
pip install beanone-apikey
Quick Start
from fastapi import FastAPI
from apikey import api_key_router
app = FastAPI()
app.include_router(api_key_router)
Features
- API key generation and management
- API key validation
- JWT validation
- API key listing and deletion
- Secure key storage with hashing
- Async database operations
- FastAPI integration
API Endpoints
| Endpoint | Method | Description |
|---|---|---|
/api-keys/ |
POST | Create a new API key |
/api-keys/ |
GET | List all API keys |
/api-keys/{key_id} |
DELETE | Delete an API key |
Authentication
The library supports two authentication methods:
-
JWT Authentication
- Validates JWTs issued by the login service
- Extracts user information from JWT claims
- Supports audience validation
-
API Key Authentication
- Validates API keys in requests
- Supports both header and query parameter authentication
- Checks key status and expiration
Configuration
Environment variables:
DATABASE_URL: Database connection URL (default: sqlite+aiosqlite:///./apikey.db)- For development:
sqlite+aiosqlite:///./apikey.db - For production:
postgresql+asyncpg://user:password@host:5432/dbname
- For development:
JWT_SECRET: Secret for JWT validationJWT_ALGORITHM: JWT algorithm (default: HS256)LOGIN_URL: Login service URL (default: http://localhost:8001)
Database Configuration
- For development: SQLite (default)
- For production: PostgreSQL
- Use the full connection URL in
DATABASE_URL - Example:
postgresql+asyncpg://postgres:password@localhost:5432/apikeydb
- Use the full connection URL in
Development
- Clone the repository
- Install development dependencies:
pip install -e ".[dev]"
- Run tests:
pytest
Starting a New Development Instance
-
Set up the environment
# Create and activate virtual environment python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate # Install dependencies pip install -r requirements.txt pip install -r requirements-test.txt
-
Configure environment variables Create a
.envfile in the project root:# Database configuration # For local deployment only DATABASE_URL=sqlite+aiosqlite:///./apikey.db # DATABASE_URL=postgresql+asyncpg://postgres:password@localhost:5433/apikeydb # For PostgreSQL with Docker # JWT configuration: should match that of the Login service JWT_SECRET=supersecretjwtkey JWT_ALGORITHM=HS256 LOGIN_URL=http://localhost:8001
-
Start the development server
# Using uvicorn directly uvicorn src.apikey.service:app --reload --port 8002 # Or using Docker Compose for development docker compose -f docker-compose.dev.yml build --no-cache docker compose -f docker-compose.dev.yml up
-
Verify the setup
- Check the health endpoint:
http://localhost:8002/health - Access the API documentation:
http://localhost:8002/docs - Run the test suite:
pytest
- Check the health endpoint:
-
Development workflow
- The server will automatically reload on code changes
- Use the API documentation to test endpoints
- Check logs:
docker-compose -f docker-compose.dev.yml logs -f apikey
Docker Deployment
Production Deployment
- Create a
.envfile with required environment variables - Run with Docker Compose:
docker-compose up -d
The service will be available at http://localhost:8002
Development Deployment
- Create a
.envfile with required environment variables - Run with development Docker Compose:
docker compose -f docker-compose.dev.yml up -d
Development features:
- Hot reload enabled
- Source code mounted for live updates
- PostgreSQL database with persistent volume
- Health checks configured
Docker Configuration
- Service runs on port 8002
- Health check endpoint:
/health - Resource limits:
- CPU: 0.5 cores
- Memory: 512MB
- Logging: JSON format with rotation (10MB max, 3 files)
Architecture
This library is designed to be integrated into services that need to:
- Manage API keys for their users
- Validate incoming requests using either JWTs or API keys
- Store and manage API key data
The library follows a distributed API key management pattern where:
- Each service maintains its own API key database
- API key validation is performed locally
- JWT validation is performed against the login service
Security
- API keys are hashed before storage
- JWT validation includes audience checks
- API key validation checks status and expiration
- All endpoints require authentication
- Database operations use parameterized queries
- Non-root user in Docker container
- Resource limits enforced
- Health checks implemented
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file beanone_apikey-0.3.0.tar.gz.
File metadata
- Download URL: beanone_apikey-0.3.0.tar.gz
- Upload date:
- Size: 31.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
27e73a02724cf0e8218ce2e3259543312277dd2efe4bae634e1244859d600dd8
|
|
| MD5 |
0409863e9e91bb5d9f94ce29092d58db
|
|
| BLAKE2b-256 |
e25f46d8c86508992d38d89e0a75bf07e97ba2c6e99241a9567eda6d93f16365
|
Provenance
The following attestation bundles were made for beanone_apikey-0.3.0.tar.gz:
Publisher:
publish.yml on beanone/apikey
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
beanone_apikey-0.3.0.tar.gz -
Subject digest:
27e73a02724cf0e8218ce2e3259543312277dd2efe4bae634e1244859d600dd8 - Sigstore transparency entry: 216127681
- Sigstore integration time:
-
Permalink:
beanone/apikey@351384d70a3e0fd4f7d394acd3e4b4012c7164c0 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/beanone
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@351384d70a3e0fd4f7d394acd3e4b4012c7164c0 -
Trigger Event:
push
-
Statement type:
File details
Details for the file beanone_apikey-0.3.0-py3-none-any.whl.
File metadata
- Download URL: beanone_apikey-0.3.0-py3-none-any.whl
- Upload date:
- Size: 13.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06fc6e500c9d31f4da29558f2f2033e8dda3cd1cb9732c60ca2effaa1a9335c3
|
|
| MD5 |
c2e77ff80575363269adf1827d86b097
|
|
| BLAKE2b-256 |
bc543f418c1463ce99c3414cc1e265ebc2b44945a096d797aa2e87970bfb3946
|
Provenance
The following attestation bundles were made for beanone_apikey-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on beanone/apikey
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
beanone_apikey-0.3.0-py3-none-any.whl -
Subject digest:
06fc6e500c9d31f4da29558f2f2033e8dda3cd1cb9732c60ca2effaa1a9335c3 - Sigstore transparency entry: 216127682
- Sigstore integration time:
-
Permalink:
beanone/apikey@351384d70a3e0fd4f7d394acd3e4b4012c7164c0 -
Branch / Tag:
refs/tags/v0.3.0 - Owner: https://github.com/beanone
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@351384d70a3e0fd4f7d394acd3e4b4012c7164c0 -
Trigger Event:
push
-
Statement type: