去掉动态码登入

This commit is contained in:
lu7213 committed 2026-11-16 06:49:49 -05:00
1 parent 98657ace5b
commit 4dbc3e767b
5 files changed
+41 -26

No files matched your search

@@ -4,8 +4,6 @@ import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.TotpUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.framework.web.service.TotpAuthService;
import com.ruoyi.framework.web.service.TotpSecretCipher;
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
@@ -50,8 +48,7 @@ public class TotpBindingServiceTest {
user.setPassword(SecurityUtils.encryptPassword("test-only-password"));
when(users.selectUserById(7L)).thenReturn(user);
cipher = new TotpSecretCipher(Base64.getEncoder().encodeToString(new byte[32]));
service = new TotpAuthService(mapper, cipher, redis, users, mock(UserDetailsServiceImpl.class),
mock(TokenService.class), mock(SysLoginService.class));
service = new TotpAuthService(mapper, cipher, redis, users, mock(UserDetailsServiceImpl.class));
ReflectionTestUtils.setField(service, "issuer", "USDT PAY");
secret = TotpUtils.newSecret();
encrypted = cipher.encrypt(7L, secret);
@@ -114,6 +111,37 @@ public class TotpBindingServiceTest {
expectFailure(() -> service.setup(7L, "test-only-password"));
}
@Test
public void unboundUserDoesNotRequireSecondFactor() {
service.verifyBoundUser(7L, null);
verify(mapper, never()).consume(anyLong(), anyString(), anyLong());
}
@Test
public void boundUserRequiresSixDigitsAndAtomicConsumption() {
when(mapper.selectSecret(7L)).thenReturn(encrypted);
expectFailure(() -> service.verifyBoundUser(7L, null));
expectFailure(() -> service.verifyBoundUser(7L, ""));
expectFailure(() -> service.verifyBoundUser(7L, "12345"));
expectFailure(() -> service.verifyBoundUser(7L, "abcdef"));
verify(mapper, never()).consume(anyLong(), anyString(), anyLong());
// 正确动态码仍必须原子消费成功;重复或并发消费失败不能登录。
expectFailure(() -> service.verifyBoundUser(7L, currentCode()));
when(mapper.consume(eq(7L), eq(encrypted), anyLong())).thenReturn(1);
service.verifyBoundUser(7L, currentCode());
}
@Test
public void boundUserCannotLoginWhenCredentialStoreFails() {
when(mapper.selectSecret(7L)).thenThrow(new IllegalStateException("database unavailable"));
try {
service.verifyBoundUser(7L, null);
fail("数据库异常不能跳过第二因素");
} catch (IllegalStateException expected) {
verify(mapper, never()).consume(anyLong(), anyString(), anyLong());
}
}
private String currentCode() {
return TotpUtils.generate(secret, Instant.now().getEpochSecond() / 30);
}