Created
November 20, 2012 11:19
-
-
Save Seldaek/4117358 to your computer and use it in GitHub Desktop.
Simple Symfony Auth example
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\DemoBundle\Security; | |
use Symfony\Component\Security\Core\Authentication\SimpleFormAuthenticatorInterface; | |
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
use Symfony\Component\Security\Core\Exception\AuthenticationException; | |
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | |
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\Security\Core\User\User; | |
use Symfony\Component\Security\Core\User\UserProviderInterface; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; | |
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; | |
class Authenticator implements SimpleFormAuthenticatorInterface, UserProviderInterface | |
{ | |
public function createToken(Request $request, $username, $password, $providerKey) | |
{ | |
return new UsernamePasswordToken($username, $password, $providerKey); | |
} | |
public function authenticate(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) | |
{ | |
if ($token->getUsername() === 'foo' && $token->getCredentials() === 'bar') { | |
$user = $userProvider->loadUserByUsername('foo'); | |
return new UsernamePasswordToken($user, 'bar', 'dummy', $user->getRoles()); | |
} | |
throw new AuthenticationException('Invalid username or password'); | |
} | |
public function supports(TokenInterface $token, $providerKey) | |
{ | |
return $token instanceof UsernamePasswordToken && $token->getProviderKey() === $providerKey; | |
} | |
public function handleAuthenticationFailure(GetResponseEvent $event, AuthenticationException $exception) | |
{ | |
$event->setResponse(new Response('FAIL', 400)); | |
} | |
public function loadUserByUsername($username) | |
{ | |
if ($username === 'foo') { | |
return new User('foo', 'bar', array('ROLE_USER')); | |
} | |
throw new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username)); | |
} | |
public function refreshUser(UserInterface $user) | |
{ | |
if (!$user instanceof User) { | |
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); | |
} | |
return $this->loadUserByUsername($user->getUsername()); | |
} | |
public function supportsClass($class) | |
{ | |
return $class === 'Symfony\Component\Security\Core\User\User'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks interesting! An implementation like this might start to make things more clear.
Thinking back to my original issue, I need to be able to pull in additional information beyond the user like what system they're logging into. That's probably in "createToken", eh?