Skip to content

Commit

Permalink
feat(admin_audit): write admin audit log for AnyLoginFailedEvent
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Feb 6, 2025
1 parent 7af66ea commit b889d0b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
2 changes: 2 additions & 0 deletions apps/admin_audit/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Authentication\Events\AnyLoginFailedEvent;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengeFailed;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengePassed;
use OCP\Console\ConsoleEvent;
Expand Down Expand Up @@ -105,6 +106,7 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(UserLoggedInWithCookieEvent::class, AuthEventListener::class);
$context->registerEventListener(UserLoggedInEvent::class, AuthEventListener::class);
$context->registerEventListener(BeforeUserLoggedOutEvent::class, AuthEventListener::class);
$context->registerEventListener(AnyLoginFailedEvent::class, AuthEventListener::class);

// File events
$context->registerEventListener(BeforePreviewFetchedEvent::class, FileEventListener::class);
Expand Down
18 changes: 17 additions & 1 deletion apps/admin_audit/lib/Listener/AuthEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\AdminAudit\Listener;

use OCA\AdminAudit\Actions\Action;
use OCP\Authentication\Events\AnyLoginFailedEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\User\Events\BeforeUserLoggedInEvent;
Expand All @@ -18,7 +19,7 @@
use OCP\User\Events\UserLoggedInWithCookieEvent;

/**
* @template-implements IEventListener<BeforeUserLoggedInEvent|UserLoggedInWithCookieEvent|UserLoggedInEvent|BeforeUserLoggedOutEvent>
* @template-implements IEventListener<BeforeUserLoggedInEvent|UserLoggedInWithCookieEvent|UserLoggedInEvent|BeforeUserLoggedOutEvent|AnyLoginFailedEvent>
*/
class AuthEventListener extends Action implements IEventListener {
public function handle(Event $event): void {
Expand All @@ -28,6 +29,8 @@ public function handle(Event $event): void {
$this->userLoggedIn($event);
} elseif ($event instanceof BeforeUserLoggedOutEvent) {
$this->beforeUserLogout($event);
} elseif ($event instanceof AnyLoginFailedEvent) {
$this->anyLoginFailed($event);
}
}

Expand Down Expand Up @@ -64,4 +67,17 @@ private function beforeUserLogout(BeforeUserLoggedOutEvent $event): void {
[]
);
}

private function anyLoginFailed(AnyLoginFailedEvent $event): void {
$this->log(
'Login failed: "%s"',
[
'loginName' => $event->getLoginName()
],
[
'loginName',
],
true
);
}
}
2 changes: 1 addition & 1 deletion lib/public/Authentication/Events/AnyLoginFailedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string $loginName, ?string $password) {
/**
* @since 26.0.0
*/
public function geLoginName(): string {
public function getLoginName(): string {
return $this->loginName;
}

Expand Down

0 comments on commit b889d0b

Please sign in to comment.