Skip to content

J0E-E/game-score-lambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Game Score Lambda Function

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.

Features

  • 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

Usage

The function accepts JSON requests with the following structure:

Request Headers

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)

Request Body (JSON)

{ "action": "set" | "get", "game": "snake", // only for "set" action: "player": "Joey", "score": 1234, "date": "2025-06-10T12:34:56Z" }

Actions

set

  • 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

get

  • Returns the top 10 scores for the given game
  • Fails gracefully if the game database doesn't exist

Environment Variables

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

Local Testing

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.

Dependencies

  • Python 3.10+
  • pymongo
  • certifi

Install with:

pip install pymongo certifi

License

MIT License

About

A secure AWS Lambda function for submitting and retrieving top scores across multiple games to a MongoDB service. Features HMAC authentication, timestamp validation, and MongoDB storage with automatic top-10 score trimming.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages