86 lines
2.9 KiB
PHP
86 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace crmeb\services;
|
|
|
|
use Fastknife\Domain\Vo\PointVo;
|
|
use Fastknife\Exception\ParamException;
|
|
use Fastknife\Service\BlockPuzzleCaptchaService as baseBlockPuzzleCaptchaService;
|
|
|
|
class BlockPuzzleCaptchaService extends baseBlockPuzzleCaptchaService
|
|
{
|
|
/**
|
|
* 验证
|
|
* @param string $token
|
|
* @param string $pointJson
|
|
* @param null $callback
|
|
*/
|
|
public function validate($token, $pointJson, $callback = null)
|
|
{
|
|
//获取并设置 $this->originData
|
|
$this->setOriginData($token);
|
|
//数据处理类
|
|
$blockData = $this->factory->makeBlockData();
|
|
// 数据处理 解密处理
|
|
$pattern = '/^[a-zA-Z0-9\/\r\n+]*={0,2}$/'; // Base64编码规则正则表达式
|
|
$isBase64 = preg_match($pattern, $pointJson);
|
|
if($isBase64){
|
|
// 如果是base64 则是加密数据,需要解密
|
|
$decoded = base64_decode($pointJson);
|
|
$pointJson = openssl_decrypt($decoded, 'AES-128-ECB', $this->originData['secretKey'],OPENSSL_RAW_DATA,NULL);
|
|
}
|
|
$pointJson = json_decode($pointJson);
|
|
//解码出来的前端坐标
|
|
$targetPoint = new PointVo($pointJson->x, $pointJson->y);
|
|
//检查
|
|
$blockData->check($this->originData['point'], $targetPoint);
|
|
if (
|
|
abs($pointJson->x - $targetPoint->x) <= $blockData->getFaultOffset() && $pointJson->y == $targetPoint->y
|
|
) {
|
|
return;
|
|
}
|
|
if($callback instanceof \Closure){
|
|
$callback();
|
|
}
|
|
}
|
|
|
|
public function verificationByEncryptCode(string $encryptCode){
|
|
$result = explode('---',$encryptCode);
|
|
if(empty($result)) throw new ParamException('参数错误!');
|
|
$this->validate($result[0], $result[1], function () use ($result,$encryptCode) {
|
|
$cacheEntity = $this->factory->getCacheInstance();
|
|
$cacheEntity->delete($result['token']);
|
|
$cacheEntity->delete($encryptCode);
|
|
});
|
|
|
|
}
|
|
|
|
|
|
public function verificationByEncryptCodeV2(string $encryptCode,string $token){
|
|
$decoded = base64_decode($encryptCode);
|
|
if($decoded !== false && json_last_error() === JSON_ERROR_NONE){
|
|
//获取并设置 $this->originData
|
|
$this->setOriginData($token);
|
|
// 如果是base64 则是加密数据,需要解密
|
|
$encryptCode = openssl_decrypt($decoded, 'AES-128-ECB', $this->originData['secretKey'],OPENSSL_RAW_DATA,NULL);
|
|
}
|
|
$result = explode('---',$encryptCode);
|
|
if(empty($result)) throw new ParamException('参数错误!');
|
|
|
|
$this->validate($result[0], $result[1], function () use ($result,$encryptCode) {
|
|
$cacheEntity = $this->factory->getCacheInstance();
|
|
$cacheEntity->delete($result['token']);
|
|
$cacheEntity->delete($encryptCode);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|