You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hydro/packages/recaptcha/handler.ts

28 lines
1.3 KiB
TypeScript

import superagent from 'superagent';
import { ForbiddenError, ValidationError } from 'hydrooj/src/error';
import { PRIV } from 'hydrooj/src/model/builtin';
import * as system from 'hydrooj/src/model/system';
import * as bus from 'hydrooj/src/service/bus';
bus.on('handler/before/UserRegister', async (thisArg) => {
if (!system.get('recaptcha.key')) return;
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 ForbiddenError('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">`;
});