Skip to content

Commit 1199de7

Browse files
authored
Merge pull request #72 from akrabat/hotfix/63-event-listener-priority
Ensure event listeners have a valid priority when added via the AuthorizationServerFactory
2 parents f37ef8b + 39363a7 commit 1199de7

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/AuthorizationServerFactory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use League\OAuth2\Server\AuthorizationServer;
99
use Psr\Container\ContainerInterface;
1010

11+
use function is_int;
1112
use function is_string;
1213
use function sprintf;
1314

@@ -78,7 +79,7 @@ private function addListeners(
7879
foreach ($listeners as $idx => $listenerConfig) {
7980
$event = $listenerConfig[0];
8081
$listener = $listenerConfig[1];
81-
$priority = $listenerConfig[2] ?? null;
82+
$priority = $listenerConfig[2] ?? 0;
8283
if (is_string($listener)) {
8384
if (! $container->has($listener)) {
8485
throw new Exception\InvalidConfigException(sprintf(
@@ -92,6 +93,14 @@ private function addListeners(
9293
}
9394
$listener = $container->get($listener);
9495
}
96+
if (! is_int($priority)) {
97+
throw new Exception\InvalidConfigException(sprintf(
98+
'The third element of event_listeners config at index "%s" (priority) '
99+
. 'is expected to be an integer, received "%s"',
100+
$idx,
101+
$priority
102+
));
103+
}
95104
$authServer->getEmitter()
96105
->addListener($event, $listener, $priority);
97106
}

test/AuthorizationServerFactoryTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MezzioTest\Authentication\OAuth2;
66

7+
use Laminas\Diactoros\ServerRequest;
78
use League\Event\ListenerInterface;
89
use League\Event\ListenerProviderInterface;
910
use League\OAuth2\Server\AuthorizationServer;
@@ -133,6 +134,43 @@ static function (RequestEvent $event): void {
133134
$result = $factory($mockContainer);
134135

135136
self::assertInstanceOf(AuthorizationServer::class, $result);
137+
138+
// Ensure listeners have been registered correctly. If they have not, then emitting an event will fail
139+
$request = $this->createMock(ServerRequest::class);
140+
$result->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
141+
}
142+
143+
public function testInvokeWithListenerConfigFailsIfPriorityIsNotAnInteger(): void
144+
{
145+
$mockContainer = $this->getContainerMock();
146+
$mockListener = $this->createMock(ListenerInterface::class);
147+
$mockContainer->set(ListenerInterface::class, $mockListener);
148+
149+
$config = [
150+
'authentication' => [
151+
'private_key' => __DIR__ . '/TestAsset/private.key',
152+
'encryption_key' => 'iALlwJ1sH77dmFCJFo+pMdM6Af4bF/hCca1EDDx7MwE=',
153+
'access_token_expire' => 'P1D',
154+
'grants' => [
155+
ClientCredentialsGrant::class => ClientCredentialsGrant::class,
156+
],
157+
'event_listeners' => [
158+
[
159+
RequestEvent::CLIENT_AUTHENTICATION_FAILED,
160+
ListenerInterface::class,
161+
'one',
162+
],
163+
],
164+
],
165+
];
166+
167+
$mockContainer->set('config', $config);
168+
169+
$factory = new AuthorizationServerFactory();
170+
171+
$this->expectException(InvalidConfigException::class);
172+
173+
$factory($mockContainer);
136174
}
137175

138176
public function testInvokeWithListenerConfigMissingServiceThrowsException(): void

0 commit comments

Comments
 (0)