Skip to content

Commit 39363a7

Browse files
committed
Ensure listener config priorty is an integer
Default to zero if not set and throw an exception if priority is not an integer.
1 parent 0df81b0 commit 39363a7

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,39 @@ static function (RequestEvent $event): void {
140140
$result->getEmitter()->emit(new RequestEvent(RequestEvent::CLIENT_AUTHENTICATION_FAILED, $request));
141141
}
142142

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);
174+
}
175+
143176
public function testInvokeWithListenerConfigMissingServiceThrowsException(): void
144177
{
145178
$mockContainer = $this->getContainerMock();

0 commit comments

Comments
 (0)