fix(security): emit authentication success events
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -10,6 +10,7 @@ use KTXC\Resource\ProviderManager;
|
||||
use KTXC\Security\Authentication\AuthenticationRequest;
|
||||
use KTXC\Security\AuthenticationManager;
|
||||
use KTXC\Security\Event\AuthenticationFailedEvent;
|
||||
use KTXC\Security\Event\AuthenticationSucceededEvent;
|
||||
use KTXC\Service\TokenService;
|
||||
use KTXC\Service\UserAccountsService;
|
||||
use KTXF\Cache\CacheScope;
|
||||
@@ -24,6 +25,80 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class AuthenticationManagerTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function completedAuthenticationPublishesAuthenticationSucceededEvent(): void
|
||||
{
|
||||
$tenant = $this->createStub(TenantContextInterface::class);
|
||||
$tenant->method('identifier')->willReturn('tenant-a');
|
||||
$tenant->method('configuration')->willReturn(new TenantConfiguration());
|
||||
|
||||
$session = new AuthenticationSession(
|
||||
id: 'auth-session',
|
||||
state: AuthenticationSession::STATE_IDENTIFIED,
|
||||
tenantIdentifier: 'tenant-a',
|
||||
userIdentifier: 'user-a',
|
||||
userIdentity: 'person@example.com',
|
||||
methodsAvailable: ['password'],
|
||||
createdAt: time(),
|
||||
expiresAt: time() + 300,
|
||||
);
|
||||
|
||||
$cache = $this->createMock(EphemeralCacheInterface::class);
|
||||
$cache->expects($this->once())
|
||||
->method('get')
|
||||
->with('auth-session', CacheScope::Tenant, 'auth')
|
||||
->willReturn($session);
|
||||
$cache->expects($this->once())->method('set')->willReturn(true);
|
||||
$cache->expects($this->once())
|
||||
->method('delete')
|
||||
->with('auth-session', CacheScope::Tenant, 'auth')
|
||||
->willReturn(true);
|
||||
|
||||
$provider = new SuccessfulAuthenticationProvider();
|
||||
$providers = $this->createStub(ProviderManager::class);
|
||||
$providers->method('resolve')->willReturn($provider);
|
||||
|
||||
$users = $this->createMock(UserAccountsService::class);
|
||||
$users->expects($this->once())
|
||||
->method('fetchByIdentifier')
|
||||
->with('user-a')
|
||||
->willReturn([
|
||||
'uid' => 'user-a',
|
||||
'identity' => 'person@example.com',
|
||||
'label' => 'Person',
|
||||
'permissions' => [],
|
||||
]);
|
||||
|
||||
$tokens = $this->createStub(TokenService::class);
|
||||
$tokens->method('createToken')->willReturn('token');
|
||||
|
||||
$events = $this->createMock(EventDispatcherInterface::class);
|
||||
$events->expects($this->once())
|
||||
->method('dispatch')
|
||||
->with(self::callback(static fn($event): bool =>
|
||||
$event instanceof AuthenticationSucceededEvent
|
||||
&& $event->getUserId() === 'user-a'
|
||||
&& $event->getTenantId() === 'tenant-a'
|
||||
));
|
||||
|
||||
$manager = new AuthenticationManager(
|
||||
$tenant,
|
||||
$cache,
|
||||
$providers,
|
||||
$tokens,
|
||||
$users,
|
||||
$events,
|
||||
);
|
||||
|
||||
$response = $manager->handle(AuthenticationRequest::verify(
|
||||
'auth-session',
|
||||
'password',
|
||||
'correct',
|
||||
));
|
||||
|
||||
self::assertTrue($response->isSuccess());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function rejectedCredentialPublishesAuthenticationFailedEvent(): void
|
||||
{
|
||||
@@ -145,3 +220,64 @@ final class AuthenticationManagerTest extends TestCase
|
||||
self::assertSame(1, $provider->verificationCount);
|
||||
}
|
||||
}
|
||||
|
||||
final class SuccessfulAuthenticationProvider implements AuthenticationProviderInterface
|
||||
{
|
||||
public function type(): string
|
||||
{
|
||||
return 'authentication';
|
||||
}
|
||||
|
||||
public function identifier(): string
|
||||
{
|
||||
return 'password';
|
||||
}
|
||||
|
||||
public function label(): string
|
||||
{
|
||||
return 'Password';
|
||||
}
|
||||
|
||||
public function description(): string
|
||||
{
|
||||
return 'Successful test provider';
|
||||
}
|
||||
|
||||
public function method(): string
|
||||
{
|
||||
return self::METHOD_CREDENTIAL;
|
||||
}
|
||||
|
||||
public function icon(): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function verify(ProviderContext $context, string $secret): ProviderResult
|
||||
{
|
||||
return ProviderResult::success();
|
||||
}
|
||||
|
||||
public function beginChallenge(ProviderContext $context): ProviderResult
|
||||
{
|
||||
return ProviderResult::failed();
|
||||
}
|
||||
|
||||
public function verifyChallenge(ProviderContext $context, string $code): ProviderResult
|
||||
{
|
||||
return ProviderResult::failed();
|
||||
}
|
||||
|
||||
public function beginRedirect(
|
||||
ProviderContext $context,
|
||||
string $callbackUrl,
|
||||
?string $returnUrl = null,
|
||||
): ProviderResult {
|
||||
return ProviderResult::failed();
|
||||
}
|
||||
|
||||
public function completeRedirect(ProviderContext $context, array $params): ProviderResult
|
||||
{
|
||||
return ProviderResult::failed();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user