This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/audit/models.py

76 lines
2.7 KiB
Python
Raw Normal View History

2018-11-23 16:05:41 +00:00
"""passbook audit models"""
from django.conf import settings
2018-12-10 14:26:28 +00:00
from django.contrib.auth.models import AnonymousUser
2019-02-21 15:06:57 +00:00
from django.contrib.postgres.fields import JSONField
from django.core.exceptions import ValidationError
2018-11-23 16:05:41 +00:00
from django.db import models
from django.utils.translation import gettext as _
2019-03-03 19:54:23 +00:00
from ipware import get_client_ip
2019-10-01 08:24:10 +00:00
from structlog import get_logger
2018-11-23 16:05:41 +00:00
from passbook.lib.models import UUIDModel
2018-11-23 16:05:41 +00:00
LOGGER = get_logger()
2018-11-23 16:05:41 +00:00
2019-10-28 13:26:34 +00:00
class Event(UUIDModel):
"""An individual audit log event"""
2018-11-23 16:05:41 +00:00
ACTION_LOGIN = 'login'
ACTION_LOGIN_FAILED = 'login_failed'
ACTION_LOGOUT = 'logout'
ACTION_AUTHORIZE_APPLICATION = 'authorize_application'
ACTION_SUSPICIOUS_REQUEST = 'suspicious_request'
ACTION_SIGN_UP = 'sign_up'
ACTION_PASSWORD_RESET = 'password_reset' # noqa # nosec
2018-12-10 13:21:42 +00:00
ACTION_INVITE_CREATED = 'invitation_created'
ACTION_INVITE_USED = 'invitation_used'
ACTIONS = (
(ACTION_LOGIN, ACTION_LOGIN),
(ACTION_LOGIN_FAILED, ACTION_LOGIN_FAILED),
(ACTION_LOGOUT, ACTION_LOGOUT),
(ACTION_AUTHORIZE_APPLICATION, ACTION_AUTHORIZE_APPLICATION),
(ACTION_SUSPICIOUS_REQUEST, ACTION_SUSPICIOUS_REQUEST),
(ACTION_SIGN_UP, ACTION_SIGN_UP),
(ACTION_PASSWORD_RESET, ACTION_PASSWORD_RESET),
(ACTION_INVITE_CREATED, ACTION_INVITE_CREATED),
(ACTION_INVITE_USED, ACTION_INVITE_USED),
)
2018-11-23 16:05:41 +00:00
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
action = models.TextField(choices=ACTIONS)
2018-11-23 16:05:41 +00:00
date = models.DateTimeField(auto_now_add=True)
app = models.TextField()
2019-02-21 15:06:57 +00:00
context = JSONField(default=dict, blank=True)
request_ip = models.GenericIPAddressField()
2018-12-13 17:01:45 +00:00
created = models.DateTimeField(auto_now_add=True)
@staticmethod
def create(action, request, **kwargs):
2019-10-28 13:26:34 +00:00
"""Create Event from arguments"""
client_ip, _ = get_client_ip(request)
2019-02-26 09:57:05 +00:00
if not hasattr(request, 'user'):
user = None
else:
user = request.user
2018-12-10 14:26:28 +00:00
if isinstance(user, AnonymousUser):
user = kwargs.get('user', None)
2019-10-28 13:26:34 +00:00
entry = Event.objects.create(
action=action,
2018-12-10 14:26:28 +00:00
user=user,
2018-12-10 13:26:10 +00:00
# User 255.255.255.255 as fallback if IP cannot be determined
request_ip=client_ip or '255.255.255.255',
2019-02-21 15:06:57 +00:00
context=kwargs)
LOGGER.debug("Created Audit entry", action=action,
user=user, from_ip=client_ip, context=kwargs)
return entry
2018-11-23 16:05:41 +00:00
def save(self, *args, **kwargs):
if not self._state.adding:
raise ValidationError("you may not edit an existing %s" % self._meta.model_name)
2018-11-23 16:05:41 +00:00
super().save(*args, **kwargs)
class Meta:
verbose_name = _('Audit Entry')
verbose_name_plural = _('Audit Entries')