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
Metadata
Release files for spectra-auth 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| spectra_auth-1.0.0.tar.gz | 6.6 kB | Without provenance | Details for spectra_auth-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| spectra_auth-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for spectra_auth-1.0.0-py3-none-any.whl |
Total release size: 15.2 kB
Release files / spectra_auth-1.0.0.tar.gz
| Download URL | spectra_auth-1.0.0.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8f248ef901635ad94df419499a0e623b1b1429d50999b281e7d5567245c15928
|
|
BLAKE2b-256 checksum How to use checksums |
c2d2b346a8c4691453d4abeb78b2b1798a018e80cdc0ebfae1bef38baaf6d3ab
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / spectra_auth-1.0.0-py3-none-any.whl
| Download URL | spectra_auth-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc34cc6993e9ae1e416aa27e599e65ef92588edceae65c05ced9c825ca79a64c
|
|
BLAKE2b-256 checksum How to use checksums |
05e304fa200a21ef6f1605c1399e42eab72187ebfdb697991c998ebfe25b9811
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Security for spectra-auth 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.