/**
* Configure the Two-factor Authentication.
*
* @return \Cake\Network\Response|void
*/
public function configure()
{
$this->loadModel('Users');
$user = $this->Users->find()->contain(['UsersTwoFactorAuth'])->where(['Users.id' => $this->Auth->user('id')])->select(['Users.id', 'Users.username', 'Users.two_factor_auth_enabled', 'UsersTwoFactorAuth.id', 'UsersTwoFactorAuth.user_id', 'UsersTwoFactorAuth.secret', 'UsersTwoFactorAuth.username'])->first();
if ($user->two_factor_auth_enabled == true) {
$this->Flash->error(__('You have already set-up the Two-factor authentication.'));
return $this->redirect(['controller' => 'users', 'action' => 'security']);
}
$tfa = new TwoFactorAuth('Xeta');
if (!is_null($user->users_two_factor_auth)) {
$secret = $user->users_two_factor_auth->secret;
} else {
$this->loadModel('UsersTwoFactorAuth');
$secret = $tfa->createSecret();
$data = ['user_id' => $this->Auth->user('id'), 'secret' => $secret, 'username' => $user->username];
$entity = $this->UsersTwoFactorAuth->newEntity($data);
$this->UsersTwoFactorAuth->save($entity);
}
$imgSrc = $tfa->getQRCodeImageAsDataUri($user->username, $secret);
$secretCode = chunk_split($secret, 4, ' ');
$this->set(compact('imgSrc', 'secretCode'));
}