feat(server): 新增修改密码接口 POST /api/auth/password(验原密码、作废会话)
This commit is contained in:
1 parent
ad2322d558
commit
4cbf5a74f0
13 files changed
+528
-20
No files matched your search
@@ -202,6 +202,19 @@ func (s *Service) SetPassword(ctx context.Context, username, password string, mu
|
||||
return s.RevokeUserSessions(ctx, id)
|
||||
}
|
||||
|
||||
// VerifyPassword 校验指定用户的原密码,不创建会话(供「修改密码」等场景使用)。
|
||||
func (s *Service) VerifyPassword(ctx context.Context, userID int64, password string) error {
|
||||
var hash string
|
||||
if err := s.db.QueryRowContext(ctx,
|
||||
`SELECT password_hash FROM users WHERE id = ? AND active = 1`, userID).Scan(&hash); err != nil {
|
||||
return ErrInvalidCredentials
|
||||
}
|
||||
if bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) != nil {
|
||||
return ErrInvalidCredentials
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Authenticate 校验用户名密码并创建会话,返回用户与不落库的明文 token。
|
||||
func (s *Service) Authenticate(ctx context.Context, username, password, ip, userAgent string) (*User, string, error) {
|
||||
if !s.AllowLogin(ip) {
|
||||
|
||||
Reference in new issue
Block a user