PHP code example of yeebase / twofactorauthentication
1. Go to this page and download the library: Download yeebase/twofactorauthentication library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yeebase / twofactorauthentication example snippets
declare(strict_types=1);
namespace Some\Package\Controller;
use Neos\Error\Messages\Message;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Security\Account;
use Neos\Flow\Security\Context;
use Neos\Flow\Security\Exception\AccessDeniedException;
use Yeebase\TwoFactorAuthentication\Domain\ValueObjects\OneTimePassword;
use Yeebase\TwoFactorAuthentication\Domain\ValueObjects\SecretWithHmac;
use Yeebase\TwoFactorAuthentication\Exception\InvalidOtpException;
use Yeebase\TwoFactorAuthentication\Service\TwoFactorAuthenticationService;
class TwoFactorAuthenticationSetupController extends ActionController
{
/**
* @var Account
*/
private $authenticatedAccount;
/**
* @Flow\Inject
* @var Context
*/
protected $securityContext;
/**
* @Flow\Inject
* @var TwoFactorAuthenticationService
*/
protected $twoFactorAuthenticationService;
protected function initializeAction(): void
{
parent::initializeAction();
$this->authenticatedAccount = $this->securityContext->getAccountByAuthenticationProviderName('Some.Package:Default');
if ($this->authenticatedAccount === null) {
throw new AccessDeniedException('...');
}
}
public function indexAction(): void
{
$twoFactorAuthenticationEnabled = $this->twoFactorAuthenticationService->isTwoFactorAuthenticationEnabledFor($this->authenticatedAccount);
$this->view->assign('2faEnabled', $twoFactorAuthenticationEnabled);
if (!$twoFactorAuthenticationEnabled) {
$holder = $this->authenticatedAccount->getAccountIdentifier();
$qrCode = $this->twoFactorAuthenticationService->generateActivationQrCode($holder);
$this->view->assignMultiple([
'secretWithHmac' => SecretWithHmac::fromSecret($qrCode->getSecret()),
'qrCode' => $qrCode->renderSvg(200),
]);
}
}
public function enableAction(SecretWithHmac $secretWithHmac, OneTimePassword $otp): void
{
try {
$this->twoFactorAuthenticationService->enableTwoFactorAuthentication($this->authenticatedAccount, $secretWithHmac->getSecret(), $otp);
} catch (InvalidOtpException $exception) {
$this->addFlashMessage('Invalid One-time Password', 'Invalid OTP', Message::SEVERITY_ERROR);
$this->redirect('index');
}
$this->addFlashMessage('Two-Factor-Authentication was activated!', '2FA enabled', Message::SEVERITY_OK);
$this->redirect('index');
}
public function disableAction(): void
{
$this->twoFactorAuthenticationService->disableTwoFactorAuthentication($this->authenticatedAccount);
$this->addFlashMessage('Two-Factor-Authentication was deactivated!', '2FA disabled', Message::SEVERITY_NOTICE);
$this->redirect('index');
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.