Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Symfony Auth example
<?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';
}
}
@atrauzzi

This comment has been minimized.

Copy link

atrauzzi commented Nov 26, 2012

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.