changePasswordLogic.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package auth
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "perms-system-server/internal/consts"
  7. "perms-system-server/internal/loaders"
  8. "perms-system-server/internal/middleware"
  9. userModel "perms-system-server/internal/model/user"
  10. "perms-system-server/internal/response"
  11. "perms-system-server/internal/svc"
  12. "perms-system-server/internal/types"
  13. "perms-system-server/internal/util"
  14. "github.com/zeromicro/go-zero/core/limit"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. "golang.org/x/crypto/bcrypt"
  17. )
  18. type ChangePasswordLogic struct {
  19. logx.Logger
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. }
  23. func NewChangePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangePasswordLogic {
  24. return &ChangePasswordLogic{
  25. Logger: logx.WithContext(ctx),
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. }
  29. }
  30. // ChangePassword 修改密码。已登录用户验证原密码后设置新密码,同时递增 tokenVersion 使所有已签发令牌失效,强制重新登录。
  31. func (l *ChangePasswordLogic) ChangePassword(req *types.ChangePasswordReq) error {
  32. if msg := util.ValidatePassword(req.NewPassword); msg != "" {
  33. return response.ErrBadRequest(msg)
  34. }
  35. userId := middleware.GetUserId(l.ctx)
  36. if l.svcCtx.TokenOpLimiter != nil {
  37. code, _ := l.svcCtx.TokenOpLimiter.Take(fmt.Sprintf("chpwd:%d", userId))
  38. if code == limit.OverQuota {
  39. return response.ErrTooManyRequests("操作过于频繁,请稍后再试")
  40. }
  41. }
  42. user, err := l.svcCtx.SysUserModel.FindOne(l.ctx, userId)
  43. if err != nil {
  44. return response.ErrNotFound("用户不存在")
  45. }
  46. if user.Status != consts.StatusEnabled {
  47. return response.ErrForbidden("账号已被冻结")
  48. }
  49. if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.OldPassword)); err != nil {
  50. logx.WithContext(l.ctx).Infof("change-password old-password mismatch userId=%d", userId)
  51. return response.ErrBadRequest("原密码错误")
  52. }
  53. if req.OldPassword == req.NewPassword {
  54. return response.ErrBadRequest("新密码不能与原密码相同")
  55. }
  56. hashed, err := bcrypt.GenerateFromPassword([]byte(req.NewPassword), bcrypt.DefaultCost)
  57. if err != nil {
  58. return err
  59. }
  60. // 审计 H-R11-1:把上面已经读到的 user.UpdateTime / user.Username 作为乐观锁 expected 透传;
  61. // UpdatePassword 内部不再 FindOne 自对齐,CAS 的 expected 与"外层校验旧密码所依赖的那一份快照"
  62. // 严格绑定——任何并发 UpdatePassword / UpdateProfile / UpdateStatus 都会让 DB 的 updateTime
  63. // 变化,WHERE 不再命中,ErrUpdateConflict 上抛 409,迫使会话刷新后重试。
  64. if err := l.svcCtx.SysUserModel.UpdatePassword(l.ctx, userId, user.Username, string(hashed), consts.MustChangePasswordNo, user.UpdateTime); err != nil {
  65. // 审计 M-R10-4:与 UpdateUserLogic / UpdateRoleLogic / UpdateUserStatusLogic 口径对齐,
  66. // 把乐观锁失败显式映射成 409,避免 raw error 被 rest 框架兜成 500、前端错把"并发冲突"
  67. // 当作系统故障处理,告警看板也不会把这类事件归到 5xx 噪声池。
  68. if errors.Is(err, userModel.ErrUpdateConflict) {
  69. return response.ErrConflict("密码已被其他会话修改,请刷新后重试")
  70. }
  71. return err
  72. }
  73. // 审计 L-R13-5 方案 A:密码变更会同步递增 tokenVersion 使旧令牌失效;UD 缓存必须立即
  74. // 刷新,否则中间件读到的仍是旧 tokenVersion,client 可以继续用旧 token 5 分钟。detach ctx
  75. // 把这次失效从请求生命周期里摘出来。
  76. cleanCtx, cancel := loaders.DetachCacheCleanCtx(l.ctx)
  77. defer cancel()
  78. l.svcCtx.UserDetailsLoader.Clean(cleanCtx, userId)
  79. return nil
  80. }