request->get("token"); $admin = Db::name("admin")->where("salt",$salt)->find(); if(empty($admin)){ echo "非法操作";exit; } if($admin['secret']){ echo "请勿重复绑定";exit; } if($admin['secret_y'] && $admin['updatetime']>time()-600){ import('lib.GoogleAuthenticator', EXTEND_PATH , '.php'); $ga = new \PHPGangsta_GoogleAuthenticator(); $secret = $admin['secret_y']; $qrCodeUrl = $ga->getQRCodeGoogleUrl('HASH', $secret); }else{ import('lib.GoogleAuthenticator', EXTEND_PATH , '.php'); $ga = new \PHPGangsta_GoogleAuthenticator(); $secret = $ga->createSecret(); $qrCodeUrl = $ga->getQRCodeGoogleUrl('HASH', $secret); Db::name("admin")->where("id",$admin['id'])->update(['secret_y'=>$secret,'updatetime'=>time()]); } echo '
'.$secret.'
'; } /** * 生成私钥二维码 */ public function get_keys(){ import('lib.GoogleAuthenticator', EXTEND_PATH , '.php'); $ga = new \PHPGangsta_GoogleAuthenticator(); $secret = $ga->createSecret(); $qrCodeUrl = $ga->getQRCodeGoogleUrl('APEC', $secret); $data = array( "secret" => $secret, "code_url" => $qrCodeUrl, ); $this->success("ok",$data); } /** * 验证并绑定私钥 */ public function band_secret() { $user_id = $this->auth->id; $secret = $this->request->post("secret"); $code = $this->request->post("code"); if(empty($secret)){ $this->error(__("请输入私钥")); } if(empty($code)){ $this->error(__("请输入验证码")); } $user = Db::name("user")->where("id",$user_id)->find(); if($user['secret']){ $this->error(__("请勿重复绑定")); } import('lib.GoogleAuthenticator', EXTEND_PATH , '.php'); $ga = new \PHPGangsta_GoogleAuthenticator(); $checkResult = $ga->verifyCode($secret, $code, 1); // 2 = 2*30sec clock tolerance if ($checkResult) { $user_update = array( "secret" => base64_encode($secret), "updatetime" => time(), ); $res = Db::name("user")->where("id",$user_id)->update($user_update); if($res){ $this->success(__("绑定成功")); }else{ $this->error(__("绑定失败")); } } else { $this->error(__("绑定失败"),$checkResult); } } /** * 验证私钥验证码 */ public function check_code($code) { $secret = $this->auth->secret; if(!$secret){ $this->error(__("请先绑定谷歌验证")); } import('lib.GoogleAuthenticator', EXTEND_PATH , '.php'); $ga = new \PHPGangsta_GoogleAuthenticator(); $checkResult = $ga->verifyCode(base64_decode($secret), $code, 1); // 2 = 2*30sec clock tolerance if ($checkResult) { return true; } else { return false; } } public function check_test() { $code = $this->request->post("code"); controller("Google")->check_code($code); } /** * 验证并绑定私钥 */ public function jiechu_band() { $user_id = $this->auth->id; $secret = $this->auth->secret; $code = $this->request->post("code"); if(empty($code)){ $this->error(__("请输入验证码")); } if(!$secret){ $this->error(__("暂未绑定")); } import('lib.GoogleAuthenticator', EXTEND_PATH , '.php'); $ga = new \PHPGangsta_GoogleAuthenticator(); $checkResult = $ga->verifyCode(base64_decode($secret), $code, 1); // 2 = 2*30sec clock tolerance if ($checkResult) { $user_update = array( "secret" => "", "updatetime" => time(), ); $res = Db::name("user")->where("id",$user_id)->update($user_update); if($res){ $this->success(__("解绑成功")); }else{ $this->error(__("解绑失败")); } } else { $this->error(__("验证失败"),$checkResult); } } }