Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions auditing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
user_login_failed,
user_logged_out)
from django.dispatch import receiver
from django.conf import settings

from . utils import get_request_info


logger = logging.getLogger(__name__)

USER_FIELD = getattr(settings, 'AUDIT_USERNAME_FIELD', 'username')


@receiver(user_logged_in)
def login_logger(sender, request, **kwargs):
_msg = '"Django Login successful", "username": "{}", {}'
logger.info(_msg.format(request.POST['username'],
logger.info(_msg.format(request.POST[USER_FIELD],
get_request_info(request)))


@receiver(user_login_failed)
def login_failed_logger(sender, request, **kwargs):
_msg = '"Django Login failed", "username": "{}", {}'
logger.warn(_msg.format(request.POST['username'],
logger.warn(_msg.format(request.POST[USER_FIELD],
get_request_info(request)))


Expand Down
8 changes: 7 additions & 1 deletion auditing/middlewares.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import http
import logging
import re
import json

from django.conf import settings
try:
Expand Down Expand Up @@ -49,7 +50,12 @@ def process_response(self, request, response):
response_headers = [(str(k), str(v)) for k, v in response.items()]
for cookie in response.cookies.values():
response_headers.append(('Set-Cookie', str(cookie.output(header=''))))
head_items = ['"{}": "{}"'.format(*hea) for hea in response_headers]

head_items = [
# Use json.dumps on value field below to ensure valid json
'"{}": "{}"'.format(k, json.dumps(v))
for k, v in response_headers
]
headers = ', '.join(head_items)
_msg = '"Http Response", "method": "{}", "url": "{}", "status": "{}", {}'.format(request.method,
request.build_absolute_uri(),
Expand Down