Skip to content

Commit 978087a

Browse files
committed
feature thephpleague#186 Add compatibility with league/oauth2-server:^9 (ajgarlag)
This PR was squashed before being merged into the 0.9-dev branch. Discussion ---------- Add compatibility with `league/oauth2-server:^9` Commits ------- 5e25146 Add compatibility with `league/oauth2-server:^9`
2 parents 7ae9437 + 5e25146 commit 978087a

29 files changed

+147
-118
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"ext-openssl": "*",
2121
"doctrine/doctrine-bundle": "^2.8.0",
2222
"doctrine/orm": "^2.14|^3.0",
23-
"league/oauth2-server": "^8.3",
23+
"league/oauth2-server": "^9",
2424
"nyholm/psr7": "^1.4",
2525
"psr/http-factory": "^1.0",
2626
"symfony/event-dispatcher": "^5.4|^6.2|^7.0",

src/Command/CreateClientCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ private function buildClientFromInput(InputInterface $input): ClientInterface
135135
$client->setActive(true);
136136
$client->setAllowPlainTextPkce($input->getOption('allow-plain-text-pkce'));
137137

138-
/** @var list<string> $redirectUriStrings */
138+
/** @var list<non-empty-string> $redirectUriStrings */
139139
$redirectUriStrings = $input->getOption('redirect-uri');
140-
/** @var list<string> $grantStrings */
140+
/** @var list<non-empty-string> $grantStrings */
141141
$grantStrings = $input->getOption('grant-type');
142-
/** @var list<string> $scopeStrings */
142+
/** @var list<non-empty-string> $scopeStrings */
143143
$scopeStrings = $input->getOption('scope');
144144

145145
return $client

src/Command/ListClientsCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8080

8181
private function getFindByCriteria(InputInterface $input): ClientFilter
8282
{
83-
/** @var list<string> $grantStrings */
83+
/** @var list<non-empty-string> $grantStrings */
8484
$grantStrings = $input->getOption('grant-type');
85-
/** @var list<string> $redirectUriStrings */
85+
/** @var list<non-empty-string> $redirectUriStrings */
8686
$redirectUriStrings = $input->getOption('redirect-uri');
87-
/** @var list<string> $scopeStrings */
87+
/** @var list<non-empty-string> $scopeStrings */
8888
$scopeStrings = $input->getOption('scope');
8989

9090
return ClientFilter::create()

src/Converter/UserConverter.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
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+
1326
/**
1427
* @psalm-suppress DeprecatedMethod
1528
* @psalm-suppress UndefinedInterfaceMethod
@@ -18,9 +31,16 @@ public function toLeague(?UserInterface $user): UserEntityInterface
1831
{
1932
$userEntity = new User();
2033
if ($user instanceof UserInterface) {
21-
$userEntity->setIdentifier(method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername());
34+
$identifier = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
35+
if ('' === $identifier) {
36+
$identifier = $this->anonymousUserIdentifier;
37+
}
38+
} else {
39+
$identifier = $this->anonymousUserIdentifier;
2240
}
2341

42+
$userEntity->setIdentifier($identifier);
43+
2444
return $userEntity;
2545
}
2646
}

src/DBAL/Type/ImplodedArray.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function convertToPHPValue(mixed $value, AbstractPlatform $platform): arr
4949

5050
\assert(\is_string($value), 'Expected $value of be either a string or null.');
5151

52+
/** @var list<non-empty-string> $values */
5253
$values = explode(self::VALUE_DELIMITER, $value);
5354

5455
return $this->convertDatabaseValues($values);
@@ -87,7 +88,7 @@ private function assertValueCanBeImploded($value): void
8788
}
8889

8990
/**
90-
* @param list<string> $values
91+
* @param list<non-empty-string> $values
9192
*
9293
* @return list<T>
9394
*/

src/DBAL/Type/Scope.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getName(): string
2222
}
2323

2424
/**
25-
* @param list<string> $values
25+
* @param list<non-empty-string> $values
2626
*
2727
* @return list<ScopeModel>
2828
*/

