mirror of
https://github.com/whyour/qinglong.git
synced 2025-05-23 14:56:07 +08:00
修改两步验证逻辑
This commit is contained in:
parent
035c69898f
commit
ab4f167bda
|
@ -151,13 +151,15 @@ 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);
|
const data = await authService.twoFactorLogin(req.body, req);
|
||||||
res.send(data);
|
res.send(data);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.error('🔥 error: %o', e);
|
logger.error('🔥 error: %o', e);
|
||||||
|
|
|
@ -34,6 +34,7 @@ export default class AuthService {
|
||||||
lastip,
|
lastip,
|
||||||
lastaddr,
|
lastaddr,
|
||||||
twoFactorActived,
|
twoFactorActived,
|
||||||
|
isTwoFactorChecking,
|
||||||
} = content;
|
} = content;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
|
@ -58,30 +59,35 @@ export default class AuthService {
|
||||||
|
|
||||||
const { ip, address } = await getNetIp(req);
|
const { ip, address } = await getNetIp(req);
|
||||||
if (username === cUsername && password === cPassword) {
|
if (username === cUsername && password === cPassword) {
|
||||||
|
if (twoFactorActived && !isTwoFactorChecking) {
|
||||||
|
this.updateAuthInfo(content, {
|
||||||
|
isTwoFactorChecking: true,
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
code: 420,
|
||||||
|
message: '请输入两步验证token',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const data = createRandomString(50, 100);
|
const data = createRandomString(50, 100);
|
||||||
const expiration = twoFactorActived ? 30 : 3;
|
const expiration = twoFactorActived ? 30 : 3;
|
||||||
let token = jwt.sign({ data }, config.secret as any, {
|
let token = jwt.sign({ data }, config.secret as any, {
|
||||||
expiresIn: 60 * 60 * 24 * expiration,
|
expiresIn: 60 * 60 * 24 * expiration,
|
||||||
algorithm: 'HS384',
|
algorithm: 'HS384',
|
||||||
});
|
});
|
||||||
|
|
||||||
this.updateAuthInfo(content, {
|
this.updateAuthInfo(content, {
|
||||||
token,
|
token,
|
||||||
lastlogon: timestamp,
|
lastlogon: timestamp,
|
||||||
retries: twoFactorActived ? retries : 0,
|
retries: 0,
|
||||||
lastip: ip,
|
lastip: ip,
|
||||||
lastaddr: address,
|
lastaddr: address,
|
||||||
|
isTwoFactorChecking: false,
|
||||||
});
|
});
|
||||||
if (twoFactorActived) {
|
return {
|
||||||
return {
|
code: 200,
|
||||||
code: 420,
|
data: { token, lastip, lastaddr, lastlogon, retries },
|
||||||
message: '请输入两步验证token',
|
};
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return {
|
|
||||||
code: 200,
|
|
||||||
data: { token, lastip, lastaddr, lastlogon, retries },
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this.updateAuthInfo(content, {
|
this.updateAuthInfo(content, {
|
||||||
retries: retries + 1,
|
retries: retries + 1,
|
||||||
|
@ -140,22 +146,24 @@ export default class AuthService {
|
||||||
return isValid;
|
return isValid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async twoFactorLogin({ code }) {
|
public async twoFactorLogin({ username, password, code }, req) {
|
||||||
const authInfo = this.getAuthInfo();
|
const authInfo = this.getAuthInfo();
|
||||||
const { token, lastip, lastaddr, lastlogon, retries, twoFactorSecret } =
|
const { isTwoFactorChecking, retries, twoFactorSecret } = authInfo;
|
||||||
authInfo;
|
if (!isTwoFactorChecking) {
|
||||||
|
return { code: 450, message: '未知错误' };
|
||||||
|
}
|
||||||
const isValid = authenticator.verify({
|
const isValid = authenticator.verify({
|
||||||
token: code,
|
token: code,
|
||||||
secret: twoFactorSecret,
|
secret: twoFactorSecret,
|
||||||
});
|
});
|
||||||
if (isValid) {
|
if (isValid) {
|
||||||
this.updateAuthInfo(authInfo, { retries: 0 });
|
return this.login({ username, password }, req);
|
||||||
return {
|
|
||||||
code: 200,
|
|
||||||
data: { token, lastip, lastaddr, lastlogon, retries },
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
this.updateAuthInfo(authInfo, { retries: retries + 1 });
|
const { ip, address } = await getNetIp(req);
|
||||||
|
this.updateAuthInfo(authInfo, {
|
||||||
|
lastip: ip,
|
||||||
|
lastaddr: address,
|
||||||
|
});
|
||||||
return { code: 430, message: '验证失败' };
|
return { code: 430, message: '验证失败' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ const Login = () => {
|
||||||
const { theme } = useTheme();
|
const { theme } = useTheme();
|
||||||
const [twoFactor, setTwoFactor] = useState(false);
|
const [twoFactor, setTwoFactor] = useState(false);
|
||||||
const [verifing, setVerifing] = useState(false);
|
const [verifing, setVerifing] = useState(false);
|
||||||
|
const [loginInfo, setLoginInfo] = useState<any>();
|
||||||
|
|
||||||
const handleOk = (values: any) => {
|
const handleOk = (values: any) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
@ -38,6 +39,10 @@ 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);
|
||||||
|
@ -54,7 +59,7 @@ const Login = () => {
|
||||||
setVerifing(true);
|
setVerifing(true);
|
||||||
request
|
request
|
||||||
.put(`${config.apiPrefix}user/two-factor/login`, {
|
.put(`${config.apiPrefix}user/two-factor/login`, {
|
||||||
data: { code: values.code },
|
data: { ...loginInfo, code: values.code },
|
||||||
})
|
})
|
||||||
.then((data: any) => {
|
.then((data: any) => {
|
||||||
if (data.code === 430) {
|
if (data.code === 430) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user