feat: back-channel logout to client apps
This commit is contained in:
1 parent
3247df1171
commit
aebfc2ca9b
4 files changed
+303
No files matched your search
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\EventListener;
|
||||
|
||||
use App\Entity\OAuth2Client;
|
||||
use App\Entity\User;
|
||||
use App\EventListener\BackchannelLogoutListener;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpClient\MockHttpClient;
|
||||
use Symfony\Component\HttpClient\Response\MockResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
use Symfony\Component\Security\Http\Event\LogoutEvent;
|
||||
|
||||
class BackchannelLogoutListenerTest extends TestCase
|
||||
{
|
||||
public function testPostsTheSubFormEncodedWithTheClientSecret(): void
|
||||
{
|
||||
$user = new User('[email protected]');
|
||||
$calls = [];
|
||||
$http = new MockHttpClient(static function (string $method, string $url, array $options) use (&$calls): MockResponse {
|
||||
$calls[] = compact('method', 'url', 'options');
|
||||
|
||||
return new MockResponse('', ['http_code' => 200]);
|
||||
});
|
||||
|
||||
$this->listener([$this->client('https://ephub.test/auth/backchannel-logout', 's3cret')], $http)
|
||||
->__invoke($this->event($user));
|
||||
|
||||
self::assertCount(1, $calls);
|
||||
self::assertSame('POST', $calls[0]['method']);
|
||||
self::assertSame('https://ephub.test/auth/backchannel-logout', $calls[0]['url']);
|
||||
self::assertContains('Authorization: Bearer s3cret', $calls[0]['options']['headers']);
|
||||
self::assertContains('Content-Type: application/x-www-form-urlencoded', $calls[0]['options']['headers']);
|
||||
self::assertSame('sub='.$user->getUuid(), $calls[0]['options']['body']);
|
||||
}
|
||||
|
||||
public function testFailingClientIsLoggedAndTheOthersAreStillCalled(): void
|
||||
{
|
||||
$calls = 0;
|
||||
$http = new MockHttpClient([
|
||||
static function () use (&$calls): MockResponse {
|
||||
++$calls;
|
||||
|
||||
return new MockResponse('', ['error' => 'Connection timed out']);
|
||||
},
|
||||
static function () use (&$calls): MockResponse {
|
||||
++$calls;
|
||||
|
||||
return new MockResponse('', ['http_code' => 401]);
|
||||
},
|
||||
static function () use (&$calls): MockResponse {
|
||||
++$calls;
|
||||
|
||||
return new MockResponse('', ['http_code' => 200]);
|
||||
},
|
||||
]);
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$logger->expects(self::exactly(2))->method('warning');
|
||||
|
||||
$this->listener([
|
||||
$this->client('https://a.test/logout', 'a'),
|
||||
$this->client('https://b.test/logout', 'b'),
|
||||
$this->client('https://c.test/logout', 'c'),
|
||||
], $http, $logger)->__invoke($this->event(new User('[email protected]')));
|
||||
|
||||
self::assertSame(3, $calls);
|
||||
}
|
||||
|
||||
public function testMalformedUrlIsLoggedInsteadOfThrown(): void
|
||||
{
|
||||
$logger = $this->createMock(LoggerInterface::class);
|
||||
$logger->expects(self::once())->method('warning');
|
||||
|
||||
$this->listener([$this->client('ftp://ephub.test/logout', 'a')], new MockHttpClient(), $logger)
|
||||
->__invoke($this->event(new User('[email protected]')));
|
||||
}
|
||||
|
||||
public function testLogoutAfterAnExternalAuthorizationTellsNobody(): void
|
||||
{
|
||||
$http = new MockHttpClient(static fn (): MockResponse => self::fail('No client may be called'));
|
||||
|
||||
$this->listener([$this->client('https://ephub.test/logout', 's')], $http)
|
||||
->__invoke($this->event(new User('[email protected]'), external: true));
|
||||
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
|
||||
public function testLogoutWithoutTokenTellsNobody(): void
|
||||
{
|
||||
$http = new MockHttpClient(static fn (): MockResponse => self::fail('No client may be called'));
|
||||
|
||||
$this->listener([$this->client('https://ephub.test/logout', 's')], $http)
|
||||
->__invoke(new LogoutEvent(new Request(), null));
|
||||
|
||||
$this->addToAssertionCount(1);
|
||||
}
|
||||
|
||||
private function client(string $logoutUrl, string $logoutSecret): OAuth2Client
|
||||
{
|
||||
return (new OAuth2Client('EPHub', md5($logoutUrl), 'secret'))
|
||||
->setLogoutUrl($logoutUrl)
|
||||
->setLogoutSecret($logoutSecret);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<OAuth2Client> $clients
|
||||
*/
|
||||
private function listener(array $clients, MockHttpClient $http, ?LoggerInterface $logger = null): BackchannelLogoutListener
|
||||
{
|
||||
$repository = self::createStub(EntityRepository::class);
|
||||
$repository->method('findAll')->willReturn($clients);
|
||||
$entityManager = self::createStub(EntityManagerInterface::class);
|
||||
$entityManager->method('getRepository')->willReturn($repository);
|
||||
|
||||
return new BackchannelLogoutListener($entityManager, $http, $logger ?? self::createStub(LoggerInterface::class));
|
||||
}
|
||||
|
||||
private function event(User $user, bool $external = false): LogoutEvent
|
||||
{
|
||||
$request = new Request();
|
||||
$session = new Session(new MockArraySessionStorage());
|
||||
$session->set('_oauth2', $external);
|
||||
$request->setSession($session);
|
||||
|
||||
return new LogoutEvent($request, new UsernamePasswordToken($user, 'main', $user->getRoles()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user