API key management and authentication middleware for Spectra analytics
Project description
spectra-auth
API key management and authentication middleware for Spectra analytics.
Installation
pip install spectra-auth
For FastAPI projects, install with the optional FastAPI dependency:
pip install "spectra-auth[fastapi]"
Configuration
Explicit (recommended)
Call configure() once at application startup before using any other functions:
from spectra_auth import configure
configure(
mongodb_uri="mongodb+srv://user:pass@cluster.mongodb.net/",
mongodb_database="spectra",
)
Environment variables (fallback)
If configure() is not called, the library reads from the environment:
| Variable | Default | Description |
|---|---|---|
MONGODB_URI |
mongodb://localhost:27017 |
MongoDB connection string |
MONGODB_DATABASE |
spectra |
Database name |
Copy .env.example to .env and fill in your values.
Usage
Starlette / FastAPI middleware
Add AuthMiddleware to enforce authentication on all routes:
from fastapi import FastAPI
from spectra_auth import configure, AuthMiddleware
configure(mongodb_uri="...", mongodb_database="spectra")
app = FastAPI()
app.add_middleware(AuthMiddleware)
Authentication flow per request:
account_idis always required — passed via theX-Account-IDheader or anaccount_idfield in the JSON body.- If the request
Origin/Referermatches an allowed origin for that account, the request passes through without an API key. - Otherwise, a valid API key must be provided via:
Authorization: Bearer <key>header (standard)api_keyfield in the JSON body (sendBeacon fallback)
The /health path is always unauthenticated.
FastAPI route dependency
Use require_api_key as a route-level dependency when you need per-route enforcement instead of middleware:
from fastapi import Depends, FastAPI
from spectra_auth import require_api_key
app = FastAPI()
@app.get("/protected", dependencies=[Depends(require_api_key)])
async def protected_route():
return {"message": "authenticated"}
API key management
from spectra_auth import create_api_key, delete_api_key, verify_key
# Create a new key (returns the raw key — store it securely, it is never stored in plain text)
raw_key = create_api_key(account_id="acct_123", label="Production")
# Verify a key
is_valid = verify_key(raw_key, account_id="acct_123")
# Delete a key
deleted = delete_api_key(raw_key)
Allowed origins
Allowed origins let browser clients send requests without an API key, identified by their Origin header:
from spectra_auth import add_allowed_origin, remove_allowed_origin, get_allowed_origins
add_allowed_origin("acct_123", "https://yourapp.com")
origins = get_allowed_origins("acct_123")
# ["https://yourapp.com"]
remove_allowed_origin("acct_123", "https://yourapp.com")
MongoDB schema
spectra-auth expects two collections in your database:
| Collection | Key fields |
|---|---|
api_keys |
account_id, key_hash, created_at, label |
allowed_origins |
account_id, origins (array) |
Indexes are created automatically on first use.
License
MIT
Project details
Release history Release notifications | RSS feed
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 spectra_auth-1.0.0.tar.gz.
File metadata
- Download URL: spectra_auth-1.0.0.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f248ef901635ad94df419499a0e623b1b1429d50999b281e7d5567245c15928
|
|
| MD5 |
7f16f27d66288a3d3d5c3e8c84083a5a
|
|
| BLAKE2b-256 |
c2d2b346a8c4691453d4abeb78b2b1798a018e80cdc0ebfae1bef38baaf6d3ab
|
File details
Details for the file spectra_auth-1.0.0-py3-none-any.whl.
File metadata
- Download URL: spectra_auth-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc34cc6993e9ae1e416aa27e599e65ef92588edceae65c05ced9c825ca79a64c
|
|
| MD5 |
ab9879d62f2ef5bb1f21690f719babe2
|
|
| BLAKE2b-256 |
05e304fa200a21ef6f1605c1399e42eab72187ebfdb697991c998ebfe25b9811
|