Last active
February 3, 2016 13:56
-
-
Save jenkoian/9dacbc175bf6fd163096 to your computer and use it in GitHub Desktop.
Legacy escape route LegacyAuthenticator.php
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 | |
namespace Acme\Framework\Security; | |
use Acme\Authentication\AuthenticationSessionInterface; | |
use Acme\Authentication\User; | |
use Acme\Authentication\UserManagerInterface; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Symfony\Component\HttpFoundation\Request; | |
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\Core\User\UserProviderInterface; | |
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator; | |
final class LegacyAuthenticator extends AbstractGuardAuthenticator | |
{ | |
/** | |
* @var AuthenticationSessionInterface | |
*/ | |
private $authenticationSession; | |
/** | |
* @var UserManagerInterface | |
*/ | |
private $userManager; | |
/** | |
* @param AuthenticationSessionInterface $authenticationSession | |
* @param UserManagerInterface $userManager | |
*/ | |
public function __construct( | |
AuthenticationSessionInterface $authenticationSession, | |
UserManagerInterface $userManager | |
) { | |
$this->authenticationSession = $authenticationSession; | |
$this->userManager = $userManager; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function start(Request $request, AuthenticationException $authException = null) | |
{ | |
return new RedirectResponse('/path/to/login/page'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getCredentials(Request $request) | |
{ | |
return [ | |
'username' => $this->authenticationSession->getUsername(), | |
'password' => $this->authenticationSession->getPassword() | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getUser($credentials, UserProviderInterface $userProvider) | |
{ | |
// Legacy app had a method of logging in via an already hashed password which is what relogin() is here. | |
if ($this->userManager->relogin($credentials['username'], $credentials['password'])) { | |
return new User( | |
$this->userManager->getUsername(), | |
$this->userManager->getFirstName(), | |
$this->userManager->getLastName(), | |
$this->userManager->getAvatar(), | |
$this->userManager->getRoles() | |
); | |
} | |
return null; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function checkCredentials($credentials, UserInterface $user) | |
{ | |
return true; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) | |
{ | |
return new RedirectResponse('/path/to/login/page'); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) | |
{ | |
// Just continue with request | |
return null; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function supportsRememberMe() | |
{ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment