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 {
|
||||
|
||||
@@ -177,16 +177,16 @@ func (s *ServiceSuite) TestWrongLegacyPasswordDoesNotUpgrade() {
|
||||
s.sessions.AssertNotCalled(s.T(), "UpdatePasswordHash", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestPasswordUpgradeFailureIsNotAuthenticationFailure() {
|
||||
func (s *ServiceSuite) TestPasswordUpgradeFailureDoesNotBlockLogin() {
|
||||
oldHash := obsoleteHash("secret-password-123", false)
|
||||
s.sessions.EXPECT().Credentials(mock.Anything, "alice").
|
||||
Return(domain.UserCredentials{User: s.user, PasswordHash: oldHash}, nil).Once()
|
||||
s.sessions.On("UpdatePasswordHash", mock.Anything, s.user.ID, oldHash, mock.Anything).
|
||||
Return(false, domain.ErrUnavailable).Once()
|
||||
|
||||
_, err := s.service.Authenticate(s.T().Context(), "alice", "secret-password-123")
|
||||
s.Require().ErrorIs(err, domain.ErrUnavailable)
|
||||
s.Require().NotErrorIs(err, domain.ErrUnauthenticated)
|
||||
got, err := s.service.Authenticate(s.T().Context(), "alice", "secret-password-123")
|
||||
s.Require().NoError(err)
|
||||
s.Equal(s.user, got)
|
||||
}
|
||||
|
||||
func obsoleteHash(plain string, legacy bool) string {
|
||||
|
||||
Reference in New Issue
Block a user