public function qrAction() { // 生成二维码 $username = urlencode('账号:') . '*****@*****.**'; $secretKey = 'DPI45HCE'; $url = "otpauth://totp/{$username}?secret={$secretKey}&issuer=" . urlencode('XXTIME.COM'); $qrCode = new QrCode(); $qrCode->setText($url)->setSize(200)->setPadding(10)->setErrorCorrection('low')->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))->setImageType(QrCode::IMAGE_TYPE_PNG); header('Content-Type: ' . $qrCode->getContentType()); $qrCode->render(); exit; // 验证 $totp = new PHPGangsta_GoogleAuthenticator(); $secretKey = $totp->createSecret(32); $oneCode = $totp->getCode($secretKey); $checkResult = $totp->verifyCode($secretKey, $oneCode, 2); // 2 = 2*30sec clock tolerance if ($checkResult) { echo 'OK'; dd($secret, $oneCode); } else { echo 'FAILED'; } exit; }
public function qrAction() { $username = $this->session->get('username'); if (!$username) { exit('No Permission'); } // 二维码生成与验证 $otp = new PHPGangsta_GoogleAuthenticator(); if ($_POST) { // 验证 开启二次验证是否正确 $code = $this->request->get('code', 'int'); $secret_key = $this->session->get('secret_key'); $checkResult = $otp->verifyCode($secret_key, $code, 2); if (!$checkResult) { Utils::outputJSON(array('code' => 0, 'message' => 'Verify Success')); $user_id = $this->session->get('user_id'); $this->authModel->setOTPKey($user_id, $secret_key); } Utils::outputJSON(array('code' => 1, 'message' => 'Verify Failed')); } $secret_key = $otp->createSecret(32); $this->session->set('secret_key', $secret_key); $username = urlencode('账号:') . $username; $url = "otpauth://totp/{$username}?secret={$secret_key}&issuer=" . urlencode('XXTIME.COM'); $qrCode = new QrCode(); $qrCode->setText($url)->setSize(200)->setPadding(10)->setErrorCorrection('low')->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))->setImageType(QrCode::IMAGE_TYPE_PNG); header('Content-Type: ' . $qrCode->getContentType()); $qrCode->render(); exit; }