<?php
namespace App\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
use Symfony\Component\Security\Http\HttpUtils;
class AuthTokenAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface
{
/**
* Durée de validité du token en secondes, 12 heures
*/
const TOKEN_VALIDITY_DURATION = 12 * 3600;
protected $httpUtils;
public function __construct(HttpUtils $httpUtils)
{
$this->httpUtils = $httpUtils;
}
public function createToken(Request $request, $providerKey)
{
$targetUrl = '/auth-tokens';
$targetUrl1 = '/logo';
$targetUrl3 = '/cp';
$targetUrl2 = '/suivitOuvertureMailling';
$targetUrl4 = '/conge';
$targetUrl5 = '/pdf';
$targetUrl6 = '/update/pressroom';
$targetUrl7 = '/image/pressroom';
$targetUrl8 = '/maillings/information';
$targetUrl9 = '/demande/contact';
$targetUrl10 = '/communique/site';
$targetUrl11 = '/desabonner';
$targetUrl12 = '/api/article_sites';
$targetUrl13 = '/image/site';
$targetUrl14 = '/maillings/getNbEnvoyer';
$targetUrl15 = '/api/article_sites';
$targetUrl16 = '/api/mot_cle_sites';
$targetUrl17 = '/cp/client/intranet';
$targetUrl18 = '/api/statistique/clients/agence';
$targetUrl19 = '/mailing/contactsenvoireel';
if ($request->getMethod() === "POST" && $this->httpUtils->checkRequestPath($request, $targetUrl)) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl1) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl3) === 0) {
return;
}
if ($request->getMethod() === "POST" && $this->httpUtils->checkRequestPath($request, $targetUrl2)) {
return;
}
if ($request->getMethod() === "GET" && stripos($request->getPathInfo(), $targetUrl4) === 0) {
return;
}
if ($request->getMethod() === "POST" && stripos($request->getPathInfo(), $targetUrl5) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl6) === 0) {
return;
}
if ($request->getMethod() === "POST" AND stripos($request->getPathInfo(), $targetUrl6) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl7) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl8) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl10) === 0) {
return;
}
if ($request->getMethod() === "POST" && stripos($request->getPathInfo(), $targetUrl9) === 0) {
return;
}
if ($request->getMethod() === "GET" && stripos($request->getPathInfo(), $targetUrl11) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl12) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl13) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl14) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl15) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl16) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl17) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl18) === 0) {
return;
}
if ($request->getMethod() === "GET" AND stripos($request->getPathInfo(), $targetUrl19) === 0) {
return;
}
$authTokenHeader = $request->headers->get('X-Auth-Token');
if (!$authTokenHeader) {
throw new BadCredentialsException('X-Auth-Token header is required');
}
return new PreAuthenticatedToken(
'anon.',
$authTokenHeader,
$providerKey
);
}
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
{
if (!$userProvider instanceof AuthTokenUserProvider) {
throw new \InvalidArgumentException(
sprintf(
'The user provider must be an instance of AuthTokenUserProvider (%s was given).',
get_class($userProvider)
)
);
}
$authTokenHeader = $token->getCredentials();
$authToken = $userProvider->getAuthToken($authTokenHeader);
if (!$authToken || !$this->isTokenValid($authToken)) {
throw new BadCredentialsException('Invalid authentication token');
}
$user = $authToken->getAuthUser();
$pre = new PreAuthenticatedToken(
$user,
$authTokenHeader,
$providerKey,
$user->getRoles()
);
$pre->setAuthenticated(true);
return $pre;
}
public function supportsToken(TokenInterface $token, $providerKey)
{
return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
}
/**
* Vérifie la validité du token
*/
public function isTokenValid($authToken)
{
return (time() - $authToken->getCreatedAt()->getTimestamp()) < self::TOKEN_VALIDITY_DURATION;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
throw $exception;
}
}