forked from mtusi2026/mtusi2026lab7
lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -2,6 +2,7 @@ package session_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"golang.org/x/crypto/argon2"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
"mtusi-messenger/internal/platform/password"
|
||||
@@ -144,3 +146,58 @@ func (s *ServiceSuite) TestAuthenticatePropagatesUnknownUser() {
|
||||
_, err := s.service.Authenticate(s.T().Context(), "nobody", "secret-password-123")
|
||||
s.Require().ErrorIs(err, domain.ErrUnauthenticated)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestAuthenticateUpgradesHashesWithCAS() {
|
||||
plain := "secret-password-123"
|
||||
for _, legacy := range []bool{false, true} {
|
||||
oldHash := obsoleteHash(plain, legacy)
|
||||
for _, updated := range []bool{true, 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.MatchedBy(func(hash string) bool {
|
||||
verified, err := password.Verify(plain, hash)
|
||||
return err == nil && verified.Match && !verified.NeedsRehash
|
||||
})).Return(updated, nil).Once()
|
||||
|
||||
got, err := s.service.Authenticate(s.T().Context(), "alice", plain)
|
||||
s.Require().NoError(err)
|
||||
s.Equal(s.user, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestWrongLegacyPasswordDoesNotUpgrade() {
|
||||
oldHash := obsoleteHash("secret-password-123", true)
|
||||
s.sessions.EXPECT().Credentials(mock.Anything, "alice").
|
||||
Return(domain.UserCredentials{User: s.user, PasswordHash: oldHash}, nil).Once()
|
||||
|
||||
_, err := s.service.Authenticate(s.T().Context(), "alice", "wrong-password-value")
|
||||
s.Require().ErrorIs(err, domain.ErrUnauthenticated)
|
||||
s.sessions.AssertNotCalled(s.T(), "UpdatePasswordHash", mock.Anything, mock.Anything, mock.Anything, mock.Anything)
|
||||
}
|
||||
|
||||
func (s *ServiceSuite) TestPasswordUpgradeFailureIsNotAuthenticationFailure() {
|
||||
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)
|
||||
}
|
||||
|
||||
func obsoleteHash(plain string, legacy bool) string {
|
||||
var salt [16]byte
|
||||
memory, threads := uint32(32), uint8(1)
|
||||
prefix := "$argon2id$v=19$m=32,t=1,p=1$"
|
||||
if legacy {
|
||||
memory, threads = 64*1024, 4
|
||||
prefix = "argon2id$"
|
||||
}
|
||||
hash := argon2.IDKey([]byte(plain), salt[:], 1, memory, threads, 32)
|
||||
return prefix + base64.RawStdEncoding.EncodeToString(salt[:]) + "$" +
|
||||
base64.RawStdEncoding.EncodeToString(hash)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user