src/DependencyInjection/Configuration.php

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

77
use Defuse\Crypto\Key;
8+
use League\Bundle\OAuth2ServerBundle\Converter\UserConverter;
89
use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
910
use League\Bundle\OAuth2ServerBundle\Model\Client;
1011
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
@@ -31,6 +32,11 @@ public function getConfigTreeBuilder(): TreeBuilder
3132
->defaultValue('ROLE_OAUTH2_')
3233
->cannotBeEmpty()
3334
->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()
3440
->end();
3541

3642
return $treeBuilder;

src/DependencyInjection/LeagueOAuth2ServerExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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;
1112
use League\Bundle\OAuth2ServerBundle\DBAL\Type\Grant as GrantType;
1213
use League\Bundle\OAuth2ServerBundle\DBAL\Type\RedirectUri as RedirectUriType;
1314
use League\Bundle\OAuth2ServerBundle\DBAL\Type\Scope as ScopeType;
@@ -68,6 +69,9 @@ public function load(array $configs, ContainerBuilder $container)
6869
$container->findDefinition(OAuth2Authenticator::class)
6970
->setArgument(3, $config['role_prefix']);
7071

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

src/Event/AuthorizationRequestResolveEvent.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use League\Bundle\OAuth2ServerBundle\Model\ClientInterface;
88
use League\Bundle\OAuth2ServerBundle\ValueObject\Scope;
9-
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
9+
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
1010
use Symfony\Component\HttpFoundation\Response;
1111
use Symfony\Component\Security\Core\User\UserInterface;
1212
use Symfony\Contracts\EventDispatcher\Event;
@@ -17,7 +17,7 @@ final class AuthorizationRequestResolveEvent extends Event
1717
public const AUTHORIZATION_DENIED = false;
1818

1919
/**
20-
* @var AuthorizationRequest
20+
* @var AuthorizationRequestInterface
2121
*/
2222
private $authorizationRequest;
2323

@@ -49,7 +49,7 @@ final class AuthorizationRequestResolveEvent extends Event
4949
/**
5050
* @param Scope[] $scopes
5151
*/
52-
public function __construct(AuthorizationRequest $authorizationRequest, array $scopes, ClientInterface $client)
52+
public function __construct(AuthorizationRequestInterface $authorizationRequest, array $scopes, ClientInterface $client)
5353
{
5454
$this->authorizationRequest = $authorizationRequest;
5555
$this->scopes = $scopes;
@@ -137,12 +137,12 @@ public function getState(): ?string
137137
return $this->authorizationRequest->getState();
138138
}
139139

140-
public function getCodeChallenge(): string
140+
public function getCodeChallenge(): ?string
141141
{
142142
return $this->authorizationRequest->getCodeChallenge();
143143
}
144144

145-
public function getCodeChallengeMethod(): string
145+
public function getCodeChallengeMethod(): ?string
146146
{
147147
return $this->authorizationRequest->getCodeChallengeMethod();
148148
}

src/Event/AuthorizationRequestResolveEventFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use League\Bundle\OAuth2ServerBundle\Converter\ScopeConverterInterface;
88
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
9-
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
9+
use League\OAuth2\Server\RequestTypes\AuthorizationRequestInterface;
1010

1111
class AuthorizationRequestResolveEventFactory
1212
{
@@ -26,7 +26,7 @@ public function __construct(ScopeConverterInterface $scopeConverter, ClientManag
2626
$this->clientManager = $clientManager;
2727
}
2828

29-
public function fromAuthorizationRequest(AuthorizationRequest $authorizationRequest): AuthorizationRequestResolveEvent
29+
public function fromAuthorizationRequest(AuthorizationRequestInterface $authorizationRequest): AuthorizationRequestResolveEvent
3030
{
3131
$scopes = $this->scopeConverter->toDomainArray(array_values($authorizationRequest->getScopes()));
3232

0 commit comments

Comments
 (0)