mirror of
https://github.com/maelgangloff/domain-watchdog.git
synced 2025-12-29 16:15:04 +00:00
feat: issue JWT cookie when OAuth
This commit is contained in:
@@ -3,13 +3,9 @@
|
|||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
|
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
|
||||||
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
use Symfony\Component\Security\Core\User\UserInterface;
|
|
||||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
|
||||||
|
|
||||||
class HomeController extends AbstractController
|
class HomeController extends AbstractController
|
||||||
{
|
{
|
||||||
@@ -25,11 +21,4 @@ class HomeController extends AbstractController
|
|||||||
{
|
{
|
||||||
return $clientRegistry->getClient('oauth')->redirect();
|
return $clientRegistry->getClient('oauth')->redirect();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route(path: "/login/oauth/token", name: "oauth_connect_token")]
|
|
||||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
|
||||||
public function loginOAuthToken(UserInterface $user, JWTTokenManagerInterface $JWTManager): JsonResponse
|
|
||||||
{
|
|
||||||
return new JsonResponse(['token' => $JWTManager->create($user)]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,8 @@ use App\Repository\UserRepository;
|
|||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
|
use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
|
||||||
use KnpU\OAuth2ClientBundle\Security\Authenticator\OAuth2Authenticator;
|
use KnpU\OAuth2ClientBundle\Security\Authenticator\OAuth2Authenticator;
|
||||||
|
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Cookie;
|
||||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
@@ -22,10 +24,11 @@ class OAuthAuthenticator extends OAuth2Authenticator implements AuthenticationEn
|
|||||||
{
|
{
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly ClientRegistry $clientRegistry,
|
private readonly ClientRegistry $clientRegistry,
|
||||||
private readonly UserRepository $userRepository,
|
private readonly UserRepository $userRepository,
|
||||||
private readonly EntityManagerInterface $em,
|
private readonly EntityManagerInterface $em,
|
||||||
private readonly RouterInterface $router
|
private readonly RouterInterface $router,
|
||||||
|
private readonly JWTTokenManagerInterface $JWTManager
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -67,7 +70,22 @@ class OAuthAuthenticator extends OAuth2Authenticator implements AuthenticationEn
|
|||||||
|
|
||||||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): RedirectResponse
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): RedirectResponse
|
||||||
{
|
{
|
||||||
return new RedirectResponse($this->router->generate('index'));
|
$token = $this->JWTManager->create($token->getUser());
|
||||||
|
$response = new RedirectResponse($this->router->generate('index'));
|
||||||
|
$response->headers->setCookie(
|
||||||
|
new Cookie(
|
||||||
|
'BEARER',
|
||||||
|
$token,
|
||||||
|
time() + 3600, // expiration
|
||||||
|
'/',
|
||||||
|
null,
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
'strict'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
|
||||||
|
|||||||
Reference in New Issue
Block a user