recaptcha: init support
parent
f58db3eeb2
commit
8d3dc160a8
@ -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">`;
|
||||
});
|
@ -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);
|
||||
})();
|
@ -0,0 +1,4 @@
|
||||
key:
|
||||
type: text
|
||||
secret:
|
||||
type: text
|
Loading…
Reference in New Issue