|
|
@@ -3,24 +3,60 @@ package pub
|
|
|
import (
|
|
|
"context"
|
|
|
"errors"
|
|
|
- "github.com/stretchr/testify/assert"
|
|
|
- "github.com/stretchr/testify/require"
|
|
|
- "github.com/zeromicro/go-zero/core/limit"
|
|
|
- "github.com/zeromicro/go-zero/core/stores/redis"
|
|
|
+ "testing"
|
|
|
+ "time"
|
|
|
+
|
|
|
"perms-system-server/internal/middleware"
|
|
|
"perms-system-server/internal/response"
|
|
|
"perms-system-server/internal/svc"
|
|
|
"perms-system-server/internal/testutil"
|
|
|
"perms-system-server/internal/types"
|
|
|
- "testing"
|
|
|
- "time"
|
|
|
+
|
|
|
+ "github.com/stretchr/testify/assert"
|
|
|
+ "github.com/stretchr/testify/require"
|
|
|
+ "github.com/zeromicro/go-zero/core/limit"
|
|
|
+ "github.com/zeromicro/go-zero/core/stores/redis"
|
|
|
)
|
|
|
|
|
|
+func newAdminLoginReq(username, password, managementKey string) *types.AdminLoginReq {
|
|
|
+ id, code := "cap_"+testutil.UniqueId(), "9999"
|
|
|
+ defaultCaptchaStore.Set(id, code)
|
|
|
+ return &types.AdminLoginReq{
|
|
|
+ Username: username,
|
|
|
+ Password: password,
|
|
|
+ ManagementKey: managementKey,
|
|
|
+ CaptchaId: id,
|
|
|
+ CaptchaCode: code,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// TC-1251: cap.js 已启用时管理后台传统登录接口被拒绝
|
|
|
+func TestAdminLogin_CapEnabled_Rejected(t *testing.T) {
|
|
|
+ ctx := context.Background()
|
|
|
+ svcCtx := newTestSvcCtx() // Capjs.Enable=1
|
|
|
+
|
|
|
+ logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
+ resp, err := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
+ Username: "user",
|
|
|
+ Password: "pass",
|
|
|
+ ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
+ })
|
|
|
+ require.Nil(t, resp)
|
|
|
+ require.Error(t, err)
|
|
|
+
|
|
|
+ var codeErr *response.CodeError
|
|
|
+ require.True(t, errors.As(err, &codeErr))
|
|
|
+ assert.Equal(t, 400, codeErr.Code())
|
|
|
+ assert.Contains(t, codeErr.Error(), "当前已启用人机验证")
|
|
|
+}
|
|
|
+
|
|
|
+// TC-0015: 超管正常登录
|
|
|
func TestAdminLogin_SuperAdmin(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
username := testutil.UniqueId()
|
|
|
password := "TestPass123"
|
|
|
+ captchaId, captchaCode := setupCaptcha(t)
|
|
|
|
|
|
_, cleanUser := insertTestUser(t, ctx, svcCtx, username, password, 1, 1)
|
|
|
t.Cleanup(cleanUser)
|
|
|
@@ -30,6 +66,8 @@ func TestAdminLogin_SuperAdmin(t *testing.T) {
|
|
|
Username: username,
|
|
|
Password: password,
|
|
|
ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
+ CaptchaId: captchaId,
|
|
|
+ CaptchaCode: captchaCode,
|
|
|
})
|
|
|
require.NoError(t, err)
|
|
|
require.NotNil(t, resp)
|
|
|
@@ -48,7 +86,7 @@ func TestAdminLogin_SuperAdmin(t *testing.T) {
|
|
|
// TC-0016: 普通用户被拒绝(1修复: 仅超管可通过管理后台登录)
|
|
|
func TestAdminLogin_NormalUserRejected(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
username := testutil.UniqueId()
|
|
|
password := "TestPass123"
|
|
|
|
|
|
@@ -56,11 +94,7 @@ func TestAdminLogin_NormalUserRejected(t *testing.T) {
|
|
|
t.Cleanup(cleanUser)
|
|
|
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
- resp, err := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: password,
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ resp, err := logic.AdminLogin(newAdminLoginReq(username, password, svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Nil(t, resp)
|
|
|
require.Error(t, err)
|
|
|
|
|
|
@@ -73,14 +107,10 @@ func TestAdminLogin_NormalUserRejected(t *testing.T) {
|
|
|
// TC-0017: managementKey无效
|
|
|
func TestAdminLogin_InvalidManagementKey(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
- resp, err := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: "anyone",
|
|
|
- Password: "pass",
|
|
|
- ManagementKey: "wrong-key",
|
|
|
- })
|
|
|
+ resp, err := logic.AdminLogin(newAdminLoginReq("anyone", "pass", "wrong-key"))
|
|
|
require.Nil(t, resp)
|
|
|
require.Error(t, err)
|
|
|
|
|
|
@@ -93,14 +123,10 @@ func TestAdminLogin_InvalidManagementKey(t *testing.T) {
|
|
|
// TC-0018: managementKey为空
|
|
|
func TestAdminLogin_EmptyManagementKey(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
- resp, err := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: "anyone",
|
|
|
- Password: "pass",
|
|
|
- ManagementKey: "",
|
|
|
- })
|
|
|
+ resp, err := logic.AdminLogin(newAdminLoginReq("anyone", "pass", ""))
|
|
|
require.Nil(t, resp)
|
|
|
require.Error(t, err)
|
|
|
|
|
|
@@ -113,14 +139,10 @@ func TestAdminLogin_EmptyManagementKey(t *testing.T) {
|
|
|
// TC-0019: 用户不存在
|
|
|
func TestAdminLogin_UserNotFound(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
- resp, err := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: "nonexistent_" + testutil.UniqueId(),
|
|
|
- Password: "whatever",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ resp, err := logic.AdminLogin(newAdminLoginReq("nonexistent_"+testutil.UniqueId(), "whatever", svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Nil(t, resp)
|
|
|
require.Error(t, err)
|
|
|
|
|
|
@@ -133,18 +155,14 @@ func TestAdminLogin_UserNotFound(t *testing.T) {
|
|
|
// TC-0020: 密码错误
|
|
|
func TestAdminLogin_WrongPassword(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
username := testutil.UniqueId()
|
|
|
|
|
|
_, cleanUser := insertTestUser(t, ctx, svcCtx, username, "CorrectPass", 1, 2)
|
|
|
t.Cleanup(cleanUser)
|
|
|
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
- resp, err := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: "WrongPass",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ resp, err := logic.AdminLogin(newAdminLoginReq(username, "WrongPass", svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Nil(t, resp)
|
|
|
require.Error(t, err)
|
|
|
|
|
|
@@ -157,7 +175,7 @@ func TestAdminLogin_WrongPassword(t *testing.T) {
|
|
|
// TC-0021: 账号冻结
|
|
|
func TestAdminLogin_AccountFrozen(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
username := testutil.UniqueId()
|
|
|
password := "TestPass123"
|
|
|
|
|
|
@@ -165,11 +183,7 @@ func TestAdminLogin_AccountFrozen(t *testing.T) {
|
|
|
t.Cleanup(cleanUser)
|
|
|
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
- resp, err := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: password,
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ resp, err := logic.AdminLogin(newAdminLoginReq(username, password, svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Nil(t, resp)
|
|
|
require.Error(t, err)
|
|
|
|
|
|
@@ -182,7 +196,7 @@ func TestAdminLogin_AccountFrozen(t *testing.T) {
|
|
|
// TC-0022: 不带productCode时token无权限(perms为空)
|
|
|
func TestAdminLogin_NoPermsWithoutProductCode(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
username := testutil.UniqueId()
|
|
|
password := "TestPass123"
|
|
|
|
|
|
@@ -190,11 +204,7 @@ func TestAdminLogin_NoPermsWithoutProductCode(t *testing.T) {
|
|
|
t.Cleanup(cleanUser)
|
|
|
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
- resp, err := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: password,
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ resp, err := logic.AdminLogin(newAdminLoginReq(username, password, svcCtx.Config.Auth.ManagementKey))
|
|
|
require.NoError(t, err)
|
|
|
require.NotNil(t, resp)
|
|
|
assert.NotNil(t, resp.UserInfo.Perms, "Perms 必须为非 nil 的空 slice([]string{})")
|
|
|
@@ -205,7 +215,7 @@ func TestAdminLogin_NoPermsWithoutProductCode(t *testing.T) {
|
|
|
// TC-0025: adminLogin 用户名级别限流(修复验证)
|
|
|
func TestAdminLogin_UsernameRateLimit(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
require.NotNil(t, svcCtx.UsernameLoginLimit, "UsernameLoginLimit 应被配置")
|
|
|
|
|
|
username := "rl_" + testutil.UniqueId()
|
|
|
@@ -213,11 +223,7 @@ func TestAdminLogin_UsernameRateLimit(t *testing.T) {
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
var last error
|
|
|
for i := 0; i < 11; i++ {
|
|
|
- _, last = logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: "wrong_pass",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ _, last = logic.AdminLogin(newAdminLoginReq(username, "wrong_pass", svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Error(t, last)
|
|
|
}
|
|
|
var ce *response.CodeError
|
|
|
@@ -228,14 +234,10 @@ func TestAdminLogin_UsernameRateLimit(t *testing.T) {
|
|
|
// TC-0024: SQL注入username
|
|
|
func TestAdminLogin_SQLInjection(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
- resp, err := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: "' OR 1=1 --",
|
|
|
- Password: "anything",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ resp, err := logic.AdminLogin(newAdminLoginReq("' OR 1=1 --", "anything", svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Nil(t, resp)
|
|
|
require.Error(t, err)
|
|
|
|
|
|
@@ -248,8 +250,9 @@ func TestAdminLogin_SQLInjection(t *testing.T) {
|
|
|
func newAdminLimitSvcCtx(t *testing.T, quota int) *svc.ServiceContext {
|
|
|
t.Helper()
|
|
|
cfg := testutil.GetTestConfig()
|
|
|
+ cfg.Capjs.Enable = 0
|
|
|
rds := redis.MustNewRedis(cfg.CacheRedis.Nodes[0].RedisConf)
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := svc.NewServiceContext(cfg)
|
|
|
svcCtx.UsernameLoginLimit = limit.NewPeriodLimit(300, quota, rds,
|
|
|
cfg.CacheRedis.KeyPrefix+":rl:adminlogin:ut:"+testutil.UniqueId())
|
|
|
return svcCtx
|
|
|
@@ -260,19 +263,15 @@ func TestAdminLogin_H1_SameIPSameUsername_OverQuota429(t *testing.T) {
|
|
|
svcCtx := newAdminLimitSvcCtx(t, 1)
|
|
|
username := "h1_user_" + testutil.UniqueId()
|
|
|
ctx := middleware.WithClientIP(context.Background(), "1.2.3.4")
|
|
|
- req := &types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: "bad",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- }
|
|
|
+ mk := svcCtx.Config.Auth.ManagementKey
|
|
|
|
|
|
- _, err := NewAdminLoginLogic(ctx, svcCtx).AdminLogin(req)
|
|
|
+ _, err := NewAdminLoginLogic(ctx, svcCtx).AdminLogin(newAdminLoginReq(username, "bad", mk))
|
|
|
require.Error(t, err)
|
|
|
var ce *response.CodeError
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
assert.Equal(t, 401, ce.Code(), "首次调用应被限流放行并进入业务层,得到 401")
|
|
|
|
|
|
- _, err = NewAdminLoginLogic(ctx, svcCtx).AdminLogin(req)
|
|
|
+ _, err = NewAdminLoginLogic(ctx, svcCtx).AdminLogin(newAdminLoginReq(username, "bad", mk))
|
|
|
require.Error(t, err)
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
assert.Equal(t, 429, ce.Code(), "同 IP+同 username 第二次必须 429")
|
|
|
@@ -283,26 +282,22 @@ func TestAdminLogin_H1_SameIPSameUsername_OverQuota429(t *testing.T) {
|
|
|
func TestAdminLogin_H1_DifferentIPSameUsername_IndependentBucket(t *testing.T) {
|
|
|
svcCtx := newAdminLimitSvcCtx(t, 1)
|
|
|
username := "h1_iso_" + testutil.UniqueId()
|
|
|
- req := &types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: "bad",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- }
|
|
|
+ mk := svcCtx.Config.Auth.ManagementKey
|
|
|
|
|
|
ctxA := middleware.WithClientIP(context.Background(), "10.0.0.1")
|
|
|
- _, err := NewAdminLoginLogic(ctxA, svcCtx).AdminLogin(req)
|
|
|
+ _, err := NewAdminLoginLogic(ctxA, svcCtx).AdminLogin(newAdminLoginReq(username, "bad", mk))
|
|
|
require.Error(t, err)
|
|
|
var ce *response.CodeError
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
assert.Equal(t, 401, ce.Code())
|
|
|
|
|
|
- _, err = NewAdminLoginLogic(ctxA, svcCtx).AdminLogin(req)
|
|
|
+ _, err = NewAdminLoginLogic(ctxA, svcCtx).AdminLogin(newAdminLoginReq(username, "bad", mk))
|
|
|
require.Error(t, err)
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
assert.Equal(t, 429, ce.Code(), "IP-A 配额已满")
|
|
|
|
|
|
ctxB := middleware.WithClientIP(context.Background(), "10.0.0.2")
|
|
|
- _, err = NewAdminLoginLogic(ctxB, svcCtx).AdminLogin(req)
|
|
|
+ _, err = NewAdminLoginLogic(ctxB, svcCtx).AdminLogin(newAdminLoginReq(username, "bad", mk))
|
|
|
require.Error(t, err)
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
assert.Equal(t, 401, ce.Code(),
|
|
|
@@ -313,20 +308,16 @@ func TestAdminLogin_H1_DifferentIPSameUsername_IndependentBucket(t *testing.T) {
|
|
|
func TestAdminLogin_H1_MissingClientIP_FallbackBucket(t *testing.T) {
|
|
|
svcCtx := newAdminLimitSvcCtx(t, 1)
|
|
|
username := "h1_unk_" + testutil.UniqueId()
|
|
|
- req := &types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: "bad",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- }
|
|
|
+ mk := svcCtx.Config.Auth.ManagementKey
|
|
|
ctx := context.Background()
|
|
|
|
|
|
- _, err := NewAdminLoginLogic(ctx, svcCtx).AdminLogin(req)
|
|
|
+ _, err := NewAdminLoginLogic(ctx, svcCtx).AdminLogin(newAdminLoginReq(username, "bad", mk))
|
|
|
require.Error(t, err)
|
|
|
var ce *response.CodeError
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
assert.Equal(t, 401, ce.Code())
|
|
|
|
|
|
- _, err = NewAdminLoginLogic(ctx, svcCtx).AdminLogin(req)
|
|
|
+ _, err = NewAdminLoginLogic(ctx, svcCtx).AdminLogin(newAdminLoginReq(username, "bad", mk))
|
|
|
require.Error(t, err)
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
assert.Equal(t, 429, ce.Code(),
|
|
|
@@ -339,31 +330,24 @@ func TestAdminLogin_H1_BadManagementKey_DoesNotConsumeQuota(t *testing.T) {
|
|
|
username := "h1_mk_" + testutil.UniqueId()
|
|
|
ctx := middleware.WithClientIP(context.Background(), "172.16.0.9")
|
|
|
|
|
|
- _, err := NewAdminLoginLogic(ctx, svcCtx).AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: "whatever",
|
|
|
- ManagementKey: "WRONG-KEY",
|
|
|
- })
|
|
|
+ _, err := NewAdminLoginLogic(ctx, svcCtx).AdminLogin(newAdminLoginReq(username, "whatever", "WRONG-KEY"))
|
|
|
require.Error(t, err)
|
|
|
var ce *response.CodeError
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
assert.Equal(t, 401, ce.Code())
|
|
|
assert.Equal(t, "managementKey无效", ce.Error())
|
|
|
|
|
|
- _, err = NewAdminLoginLogic(ctx, svcCtx).AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: "whatever",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ _, err = NewAdminLoginLogic(ctx, svcCtx).AdminLogin(newAdminLoginReq(username, "whatever", svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Error(t, err)
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
assert.Equal(t, 401, ce.Code(),
|
|
|
"managementKey 错误应在 Take 之前 return,不应消耗 per-IP+user 配额")
|
|
|
}
|
|
|
|
|
|
+// TC-1008: 非超管+错密码 vs 用户不存在,响应不得区分两条分支
|
|
|
func TestAdminLogin_LN3_NonSuperAdminWrongPassword_IndistinguishableFromAbsent(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
svcCtx.UsernameLoginLimit = nil
|
|
|
|
|
|
username := "ln3_nonsa_" + testutil.UniqueId()
|
|
|
@@ -374,21 +358,13 @@ func TestAdminLogin_LN3_NonSuperAdminWrongPassword_IndistinguishableFromAbsent(t
|
|
|
logic := NewAdminLoginLogic(ctx, svcCtx)
|
|
|
|
|
|
// (B) 用户存在但非超管 —— 走 新增的 dummy bcrypt 分支
|
|
|
- _, errExisting := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: "WrongPass",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ _, errExisting := logic.AdminLogin(newAdminLoginReq(username, "WrongPass", svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Error(t, errExisting)
|
|
|
var ceB *response.CodeError
|
|
|
require.True(t, errors.As(errExisting, &ceB))
|
|
|
|
|
|
// (A) 用户不存在 —— 原有 dummy bcrypt 分支
|
|
|
- _, errAbsent := logic.AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: "ln3_absent_" + testutil.UniqueId(),
|
|
|
- Password: "WhateverPass",
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ _, errAbsent := logic.AdminLogin(newAdminLoginReq("ln3_absent_"+testutil.UniqueId(), "WhateverPass", svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Error(t, errAbsent)
|
|
|
var ceA *response.CodeError
|
|
|
require.True(t, errors.As(errAbsent, &ceA))
|
|
|
@@ -404,7 +380,7 @@ func TestAdminLogin_LN3_NonSuperAdminWrongPassword_IndistinguishableFromAbsent(t
|
|
|
// 保证即使攻击者命中密码,也不得通过 response 推断该账号是"存在的普通用户"。
|
|
|
func TestAdminLogin_LN3_NonSuperAdminCorrectPassword_Still401(t *testing.T) {
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
svcCtx.UsernameLoginLimit = nil
|
|
|
|
|
|
username := "ln3_cp_" + testutil.UniqueId()
|
|
|
@@ -412,11 +388,7 @@ func TestAdminLogin_LN3_NonSuperAdminCorrectPassword_Still401(t *testing.T) {
|
|
|
_, clean := insertTestUser(t, ctx, svcCtx, username, password, 1, 2)
|
|
|
t.Cleanup(clean)
|
|
|
|
|
|
- _, err := NewAdminLoginLogic(ctx, svcCtx).AdminLogin(&types.AdminLoginReq{
|
|
|
- Username: username,
|
|
|
- Password: password,
|
|
|
- ManagementKey: svcCtx.Config.Auth.ManagementKey,
|
|
|
- })
|
|
|
+ _, err := NewAdminLoginLogic(ctx, svcCtx).AdminLogin(newAdminLoginReq(username, password, svcCtx.Config.Auth.ManagementKey))
|
|
|
require.Error(t, err)
|
|
|
var ce *response.CodeError
|
|
|
require.True(t, errors.As(err, &ce))
|
|
|
@@ -437,7 +409,7 @@ func TestAdminLogin_LN3_DummyBcryptBranches_TimingEqualized(t *testing.T) {
|
|
|
t.Skip("timing-sensitive test skipped under -short")
|
|
|
}
|
|
|
ctx := context.Background()
|
|
|
- svcCtx := newTestSvcCtx()
|
|
|
+ svcCtx := newAdminCaptchaDisabledSvcCtx()
|
|
|
svcCtx.UsernameLoginLimit = nil
|
|
|
|
|
|
normalUser := "ln3_t_nm_" + testutil.UniqueId()
|
|
|
@@ -448,12 +420,12 @@ func TestAdminLogin_LN3_DummyBcryptBranches_TimingEqualized(t *testing.T) {
|
|
|
mk := svcCtx.Config.Auth.ManagementKey
|
|
|
|
|
|
measure := func(username, password string) time.Duration {
|
|
|
- _, _ = logic.AdminLogin(&types.AdminLoginReq{Username: username, Password: password, ManagementKey: mk})
|
|
|
+ _, _ = logic.AdminLogin(newAdminLoginReq(username, password, mk))
|
|
|
const N = 3
|
|
|
var total time.Duration
|
|
|
for i := 0; i < N; i++ {
|
|
|
start := time.Now()
|
|
|
- _, _ = logic.AdminLogin(&types.AdminLoginReq{Username: username, Password: password, ManagementKey: mk})
|
|
|
+ _, _ = logic.AdminLogin(newAdminLoginReq(username, password, mk))
|
|
|
total += time.Since(start)
|
|
|
}
|
|
|
return total / N
|