Created
February 9, 2020 22:06
-
-
Save wouterj/8922030b3efda95967d357ba05929f0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* This file is part of the Symfony package. | |
* | |
* (c) Fabien Potencier <[email protected]> | |
* | |
* For the full copyright and license information, please view the LICENSE | |
* file that was distributed with this source code. | |
*/ | |
namespace Symfony\Component\Security\Http\Authenticator\Token; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken; | |
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
use Symfony\Component\Security\Core\Exception\AuthenticationException; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; | |
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface; | |
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy; | |
/** | |
* RememberMeListener implements authentication capabilities via a cookie. | |
* | |
* @author Johannes M. Schmitt <[email protected]> | |
* @author Wouter de Jong <[email protected]> | |
* | |
* @final | |
*/ | |
class RememberMeAuthenticator implements AuthenticatorInterface | |
{ | |
private $rememberMeServices; | |
private $secret; | |
private $tokenStorage; | |
private $sessionStrategy; | |
public function __construct(RememberMeServicesInterface $rememberMeServices, string $secret, TokenStorageInterface $tokenStorage, ?SessionAuthenticationStrategy $sessionStrategy = null) | |
{ | |
$this->rememberMeServices = $rememberMeServices; | |
$this->secret = $secret; | |
$this->tokenStorage = $tokenStorage; | |
$this->sessionStrategy = $sessionStrategy; | |
} | |
public function supports(Request $request): ?bool | |
{ | |
return null !== $this->tokenStorage->getToken(); | |
} | |
public function getCredentials(Request $request) | |
{ | |
return $request; | |
} | |
/** | |
* @param Request $credentials | |
*/ | |
public function getUser($credentials): ?UserInterface | |
{ | |
return $this->rememberMeServices->autoLogin($credentials)->getUser(); | |
} | |
public function createAuthenticatedToken(UserInterface $user, string $providerKey): TokenInterface | |
{ | |
return new RememberMeToken($user, $providerKey, $this->secret); | |
} | |
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | |
{ | |
$this->rememberMeServices->loginFail($request, $exception); | |
return null; | |
} | |
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey): ?Response | |
{ | |
if ($request->hasSession() && $request->getSession()->isStarted()) { | |
$this->sessionStrategy->onAuthentication($request, $token); | |
} | |
return null; | |
} | |
public function supportsRememberMe(): bool | |
{ | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment