src/Security/AuthTokenAuthenticator.php line 141

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  7. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  8. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  9. use Symfony\Component\Security\Core\User\UserProviderInterface;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  11. use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
  12. use Symfony\Component\Security\Http\HttpUtils;
  13. class AuthTokenAuthenticator implements SimplePreAuthenticatorInterfaceAuthenticationFailureHandlerInterface
  14. {
  15.     /**
  16.      * Durée de validité du token en secondes, 12 heures
  17.      */
  18.     const TOKEN_VALIDITY_DURATION 12 3600;
  19.     protected $httpUtils;
  20.     public function __construct(HttpUtils $httpUtils)
  21.     {
  22.         $this->httpUtils $httpUtils;
  23.     }
  24.     public function createToken(Request $request$providerKey)
  25.     {
  26.         $targetUrl '/auth-tokens';
  27.         $targetUrl1 '/logo';
  28.         $targetUrl3 '/cp';
  29.         $targetUrl2 '/suivitOuvertureMailling';
  30.         $targetUrl4 '/conge';
  31.         $targetUrl5 '/pdf';
  32.         $targetUrl6 '/update/pressroom';
  33.         $targetUrl7 '/image/pressroom';
  34.         $targetUrl8 '/maillings/information';
  35.         $targetUrl9 '/demande/contact';
  36.         $targetUrl10 '/communique/site';
  37.         $targetUrl11 '/desabonner';
  38.         $targetUrl12 '/api/article_sites';
  39.         $targetUrl13 '/image/site';
  40.         $targetUrl14 '/maillings/getNbEnvoyer';
  41.         $targetUrl15 '/api/article_sites';
  42.         $targetUrl16 '/api/mot_cle_sites';
  43.         $targetUrl17 '/cp/client/intranet';
  44.         $targetUrl18 '/api/statistique/clients/agence';
  45.         $targetUrl19 '/mailing/contactsenvoireel';
  46.         if ($request->getMethod() === "POST" && $this->httpUtils->checkRequestPath($request$targetUrl)) {
  47.             return;
  48.         }
  49.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl1) === 0) {
  50.             return;
  51.         }
  52.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl3) === 0) {
  53.             return;
  54.         }
  55.         if ($request->getMethod() === "POST" && $this->httpUtils->checkRequestPath($request$targetUrl2)) {
  56.             return;
  57.         }
  58.         if ($request->getMethod() === "GET" && stripos($request->getPathInfo(), $targetUrl4) === 0) {
  59.             return;
  60.         }
  61.         if ($request->getMethod() === "POST" && stripos($request->getPathInfo(), $targetUrl5) === 0) {
  62.             return;
  63.         }
  64.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl6) === 0) {
  65.             return;
  66.         }
  67.         if ($request->getMethod() === "POST" AND stripos($request->getPathInfo(), $targetUrl6) === 0) {
  68.             return;
  69.         }
  70.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl7) === 0) {
  71.             return;
  72.         }
  73.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl8) === 0) {
  74.             return;
  75.         }
  76.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl10) === 0) {
  77.             return;
  78.         }
  79.         if ($request->getMethod() === "POST" && stripos($request->getPathInfo(), $targetUrl9) === 0) {
  80.             return;
  81.         }
  82.         if ($request->getMethod() === "GET" && stripos($request->getPathInfo(), $targetUrl11) === 0) {
  83.             return;
  84.         }
  85.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl12) === 0) {
  86.             return;
  87.         }
  88.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl13) === 0) {
  89.             return;
  90.         }
  91.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl14) === 0) {
  92.             return;
  93.         }
  94.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl15) === 0) {
  95.             return;
  96.         }
  97.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl16) === 0) {
  98.             return;
  99.         }
  100.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl17) === 0) {
  101.             return;
  102.         }
  103.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl18) === 0) {
  104.             return;
  105.         }
  106.         if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl19) === 0) {
  107.             return;
  108.         }
  109.         $authTokenHeader $request->headers->get('X-Auth-Token');
  110.         if (!$authTokenHeader) {
  111.             throw new BadCredentialsException('X-Auth-Token header is required');
  112.         }
  113.         return new PreAuthenticatedToken(
  114.             'anon.',
  115.             $authTokenHeader,
  116.             $providerKey
  117.         );
  118.     }
  119.     public function authenticateToken(TokenInterface $tokenUserProviderInterface $userProvider$providerKey)
  120.     {
  121.         if (!$userProvider instanceof AuthTokenUserProvider) {
  122.             throw new \InvalidArgumentException(
  123.                 sprintf(
  124.                     'The user provider must be an instance of AuthTokenUserProvider (%s was given).',
  125.                     get_class($userProvider)
  126.                 )
  127.             );
  128.         }
  129.         $authTokenHeader $token->getCredentials();
  130.         $authToken $userProvider->getAuthToken($authTokenHeader);
  131.         if (!$authToken || !$this->isTokenValid($authToken)) {
  132.             throw new BadCredentialsException('Invalid authentication token');
  133.         }
  134.         $user $authToken->getAuthUser();
  135.         $pre = new PreAuthenticatedToken(
  136.             $user,
  137.             $authTokenHeader,
  138.             $providerKey,
  139.             $user->getRoles()
  140.         );
  141.         $pre->setAuthenticated(true);
  142.         return $pre;
  143.     }
  144.     public function supportsToken(TokenInterface $token$providerKey)
  145.     {
  146.         return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
  147.     }
  148.     /**
  149.      * Vérifie la validité du token
  150.      */
  151.     public function isTokenValid($authToken)
  152.     {
  153.         return (time() - $authToken->getCreatedAt()->getTimestamp()) < self::TOKEN_VALIDITY_DURATION;
  154.     }
  155.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
  156.     {
  157.         throw $exception;
  158.     }
  159. }