Repository navigation
feat(Audit logs): Emit authentication audit logs #8931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
7b5ee28
audit logs finally _organised_
emyller d00efb9
first, red
emyller f84b187
Merge remote-tracking branch 'github/main' into feat/org-level-audit-…
emyller 5e649af
`select_related` needs a foreign key
emyller d91ac03
tests go happy
emyller 2b63d2f
fix `is_system_event` filter 😬
emyller 591595e
also in ALL logs
emyller f0156eb
all paths covered
emyller e56f943
tighten up org logs in the all view
emyller 0b47b94
chore: Update documentation artefacts
flagsmith-engineering[bot] a142bac
404
emyller c1ccfd6
accurate api spec
emyller 41fee7b
split migrations
emyller e144ed7
chore: Update documentation artefacts
flagsmith-engineering[bot] d711624
tdd parkour
emyller 3f1b1cf
docs
emyller bfafb82
logout audit log
emyller d8f26c8
better index management
emyller edd4e62
looks good and reads nice
emyller 7a05088
Merge remote-tracking branch 'github/main' into feat/org-level-audit-…
emyller 765fad9
chore: Update documentation artefacts
flagsmith-engineering[bot] 090696c
Merge remote-tracking branch 'github/feat/org-level-audit-logs-pt1' i…
emyller 6600f2d
password change
emyller 640fbde
Merge remote-tracking branch 'github/main' into feat/auth-audit-logs
emyller 0c740b9
fix test module name
emyller af53f26
login audit logs
emyller d4d26b2
2fa toggle audit logs
emyller f280735
better name + saml
emyller 076a51d
audit log when resetting a password
emyller a84cfba
for the sake of clarity
emyller eebf059
refactor
emyller c07ee81
improve events doc
emyller 284a40c
locked out
emyller 756750d
it doesnt work if you retry
emyller c79ea8a
oauth login
emyller b0c99b6
oauth logout
emyller eb13eeb
move out
emyller c9ec50a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 84174c5
chore: Update documentation artefacts
flagsmith-engineering[bot] babcf47
Merge remote-tracking branch 'github/main' into feat/auth-audit-logs
emyller cf2c835
ldap
emyller 5aa8382
prevent field error
emyller 8507021
why would anybody revoke a session token
emyller 2410ac7
move to flagsmith-private
emyller 36f2d29
fix coverage
emyller 17cccbc
more, mOArrrr
emyller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| from typing import Any | ||
|
|
||
| from axes.signals import user_locked_out # type: ignore[import-untyped] | ||
| from django.conf import settings | ||
| from django.contrib.auth.signals import user_logged_out | ||
| from django.dispatch import receiver | ||
| from django.http import HttpRequest | ||
| from oauth2_provider.models import AccessToken, Application | ||
| from oauth2_provider.signals import app_authorized | ||
|
|
||
| from audit.constants import ( | ||
| SSO_PROVIDER_NAMES, | ||
| USER_2FA_DISABLED_MESSAGE, | ||
| USER_2FA_ENABLED_MESSAGE, | ||
| USER_LOCKED_OUT_MESSAGE, | ||
| USER_LOGGED_IN_TO_OAUTH_APPLICATION_MESSAGE, | ||
| USER_LOGGED_IN_WITH_2FA_MESSAGE, | ||
| USER_LOGGED_IN_WITH_SSO_MESSAGE, | ||
| USER_LOGGED_IN_WITHOUT_2FA_MESSAGE, | ||
| USER_LOGGED_OUT_MESSAGE, | ||
| USER_LOGGED_OUT_OF_OAUTH_APPLICATION_MESSAGE, | ||
| USER_LOGIN_2FA_CODE_REJECTED_MESSAGE, | ||
| USER_LOGIN_PASSWORD_REJECTED_MESSAGE, | ||
| USER_LOGIN_REQUIRES_2FA_MESSAGE, | ||
| USER_LOGIN_WHILE_LOCKED_OUT_MESSAGE, | ||
| USER_PASSWORD_CHANGED_MESSAGE, | ||
| ) | ||
| from audit.models import AuditLog | ||
| from audit.types import AxesLoginRequest | ||
| from custom_auth.signals import ( | ||
| user_2fa_disabled, | ||
| user_2fa_enabled, | ||
| user_login_2fa_code_rejected, | ||
| user_login_2fa_code_verified, | ||
| user_login_password_rejected, | ||
| user_login_password_verified, | ||
| user_login_sso_completed, | ||
| user_password_changed, | ||
| ) | ||
| from oauth2_metadata.signals import oauth_session_revoked | ||
| from users.auth_type import AuthType | ||
| from users.models import FFAdminUser | ||
|
|
||
|
|
||
| def _create_user_audit_logs(user: FFAdminUser, log: str) -> None: | ||
| # We don't bulk_create because we need the post_save signal triggered | ||
| for organisation in user.organisations.all(): | ||
| AuditLog.objects.create(organisation=organisation, author=user, log=log) | ||
|
|
||
|
|
||
| @receiver(user_login_password_verified) | ||
| def create_user_login_password_verified_audit_logs( | ||
| sender: type[FFAdminUser], | ||
| user: FFAdminUser, | ||
| is_2fa_required: bool, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| message = ( | ||
| USER_LOGIN_REQUIRES_2FA_MESSAGE | ||
| if is_2fa_required | ||
| else USER_LOGGED_IN_WITHOUT_2FA_MESSAGE | ||
| ) | ||
| _create_user_audit_logs(user, message % user.email) | ||
|
|
||
|
|
||
| @receiver(user_login_password_rejected) | ||
| def create_user_login_password_rejected_audit_logs( | ||
| sender: type[FFAdminUser], | ||
| user: FFAdminUser, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| _create_user_audit_logs(user, USER_LOGIN_PASSWORD_REJECTED_MESSAGE % user.email) | ||
|
|
||
|
|
||
| @receiver(user_login_2fa_code_verified) | ||
| def create_user_login_2fa_code_verified_audit_logs( | ||
| sender: type[FFAdminUser], | ||
| user: FFAdminUser, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| _create_user_audit_logs(user, USER_LOGGED_IN_WITH_2FA_MESSAGE % user.email) | ||
|
|
||
|
|
||
| @receiver(user_login_2fa_code_rejected) | ||
| def create_user_login_2fa_code_rejected_audit_logs( | ||
| sender: type[FFAdminUser], | ||
| user: FFAdminUser, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| _create_user_audit_logs(user, USER_LOGIN_2FA_CODE_REJECTED_MESSAGE % user.email) | ||
|
|
||
|
|
||
| @receiver(user_locked_out) | ||
| def create_user_locked_out_audit_logs( | ||
| sender: str, | ||
| request: AxesLoginRequest, | ||
| username: str, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| try: | ||
| user = FFAdminUser.objects.get_by_natural_key(username) | ||
| except FFAdminUser.DoesNotExist: | ||
| return | ||
| message = ( | ||
| USER_LOGIN_WHILE_LOCKED_OUT_MESSAGE | ||
| if request.axes_failures_since_start > settings.AXES_FAILURE_LIMIT | ||
| else USER_LOCKED_OUT_MESSAGE | ||
| ) | ||
| _create_user_audit_logs(user, message % user.email) | ||
|
|
||
|
|
||
| @receiver(user_login_sso_completed) | ||
| def create_user_login_sso_completed_audit_logs( | ||
| sender: type[FFAdminUser], | ||
| user: FFAdminUser, | ||
| auth_type: AuthType, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| provider_name = SSO_PROVIDER_NAMES[auth_type] | ||
| _create_user_audit_logs( | ||
| user, USER_LOGGED_IN_WITH_SSO_MESSAGE % (user.email, provider_name) | ||
| ) | ||
|
|
||
|
|
||
| @receiver(app_authorized) | ||
| def create_user_oauth_login_audit_logs( | ||
| sender: object, | ||
| request: HttpRequest, | ||
| token: AccessToken, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| if request.POST.get("grant_type") != "authorization_code": | ||
| return # pragma: no cover | ||
| user: FFAdminUser = token.user | ||
| _create_user_audit_logs( | ||
| user, | ||
| USER_LOGGED_IN_TO_OAUTH_APPLICATION_MESSAGE | ||
| % (user.email, token.application.name), | ||
| ) | ||
|
|
||
|
|
||
| @receiver(oauth_session_revoked) | ||
| def create_user_oauth_logout_audit_logs( | ||
| sender: object, | ||
| user: FFAdminUser, | ||
| application: Application, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| _create_user_audit_logs( | ||
| user, | ||
| USER_LOGGED_OUT_OF_OAUTH_APPLICATION_MESSAGE % (user.email, application.name), | ||
| ) | ||
|
|
||
|
|
||
| @receiver(user_2fa_enabled) | ||
| def create_user_2fa_enabled_audit_logs( | ||
| sender: type[FFAdminUser], | ||
| user: FFAdminUser, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| _create_user_audit_logs(user, USER_2FA_ENABLED_MESSAGE % user.email) | ||
|
|
||
|
|
||
| @receiver(user_2fa_disabled) | ||
| def create_user_2fa_disabled_audit_logs( | ||
| sender: type[FFAdminUser], | ||
| user: FFAdminUser, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| _create_user_audit_logs(user, USER_2FA_DISABLED_MESSAGE % user.email) | ||
|
|
||
|
|
||
| @receiver(user_logged_out) | ||
| def create_user_logged_out_audit_logs( | ||
| sender: type[FFAdminUser], | ||
| user: FFAdminUser | None, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| if user is None: | ||
| return | ||
| _create_user_audit_logs(user, USER_LOGGED_OUT_MESSAGE % user.email) | ||
|
|
||
|
|
||
| @receiver(user_password_changed) | ||
| def create_user_password_changed_audit_logs( | ||
| sender: type[FFAdminUser], | ||
| user: FFAdminUser, | ||
| **kwargs: Any, | ||
| ) -> None: | ||
| _create_user_audit_logs(user, USER_PASSWORD_CHANGED_MESSAGE % user.email) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.