onconova.core.history.middleware
SENSITIVE_KEYS
module-attribute
¶
audit_logger
module-attribute
¶
ASGIRequest
¶
Bases: DjangoRequest
, ASGIRequest
ASGIRequest is a request class that inherits from both DjangoRequest and DjangoASGIRequest, combining their functionality to handle HTTP requests in ASGI-compatible Django applications.
This class serves as a unified interface for processing requests in environments where ASGI support is required, such as asynchronous web servers.
Inheritance
DjangoRequest: Standard Django HTTP request handling. DjangoASGIRequest: ASGI-specific request handling for asynchronous support.
AuditLogMiddleware(get_response)
¶
Middleware that logs detailed audit information for each HTTP request and response.
This middleware captures and logs the following information: - User identification (ID, username, access level), or marks as anonymous if unauthenticated. - Request metadata such as IP address, HTTP method, endpoint path, user agent, and processing duration. - Request and response data, with sensitive fields redacted and optionally compressed and base64-encoded. - HTTP status code of the response.
Sensitive fields in request and response data are redacted based on a predefined list of keys. Handles both JSON and non-JSON responses gracefully, and ensures that unreadable or non-JSON data is marked accordingly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
callable
|
The next middleware or view in the Django request/response chain. |
required |
Source code in onconova/core/history/middleware.py
get_response
instance-attribute
¶
__call__(request)
¶
Handles incoming HTTP requests, measures processing time, and logs audit information.
This method records the start time, processes the request, and calculates the duration. It extracts user information (ID, username, access level), request metadata (IP, method, path, user agent), and both request and response data (optionally compressed and base64-encoded, except for 'openapi.json' endpoints). All relevant details are logged using the audit_logger for auditing purposes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
HttpRequest
|
The incoming HTTP request object. |
required |
Returns:
Name | Type | Description |
---|---|---|
response |
Any
|
The HTTP response object generated by processing the request. |
Source code in onconova/core/history/middleware.py
compress_b64(data)
staticmethod
¶
Compresses a string using gzip and encodes the result in base64.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
str
|
The input string to be compressed and encoded. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The base64-encoded compressed string, or "[compress-error]" if compression fails. |
Source code in onconova/core/history/middleware.py
get_client_ip(request)
staticmethod
¶
Retrieve the client's IP address from the given Django request object.
This function checks for the 'HTTP_X_FORWARDED_FOR' header, which is set by proxies to indicate the original IP address of the client. If present, it returns the first IP address in the list. If not present, it falls back to the 'REMOTE_ADDR' value, which contains the direct IP address of the client.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
HttpRequest
|
The Django request object. |
required |
Returns:
Type | Description |
---|---|
str | None
|
The client's IP address as a string, or None if not found. |
Source code in onconova/core/history/middleware.py
get_request_data(request)
¶
Extracts and redacts request data for auditing purposes.
For POST, PUT, and PATCH requests, attempts to decode and parse the request body as JSON, then redacts sensitive information and returns the result as a compact JSON string. For other request methods, redacts sensitive information from query parameters and returns the result as a compact JSON string.
In case of any exception during processing, logs the exception and returns "[unreadable]".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
HttpRequest
|
The HTTP request object. |
required |
Returns:
Type | Description |
---|---|
str
|
A compact JSON string of the redacted request data, or "[unreadable]" if an error occurs. |
Source code in onconova/core/history/middleware.py
get_response_data(response)
¶
Extracts and returns the JSON-encoded data from a response object.
If the response has a 'data' attribute, it redacts sensitive information and serializes it to a compact JSON string. If the response is a JSON HTTP response, it decodes and returns the content as a string. For non-JSON responses, returns a placeholder string. In case of any exception, logs the error and returns an unreadable placeholder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
Any
|
The response object to extract data from. |
required |
Returns:
Type | Description |
---|---|
str
|
The JSON string representation of the response data, a placeholder for non-JSON responses, or an unreadable placeholder in case of errors. |
Source code in onconova/core/history/middleware.py
redact(data)
staticmethod
¶
Recursively redacts sensitive information from dictionaries and lists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
object
|
The input data to be redacted. Can be a dictionary, list, or other object. |
required |
Returns:
Name | Type | Description |
---|---|---|
object |
object
|
The redacted data with sensitive values replaced by "[REDACTED]". |
Notes
- Keys in dictionaries that match any string in SENSITIVE_KEYS (case-insensitive) will have their values replaced.
- Lists are processed recursively.
- Non-dict and non-list objects are returned unchanged.
Source code in onconova/core/history/middleware.py
DjangoRequest
¶
Although Django's auth middleware sets the user in middleware, apps like django-rest-framework set the user in the view layer. This creates issues for pghistory tracking since the context needs to be set before DB operations happen.
This special WSGIRequest updates pghistory context when the request.user attribute is updated.
__setattr__(attr, value)
¶
Source code in onconova/core/history/middleware.py
HistoryMiddleware
¶
Bases: HistoryMiddleware
Custom middleware for tracking request history with additional context.
This middleware extends pghistory.middleware.HistoryMiddleware
to enrich the
history context with the requesting user's username and IP address. It overrides
the get_context
method to add these details to the context used for history
tracking.
__call__(request)
¶
Source code in onconova/core/history/middleware.py
get_context(request)
¶
Returns a context dictionary for the given request, extending the parent context with additional information: - 'username': The username of the authenticated user, or None if unavailable. - 'ip_address': The IP address of the request, or 'unknown' if not found.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
HttpRequest
|
The HTTP request object. |
required |
Returns:
Type | Description |
---|---|
dict
|
The context dictionary containing user and request metadata. |
Source code in onconova/core/history/middleware.py
WSGIRequest
¶
Bases: DjangoRequest
, WSGIRequest
WSGIRequest is a subclass that combines functionality from both DjangoRequest and DjangoWSGIRequest.
This class is intended to represent an HTTP request in a WSGI-compliant Django application, inheriting all attributes and methods from its parent classes.
Inheritance
DjangoRequest: Provides core request handling features. DjangoWSGIRequest: Adds WSGI-specific request capabilities.
No additional attributes or methods are defined in this subclass.