Skip to content

Commit 978087a

Browse files
committed
feature #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

src/Event/ScopeResolveEvent.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ final class ScopeResolveEvent extends Event
2727
private $client;
2828

2929
/**
30-
* @var string|null
30+
* @var string|int|null
3131
*/
3232
private $userIdentifier;
3333

3434
/**
3535
* @param list<Scope> $scopes
3636
*/
37-
public function __construct(array $scopes, Grant $grant, AbstractClient $client, ?string $userIdentifier)
37+
public function __construct(array $scopes, Grant $grant, AbstractClient $client, string|int|null $userIdentifier)
3838
{
3939
$this->scopes = $scopes;
4040
$this->grant = $grant;
@@ -68,7 +68,7 @@ public function getClient(): AbstractClient
6868
return $this->client;
6969
}
7070

71-
public function getUserIdentifier(): ?string
71+
public function getUserIdentifier(): string|int|null
7272
{
7373
return $this->userIdentifier;
7474
}

src/EventListener/AddClientDefaultScopesListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
class AddClientDefaultScopesListener
1616
{
1717
/**
18-
* @var list<string>
18+
* @var list<non-empty-string>
1919
*/
2020
private $defaultScopes;
2121

2222
/**
23-
* @param list<string> $defaultScopes
23+
* @param list<non-empty-string> $defaultScopes
2424
*/
2525
public function __construct(array $defaultScopes)
2626
{

src/Model/AbstractClient.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
abstract class AbstractClient implements ClientInterface
1717
{
1818
private string $name;
19+
/** @var non-empty-string */
1920
protected string $identifier;
2021
private ?string $secret;
2122

@@ -33,6 +34,8 @@ abstract class AbstractClient implements ClientInterface
3334

3435
/**
3536
* @psalm-mutation-free
37+
*
38+
* @param non-empty-string $identifier
3639
*/
3740
public function __construct(string $name, string $identifier, ?string $secret)
3841
{

src/Model/ClientInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
interface ClientInterface
1515
{
16+
/**
17+
* @return non-empty-string
18+
*/
1619
public function getIdentifier(): string;
1720

1821
public function getSecret(): ?string;

src/Repository/AccessTokenRepository.php

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ public function __construct(
4242
$this->scopeConverter = $scopeConverter;
4343
}
4444

45-
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null)
45+
public function getNewToken(ClientEntityInterface $clientEntity, array $scopes, ?string $userIdentifier = null): AccessTokenEntityInterface
4646
{
47-
/** @var int|string|null $userIdentifier */
4847
$accessToken = new AccessTokenEntity();
4948
$accessToken->setClient($clientEntity);
50-
$accessToken->setUserIdentifier($userIdentifier);
49+
if (null !== $userIdentifier && '' !== $userIdentifier) {
50+
$accessToken->setUserIdentifier($userIdentifier);
51+
}
5152

5253
foreach ($scopes as $scope) {
5354
$accessToken->addScope($scope);
@@ -69,10 +70,7 @@ public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEnt
6970
$this->accessTokenManager->save($accessToken);
7071
}
7172

72-
/**
73-
* @param string $tokenId
74-
*/
75-
public function revokeAccessToken($tokenId): void
73+
public function revokeAccessToken(string $tokenId): void
7674
{
7775
$accessToken = $this->accessTokenManager->find($tokenId);
7876

@@ -85,10 +83,7 @@ public function revokeAccessToken($tokenId): void
8583
$this->accessTokenManager->save($accessToken);
8684
}
8785

88-
/**
89-
* @param string $tokenId
90-
*/
91-
public function isAccessTokenRevoked($tokenId): bool
86+
public function isAccessTokenRevoked(string $tokenId): bool
9287
{
9388
$accessToken = $this->accessTokenManager->find($tokenId);
9489

@@ -105,9 +100,6 @@ private function buildAccessTokenModel(AccessTokenEntityInterface $accessTokenEn
105100
$client = $this->clientManager->find($accessTokenEntity->getClient()->getIdentifier());
106101

107102
$userIdentifier = $accessTokenEntity->getUserIdentifier();
108-
if (null !== $userIdentifier) {
109-
$userIdentifier = (string) $userIdentifier;
110-
}
111103

112104
return new AccessTokenModel(
113105
$accessTokenEntity->getIdentifier(),

src/Repository/AuthCodeRepository.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ public function getNewAuthCode(): AuthCode
4646
return new AuthCode();
4747
}
4848

49-
/**
50-
* @return void
51-
*/
52-
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
49+
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity): void
5350
{
5451
$authorizationCode = $this->authorizationCodeManager->find($authCodeEntity->getIdentifier());
5552

@@ -62,7 +59,7 @@ public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
6259
$this->authorizationCodeManager->save($authorizationCode);
6360
}
6461

65-
public function revokeAuthCode($codeId): void
62+
public function revokeAuthCode(string $codeId): void
6663
{
6764
$authorizationCode = $this->authorizationCodeManager->find($codeId);
6865

@@ -75,7 +72,7 @@ public function revokeAuthCode($codeId): void
7572
$this->authorizationCodeManager->save($authorizationCode);
7673
}
7774

78-
public function isAuthCodeRevoked($codeId): bool
75+
public function isAuthCodeRevoked(string $codeId): bool
7976
{
8077
$authorizationCode = $this->authorizationCodeManager->find($codeId);
8178

@@ -93,7 +90,7 @@ private function buildAuthorizationCode(AuthCodeEntityInterface $authCodeEntity)
9390

9491
$userIdentifier = $authCodeEntity->getUserIdentifier();
9592
if (null !== $userIdentifier) {
96-
$userIdentifier = (string) $userIdentifier;
93+
$userIdentifier = $userIdentifier;
9794
}
9895

9996
return new AuthorizationCode(

src/Repository/ClientRepository.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use League\Bundle\OAuth2ServerBundle\Entity\Client as ClientEntity;
88
use League\Bundle\OAuth2ServerBundle\Manager\ClientManagerInterface;
99
use League\Bundle\OAuth2ServerBundle\Model\ClientInterface;
10+
use League\OAuth2\Server\Entities\ClientEntityInterface;
1011
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1112

1213
final class ClientRepository implements ClientRepositoryInterface
@@ -21,7 +22,7 @@ public function __construct(ClientManagerInterface $clientManager)
2122
$this->clientManager = $clientManager;
2223
}
2324

24-
public function getClientEntity($clientIdentifier)
25+
public function getClientEntity(string $clientIdentifier): ?ClientEntityInterface
2526
{
2627
$client = $this->clientManager->find($clientIdentifier);
2728

@@ -32,7 +33,7 @@ public function getClientEntity($clientIdentifier)
3233
return $this->buildClientEntity($client);
3334
}
3435

35-
public function validateClient($clientIdentifier, $clientSecret, $grantType): bool
36+
public function validateClient(string $clientIdentifier, ?string $clientSecret, ?string $grantType): bool
3637
{
3738
$client = $this->clientManager->find($clientIdentifier);
3839

0 commit comments

Comments
 (0)