security
Security and Request Handling UtilitiesLink
This module provides decorators and utility functions to enhance the security of Flask-based Web APIs. It offers mechanisms for rate limiting to prevent abuse and utilities for extracting JSON Web Tokens (JWT) from HTTP headers to facilitate authentication.
Key FeaturesLink
- Rate Limiting: A thread-safe decorator to restrict the number of requests from a specific IP address within a rolling time window.
- JWT Extraction: Functions to seamlessly extract Bearer tokens from the
Authorizationheader and inject them into function arguments. - Flask Integration: Designed to work directly with Flask's request context and response objects, returning standard HTTP status codes (e.g., 429 Too Many Requests).
Usage ExamplesLink
The following example demonstrates how to protect a Flask-RESTful resource using the security decorators:
from flask import Flask
from flask_restful import Api, Resource
from security import rate_limit, add_jwt_from_header
app = Flask(__name__)
api = Api(app)
class SecureResource(Resource):
@rate_limit(max_requests=10, window_seconds=60)
@add_jwt_from_header
def get(self, **kwargs):
# The 'token' is automatically extracted and passed in kwargs
token = kwargs.get('token')
return {"message": "Access granted", "token_found": bool(token)}
api.add_resource(SecureResource, '/secure')
if __name__ == '__main__':
app.run()
add_jwt_from_header
Link
add_jwt_from_header(f)
Retrieve the JSON Web Token from the header and add it to keyword arguments, if any.
This enables automatic token definition from the request header.
Source code in plantdb/server/core/security.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | |
jwt_from_header
Link
jwt_from_header(request)
Extract the JWT from the Authorizationrequest header.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
An HTTP request exposing a |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The extracted JWT or |
Notes
- The function expects the header to be in the form
Bearer <token>. - The function performs a simple string replacement and does not perform any verification.
Source code in plantdb/server/core/security.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
rate_limit
Link
rate_limit(max_requests=5, window_seconds=60)
Limits the number of requests a client can make within a specified time window.
This function is a decorator that enforces rate limiting based on the maximum
number of allowed requests (max_requests) and the time window size in seconds
(window_seconds). It tracks incoming requests from clients using their IP
addresses and ensures that they do not exceed the specified limit within the
time window. If the limit is exceeded, it returns an HTTP 429 response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
The maximum number of requests permitted within the time window (default is 5). |
5
|
|
int
|
The duration of the rate-limiting window, in seconds (default is 60 seconds). |
60
|
Returns:
| Name | Type | Description |
|---|---|---|
decorator |
Callable
|
A decorator that can wrap any function or endpoint to enforce rate limiting. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If the rate limit is exceeded, it returns an HTTP 429 ("Too Many Requests") response to the client. |
Notes
This implementation uses a thread lock to ensure thread safety when handling
requests, making it suitable for multithreaded environments. The requests
data structure is a defaultdict that maps client IPs to a list of their
request timestamps. Old requests outside the rate-limiting window are removed
to maintain efficient memory usage.
Source code in plantdb/server/core/security.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
sanitize_ids
Link
sanitize_ids(*param_names)
Decorator that sanitizes the given identifier parameters using sanitize_name.
If sanitization fails, logs a warning and returns a 400 response.
Source code in plantdb/server/core/security.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 | |
sanitize_name
Link
sanitize_name(name)
Sanitizes and validates the provided name.
The function ensures that the input string adheres to predefined naming rules by:
- stripping leading/trailing spaces,
- isolating the last segment after splitting by slashes,
- validating the name against an alphanumeric pattern
with optional underscores (
_), dashes (-), or periods (.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The name to sanitize and validate. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A sanitized name that conforms to the rules. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided name contains invalid characters or does not meet the naming rules. |
Source code in plantdb/server/core/security.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
use_guest_as_default
Link
use_guest_as_default(f)
Injects a guest user when no authentication token is provided.
This enables methods that expect an authenticated user to operate transparently with a default guest context.
Source code in plantdb/server/core/security.py
187 188 189 190 191 192 193 194 195 196 197 198 199 | |