Skip to content

Commit

Permalink
Implement AuthenticatorInterface::createToken() (Symfony 5.4)
Browse files Browse the repository at this point in the history
  • Loading branch information
chalasr committed Aug 10, 2021
1 parent 72d3192 commit 070f68e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Security/Authenticator/JWTAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
Expand Down Expand Up @@ -219,4 +220,17 @@ public function createAuthenticatedToken(PassportInterface $passport, string $fi

return $token;
}

public function createToken(Passport $passport, string $firewallName): TokenInterface
{
$token = parent::createToken($passport, $firewallName);

if (!$passport instanceof SelfValidatingPassport) {
throw new \LogicException(sprintf('Expected "%s" but got "%s".', SelfValidatingPassport::class, get_debug_type($passport)));
}

$this->eventDispatcher->dispatch(new JWTAuthenticatedEvent($passport->getAttribute('payload'), $token), Events::JWT_AUTHENTICATED);

return $token;
}
}
7 changes: 6 additions & 1 deletion Tests/Security/Authenticator/JWTAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\FormLoginAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
Expand Down Expand Up @@ -226,7 +227,11 @@ public function testCreateAuthenticatedToken()
$passport->method('getUser')->willReturn($user);
$passport->method('getAttribute')->with('payload')->willReturn(['claim' => 'val']);

$authenticator->createToken($passport, 'dummy');
if (method_exists(FormLoginAuthenticator::class, 'createToken')) {
$authenticator->createToken($passport, 'dummy');
} else {
$authenticator->createAuthenticatedToken($passport, 'dummy');
}
}

private function getJWTManagerMock($userIdentityField = null, $userIdClaim = null)
Expand Down

0 comments on commit 070f68e

Please sign in to comment.