This AWS Lambda function serves as a centralized, secure backend for updating and retrieving top scores (10) across multiple games. It uses HMAC-based authentication, enforces timestamp validity, and stores scores in MongoDB.
- Secure HMAC signature verification
- Timestamp validation (5-minute drift tolerance)
- MongoDB backend for storing and retrieving scores
- Supports multiple games using dynamic database selection
- Automatically trims scores to top 10 per game
The function accepts JSON requests with the following structure:
| Header | Required | Description |
|---|---|---|
| x-hmac-signature | Yes (in production) | Base64-encoded HMAC SHA256 signature |
| x-timestamp | Yes (in production) | ISO 8601 UTC timestamp (Z or +00:00) |
{ "action": "set" | "get", "game": "snake", // only for "set" action: "player": "Joey", "score": 1234, "date": "2025-06-10T12:34:56Z" }
- Adds a new score for the given game
- Keeps only the top 10 scores, sorted by score (and date as tie-breaker)
- Creates the game-specific database and collection if it doesn't exist
- Returns the top 10 scores for the given game
- Fails gracefully if the game database doesn't exist
| Variable | Required | Description |
|---|---|---|
| PYTHON_ENV | No | 'production' enables HMAC and timestamp checks |
| HMAC_SECRET | Yes (in production) | Secret used to verify HMAC signatures |
| MONGO_URI | Yes | Full connection URI for your MongoDB cluster |
You can run the script locally with a simulated event:
python lambda_function.py
Make sure to define your environment variables or load them from a .env file.
- Python 3.10+
- pymongo
- certifi
Install with:
pip install pymongo certifi
MIT License