Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppBundle\Security;
- use Psr\Container\ContainerInterface;
- use Psr\Log\LoggerInterface;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
- use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
- use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
- use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
- use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
- use Symfony\Component\Security\Core\Exception\AuthenticationException;
- use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
- use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
- use Symfony\Component\Security\Core\User\UserInterface;
- use Symfony\Component\Security\Core\User\UserProviderInterface;
- use Symfony\Component\Security\Csrf\TokenStorage\TokenStorageInterface;
- use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
- class JsonAuthenticator extends AbstractGuardAuthenticator
- {
- private $container = null;
- private $logger = null;
- private $encoder = null;
- public function __construct(ContainerInterface $container, LoggerInterface $logger, UserPasswordEncoderInterface $encoder)
- {
- $this->container = $container;
- $this->logger = $logger;
- $this->encoder = $encoder;
- }
- public function start(Request $request, AuthenticationException $authException = null)
- {
- return new JsonResponse(array(), Response::HTTP_UNAUTHORIZED);
- }
- public function getCredentials(Request $request)
- {
- if ( $request->getPathInfo() != "/auth/sign-in" ) {
- return null;
- }
- $json = json_decode( $request->getContent()
- return array(
- 'username' => $json->username,
- 'password' => $json->password
- );
- }
- public function getUser($credentials, UserProviderInterface $userProvider)
- {
- try {
- return $userProvider->loadUserByUsername($credentials['username']);
- } catch (UsernameNotFoundException $e) {
- return null;
- }
- }
- public function checkCredentials($credentials, UserInterface $user)
- {
- return $this->encoder->isPasswordValid($user, $credentials['password']);
- }
- public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
- {
- return new JsonResponse(array(), Response::HTTP_FORBIDDEN);
- }
- public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
- {
- // if /auth/sign-in was requested, return HTTP 200 OK response
- if ( $request->getPathInfo() == "/auth/sign-in" ) {
- return new JsonResponse(array(), Response::HTTP_OK);
- }
- // if something else was requested - return null and allow the request to proceed
- return null;
- }
- public function supportsRememberMe()
- {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement