去掉动态码登入
This commit is contained in:
1 parent
98657ace5b
commit
4dbc3e767b
5 files changed
+41
-26
No files matched your search
+5
-2
@@ -1,6 +1,6 @@
|
||||
# Google Authenticator 动态码登录
|
||||
|
||||
保留原密码登录和 Google OAuth 登录,新增独立的“账号 + 六位动态码 + 图形验证码”登录方式。动态码登录不是强制双因素验证,不改变其他登录方式的认证规则。
|
||||
保留密码登录、Google OAuth 登录和独立的“账号 + 六位动态码 + 图形验证码”登录方式。已绑定 Google Authenticator 的账号,在密码登录(含 `/simple-login`)及 Google 登录交换票据时,必须提供有效且未使用的六位授权码 `totpCode`,否则不签发令牌。未绑定账号无需填写。
|
||||
|
||||
## 部署
|
||||
|
||||
@@ -25,12 +25,15 @@ $totpRng.Dispose()
|
||||
- 输入本地密码生成二维码;用 Google Authenticator 扫码,或手动输入页面展示的设置密钥,再输入手机六位动态码确认绑定。二维码在浏览器本地生成。
|
||||
- 临时绑定十分钟过期,未经验证不会启用。确认绑定所用码已消费,等手机刷新下一组码后再用于登录。
|
||||
- 登录页切换“动态码登录”,输入本地用户名、六位动态码和图形验证码。前导零必须保留。
|
||||
- 解绑需要本地密码及未使用过的动态码。遗失认证器时可用原密码或已绑定 Google 登录;需要重置绑定时由管理员核实身份后通过受控流程处理,系统不公开免验证重置接口。
|
||||
- 密码登录时,已绑定用户必须填写授权码。Google 授权返回后填写授权码并继续;票据只有 60 秒有效且仅能交换一次,验证失败或过期需重新发起 Google 登录。
|
||||
- 解绑需要本地密码及未使用过的动态码。遗失认证器时,密码或 Google 身份均不能绕过授权码;需要重置绑定时由管理员核实身份后通过受控流程处理,系统不公开免验证重置接口。
|
||||
|
||||
## API
|
||||
|
||||
| 方法 / 路径 | 认证 / 请求 |
|
||||
| --- | --- |
|
||||
| `POST /login`、`POST /simple-login` | 原登录参数增加 `totpCode`,已绑定用户必填 |
|
||||
| `POST /auth/google/exchange` | `ticket, totpCode`,已绑定用户必填授权码;失败后票据不可复用 |
|
||||
| `POST /auth/totp/login` | 匿名;`username, totpCode, code, uuid`,其中 code 是图形验证码;返回原有 `token` 字段 |
|
||||
| `GET /auth/totp/binding` | 当前 JWT;返回 `data.enabled`、`data.bound`,不返回密钥 |
|
||||
| `POST /auth/totp/setup` | 当前 JWT + `password`;返回 `data.secret`、`data.otpauthUrl`,禁止缓存或记录响应 |
|
||||
|
||||
+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