Commit 7bb0a649 by Zia Fazal Committed by Chris Dodge

Create new Account/Login Audit Log

The API should provide for an audit log when a user creates a new
account or logs into the system.
parent 8772a67f
# pylint: disable=E1101 # pylint: disable=E1101
""" API implementation for session-oriented interactions. """ """ API implementation for session-oriented interactions. """
import logging
from django.conf import settings from django.conf import settings
from django.contrib.auth import authenticate, login from django.contrib.auth import authenticate, login
...@@ -21,6 +22,8 @@ from api_manager.permissions import ApiKeyHeaderPermission ...@@ -21,6 +22,8 @@ from api_manager.permissions import ApiKeyHeaderPermission
from api_manager.serializers import UserSerializer from api_manager.serializers import UserSerializer
from student.models import LoginFailures from student.models import LoginFailures
AUDIT_LOG = logging.getLogger("audit")
def _generate_base_uri(request): def _generate_base_uri(request):
""" """
...@@ -82,6 +85,9 @@ class SessionsList(APIView): ...@@ -82,6 +85,9 @@ class SessionsList(APIView):
response_data['user'] = user_dto.data response_data['user'] = user_dto.data
response_data['uri'] = '{}/{}'.format(base_uri, request.session.session_key) response_data['uri'] = '{}/{}'.format(base_uri, request.session.session_key)
response_status = status.HTTP_201_CREATED response_status = status.HTTP_201_CREATED
# add to audit log
AUDIT_LOG.info(u"API::User logged in successfully with user-id - {0}".format(user.id))
else: else:
response_status = status.HTTP_401_UNAUTHORIZED response_status = status.HTTP_401_UNAUTHORIZED
else: else:
...@@ -91,7 +97,9 @@ class SessionsList(APIView): ...@@ -91,7 +97,9 @@ class SessionsList(APIView):
LoginFailures.increment_lockout_counter(existing_user) LoginFailures.increment_lockout_counter(existing_user)
response_status = status.HTTP_401_UNAUTHORIZED response_status = status.HTTP_401_UNAUTHORIZED
AUDIT_LOG.warn(u"API::User authentication failed with user-id - {0}".format(existing_user.id))
else: else:
AUDIT_LOG.warn(u"API::Failed login attempt with unknown email/username")
response_status = status.HTTP_404_NOT_FOUND response_status = status.HTTP_404_NOT_FOUND
return Response(response_data, status=response_status) return Response(response_data, status=response_status)
...@@ -131,5 +139,9 @@ class SessionsDetail(APIView): ...@@ -131,5 +139,9 @@ class SessionsDetail(APIView):
base_uri = _generate_base_uri(request) base_uri = _generate_base_uri(request)
engine = import_module(settings.SESSION_ENGINE) engine = import_module(settings.SESSION_ENGINE)
session = engine.SessionStore(session_id) session = engine.SessionStore(session_id)
user_id = session[SESSION_KEY]
AUDIT_LOG.info(u"API::User session terminated for user-id - {0}".format(user_id))
session.flush() session.flush()
return Response(response_data, status=status.HTTP_204_NO_CONTENT) return Response(response_data, status=status.HTTP_204_NO_CONTENT)
return Response(response_data, status=status.HTTP_204_NO_CONTENT)
...@@ -27,7 +27,7 @@ from util.password_policy_validators import ( ...@@ -27,7 +27,7 @@ from util.password_policy_validators import (
) )
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
AUDIT_LOG = logging.getLogger("audit")
def _generate_base_uri(request): def _generate_base_uri(request):
""" """
...@@ -144,6 +144,9 @@ class UsersList(APIView): ...@@ -144,6 +144,9 @@ class UsersList(APIView):
password_history_entry = PasswordHistory() password_history_entry = PasswordHistory()
password_history_entry.create(user) password_history_entry.create(user)
# add to audit log
AUDIT_LOG.info(u"API::New account created with user-id - {0}".format(user.id))
# CDODGE: @TODO: We will have to extend this to look in the CourseEnrollmentAllowed table and # CDODGE: @TODO: We will have to extend this to look in the CourseEnrollmentAllowed table and
# auto-enroll students when they create a new account. Also be sure to remove from # auto-enroll students when they create a new account. Also be sure to remove from
# the CourseEnrollmentAllow table after the auto-registration has taken place # the CourseEnrollmentAllow table after the auto-registration has taken place
...@@ -155,6 +158,7 @@ class UsersList(APIView): ...@@ -155,6 +158,7 @@ class UsersList(APIView):
status_code = status.HTTP_409_CONFLICT status_code = status.HTTP_409_CONFLICT
response_data['message'] = "User '%s' already exists", username response_data['message'] = "User '%s' already exists", username
response_data['field_conflict'] = "username" response_data['field_conflict'] = "username"
return Response(response_data, status=status_code) return Response(response_data, status=status_code)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment