forked from mtusi2026/mtusi2026lab7
lab7: исправления по независимому ревью
This commit is contained in:
@@ -56,24 +56,23 @@ func (s *Service) Authenticate(ctx context.Context, username, plain string) (dom
|
||||
return domain.User{}, fmt.Errorf("authenticate: %w", domain.ErrUnauthenticated)
|
||||
}
|
||||
if verified.NeedsRehash {
|
||||
hash, err := password.Hash(plain)
|
||||
if errors.Is(err, password.ErrBusy) {
|
||||
// The credential is valid; a contended optional upgrade must not
|
||||
// turn a successful login into a failure.
|
||||
return credentials.User, nil
|
||||
}
|
||||
if err != nil {
|
||||
return domain.User{}, fmt.Errorf("rehash password: %w", err)
|
||||
}
|
||||
// A concurrent upgrade wins the CAS; it must not invalidate this login.
|
||||
if _, err := s.sessions.UpdatePasswordHash(ctx, credentials.User.ID, credentials.PasswordHash, hash); err != nil {
|
||||
return domain.User{}, fmt.Errorf("upgrade password: %w", err)
|
||||
}
|
||||
// Optional upgrade: a busy derivation or transient write error must not
|
||||
// turn an already verified login into a failure.
|
||||
s.rehash(ctx, credentials.User.ID, credentials.PasswordHash, plain)
|
||||
}
|
||||
|
||||
return credentials.User, nil
|
||||
}
|
||||
|
||||
func (s *Service) rehash(ctx context.Context, userID uuid.UUID, oldHash, plain string) {
|
||||
hash, err := password.Hash(plain)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
// A concurrent upgrade wins the CAS; both outcomes are acceptable.
|
||||
_, _ = s.sessions.UpdatePasswordHash(ctx, userID, oldHash, hash)
|
||||
}
|
||||
|
||||
func (s *Service) Resolve(ctx context.Context, raw string) (domain.User, error) {
|
||||
hash, err := s.tokens.Hash(raw)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user