友好的captcha-javascript
一个 Javascript 客户端 友好的卡普查语Name 服务。 这个客户端可以方便地与友好的Captcha API整合和验证Captcha响应.
这个图书馆是用来 友谊 Captcha v2 只有这个 如果你在找V1,请看 这里
这是您在后端代码中使用的库。关于您在前端使用的代码,请参见 @ 友好的captcha/ sdk。 。 。 .
安装
使用 国家预防机制
npm install @friendlycaptcha/server-sdk
使用量
下面是一些如何使用客户的基本例子.
以更详细的例子来看 实例 目录.
初始化
import { FriendlyCaptchaClient } from "@friendlycaptcha/server-sdk";
const frcClient = new FriendlyCaptchaClient({
apiKey: "YOUR_API_KEY",
sitekey: "YOUR_SITEKEY",
});
正在验证 Captcha 响应
打电话后 verifyCaptchaResponse 使用 captcha 响应后,结果对象上有两个功能,您应当检查:
wasAbleToVerify()说明我们是否能够核实Captcha的答复。 这将是false以防网络/我们的服务出现问题或配置出现错误.shouldAccept()说明Captcha的答复是否正确。 如果客户端以非限制性模式运行( 默认) 和wasAbleToVerify()已返回false,这将是true。 。 。 .
下面是这种行为的一些例子.
校验准确的随机反应
const result = await frcClient.verifyCaptchaResponse("CORRECT_CAPTCHA_RESPONSE_HERE");
console.log(result.wasAbleToVerify()); // true
console.log(result.shouldAccept()); // true
验证一个不正确的控制反应, 且不会出现问题 :
const result = await frcClient.verifyCaptchaResponse("INCORRECT_CAPTCHA_RESPONSE_HERE");
console.log(result.wasAbleToVerify()); // true
console.log(result.shouldAccept()); // false
在非限制性模式下运行( 默认) 时验证对问题( 网络问题或不良配置) 的错误的 captcha 响应 :
const result = await frcClient.verifyCaptchaResponse("INCORRECT_CAPTCHA_RESPONSE_HERE");
console.log(result.wasAbleToVerify()); // false
console.log(result.shouldAccept()); // true
严格运行时验证对问题(网络/服务问题或不良配置)的错误回应:
const frcClient = new FriendlyCaptchaClient({
...
strict: true,
});
const result = await frcClient.verifyCaptchaResponse("INCORRECT_CAPTCHA_RESPONSE_HERE");
console.log(result.wasAbleToVerify()); // false
console.log(result.shouldAccept()); // false
获取风险情报
您可以为给定的符号获取风险情报数据 。 这提供了关于请求风险状况的详细资料,包括网络数据、地理定位、浏览器细节和风险分数.
const result = await frcClient.retrieveRiskIntelligence("RISK_INTELLIGENCE_TOKEN_HERE");
// Check if we were able to retrieve the risk intelligence data
if (result.wasAbleToRetrieve()) {
// Check if the token is valid and data was retrieved successfully
if (result.isValid()) {
const response = result.getResponse();
console.log("Risk Intelligence Data:", response.data);
} else {
// Token was invalid or expired
const error = result.getResponseError();
console.log("Error:", error?.error_code, error?.detail);
}
} else {
// Network issue or configuration problem
if (result.isClientError()) {
console.log("Configuration error - check your API key");
} else {
console.log("Network issue or service temporarily unavailable");
}
}
配置
配置
客户端提供多个配置选项:
- 键: 您的友好 Captcha API 密钥 .
- 站台密钥您的友好卡普查网站密钥.
- 严格: (可选) 如果客户端完全无法验证 Captcha 响应( 例如网络故障或配置错误) , 默认
verifyCaptchaResponse返回时true随便 路过strict: true它会回来false相反:每个答复都需要严格核实. - 尾点: (可选)站点校验API的端点域. 简称
eu或者说global还接受。 默认是global。 。 。 . - 获取: (可选) 获取要使用的执行 。 默认为
globalThis.fetch。 。 。 .
发展
安装依赖关系
npm install
运行测试
首先运行 SDK 测试服务器,然后运行 npm test。 。 。 .
docker run -p 1090:1090 friendlycaptcha/sdk-testserver:latest
npm test
生产建筑
npm run build:dist
许可证
开源 麻省理工学院。 。 。 .