recaptcha: init support

This commit is contained in:
undefined committed 2021-03-30 23:47:10 +08:00
1 parent f58db3eeb2
commit 8d3dc160a8
11 files changed
+80 -8

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
import superagent from 'superagent';
import { ValidationError } from 'hydrooj/dist/error';
import { PRIV } from 'hydrooj/dist/model/builtin';
import * as system from 'hydrooj/dist/model/system';
import * as bus from 'hydrooj/dist/service/bus';
bus.on('handler/before/UserRegister', async (thisArg) => {
if (thisArg.request.method !== 'post') {
thisArg.UiContext.recaptchaKey = system.get('recaptcha.key');
return;
}
if (thisArg.user.hasPriv(PRIV.PRIV_UNLIMITED_ACCESS)) return;
if (!thisArg.args.captcha) throw new ValidationError('captcha');
const response = await superagent.post('https://recaptcha.net/recaptcha/api/siteverify')
.field('secret', system.get('recaptcha.secret'))
.field('response', thisArg.args.captcha)
.field('remoteip', thisArg.request.ip);
if (!response.body.success) throw new ValidationError('captcha fail');
});
bus.on('handler/after/UserRegister', async (thisArg) => {
thisArg.response.body.captcha = `\
<script src="https://recaptcha.net/recaptcha/api.js?render=${system.get('recaptcha.key')}"></script>
<input type="text" name="captcha" id="_captcha" style="display:none">
<input type="submit" id="_submit" style="display:none">`;
});
+12
View File
@@ -0,0 +1,12 @@
{
"name": "@hydrooj/recaptcha",
"version": "1.0.0",
"description": "Google reCAPTCHA",
"main": "package.json",
"repository": "https://github.com/hydro-dev/Hydro.git",
"author": "undefined",
"license": "AGPL-3.0-only",
"dependencies": {
"superagent": "^6.1.0"
}
}
@@ -0,0 +1,18 @@
(() => {
const { AutoloadPage } = window.Hydro;
const page = new AutoloadPage('recaptcha', () => {
function captcha(event) {
event.preventDefault();
grecaptcha.ready(function () {
grecaptcha.execute(UiContext.recaptchaKey, { action: 'submit' }).then(function (token) {
document.getElementById('_captcha').value = token;
document.getElementById('_submit').click();
});
});
}
document.getElementById('submit').onclick = captcha;
});
window.Hydro.extraPages.push(page);
})();
+4
View File
@@ -0,0 +1,4 @@
key:
type: text
secret:
type: text