Backend for Minecraft server management platform with professional authentication, authorization, and admin dashboard.
- JWT tokens stored in httpOnly cookies
- CSRF protection for all POST, PUT, and DELETE requests
- Password hashing using the Argon2 algorithm
- User role system (user / admin)
@admin_requireddecorator for admin-only endpoints
- Full CRUD operations (Create, Read, Update, Delete)
- Permission system (owner or admin)
- Data validation before saving to the database
- User–Server relationships
- General statistics (users, servers, admins)
- User distribution by role
- Servers grouped by game mode
- SQL aggregations (e.g., COUNT, GROUP BY)
| Category | Technology |
|---|---|
| Framework | Flask 3.0+ |
| Database | SQLite (SQLAlchemy ORM) |
| Authentication | Flask-JWT-Extended (cookies) |
| Hashing | Argon2 |
| CORS | Flask-CORS |
| Environment | python-dotenv |
git clone https://github.com/yourusername/minecraft-server-hub-backend.git
cd minecraft-server-hub-backend
Windows: python -m venv venv + venv\Scripts\activate
macOS/Linux: python3 -m venv venv + source venv/bin/activate
pip install -r requirements.txt
cp .env.example .env
JWT_SECRET_KEY=your-very-long-and-random-secret-key
python app.py
Server will be available at: http://127.0.0.1:5000
| Variable | Description | Required |
|---|---|---|
| JWT_SECRET_KEY | Key for signing JWT tokens | ✅ Yes |
| Variable | Default Value | Description |
|---|---|---|
| SQLALCHEMY_DATABASE_URI | sqlite:///database.db | Database connection |
| JWT_TOKEN_LOCATION | ["cookies"] | Token storage location |
| JWT_COOKIE_SECURE | False | HTTPS only in production |
| JWT_COOKIE_CSRF_PROTECT | True | CSRF protection |
| JWT_CSRF_HEADER_NAME | X-CSRF-TOKEN | CSRF header name |
minecraft-server-hub-backend/
├── app.py # Main Flask application file
├── models.py # Database models (User, Server)
├── utils.py # Helper functions (password hashing)
├── .env # Environment variables (not in git!)
├── .gitignore # Ignored files
├── requirements.txt # Python dependencies
└── README.md # DocumentationBody:
{
"username": "new_user",
"password": "secure_password",
"role": "user"
}Response (201):
{
"message": "User created successfully"
}Body:
{
"username": "new_user",
"password": "secure_password"
}Response (200):
{
"msg": "login successful"
}Sets cookies: access_token_cookie + csrf_access_token
Response (200):
{
"msg": "Logout successful"
}Response (200):
[
{
"id": 1,
"name": "My Server",
"ip_address": "192.168.1.100",
"version": "1.20.1",
"game_mode": "survival",
"image_url": "https://example.com/image.png",
"owner_id": 1
}
]Response (200):
{
"id": 1,
"name": "My Server",
"ip_address": "192.168.1.100",
"version": "1.20.1",
"game_mode": "survival",
"image_url": "https://example.com/image.png",
"owner_id": 1
}Body:
{
"name": "New Server",
"ip_address": "192.168.1.100",
"version": "1.20.1",
"game_mode": "survival",
"image_url": "https://example.com/image.png"
}Response (201):
{
"message": "Succesfully created server!"
}Body (only fields to update):
{
"name": "Updated Name",
"version": "1.20.4"
}Response (200):
{
"message": "Server updated successfully!"
}Response (204): No ContentRequired: admin role
Response (200):
{
"total_user_count": 50,
"total_server_count": 40,
"total_admin_count": 5,
"users_by_role": {
"user": 45,
"admin": 5
},
"server_by_game_mode": {
"survival": 15,
"creative": 8,
"pvp": 12
}
}- Login → Server returns token in httpOnly cookie
- CSRF Token → Additional cookie csrf_access_token
- Every Request → Browser automatically sends cookies
- CSRF Header → For POST/PUT/DELETE add header X-CSRF-TOKEN
function getCookie(name) {
const value = '; ' + document.cookie;
const parts = value.split('; ' + name + '=');
if (parts.length === 2) return parts.pop().split(';').shift();
}
// Usage in fetch
const csrfToken = getCookie('csrf_access_token');
fetch('/api/servers', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': csrfToken
},
body: JSON.stringify({...})
});- Login (POST /api/auth/login)
- Copy CSRF token from cookies
- Add header X-CSRF-TOKEN to POST/PUT/DELETE requests
- Enable "Automatically follow redirects" and "Persist cookies"
| Endpoint | Method | Auth | CSRF | Status |
|---|---|---|---|---|
| /api/auth/register | POST | ❌ | ❌ | ✅ |
| /api/auth/login | POST | ❌ | ❌ | ✅ |
| /api/auth/logout | POST | ✅ | ✅ | ✅ |
| /api/servers | GET | ✅ | ❌ | ✅ |
| /api/servers | POST | ✅ | ✅ | ✅ |
| /api/servers/ | PUT | ✅ | ✅ | ✅ |
| /api/servers/ | DELETE | ✅ | ✅ | ✅ |
| /api/admin/stats | GET | ✅ + Admin | ❌ | ✅ |
| Feature | Status | Description |
|---|---|---|
| JWT Cookies | ✅ | Tokens in httpOnly cookies |
| CSRF Protection | ✅ | CSRF token validation |
| Password Hashing | ✅ | Argon2 (most secure) |
| SQL Injection | ✅ | SQLAlchemy ORM |
| CORS | ✅ | Configurable origins |
| Role-Based Access | ✅ | User / Admin |
Before deploy, change in app.py:
- JWT_COOKIE_SECURE = True (HTTPS only)
- JWT_COOKIE_SAMESITE = "Lax" (CSRF protection)
- CORS origins = ["https://your-domain.com"] (specific domains)
- app.debug = False (disable debug mode)
If you like this project, give it a ⭐