Skip to content

Instantly share code, notes, and snippets.

@NandoKstroNet
Last active May 23, 2018 15:46
Show Gist options
  • Save NandoKstroNet/d20396de35f23652e93cdf5d4920269f to your computer and use it in GitHub Desktop.
Save NandoKstroNet/d20396de35f23652e93cdf5d4920269f to your computer and use it in GitHub Desktop.
CustomAuthenticator para o Symfony Security Guard. Symfony 3.4 -- curso Construindo API com Symfony.
<?php
namespace ApiBundle\Security;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Lexik\Bundle\JWTAuthenticationBundle\Encoder\JWTEncoderInterface;
use Lexik\Bundle\JWTAuthenticationBundle\TokenExtractor\AuthorizationHeaderTokenExtractor;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
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\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
class CustomAuthenticator extends AbstractGuardAuthenticator
{
/**
* @var JWTEncoderInterface
*/
private $jwtEncoder;
/**
* @var EntityManager
*/
private $em;
public function __construct(JWTEncoderInterface $jwtEncoder, EntityManagerInterface $em)
{
$this->jwtEncoder = $jwtEncoder;
$this->em = $em;
}
public function supports(Request $request)
{
return $request->headers->has('Authorization');
}
public function start(Request $request, AuthenticationException $authException = null)
{
$data = array(
'message' => 'JWT Token is Required'
);
return new JsonResponse($data, Response::HTTP_UNAUTHORIZED);
}
public function getCredentials(Request $request)
{
$extractor = new AuthorizationHeaderTokenExtractor('Bearer', 'Authorization');
$token = $extractor->extract($request);
if(!$token) {
return;
}
return $token;
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
$data = $this->jwtEncoder->decode($credentials);
if($data === false) {
throw new CustomUserMessageAuthenticationException('Invalid Token');
}
$user = $this->em->getRepository('ApiBundle:User')
->findOneByEmail($data['username']);
return $user;
}
public function checkCredentials($credentials, UserInterface $user)
{
return true;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
{
$data = array(
'message' => strtr($exception->getMessageKey(), $exception->getMessageData())
);
return new JsonResponse($data, Response::HTTP_FORBIDDEN);
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
{
return null;
}
public function supportsRememberMe()
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment