lab7: исправления по независимому ревью

This commit is contained in:
Vladislav Kunetskiy
2026-11-10 14:22:47 +03:00
parent d1dc6d6b78
commit 30b6672058
18 changed files with 20572 additions and 20366 deletions
+12 -13
View File
@@ -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 {