mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: user: remove sms
This commit is contained in:
1 file changed
+22
-37
@@ -1,14 +1,13 @@
|
|||||||
import { generateAuthenticationOptions, verifyAuthenticationResponse } from '@simplewebauthn/server';
|
import { generateAuthenticationOptions, verifyAuthenticationResponse } from '@simplewebauthn/server';
|
||||||
import moment from 'moment-timezone';
|
import moment from 'moment-timezone';
|
||||||
import type { Binary } from 'mongodb';
|
import type { Binary } from 'mongodb';
|
||||||
import { Time } from '@hydrooj/utils';
|
|
||||||
import type { Context } from '../context';
|
import type { Context } from '../context';
|
||||||
import {
|
import {
|
||||||
AuthOperationError, BlacklistedError, BuiltinLoginError, ForbiddenError, InvalidTokenError,
|
AuthOperationError, BlacklistedError, BuiltinLoginError, ForbiddenError, InvalidTokenError,
|
||||||
SystemError, UserAlreadyExistError, UserFacingError,
|
SystemError, UserAlreadyExistError, UserFacingError,
|
||||||
UserNotFoundError, ValidationError, VerifyPasswordError,
|
UserNotFoundError, ValidationError, VerifyPasswordError,
|
||||||
} from '../error';
|
} from '../error';
|
||||||
import { Udoc, User } from '../interface';
|
import { TokenDoc, Udoc, User } from '../interface';
|
||||||
import avatar from '../lib/avatar';
|
import avatar from '../lib/avatar';
|
||||||
import { sendMail } from '../lib/mail';
|
import { sendMail } from '../lib/mail';
|
||||||
import { verifyTFA } from '../lib/verifyTFA';
|
import { verifyTFA } from '../lib/verifyTFA';
|
||||||
@@ -197,10 +196,8 @@ export class UserRegisterHandler extends Handler {
|
|||||||
this.response.template = 'user_register.html';
|
this.response.template = 'user_register.html';
|
||||||
}
|
}
|
||||||
|
|
||||||
@post('mail', Types.Email, true)
|
@post('mail', Types.Email)
|
||||||
@post('phone', Types.String, true, (s) => /^\d{11}$/.test(s))
|
async post({ }, mail: string) {
|
||||||
async post(domainId: string, mail: string, phoneNumber: string) {
|
|
||||||
if (mail) {
|
|
||||||
if (await user.getByEmail('system', mail)) throw new UserAlreadyExistError(mail);
|
if (await user.getByEmail('system', mail)) throw new UserAlreadyExistError(mail);
|
||||||
const mailDomain = mail.split('@')[1];
|
const mailDomain = mail.split('@')[1];
|
||||||
if (await BlackListModel.get(`mail::${mailDomain}`)) throw new BlacklistedError(mailDomain);
|
if (await BlackListModel.get(`mail::${mailDomain}`)) throw new BlacklistedError(mailDomain);
|
||||||
@@ -226,36 +223,26 @@ export class UserRegisterHandler extends Handler {
|
|||||||
this.response.template = 'user_register_mail_sent.html';
|
this.response.template = 'user_register_mail_sent.html';
|
||||||
this.response.body = { mail };
|
this.response.body = { mail };
|
||||||
} else this.response.redirect = this.url('user_register_with_code', { code: t[0] });
|
} else this.response.redirect = this.url('user_register_with_code', { code: t[0] });
|
||||||
} else if (phoneNumber) {
|
|
||||||
if (!global.Hydro.lib.sendSms) throw new SystemError('Cannot send sms');
|
|
||||||
await Promise.all([
|
|
||||||
this.limitRate(`send_sms_${phoneNumber}`, 60, 1, false),
|
|
||||||
this.limitRate('send_sms', 3600, 15, false),
|
|
||||||
oplog.log(this, 'user.register', {}),
|
|
||||||
]);
|
|
||||||
const id = String.random(6, '0123456789');
|
|
||||||
await token.add(
|
|
||||||
token.TYPE_REGISTRATION,
|
|
||||||
10 * Time.minute,
|
|
||||||
{ phone: phoneNumber },
|
|
||||||
id,
|
|
||||||
);
|
|
||||||
await global.Hydro.lib.sendSms(phoneNumber, 'register', id);
|
|
||||||
this.response.body = { phone: phoneNumber };
|
|
||||||
this.response.template = 'user_register_sms.html';
|
|
||||||
} else throw new ValidationError('mail');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class UserRegisterWithCodeHandler extends Handler {
|
class UserRegisterWithCodeHandler extends Handler {
|
||||||
noCheckPermView = true;
|
noCheckPermView = true;
|
||||||
|
tdoc: TokenDoc;
|
||||||
|
|
||||||
@param('code', Types.String)
|
@param('code', Types.String)
|
||||||
async get(domainId: string, code: string) {
|
async prepare({ }, code: string) {
|
||||||
|
this.tdoc = await token.get(code, token.TYPE_REGISTRATION);
|
||||||
|
if (!this.tdoc || (!this.tdoc.mail && !this.tdoc.phone)) {
|
||||||
|
// prevent brute forcing tokens
|
||||||
|
await this.limitRate('user_register_with_code', 60, 5, false);
|
||||||
|
throw new InvalidTokenError(token.TYPE_TEXTS[token.TYPE_REGISTRATION], code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async get() {
|
||||||
this.response.template = 'user_register_with_code.html';
|
this.response.template = 'user_register_with_code.html';
|
||||||
const tdoc = await token.get(code, token.TYPE_REGISTRATION);
|
this.response.body = this.tdoc;
|
||||||
if (!tdoc) throw new InvalidTokenError(token.TYPE_TEXTS[token.TYPE_REGISTRATION], code);
|
|
||||||
this.response.body = tdoc;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@param('password', Types.Password)
|
@param('password', Types.Password)
|
||||||
@@ -266,26 +253,24 @@ class UserRegisterWithCodeHandler extends Handler {
|
|||||||
domainId: string, password: string, verify: string,
|
domainId: string, password: string, verify: string,
|
||||||
uname: string, code: string,
|
uname: string, code: string,
|
||||||
) {
|
) {
|
||||||
const tdoc = await token.get(code, token.TYPE_REGISTRATION);
|
|
||||||
if (!tdoc || (!tdoc.mail && !tdoc.phone)) throw new InvalidTokenError(token.TYPE_TEXTS[token.TYPE_REGISTRATION], code);
|
|
||||||
if (password !== verify) throw new VerifyPasswordError();
|
if (password !== verify) throw new VerifyPasswordError();
|
||||||
if (tdoc.phone) tdoc.mail = `${String.random(12)}@hydro.local`;
|
if (this.tdoc.phone) this.tdoc.mail = `${String.random(12)}@hydro.local`;
|
||||||
const uid = await user.create(tdoc.mail, uname, password, undefined, this.request.ip);
|
const uid = await user.create(this.tdoc.mail, uname, password, undefined, this.request.ip);
|
||||||
await token.del(code, token.TYPE_REGISTRATION);
|
await token.del(code, token.TYPE_REGISTRATION);
|
||||||
const [id, mailDomain] = tdoc.mail.split('@');
|
const [id, mailDomain] = this.tdoc.mail.split('@');
|
||||||
const $set: any = tdoc.set || {};
|
const $set: any = this.tdoc.set || {};
|
||||||
if (tdoc.phone) $set.phone = tdoc.phone;
|
if (this.tdoc.phone) $set.phone = this.tdoc.phone;
|
||||||
if (mailDomain === 'qq.com' && !Number.isNaN(+id)) $set.avatar = `qq:${id}`;
|
if (mailDomain === 'qq.com' && !Number.isNaN(+id)) $set.avatar = `qq:${id}`;
|
||||||
if (this.session.viewLang) $set.viewLang = this.session.viewLang;
|
if (this.session.viewLang) $set.viewLang = this.session.viewLang;
|
||||||
if (Object.keys($set).length) await user.setById(uid, $set);
|
if (Object.keys($set).length) await user.setById(uid, $set);
|
||||||
if (tdoc.oauth) await oauth.set(tdoc.oauth[1], uid);
|
if (this.tdoc.oauth) await oauth.set(this.tdoc.oauth[1], uid);
|
||||||
this.context.HydroContext.user = await user.getById(domainId, uid);
|
this.context.HydroContext.user = await user.getById(domainId, uid);
|
||||||
this.session.viewLang = '';
|
this.session.viewLang = '';
|
||||||
this.session.uid = uid;
|
this.session.uid = uid;
|
||||||
this.session.sudoUid = null;
|
this.session.sudoUid = null;
|
||||||
this.session.scope = PERM.PERM_ALL.toString();
|
this.session.scope = PERM.PERM_ALL.toString();
|
||||||
this.session.recreate = true;
|
this.session.recreate = true;
|
||||||
this.response.redirect = tdoc.redirect || this.url('home_settings', { category: 'preference' });
|
this.response.redirect = this.tdoc.redirect || this.url('home_settings', { category: 'preference' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user