PHP code example of triopsi / simple-two-factor
1. Go to this page and download the library: Download triopsi/simple-two-factor 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/ */
triopsi / simple-two-factor example snippets
public function bootstrap(): void {
parent::bootstrap();
$this->addPlugin('SimpleTwoFactor');
}
use SimpleTwoFactor\Middleware\TwoFactorMiddleware;
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue {
$middlewareQueue
->add(new ErrorHandlerMiddleware(Configure::read('Error')))
->add(new AssetMiddleware())
->add(new RoutingMiddleware($this))
->add(new BodyParserMiddleware())
->add(new AuthenticationMiddleware($this))
->add(new TwoFactorMiddleware()); // Add TwoFactorMiddleware here
return $middlewareQueue;
}
use SimpleTwoFactor\Middleware\TwoFactorMiddleware;
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue {
$middlewareQueue
->add(new ErrorHandlerMiddleware(Configure::read('Error')))
->add(new AssetMiddleware())
->add(new RoutingMiddleware($this))
->add(new BodyParserMiddleware())
->add(new AuthenticationMiddleware($this))
->add(new TwoFactorMiddleware([
'redirectUrl' => '/users/verifytfa',
'userKeySecret' => 'secret_2tfa',
'isEnabled2faProperty' => 'secret_2tfa',
'issuer' => 'MyApp',
'digits' => 6,
'period' => 30,
'algorithm' => 'sha1',
'qrcodeprovider' => 'BaconQrCodeProvider'
]));
return $middlewareQueue;
}
namespace App\Controller;
use App\Controller\AppController;
use SimpleTwoFactor\Result\Result;
class UsersController extends AppController
{
public function initialize(): void {
parent::initialize();
$this->loadComponent('SimpleTwoFactor.SimpleTwoFactor');
}
public function verifytfa() {
$result = $this->SimpleTwoFactor->getResult();
if ($result->getStatus() == Result::SIMPLE_TWO_FA_AUTH_FAILED) {
$this->Flash->error('Invalid 2FA code');
} elseif ($result->getStatus() == Result::SIMPLE_TWO_FA_AUTH_SUCCESS) {
$this->Flash->success('Welcome back!');
return $this->redirect($this->Auth->redirectUrl());
}
}
}
$this->assign('title', __('Verify 2FA'));
// filepath: /var/www/html/src/Controller/UsersController.php
namespace App\Controller;
use App\Controller\AppController;
class UsersController extends AppController
{
public function initialize(): void {
parent::initialize();
$this->loadComponent('SimpleTwoFactor.SimpleTwoFactor');
}
}
// filepath: /var/www/html/src/Controller/UsersController.php
namespace App\Controller;
use App\Controller\AppController;
class UsersController extends AppController
{
public function initialize(): void {
parent::initialize();
$this->loadComponent('SimpleTwoFactor.SimpleTwoFactor');
}
public function setup2fa() {
$userIdentity = $this->Authentication->getIdentity();
$user = $this->Users->get($userIdentity->id);
$secret = $this->SimpleTwoFactor->createSecret();
$qrCodeUrl = $this->SimpleTwoFactor->getQRCodeImageAsDataUri('MyApp:' . $user->email, $secret);
if ($this->request->is('post')) {
$data = $this->request->getData();
if ( true === $this->SimpleTwoFactor->verifyCode( $data['secret'], $data['code_app'] ) ) {
$user->secret = $data['secret'];
if ($this->Users->save($user)) {
$this->Flash->success(__('The 2FA secret has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('Unable to save the 2FA secret. Please try again.'));
}
} else {
$this->Flash->error( __( 'Unfortunately the code you entered is incorrect. Please try again' ) );
}
}
$this->set(compact('qrCodeUrl', 'secret'));
}
}
// filepath: /var/www/html/templates/Users/setup2fa.php
$this->assign('title', __('Setup Two-Factor Authentication'));