@cocalc/server
Version:
CoCalc server functionality: functions used by either the hub and the next.js server
33 lines (32 loc) • 1.45 kB
JavaScript
;
/*
Verify recaptcha or throw error on failure.
Does nothing if recaptcha is not configured on the server.
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const server_settings_1 = require("@cocalc/server/settings/server-settings");
const node_fetch_1 = __importDefault(require("node-fetch"));
async function reCaptcha(req) {
const { re_captcha_v3_secret_key } = await (0, server_settings_1.getServerSettings)();
if (!re_captcha_v3_secret_key)
return;
const { reCaptchaToken } = req.body;
if (!reCaptchaToken) {
throw Error("reCaptcha token must be provided");
}
// actually check it -- get the score via post request from google.
const url = `https://www.google.com/recaptcha/api/siteverify?secret=${re_captcha_v3_secret_key}&response=${reCaptchaToken}&remoteip=${req.socket.remoteAddress}`;
const response = await (0, node_fetch_1.default)(url);
const result = await response.json();
if (!result.success) {
throw Error(`reCaptcha may be misconfigured. ${JSON.stringify(result["error-codes"])}`);
}
if (!result.score || result.score < 0.5) {
throw Error("Only humans are allowed to use this feature. Please try again.");
}
}
exports.default = reCaptcha;
//# sourceMappingURL=recaptcha.js.map