Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundle\Security;
  4.  
  5.  
  6. use Psr\Container\ContainerInterface;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  18. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. use Symfony\Component\Security\Core\User\UserProviderInterface;
  21. use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
  22. use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
  23.  
  24. class JsonAuthenticator extends AbstractGuardAuthenticator
  25. {
  26.     private $container = null;
  27.     private $logger = null;
  28.     private $encoder = null;
  29.  
  30.     public function __construct(ContainerInterface $container, LoggerInterface $logger, UserPasswordEncoderInterface $encoder)
  31.     {
  32.         $this->container = $container;
  33.         $this->logger = $logger;
  34.         $this->encoder = $encoder;
  35.     }
  36.  
  37.     public function start(Request $request, AuthenticationException $authException = null)
  38.     {
  39.         return new JsonResponse(array(), Response::HTTP_UNAUTHORIZED);
  40.     }
  41.  
  42.     public function getCredentials(Request $request)
  43.     {
  44.         if ( $request->getPathInfo() != "/auth/sign-in" ) {
  45.             return null;
  46.         }
  47.  
  48.         $json = json_decode( $request->getContent()
  49.  
  50.         return array(
  51.             'username' => $json->username,
  52.             'password' => $json->password
  53.         );
  54.  
  55.     }
  56.  
  57.     public function getUser($credentials, UserProviderInterface $userProvider)
  58.     {
  59.         try {
  60.             return $userProvider->loadUserByUsername($credentials['username']);
  61.         } catch (UsernameNotFoundException $e) {
  62.             return null;
  63.         }
  64.     }
  65.  
  66.     public function checkCredentials($credentials, UserInterface $user)
  67.     {
  68.         return $this->encoder->isPasswordValid($user, $credentials['password']);
  69.     }
  70.  
  71.     public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
  72.     {
  73.         return new JsonResponse(array(), Response::HTTP_FORBIDDEN);
  74.     }
  75.  
  76.     public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
  77.     {
  78.         // if /auth/sign-in was requested, return HTTP 200 OK response
  79.         if ( $request->getPathInfo() == "/auth/sign-in" ) {
  80.             return new JsonResponse(array(), Response::HTTP_OK);
  81.         }
  82.      
  83.         // if something else was requested - return null and allow the request to proceed
  84.         return null;
  85.     }
  86.  
  87.     public function supportsRememberMe()
  88.     {
  89.         return false;
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement