root: add errorhandling in log middleware

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-11-15 17:02:53 +01:00
parent f0e9bafa35
commit 2666aa2c73
1 changed files with 2 additions and 4 deletions

View File

@ -11,9 +11,7 @@ from django.http.response import HttpResponse
from django.utils.cache import patch_vary_headers from django.utils.cache import patch_vary_headers
from django.utils.http import http_date from django.utils.http import http_date
from structlog.stdlib import get_logger from structlog.stdlib import get_logger
from typing_extensions import runtime
from authentik.core.middleware import KEY_AUTH_VIA, KEY_USER
from authentik.lib.utils.http import get_client_ip from authentik.lib.utils.http import get_client_ip
LOGGER = get_logger("authentik.asgi") LOGGER = get_logger("authentik.asgi")
@ -71,7 +69,7 @@ class SessionMiddleware(UpstreamSessionMiddleware):
expires = None expires = None
else: else:
max_age = request.session.get_expiry_age() max_age = request.session.get_expiry_age()
expires_time = time.time() + max_age expires_time = time() + max_age
expires = http_date(expires_time) expires = http_date(expires_time)
# Save the session data and refresh the client cookie. # Save the session data and refresh the client cookie.
# Skip session save for 500 responses, refs #3881. # Skip session save for 500 responses, refs #3881.
@ -113,7 +111,7 @@ class LoggingMiddleware:
kwargs = { kwargs = {
"request_id": request.request_id, "request_id": request.request_id,
} }
kwargs.update(response.ak_context) kwargs.update(getattr(response, "ak_context", {}))
self.log(request, status_code, int((time() - start) * 1000), **kwargs) self.log(request, status_code, int((time() - start) * 1000), **kwargs)
return response return response