A Python-based authentication proxy server that validates requests against a database and forwards them to a target URL.
- Validates
X-Scope-OrgIDandapikeyheaders against database - Stores credentials in a database (SQLite or PostgreSQL)
- Management API for creating/deleting credentials
- Forwards authenticated requests to a configurable target URL
- Preserves all original request headers and body
- Install Poetry (if not already installed):
curl -sSL https://install.python-poetry.org | python3 -- Install dependencies:
poetry install- Create a
.envfile:
DATABASE_URL=sqlite:///auth_proxy.db
TARGET_URL=http://localhost:8080
PORT=3000For PostgreSQL:
DATABASE_URL=postgresql://user:password@localhost:5432/auth_proxy
Start the server:
poetry run python app.pyOr activate the virtual environment first:
poetry shell
python app.pycurl -X POST http://localhost:3000/credentials \
-H "Content-Type: application/json" \
-d '{
"tenant_id": "tenant-123",
"api_key": "secret-api-key-123"
}'curl http://localhost:3000/credentialscurl -X DELETE http://localhost:3000/credentials \
-H "Content-Type: application/json" \
-d '{
"tenant_id": "tenant-123"
}'Once credentials are created, use them in requests:
curl -X GET http://localhost:3000/api/endpoint \
-H "X-Scope-OrgID: tenant-123" \
-H "apikey: secret-api-key-123"Or using Python:
import requests
response = requests.get(
'http://localhost:3000/api/endpoint',
headers={
'X-Scope-OrgID': 'tenant-123',
'apikey': 'secret-api-key-123'
}
)- 200: Successful proxy response from target server
- 401 Unauthorized: Missing or invalid authentication headers/credentials
- 500 Proxy Error: Error connecting to target server
Edit .env to configure:
DATABASE_URL: Database connection string (SQLite or PostgreSQL)TARGET_URL: URL of the server to proxy requests toPORT: Port for the proxy server (default: 3000)
The credentials table has the following structure:
tenant_id(String, Primary Key): The tenant/organization IDapi_key(String): The API key for authenticationis_active(Boolean): Whether the credential is active