Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/admin_audit/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
use OCP\Authentication\Events\AnyLoginFailedEvent;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengeFailed;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengePassed;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered;
use OCP\Console\ConsoleEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Cache\CacheEntryInsertedEvent;
Expand Down Expand Up @@ -118,6 +120,8 @@ public function register(IRegistrationContext $context): void {
// Security events
$context->registerEventListener(TwoFactorProviderChallengePassed::class, SecurityEventListener::class);
$context->registerEventListener(TwoFactorProviderChallengeFailed::class, SecurityEventListener::class);
$context->registerEventListener(TwoFactorProviderForUserRegistered::class, SecurityEventListener::class);
$context->registerEventListener(TwoFactorProviderForUserUnregistered::class, SecurityEventListener::class);

// App management events
$context->registerEventListener(AppEnableEvent::class, AppManagementEventListener::class);
Expand Down
40 changes: 39 additions & 1 deletion apps/admin_audit/lib/Listener/SecurityEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@
use OCA\AdminAudit\Actions\Action;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengeFailed;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengePassed;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;

/**
* @template-implements IEventListener<TwoFactorProviderChallengePassed|TwoFactorProviderChallengeFailed>
* @template-implements IEventListener<TwoFactorProviderChallengePassed|TwoFactorProviderChallengeFailed|TwoFactorProviderForUserRegistered|TwoFactorProviderForUserUnregistered>
*/
class SecurityEventListener extends Action implements IEventListener {
public function handle(Event $event): void {
if ($event instanceof TwoFactorProviderChallengePassed) {
$this->twoFactorProviderChallengePassed($event);
} elseif ($event instanceof TwoFactorProviderChallengeFailed) {
$this->twoFactorProviderChallengeFailed($event);
} elseif ($event instanceof TwoFactorProviderForUserRegistered) {
$this->twoFactorProviderForUserRegistered($event);
} elseif ($event instanceof TwoFactorProviderForUserUnregistered) {
$this->twoFactorProviderForUserUnregistered($event);
}
}

Expand Down Expand Up @@ -58,4 +64,36 @@ private function twoFactorProviderChallengeFailed(TwoFactorProviderChallengeFail
]
);
}

private function twoFactorProviderForUserRegistered(TwoFactorProviderForUserRegistered $event): void {
$this->log(
'Two factor provider %s enabled for user %s (%s)',
[
'provider' => $event->getProvider()->getDisplayName(),
'uid' => $event->getUser()->getUID(),
'displayName' => $event->getUser()->getDisplayName()
],
[
'provider',
'uid',
'displayName',
Comment on lines +78 to +79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'uid',
'displayName',
'displayName',
'uid',

or the other way around in apps/admin_audit/tests/Listener/SecurityEventListenerTest.php

]
);
}

private function twoFactorProviderForUserUnregistered(TwoFactorProviderForUserUnregistered $event): void {
$this->log(
'Two factor provider %s disabled for user %s (%s)',
[
'provider' => $event->getProvider()->getDisplayName(),
'uid' => $event->getUser()->getUID(),
'displayName' => $event->getUser()->getDisplayName()
],
[
'provider',
'uid',
'displayName',
Comment on lines +94 to +95
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'uid',
'displayName',
'displayName',
'uid',

]
);
}
}
24 changes: 24 additions & 0 deletions apps/admin_audit/tests/Listener/SecurityEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use OCP\Authentication\TwoFactorAuth\IProvider;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengeFailed;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderChallengePassed;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered;
use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
Expand Down Expand Up @@ -62,4 +64,26 @@ public function testTwofactorSuccess(): void {

$this->security->handle(new TwoFactorProviderChallengePassed($this->user, $this->provider));
}

public function testTwofactorRegistered(): void {
$this->logger->expects($this->once())
->method('info')
->with(
$this->equalTo('Two factor provider myprovider enabled for user mydisplayname (myuid)'),
['app' => 'admin_audit']
);

$this->security->handle(new TwoFactorProviderForUserRegistered($this->user, $this->provider));
}

public function testTwofactorUnregistered(): void {
$this->logger->expects($this->once())
->method('info')
->with(
$this->equalTo('Two factor provider myprovider disabled for user mydisplayname (myuid)'),
['app' => 'admin_audit']
);

$this->security->handle(new TwoFactorProviderForUserUnregistered($this->user, $this->provider));
}
}
Loading