feat: add tencent captcha

pull/343/head
Seaton Jiang 2021-03-10 21:31:11 +08:00
parent b65de238f3
commit d6a4ecf5b7
No known key found for this signature in database
GPG Key ID: C1086BAE716FF138
3 changed files with 157 additions and 0 deletions

View File

@ -44,6 +44,16 @@ jQuery(document).ready(function ($) {
jQuery("#section-g_cos_secretkey").show();
}
jQuery("#g_007").click(function () {
jQuery("#section-g_007_appid").fadeToggle(400);
jQuery("#section-g_007_appsecretkey").fadeToggle(400);
});
if (jQuery("#g_007:checked").val() !== undefined) {
jQuery("#section-g_007_appid").show();
jQuery("#section-g_007_appsecretkey").show();
}
jQuery("#g_cc_switch").click(function () {
jQuery("#section-g_cc").fadeToggle(400);
});

View File

@ -282,4 +282,129 @@ if (kratos_option('g_search', false)) {
return $search;
}
}
// 腾讯云验证码
if (kratos_option('g_007', false)) {
add_action('login_head', 'add_login_head');
function add_login_head()
{
echo '<script src="https://ssl.captcha.qq.com/TCaptcha.js"></script>';
}
add_action('login_form', 'add_captcha_body');
function add_captcha_body(){ ?>
<label for="codeVerifyButton">人机验证</label>
<input type="button" name="TencentCaptcha" id="TencentCaptcha" data-appid="<?php echo kratos_option('g_007_appid'); ?>" data-cbfn="callback" class="button" value="验证" style="width: 100%;margin-bottom: 16px;height:40px;" />
<input type="hidden" id="codeCaptcha" name="codeCaptcha" value="" />
<input type="hidden" id="codeVerifyTicket" name="codeVerifyTicket" value="" />
<input type="hidden" id="codeVerifyRandstr" name="codeVerifyRandstr" value="" />
<script>
window.callback = function(res){
if(res.ret === 0)
{
var verifybutton = document.getElementById("TencentCaptcha");
document.getElementById("codeVerifyTicket").value = res.ticket;
document.getElementById("codeVerifyRandstr").value = res.randstr;
document.getElementById("codeCaptcha").value = 1;
verifybutton.setAttribute("disabled", "disabled");
verifybutton.style.cssText = "background-color:#4fb845!important; color:#fff!important; width:100%; margin-bottom:16px; height: 40px;pointer-events:none;";
verifybutton.value = "验证成功";
}
}
</script>
<?php
}
add_filter('wp_authenticate_user', 'validate_tcaptcha_login', 100, 1);
function validate_tcaptcha_login($user) {
$slide = $_POST['codeCaptcha'];
if($slide == '')
{
return new WP_Error('broke', __("错误:请进行真人验证"));
}else{
$result = validate_login($_POST['codeVerifyTicket'], $_POST['codeVerifyRandstr']);
if($result['result'])
{
return $user;
}else{
return new WP_Error('broke', $result['message']);
}
}
}
function validate_login($ticket,$randstr){
$appid = kratos_option('g_007_appid');
$appsecretkey = kratos_option('g_007_appsecretkey');
$userip = $_SERVER["REMOTE_ADDR"];
$url = "https://ssl.captcha.qq.com/ticket/verify";
$params = array(
"aid" => $appid,
"AppSecretKey" => $appsecretkey,
"Ticket" => $ticket,
"Randstr" => $randstr,
"UserIP" => $userip
);
$paramstring = http_build_query($params);
$content = txcurl($url, $paramstring);
$result = json_decode($content, true);
if($result){
if($result['response'] == 1){
return array(
'result' => 1,
'message' => ''
);
}else{
return array(
'result' => 0,
'message' => $result['err_msg']
);
}
}else{
return array(
'result' => 0,
'message' => '错误:请求异常,请稍后再试'
);
}
}
function txcurl($url, $params=false, $ispost=0)
{
$httpInfo = array();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if($ispost)
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_URL, $url);
}else{
if($params)
{
curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
}else{
curl_setopt($ch, CURLOPT_URL, $url);
}
}
$response = curl_exec($ch);
if ($response === FALSE)
{
return false;
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$httpInfo = array_merge($httpInfo, curl_getinfo($ch));
curl_close($ch);
return $response;
}
}

View File

@ -234,6 +234,28 @@ function kratos_options()
'type' => 'checkbox',
);
$options[] = array(
'name' => __('腾讯云验证码', 'kratos'),
'desc' => __('开启后台登录页面验证码功能', 'kratos'),
'id' => 'g_007',
'std' => '0',
'type' => 'checkbox',
);
$options[] = array(
'name' => __('App ID', 'kratos'),
'id' => 'g_007_appid',
'class' => 'hidden',
'type' => 'text',
);
$options[] = array(
'name' => __('App Secret Key', 'kratos'),
'id' => 'g_007_appsecretkey',
'class' => 'hidden',
'type' => 'password',
);
$options[] = array(
'name' => __('Chrome 导航栏颜色', 'kratos'),
'desc' => __('Chrome 移动端浏览器导航栏的颜色', 'kratos'),