Skip to content

Commit 44272ff

Browse files
committed
feature #196 Require a logged in user to resolve an authorization request (ajgarlag)
This PR was squashed before being merged into the 0.9-dev branch. Discussion ---------- Require a logged in user to resolve an authorization request Introduces BC breaks Fix #195 Commits ------- aeaa2d3 Require a logged in user to resolve an authorization request
2 parents 978087a + aeaa2d3 commit 44272ff

12 files changed

+122
-146
lines changed

src/Converter/UserConverter.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,16 @@
1010

1111
final class UserConverter implements UserConverterInterface
1212
{
13-
public const DEFAULT_ANONYMOUS_USER_IDENTIFIER = 'anonymous';
14-
15-
/** @var non-empty-string */
16-
private string $anonymousUserIdentifier;
17-
18-
/**
19-
* @param non-empty-string $anonymousUserIdentifier
20-
*/
21-
public function __construct(string $anonymousUserIdentifier = self::DEFAULT_ANONYMOUS_USER_IDENTIFIER)
22-
{
23-
$this->anonymousUserIdentifier = $anonymousUserIdentifier;
24-
}
25-
2613
/**
14+
* @psalm-suppress ArgumentTypeCoercion
2715
* @psalm-suppress DeprecatedMethod
2816
* @psalm-suppress UndefinedInterfaceMethod
2917
*/
30-
public function toLeague(?UserInterface $user): UserEntityInterface
18+
public function toLeague(UserInterface $user): UserEntityInterface
3119
{
3220
$userEntity = new User();
33-
if ($user instanceof UserInterface) {
34-
$identifier = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
35-
if ('' === $identifier) {
36-
$identifier = $this->anonymousUserIdentifier;
37-
}
38-
} else {
39-
$identifier = $this->anonymousUserIdentifier;
40-
}
21+
22+
$identifier = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
4123

4224
$userEntity->setIdentifier($identifier);
4325

src/Converter/UserConverterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99

1010
interface UserConverterInterface
1111
{
12-
public function toLeague(?UserInterface $user): UserEntityInterface;
12+
public function toLeague(UserInterface $user): UserEntityInterface;
1313
}

src/DependencyInjection/Configuration.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace League\Bundle\OAuth2ServerBundle\DependencyInjection;
66

77
use Defuse\Crypto\Key;
8-
use League\Bundle\OAuth2ServerBundle\Converter\UserConverter;
98
use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
109
use League\Bundle\OAuth2ServerBundle\Model\Client;
1110
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
@@ -32,11 +31,6 @@ public function getConfigTreeBuilder(): TreeBuilder
3231
->defaultValue('ROLE_OAUTH2_')
3332
->cannotBeEmpty()
3433
->end()
35-
->scalarNode('anonymous_user_identifier')
36-
->info('Set a default user identifier for anonymous users')
37-
->defaultValue(UserConverter::DEFAULT_ANONYMOUS_USER_IDENTIFIER)
38-
->cannotBeEmpty()
39-
->end()
4034
->end();
4135

4236
return $treeBuilder;

src/DependencyInjection/LeagueOAuth2ServerExtension.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantTypeInterface;
99
use League\Bundle\OAuth2ServerBundle\Command\CreateClientCommand;
1010
use League\Bundle\OAuth2ServerBundle\Command\GenerateKeyPairCommand;
11-
use League\Bundle\OAuth2ServerBundle\Converter\UserConverter;
1211
use League\Bundle\OAuth2ServerBundle\DBAL\Type\Grant as GrantType;
1312
use League\Bundle\OAuth2ServerBundle\DBAL\Type\RedirectUri as RedirectUriType;
1413
use League\Bundle\OAuth2ServerBundle\DBAL\Type\Scope as ScopeType;
@@ -69,9 +68,6 @@ public function load(array $configs, ContainerBuilder $container)
6968
$container->findDefinition(OAuth2Authenticator::class)
7069
->setArgument(3, $config['role_prefix']);
7170

72-
$container->findDefinition(UserConverter::class)
73-
->setArgument(0, $config['anonymous_user_identifier']);
74-
7571
$container->registerForAutoconfiguration(GrantTypeInterface::class)
7672
->addTag('league.oauth2_server.authorization_server.grant');
7773

src/Event/AuthorizationRequestResolveEvent.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,19 @@ final class AuthorizationRequestResolveEvent extends Event
4242
private $response;
4343

4444
/**
45-
* @var UserInterface|null
45+
* @var UserInterface
4646
*/
4747
private $user;
4848

4949
/**
5050
* @param Scope[] $scopes
5151
*/
52-
public function __construct(AuthorizationRequestInterface $authorizationRequest, array $scopes, ClientInterface $client)
52+
public function __construct(AuthorizationRequestInterface $authorizationRequest, array $scopes, ClientInterface $client, UserInterface $user)
5353
{
5454
$this->authorizationRequest = $authorizationRequest;
5555
$this->scopes = $scopes;
5656
$this->client = $client;
57+
$this->user = $user;
5758
}
5859

5960
public function getAuthorizationResolution(): bool
@@ -102,18 +103,11 @@ public function getClient(): ClientInterface
102103
/**
103104
* @psalm-mutation-free
104105
*/
105-
public function getUser(): ?UserInterface
106+
public function getUser(): UserInterface
106107
{
107108
return $this->user;
108109
}
109110

110-
public function setUser(?UserInterface $user): self
111-
{
112-
$this->user = $user;
113-
114-
return $this;
115-
}
116-
117111
/**
118112
* @return Scope[]
119113
*/

src/Event/AuthorizationRequestResolveEventFactory.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,31 @@
66

77
use League\Bundle\OAuth2ServerBundle\Converter\ScopeConverterInterface;
88
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
9-
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
9+
use Symfony\Bundle\SecurityBundle\Security;
10+
use Symfony\Component\Security\Core\Security as LegacySecurity;
1011

11-
class AuthorizationRequestResolveEventFactory
12-
{
13-
/**
14-
* @var ScopeConverterInterface
15-
*/
16-
private $scopeConverter;
17-
18-
/**
19-
* @var ClientManagerInterface
20-
*/
21-
private $clientManager;
22-
23-
public function __construct(ScopeConverterInterface $scopeConverter, ClientManagerInterface $clientManager)
12+
if (class_exists(Security::class)) {
13+
final class AuthorizationRequestResolveEventFactory
2414
{
25-
$this->scopeConverter = $scopeConverter;
26-
$this->clientManager = $clientManager;
27-
}
15+
use AuthorizationRequestResolveEventFactoryTrait;
2816

29-
public function fromAuthorizationRequest(AuthorizationRequestInterface $authorizationRequest): AuthorizationRequestResolveEvent
17+
public function __construct(ScopeConverterInterface $scopeConverter, ClientManagerInterface $clientManager, Security $security)
18+
{
19+
$this->scopeConverter = $scopeConverter;
20+
$this->clientManager = $clientManager;
21+
$this->security = $security;
22+
}
23+
}
24+
} else {
25+
final class AuthorizationRequestResolveEventFactory
3026
{
31-
$scopes = $this->scopeConverter->toDomainArray(array_values($authorizationRequest->getScopes()));
32-
33-
$client = $this->clientManager->find($authorizationRequest->getClient()->getIdentifier());
27+
use AuthorizationRequestResolveEventFactoryTrait;
3428

35-
if (null === $client) {
36-
throw new \RuntimeException(\sprintf('No client found for the given identifier \'%s\'.', $authorizationRequest->getClient()->getIdentifier()));
29+
public function __construct(ScopeConverterInterface $scopeConverter, ClientManagerInterface $clientManager, LegacySecurity $security)
30+
{
31+
$this->scopeConverter = $scopeConverter;
32+
$this->clientManager = $clientManager;
33+
$this->security = $security;
3734
}
38-
39-
return new AuthorizationRequestResolveEvent($authorizationRequest, $scopes, $client);
4035
}
4136
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace League\Bundle\OAuth2ServerBundle\Event;
6+
7+
use League\Bundle\OAuth2ServerBundle\Converter\ScopeConverterInterface;
8+
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
9+
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
10+
use Symfony\Bundle\SecurityBundle\Security;
11+
use Symfony\Component\Security\Core\Security as LegacySecurity;
12+
13+
/**
14+
* @internal
15+
*/
16+
trait AuthorizationRequestResolveEventFactoryTrait
17+
{
18+
/**
19+
* @var ScopeConverterInterface
20+
*/
21+
private $scopeConverter;
22+
23+
/**
24+
* @var ClientManagerInterface
25+
*/
26+
private $clientManager;
27+
28+
/**
29+
* @var Security|LegacySecurity
30+
*/
31+
private $security;
32+
33+
public function fromAuthorizationRequest(AuthorizationRequestInterface $authorizationRequest): AuthorizationRequestResolveEvent
34+
{
35+
$scopes = $this->scopeConverter->toDomainArray(array_values($authorizationRequest->getScopes()));
36+
37+
$client = $this->clientManager->find($authorizationRequest->getClient()->getIdentifier());
38+
39+
if (null === $client) {
40+
throw new \RuntimeException(\sprintf('No client found for the given identifier \'%s\'.', $authorizationRequest->getClient()->getIdentifier()));
41+
}
42+
43+
$user = $this->security->getUser();
44+
if (null === $user) {
45+
throw new \RuntimeException('A logged in user is required to resolve the authorization request.');
46+
}
47+
48+
return new AuthorizationRequestResolveEvent($authorizationRequest, $scopes, $client, $user);
49+
}
50+
}

src/EventListener/AuthorizationRequestUserResolvingListener.php

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/EventListener/AuthorizationRequestUserResolvingListenerTrait.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Resources/config/services.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use League\Bundle\OAuth2ServerBundle\Converter\UserConverterInterface;
2323
use League\Bundle\OAuth2ServerBundle\Event\AuthorizationRequestResolveEventFactory;
2424
use League\Bundle\OAuth2ServerBundle\EventListener\AddClientDefaultScopesListener;
25-
use League\Bundle\OAuth2ServerBundle\EventListener\AuthorizationRequestUserResolvingListener;
2625
use League\Bundle\OAuth2ServerBundle\Manager\AccessTokenManagerInterface;
2726
use League\Bundle\OAuth2ServerBundle\Manager\AuthorizationCodeManagerInterface;
2827
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
@@ -55,9 +54,11 @@
5554
use Nyholm\Psr7\Factory\Psr17Factory;
5655
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
5756
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
57+
use Symfony\Bundle\SecurityBundle\Security;
5858
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
5959
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6060
use Symfony\Component\HttpFoundation\RequestStack;
61+
use Symfony\Component\Security\Core\Security as LegacySecurity;
6162

6263
return static function (ContainerConfigurator $container): void {
6364
$container->services()
@@ -206,18 +207,6 @@
206207
->tag('controller.service_arguments')
207208
->alias(AuthorizationController::class, 'league.oauth2_server.controller.authorization')
208209

209-
// Authorization listeners
210-
->set('league.oauth2_server.listener.authorization_request_user_resolving', AuthorizationRequestUserResolvingListener::class)
211-
->args([
212-
service('security.helper'),
213-
])
214-
->tag('kernel.event_listener', [
215-
'event' => OAuth2Events::AUTHORIZATION_REQUEST_RESOLVE,
216-
'method' => 'onAuthorizationRequest',
217-
'priority' => 1024,
218-
])
219-
->alias(AuthorizationRequestUserResolvingListener::class, 'league.oauth2_server.listener.authorization_request_user_resolving')
220-
221210
// Token controller
222211
->set('league.oauth2_server.controller.token', TokenController::class)
223212
->args([
@@ -292,6 +281,7 @@
292281
->args([
293282
service(ScopeConverterInterface::class),
294283
service(ClientManagerInterface::class),
284+
service(class_exists(Security::class) ? Security::class : LegacySecurity::class),
295285
])
296286
->alias(AuthorizationRequestResolveEventFactory::class, 'league.oauth2_server.factory.authorization_request_resolve_event')
297287

0 commit comments

Comments
 (0)