去掉动态码登入
This commit is contained in:
1 parent
98657ace5b
commit
4dbc3e767b
5 files changed
+41
-26
No files matched your search
+1
-1
@@ -59,7 +59,7 @@ public class GoogleOauthController {
|
||||
@PostMapping("/exchange")
|
||||
public AjaxResult exchange(@RequestBody Map<String, String> request) {
|
||||
AjaxResult result = AjaxResult.success();
|
||||
result.put(Constants.TOKEN, googleOauthService.exchangeTicket(request.get("ticket")));
|
||||
result.put(Constants.TOKEN, googleOauthService.exchangeTicket(request.get("ticket"), request.get("totpCode")));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ public class SysLoginController
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
// 生成令牌
|
||||
String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
|
||||
loginBody.getUuid(),true);
|
||||
loginBody.getUuid(),true, loginBody.getTotpCode());
|
||||
ajax.put(Constants.TOKEN, token);
|
||||
return ajax;
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class SysLoginController
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
// 生成令牌
|
||||
String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), null,
|
||||
loginBody.getUuid(),false);
|
||||
loginBody.getUuid(),false, loginBody.getTotpCode());
|
||||
ajax.put(Constants.TOKEN, token);
|
||||
return ajax;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package com.ruoyi.web.controller.system;
|
||||
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.domain.model.LoginBody;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.framework.web.service.TotpAuthService;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
@@ -13,7 +11,7 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/** Authenticator 六位动态码;只有 login 匿名开放,管理接口使用当前 JWT 用户 ID。 */
|
||||
/** Authenticator 绑定管理;所有接口使用当前 JWT 用户 ID,不提供独立动态码登录。 */
|
||||
@RestController
|
||||
@RequestMapping("/auth/totp")
|
||||
public class TotpAuthController {
|
||||
@@ -47,21 +45,7 @@ public class TotpAuthController {
|
||||
return AjaxResult.success("Authenticator 已解绑");
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public AjaxResult login(@RequestBody TotpRequest request, HttpServletRequest servletRequest,
|
||||
HttpServletResponse response) {
|
||||
response.setHeader("Cache-Control", "no-store");
|
||||
AjaxResult result = AjaxResult.success();
|
||||
result.put(Constants.TOKEN, service.login(request.getUsername(), request.getTotpCode(),
|
||||
request.getCode(), request.getUuid(), servletRequest.getRemoteAddr()));
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 禁止生成包含密码、动态码的 toString;动态码与图形验证码分开传输。 */
|
||||
public static class TotpRequest extends LoginBody {
|
||||
private String totpCode;
|
||||
|
||||
public String getTotpCode() { return totpCode; }
|
||||
public void setTotpCode(String totpCode) { this.totpCode = totpCode; }
|
||||
}
|
||||
}
|
||||
+32
-4
@@ -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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user