RobThree\Auth\TwoFactorAuth::getQRCodeImageAsDataUri PHP Method

getQRCodeImageAsDataUri() public method

Get data-uri of QRCode
public getQRCodeImageAsDataUri ( $label, $secret, $size = 200 )
    public function getQRCodeImageAsDataUri($label, $secret, $size = 200)
    {
        if (!is_int($size) || $size <= 0) {
            throw new TwoFactorAuthException('Size must be int > 0');
        }
        return 'data:' . $this->qrcodeprovider->getMimeType() . ';base64,' . base64_encode($this->qrcodeprovider->getQRCodeImage($this->getQRText($label, $secret), $size));
    }

Usage Example

Example #1
0
 /**
  * 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'));
 }
All Usage Examples Of RobThree\Auth\TwoFactorAuth::getQRCodeImageAsDataUri