Skip to content

Commit 88fb5e9

Browse files
committed
Prevent empty identifier for user entity
1 parent ecc6667 commit 88fb5e9

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/Converter/UserConverter.php

Lines changed: 18 additions & 2 deletions
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
@@ -20,11 +33,14 @@ public function toLeague(?UserInterface $user): UserEntityInterface
2033
if ($user instanceof UserInterface) {
2134
$identifier = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
2235
if ('' === $identifier) {
23-
throw new \RuntimeException('Emtpy identifier not allowed');
36+
$identifier = $this->anonymousUserIdentifier;
2437
}
25-
$userEntity->setIdentifier($identifier);
38+
} else {
39+
$identifier = $this->anonymousUserIdentifier;
2640
}
2741

42+
$userEntity->setIdentifier($identifier);
43+
2844
return $userEntity;
2945
}
3046
}

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

0 commit comments

Comments
 (0)