On Github Tom32i / talk-two-factor
✌️ Two-Factor Authentication
in SymfonyAnonymousToken,UsernamePasswordToken,RememberMeToken
Are the username and password valid? Does the given cookie exists is session? ...
✨ Where the magic operate ✨
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