更新两步验证逻辑

This commit is contained in:
hanhh 2021-09-01 23:01:49 +08:00
parent 97baff8baf
commit 00cb915f48
3 changed files with 38 additions and 62 deletions

View File

@ -151,15 +151,13 @@ export default (app: Router) => {
celebrate({ celebrate({
body: Joi.object({ body: Joi.object({
code: Joi.string().required(), code: Joi.string().required(),
username: Joi.string().required(),
password: Joi.string().required(),
}), }),
}), }),
async (req: Request, res: Response, next: NextFunction) => { async (req: Request, res: Response, next: NextFunction) => {
const logger: Logger = Container.get('logger'); const logger: Logger = Container.get('logger');
try { try {
const authService = Container.get(AuthService); const authService = Container.get(AuthService);
const data = await authService.twoFactorLogin(req.body, req); const data = await authService.twoFactorLogin(req.body);
res.send(data); res.send(data);
} catch (e) { } catch (e) {
logger.error('🔥 error: %o', e); logger.error('🔥 error: %o', e);

View File

@ -23,7 +23,7 @@ export default class AuthService {
} }
let { username, password } = payloads; let { username, password } = payloads;
const content = fs.readFileSync(config.authConfigFile, 'utf8'); const content = this.getAuthInfo();
const timestamp = Date.now(); const timestamp = Date.now();
if (content) { if (content) {
const { const {
@ -34,8 +34,7 @@ export default class AuthService {
lastip, lastip,
lastaddr, lastaddr,
twoFactorActived, twoFactorActived,
twoFactorChecked, } = content;
} = JSON.parse(content);
if ( if (
(cUsername === 'admin' && cPassword === 'adminadmin') || (cUsername === 'admin' && cPassword === 'adminadmin') ||
@ -45,21 +44,7 @@ export default class AuthService {
return this.initAuthInfo(); return this.initAuthInfo();
} }
if (twoFactorActived && !twoFactorChecked) {
return {
code: 420,
message: '请输入两步验证token',
};
}
if (retries > 2 && Date.now() - lastlogon < Math.pow(3, retries) * 1000) { if (retries > 2 && Date.now() - lastlogon < Math.pow(3, retries) * 1000) {
fs.writeFileSync(
config.authConfigFile,
JSON.stringify({
...JSON.parse(content),
twoFactorChecked: false,
}),
);
return { return {
code: 410, code: 410,
message: `失败次数过多,请${Math.round( message: `失败次数过多,请${Math.round(
@ -79,34 +64,31 @@ export default class AuthService {
expiresIn: 60 * 60 * 24 * expiration, expiresIn: 60 * 60 * 24 * expiration,
algorithm: 'HS384', algorithm: 'HS384',
}); });
fs.writeFileSync( this.updateAuthInfo(content, {
config.authConfigFile, token,
JSON.stringify({ lastlogon: timestamp,
...JSON.parse(content), retries: twoFactorActived ? retries : 0,
token, lastip: ip,
lastlogon: timestamp, lastaddr: address,
retries: 0, });
lastip: ip, if (twoFactorActived) {
lastaddr: address, return {
twoFactorChecked: false, code: 420,
}), message: '请输入两步验证token',
); };
return { } else {
code: 200, return {
data: { token, lastip, lastaddr, lastlogon, retries }, code: 200,
}; data: { token, lastip, lastaddr, lastlogon, retries },
};
}
} else { } else {
fs.writeFileSync( this.updateAuthInfo(content, {
config.authConfigFile, retries: retries + 1,
JSON.stringify({ lastlogon: timestamp,
...JSON.parse(content), lastip: ip,
retries: retries + 1, lastaddr: address,
lastlogon: timestamp, });
lastip: ip,
lastaddr: address,
twoFactorChecked: false,
}),
);
return { code: 400, message: config.authError }; return { code: 400, message: config.authError };
} }
} else { } else {
@ -158,21 +140,22 @@ export default class AuthService {
return isValid; return isValid;
} }
public async twoFactorLogin({ username, password, code }, req) { public async twoFactorLogin({ code }) {
const authInfo = this.getAuthInfo(); const authInfo = this.getAuthInfo();
const { token, lastip, lastaddr, lastlogon, retries, twoFactorSecret } =
authInfo;
const isValid = authenticator.verify({ const isValid = authenticator.verify({
token: code, token: code,
secret: authInfo.twoFactorSecret, secret: twoFactorSecret,
}); });
if (isValid) { if (isValid) {
this.updateAuthInfo(authInfo, { twoFactorChecked: true }); this.updateAuthInfo(authInfo, { retries: 0 });
return this.login({ username, password }, req); return {
code: 200,
data: { token, lastip, lastaddr, lastlogon, retries },
};
} else { } else {
const { ip, address } = await getNetIp(req); this.updateAuthInfo(authInfo, { retries: retries + 1 });
this.updateAuthInfo(authInfo, {
lastip: ip,
lastaddr: address,
});
return { code: 430, message: '验证失败' }; return { code: 430, message: '验证失败' };
} }
} }

View File

@ -23,7 +23,6 @@ const Login = () => {
const [waitTime, setWaitTime] = useState<any>(); const [waitTime, setWaitTime] = useState<any>();
const { theme } = useTheme(); const { theme } = useTheme();
const [twoFactor, setTwoFactor] = useState(false); const [twoFactor, setTwoFactor] = useState(false);
const [loginInfo, setLoginInfo] = useState<any>();
const [verifing, setVerifing] = useState(false); const [verifing, setVerifing] = useState(false);
const handleOk = (values: any) => { const handleOk = (values: any) => {
@ -39,10 +38,6 @@ const Login = () => {
}) })
.then((data) => { .then((data) => {
if (data.code === 420) { if (data.code === 420) {
setLoginInfo({
username: values.username,
password: values.password,
});
setTwoFactor(true); setTwoFactor(true);
} else { } else {
checkResponse(data); checkResponse(data);
@ -59,7 +54,7 @@ const Login = () => {
setVerifing(true); setVerifing(true);
request request
.put(`${config.apiPrefix}user/two-factor/login`, { .put(`${config.apiPrefix}user/two-factor/login`, {
data: { ...loginInfo, code: values.code }, data: { code: values.code },
}) })
.then((data: any) => { .then((data: any) => {
if (data.code === 430) { if (data.code === 430) {