Skip to content

Commit

Permalink
Deprecate empty user identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgarlag committed Aug 19, 2024
1 parent c555d3a commit cf7f816
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Authenticator/Passport/Badge/UserBadge.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function __construct(
?callable $userLoader = null,
private ?array $attributes = null,
) {
if ('' === $userIdentifier) {
trigger_deprecation('symfony/security-http', '7.2', 'Using an empty string as user identifier is deprecated and will throw an exception in Symfony 8.0.');
// throw new BadCredentialsException('Empty user identifier.');
}

if (\strlen($userIdentifier) > self::MAX_USERNAME_LENGTH) {
throw new BadCredentialsException('Username too long.');
}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Pass the current token to the `checkPostAuth()` method of user checkers
* Deprecate argument `$secret` of `RememberMeAuthenticator`
* Deprecate passing an empty string as `$userIdentifier` argument to `UserBadge` constructor

7.1
---
Expand Down
14 changes: 14 additions & 0 deletions Tests/Authenticator/Passport/Badge/UserBadgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,29 @@
namespace Symfony\Component\Security\Http\Tests\Authenticator\Passport\Badge;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;

class UserBadgeTest extends TestCase
{
use ExpectUserDeprecationMessageTrait;

public function testUserNotFound()
{
$badge = new UserBadge('dummy', fn () => null);
$this->expectException(UserNotFoundException::class);
$badge->getUser();
}

/**
* @group legacy
*/
public function testEmptyUserIdentifier()
{
$this->expectUserDeprecationMessage('Since symfony/security-http 7.2: Using an empty string as user identifier is deprecated and will throw an exception in Symfony 8.0.');
// $this->expectException(BadCredentialsException::class)
new UserBadge('', fn () => null);
}
}

0 comments on commit cf7f816

Please sign in to comment.