Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
Description
Enable the creation of access logs when users are authenticated.
Notes
Adds a method to the AuditLog class to create from a request, with logic to handle regular logins, non-persisted authenticated requests, and logins via django-loginas. This is meant to be called from a user_logged_in signal handler, which passes the request along, or an authentication class. Technically it can be called with any request but it will only log the authentication of the user, not any action they performed.
For any request, it will store the ip, OS, and browser of the request (taken from the headers) as well as information about the user in request.user. The user id is also stored as the object_id, with the object_type being the name of the user model.
If the url_name of the resolver is 'loginas-user-login,' we also store information about the _cached_user, which represents the user making the original request. This should work even in the case of cURL requests since they require a CSRF token and session id, so the original user gets fully authenticated.
The authorization type can be set by the caller, otherwise it will default to 'django-loginas' if it's a loginas request and the name of the authenticating backend if it's a normal login request.
Related issues
Blocked by #5035 and #5044