Ultra-minimal lite validation FastAPI application for Discord bot integration. This service provides secure Discord OAuth2 guild verification and validator management functionality for the PGDN network.
This API service handles:
- Discord OAuth2 Authentication - Guild membership verification with secure state management
- Validator Management - Lite validation requests and ownership verification
- Redis Caching - High-performance caching for verification status and rate limiting
- Database Integration - PostgreSQL storage for persistent data
- Security - HMAC-signed tokens, rate limiting, and comprehensive input validation
This API requires the Discord bot service to function properly:
- Discord Bot: pgdn-network/pgdn-discord
The Discord bot handles user interactions and communicates with this API for backend operations.
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ │ │ │ │ │
│ Discord Users │◄──►│ PGDN Discord │◄──►│ PGDN API Discord│
│ │ │ Bot │ │ (This Repo) │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ │ │ │
│ Discord OAuth2 │ │ PostgreSQL │
│ & Webhooks │ │ Database │
│ │ │ │
└─────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐
│ │
│ Redis Cache │
│ │
└─────────────────┘
- OAuth2 Flow: Secure Discord authentication with CSRF protection
- HMAC State Tokens: Cryptographically signed state tokens (10-minute expiry)
- Rate Limiting: Comprehensive rate limiting across all endpoints
- Account Age Verification: Minimum Discord account age requirements
- Bearer Token Authentication: Secure API access for bot communications
- Redis Caching: Fast verification status and rate limit tracking
- Connection Pooling: Optimized database connections
- Minimal Footprint: Lightweight Alpine-based Docker container
- Validator Ownership: DNS-based validation for node operators
- Guild Verification: Automated Discord server membership checks
- Persistent Storage: Long-term verification tracking
- Audit Logging: Comprehensive request and validation logging
- Python 3.12+
- PostgreSQL database
- Redis instance
- Discord Application (OAuth2 credentials)
Create a .env file with required variables:
# Database
DATABASE_URL=postgresql://user:password@localhost:5432/pgdn
# Discord OAuth2 Configuration
OAUTH_CLIENT_ID=your_discord_app_client_id
OAUTH_CLIENT_SECRET=your_discord_app_client_secret
OAUTH_REDIRECT_URI=http://localhost:8000/api/v1/lite/public/discord/callback
STATE_SIGNING_KEY=your_32_character_signing_key
# Discord Integration
DISCORD_API_AUTH_TOKEN=your_bot_api_token
DISCORD_BOT_WEBHOOK_URL=http://localhost:8080/webhook/send-message
ALLOWED_GUILD_IDS=comma,separated,guild,ids
# Optional Configuration
DEBUG=False
ALLOWED_ORIGINS=*
VERIFY_SUCCESS_TTL_DAYS=90
MIN_ACCOUNT_AGE_DAYS=7# Clone repository
git clone https://github.com/pgdn-network/pgdn-api-discord.git
cd pgdn-api-discord
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Run the application
python main.pyThe API will be available at http://localhost:8000
# Build image
docker build -t pgdn-api-discord .
# Run container
docker run -p 8000:8000 --env-file .env pgdn-api-discordThis service is designed for Kubernetes deployment with network-level security:
apiVersion: apps/v1
kind: Deployment
metadata:
name: pgdn-api-discord
spec:
replicas: 2
selector:
matchLabels:
app: pgdn-api-discord
template:
metadata:
labels:
app: pgdn-api-discord
spec:
containers:
- name: api
image: pgdn-api-discord:latest
ports:
- containerPort: 8000
envFrom:
- secretRef:
name: pgdn-api-secretsGET /- API status and versionGET /health- Health checkGET /api/v1/lite/public/discord/start- Start OAuth2 flowGET /api/v1/lite/public/discord/callback- OAuth2 callbackGET /api/v1/lite/public/validate/{validator_id}- Validator ownership validation
POST /api/v1/lite/private/claim- Create validation requestGET /api/v1/lite/private/verify/{discord_user_id}- Check verification statusGET /api/v1/lite/private/info- Get validator informationGET/POST /api/v1/lite/private/rescan- Request validator rescanGET /api/v1/lite/private/validators- List user's validatorsPOST /api/v1/lite/private/add- Submit new validator for review
pytest# Format code (if using black)
black .
# Lint code (if using ruff)
ruff check .The application auto-creates database tables on startup. For manual setup:
# Create database
createdb pgdn
# Tables are auto-created when the application starts
python main.pyThis application implements defense-in-depth security:
- Network Isolation: Designed for Kubernetes deployment with network policies
- Authentication: Multiple layers of token-based authentication
- Input Validation: Comprehensive request validation using Pydantic
- Rate Limiting: Protection against abuse and DoS attacks
- Secrets Management: All sensitive data via environment variables
- SQL Injection Protection: Parameterized queries throughout
| Variable | Required | Default | Description |
|---|---|---|---|
DATABASE_URL |
Yes | - | PostgreSQL connection string |
OAUTH_CLIENT_ID |
Yes | - | Discord application client ID |
OAUTH_CLIENT_SECRET |
Yes | - | Discord application client secret |
STATE_SIGNING_KEY |
Yes | - | HMAC signing key (32+ chars) |
DISCORD_API_AUTH_TOKEN |
Yes | - | Bot authentication token |
ALLOWED_GUILD_IDS |
Yes | - | Comma-separated Discord guild IDs |
DEBUG |
No | False |
Enable debug mode |
ALLOWED_ORIGINS |
No | * |
CORS allowed origins |
VERIFY_SUCCESS_TTL_DAYS |
No | 90 |
Verification cache TTL |
MIN_ACCOUNT_AGE_DAYS |
No | 7 |
Minimum Discord account age |
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Discord Bot: pgdn-network/pgdn-discord - Required companion service
- PGDN Network: pgdn-network - Main organization