loginService.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package pub
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "perms-system-server/internal/consts"
  7. "perms-system-server/internal/loaders"
  8. authHelper "perms-system-server/internal/logic/auth"
  9. "perms-system-server/internal/model/user"
  10. "perms-system-server/internal/svc"
  11. "github.com/zeromicro/go-zero/core/limit"
  12. "golang.org/x/crypto/bcrypt"
  13. )
  14. // dummyBcryptHash 用于对不存在的用户名执行等时 bcrypt 比对,防止基于响应时间的用户名枚举
  15. var dummyBcryptHash, _ = bcrypt.GenerateFromPassword([]byte("dummy-anti-timing"), bcrypt.DefaultCost)
  16. type LoginResult struct {
  17. UserDetails *loaders.UserDetails
  18. AccessToken string
  19. RefreshToken string
  20. }
  21. type LoginError struct {
  22. Code int
  23. Message string
  24. }
  25. func (e *LoginError) Error() string {
  26. return e.Message
  27. }
  28. func checkUsernameLimit(svcCtx *svc.ServiceContext, clientIP, username string) bool {
  29. if svcCtx.UsernameLoginLimit == nil {
  30. return false
  31. }
  32. key := fmt.Sprintf("%s:%s", clientIP, username)
  33. code, _ := svcCtx.UsernameLoginLimit.Take(key)
  34. return code == limit.OverQuota
  35. }
  36. func ValidateProductLogin(ctx context.Context, svcCtx *svc.ServiceContext, username, password, productCode, clientIP string) (*LoginResult, error) {
  37. if checkUsernameLimit(svcCtx, clientIP, username) {
  38. return nil, &LoginError{Code: 429, Message: "该账号登录尝试过于频繁,请5分钟后再试"}
  39. }
  40. u, lookupErr := svcCtx.SysUserModel.FindOneByUsername(ctx, username)
  41. var userHash []byte
  42. if lookupErr != nil {
  43. if !errors.Is(lookupErr, user.ErrNotFound) {
  44. return nil, lookupErr
  45. }
  46. userHash = dummyBcryptHash
  47. } else {
  48. userHash = []byte(u.Password)
  49. }
  50. // 无条件执行一次 bcrypt:让"账号不存在 / 冻结 / 密码错"三条路径在耗时上完全等长,
  51. // 消除基于响应时间的账号存在性 / 冻结状态 oracle(见审计 H-2)。
  52. bcryptErr := bcrypt.CompareHashAndPassword(userHash, []byte(password))
  53. if lookupErr != nil || bcryptErr != nil {
  54. return nil, &LoginError{Code: 401, Message: "用户名或密码错误"}
  55. }
  56. // 密码正确之后再披露账号语义状态:此时攻击者已经猜中密码,再隐藏"冻结/超管"已无意义。
  57. if u.Status != consts.StatusEnabled {
  58. return nil, &LoginError{Code: 403, Message: "账号已被冻结"}
  59. }
  60. if u.IsSuperAdmin == consts.IsSuperAdminYes {
  61. return nil, &LoginError{Code: 403, Message: "超级管理员不允许通过产品端登录,请使用管理后台"}
  62. }
  63. product, err := svcCtx.SysProductModel.FindOneByCode(ctx, productCode)
  64. if err != nil {
  65. return nil, &LoginError{Code: 400, Message: "产品不存在"}
  66. }
  67. if product.Status != consts.StatusEnabled {
  68. return nil, &LoginError{Code: 403, Message: "该产品已被禁用"}
  69. }
  70. // 审计 M-R10-5:Load 内部的 loadMembership 已经做了完全等价的判断——
  71. // FindOneByProductCodeUserId 未命中 / member.Status != StatusEnabled 都会把 ud.MemberType 置空。
  72. // 删除此处重复的 FindOneByProductCodeUserId,把登录路径由 2 次 sys_product_member 查询降到 1 次。
  73. // 错误文案合并为"您不是该产品的有效成员",与 jwtauthMiddleware 的同类分支口径一致。
  74. ud, err := svcCtx.UserDetailsLoader.Load(ctx, u.Id, productCode)
  75. if err != nil {
  76. return nil, &LoginError{Code: 503, Message: "服务暂时不可用,请稍后重试"}
  77. }
  78. if !ud.IsSuperAdmin && ud.MemberType == "" {
  79. return nil, &LoginError{Code: 403, Message: "您不是该产品的有效成员"}
  80. }
  81. accessToken, err := authHelper.GenerateAccessToken(
  82. svcCtx.Config.Auth.AccessSecret,
  83. svcCtx.Config.Auth.AccessExpire,
  84. ud.UserId, ud.Username, ud.ProductCode, ud.MemberType, ud.TokenVersion,
  85. )
  86. if err != nil {
  87. return nil, err
  88. }
  89. refreshToken, err := authHelper.GenerateRefreshToken(
  90. svcCtx.Config.Auth.RefreshSecret,
  91. svcCtx.Config.Auth.RefreshExpire,
  92. ud.UserId, ud.ProductCode, ud.TokenVersion,
  93. )
  94. if err != nil {
  95. return nil, err
  96. }
  97. return &LoginResult{
  98. UserDetails: ud,
  99. AccessToken: accessToken,
  100. RefreshToken: refreshToken,
  101. }, nil
  102. }