Skip to content

Commit

Permalink
fix: send cookie after authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
maelgangloff committed Aug 5, 2024
1 parent 3341ba0 commit 4e6649f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config/packages/lexik_jwt_authentication.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ lexik_jwt_authentication:
secret_key: '%env(resolve:JWT_SECRET_KEY)%'
public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
pass_phrase: '%env(JWT_PASSPHRASE)%'
token_ttl: 3600 # in seconds, default is 3600
token_ttl: 7200 # in seconds, default is 3600
token_extractors:
authorization_header:
enabled: true
Expand Down
30 changes: 17 additions & 13 deletions src/Security/JWTAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Lexik\Bundle\JWTAuthenticationBundle\Response\JWTAuthenticationSuccessResponse;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
Expand All @@ -15,13 +16,11 @@
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

readonly class JWTAuthenticator implements AuthenticationSuccessHandlerInterface
class JWTAuthenticator implements AuthenticationSuccessHandlerInterface
{
public function __construct(
private JWTTokenManagerInterface $jwtManager,
private EventDispatcherInterface $dispatcher,
private iterable $cookieProviders = [],
private bool $removeTokenFromBodyWhenCookiesUsed = true
protected JWTTokenManagerInterface $jwtManager,
protected EventDispatcherInterface $dispatcher,
) {
}

Expand All @@ -40,21 +39,26 @@ public function handleAuthenticationSuccess(UserInterface $user, $jwt = null): R
$jwt = $this->jwtManager->create($user);
}

$jwtCookies = [];
foreach ($this->cookieProviders as $cookieProvider) {
$jwtCookies[] = $cookieProvider->createCookie($jwt);
}
$jwtCookies = [
new Cookie(
'BEARER',
$jwt,
time() + 7200, // expiration
'/',
null,
true,
true,
false,
'strict'
),
];

$response = new JWTAuthenticationSuccessResponse($jwt, [], $jwtCookies);
$event = new AuthenticationSuccessEvent(['token' => $jwt], $user, $response);

$this->dispatcher->dispatch($event, Events::AUTHENTICATION_SUCCESS);
$responseData = $event->getData();

if ($jwtCookies && $this->removeTokenFromBodyWhenCookiesUsed) {
unset($responseData['token']);
}

if ($responseData) {
$response->setData($responseData);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Security/OAuthAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
new Cookie(
'BEARER',
$token,
time() + 3600, // expiration
time() + 7200, // expiration
'/',
null,
true,
Expand Down

0 comments on commit 4e6649f

Please sign in to comment.