✌️ Two-Factor Authentication
in SymfonyProvide several separate pieces of evidence
to an authentication system
A physical device
generating a disposable code that is
near impossible to spoof.
Hold the user's authentication information
AnonymousToken,
UsernamePasswordToken,
RememberMeToken
Create and authenticate Token
Are the username and password valid?
Does the given cookie exists is session?
...
Capture the login requests,
call the authentication provider
and store the Token in the session.
✨ Where the magic operate ✨
Holds everything together and hook into the Security component
Define configuration for security.yml
!
Extends UsernamePasswordToken
and adds a OneTimePassword attribute.
class UsernamePasswordOTPToken extends UsernamePasswordToken {
/**
* @var string
*/
private $oneTimePassword;
/**
* {@inheritdoc}
*/
public function __construct($user, $credentials, $oneTimePassword, $providerKey, array $roles = array())
{
parent::__construct($user, $credentials, $providerKey, $roles);
$this->oneTimePassword = $oneTimePassword;
}
}
SimpleForm
, Guard
or full custom provider
// Check that the user exists.
try {
$user = $userProvider->loadUserByUsername($token->getUsername());
} catch (UsernameNotFoundException $e) {
throw new BadCredentialsException('User not found.');
}
// Check that the provided password is valid.
if (!$this->encoder->isPasswordValid($user, $token->getCredentials())) {
throw new BadCredentialsException('The presented password is invalid.');
}
$oneTimePassword = $token->getOneTimePassword();
// Check that the provided one-time-password is valid.
if (!$this->yubico->isValid($oneTimePassword)) {
throw new BadCredentialsException('Invalid OTP.');
}
// Check that the provided one-time-password belongs to the user.
if ($this->getYubikey($user) !== $this->yubico->getIdentity($oneTimePassword)) {
throw new BadCredentialsException('Yubico identities mismatch.');
}
// Everything's in order, move along.
return new UsernamePasswordOTPToken(
$user,
$user->getPassword(),
$oneTimePassword,
$providerKey,
$user->getRoles()
);
Protect sensitive part of your apps with mandatory Two-Factor Auth
is_granted('IS_AUTHENTICATED_TWO_FACTOR')
Ask for a OTP for a form to be valid
(just like UserPassword constraint)
Documentation
Symfony Security Custom Authentication Provider Simple Form Authenticator Yubico librairiesQuestions and feedback?
@Tom32i