src/Security/ApiKeyAuthenticator.php line 23
<?php
namespace App\Security;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use App\Repository\PermissionRepository;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
class ApiKeyAuthenticator extends AbstractAuthenticator
{
public function __construct(
private PermissionRepository $permissionRepository, private Security $security
) {
}
/**
* Called on every request to decide if this authenticator should be
* used for the request. Returning `false` will cause this authenticator
* to be skipped.
*/
public function supports(Request $request): ?bool
{
if ($this->security->getUser() == null) {
return true;
}else {
return false;
}
}
public function authenticate(Request $request): Passport
{
// $userId = $this->getUserConnected();
// if (!$userId) {
// throw new CustomUserMessageAuthenticationException('No API token provided');
// }
// implement your own logic to get the user identifier from `$apiToken`
// e.g. by looking up a user in the database using its API key
$userIdentifier = $this->permissionRepository->find(27226);
return new SelfValidatingPassport(new UserBadge($userIdentifier->getUserIdentifier()));
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
// on success, let the request continue
return null;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
{
$data = [
// you may want to customize or obfuscate the message first
'message' => strtr($exception->getMessageKey(), $exception->getMessageData())
// or to translate this message
// $this->translator->trans($exception->getMessageKey(), $exception->getMessageData())
];
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
}
public function getUserConnected() {
// Encode the token
$client = HttpClient::create();
$cookies = [
'connect.sid' => 's%3A8he92BPdiv0-fvI7MWrgG8lnGzoHbktg.lDwjNFpsg5x7aLomQBcjal4ssMjfDzK5veAtzD8yJSU',
'wwstid' => '10',
];
$cookieHeader = '';
foreach ($cookies as $name => $value) {
$cookieHeader .= $name . '=' . $value . '; ';
}
$response = $client->request('GET', 'https://dev.welcomeworkspaces.com/resa/token', [
'headers' => [
'Cookie' => $cookieHeader,
],
]);
$content = $response->getContent();
$data = json_decode($content,true);
return $this->decodeJWT($data['token']);
}
function decodeJWT($token) {
try {
$jwt = JWT::decode($token, new Key($_ENV['JWT_PUBLIC_KEY'], 'HS256'));
// Generate the JWT
return $jwt->me;
// Si la vérification de la signature réussit, le décodage est réussi et le token est valide
} catch (Exception $e) {
return null;
}
}
}