Skip to content

Commit

Permalink
Prevent empty identifier for user entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgarlag committed May 16, 2024
1 parent 98651dd commit d2ebd4f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Converter/UserConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@

final class UserConverter implements UserConverterInterface
{
public const DEFAULT_ANONYMOUS_USER_IDENTIFIER = 'anonymous';

/** @var non-empty-string */
private string $anonymousUserIdentifier;

/**
* @param non-empty-string $anonymousUserIdentifier
*/
public function __construct(string $anonymousUserIdentifier = self::DEFAULT_ANONYMOUS_USER_IDENTIFIER)
{
$this->anonymousUserIdentifier = $anonymousUserIdentifier;
}

/**
* @psalm-suppress DeprecatedMethod
* @psalm-suppress UndefinedInterfaceMethod
Expand All @@ -20,11 +33,14 @@ public function toLeague(?UserInterface $user): UserEntityInterface
if ($user instanceof UserInterface) {
$identifier = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();
if ('' === $identifier) {
throw new \RuntimeException('Emtpy identifier not allowed');
$identifier = $this->anonymousUserIdentifier;
}
$userEntity->setIdentifier($identifier);
} else {
$identifier = $this->anonymousUserIdentifier;
}

$userEntity->setIdentifier($identifier);

return $userEntity;
}
}
6 changes: 6 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace League\Bundle\OAuth2ServerBundle\DependencyInjection;

use Defuse\Crypto\Key;
use League\Bundle\OAuth2ServerBundle\Converter\UserConverter;
use League\Bundle\OAuth2ServerBundle\Model\AbstractClient;
use League\Bundle\OAuth2ServerBundle\Model\Client;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
Expand All @@ -31,6 +32,11 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultValue('ROLE_OAUTH2_')
->cannotBeEmpty()
->end()
->scalarNode('anonymous_user_identifier')
->info('Set a default user identifier for anonymous users')
->defaultValue(UserConverter::DEFAULT_ANONYMOUS_USER_IDENTIFIER)
->cannotBeEmpty()
->end()
->end();

return $treeBuilder;
Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/LeagueOAuth2ServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use League\Bundle\OAuth2ServerBundle\AuthorizationServer\GrantTypeInterface;
use League\Bundle\OAuth2ServerBundle\Command\CreateClientCommand;
use League\Bundle\OAuth2ServerBundle\Command\GenerateKeyPairCommand;
use League\Bundle\OAuth2ServerBundle\Converter\UserConverter;
use League\Bundle\OAuth2ServerBundle\DBAL\Type\Grant as GrantType;
use League\Bundle\OAuth2ServerBundle\DBAL\Type\RedirectUri as RedirectUriType;
use League\Bundle\OAuth2ServerBundle\DBAL\Type\Scope as ScopeType;
Expand Down Expand Up @@ -68,6 +69,9 @@ public function load(array $configs, ContainerBuilder $container)
$container->findDefinition(OAuth2Authenticator::class)
->setArgument(3, $config['role_prefix']);

$container->findDefinition(UserConverter::class)
->setArgument(0, $config['anonymous_user_identifier']);

$container->registerForAutoconfiguration(GrantTypeInterface::class)
->addTag('league.oauth2_server.authorization_server.grant');

Expand Down

0 comments on commit d2ebd4f

Please sign in to comment.