Skip to content

API Security - Wyatt's Notes

REST APIs are stateless by design: each request must contain all information needed for Authentication and authorization. The server does not maintain session state between requests.

Every request must include:
1. Authentication credential (Bearer token, API key, mTLS certificate)
2. Required headers (Content-Type, Accept)
3. Any correlation/tracing identifiers
The server validates credentials on every request.
No server-side session is required (but tokens must be stateless or validated).
## Simple API key in header
API_KEYS = {
"key_abc123': {'name': "service-a'', "scopes': ['read:users']},
'key_def456': {'name': "service-b'', "scopes': ['read:users', 'write:orders']},
}
@app.before_request
def validate_api_key():
api_key = request.headers.get('X-API-Key')
if not api_key or api_key not in API_KEYS:
return jsonify({"error": "Invalid API key"}), 401
g.api_key_info = API_KEYS[api_key]

Network security operates like a medieval castle with multiple defensive layers. Firewalls act as the outer walls, filtering traffic based on rules. Encryption wraps data in protective armor during transit. The principle of defense-in-depth means that if one layer fails, others continue protecting. Understanding how packets travel through these layers helps you see where vulnerabilities exist and how to close them.