forked from mtusi2026/mtusi2026lab7
lab7: безопасность, mTLS, OpenBao и PKI lifecycle
This commit is contained in:
@@ -308,3 +308,81 @@ func (_c *MockRepository_Revoke_Call) RunAndReturn(run func(ctx context.Context,
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// UpdatePasswordHash provides a mock function for the type MockRepository
|
||||
func (_mock *MockRepository) UpdatePasswordHash(ctx context.Context, userID uuid.UUID, oldHash string, newHash string) (bool, error) {
|
||||
ret := _mock.Called(ctx, userID, oldHash, newHash)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for UpdatePasswordHash")
|
||||
}
|
||||
|
||||
var r0 bool
|
||||
var r1 error
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, string, string) (bool, error)); ok {
|
||||
return returnFunc(ctx, userID, oldHash, newHash)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(0).(func(context.Context, uuid.UUID, string, string) bool); ok {
|
||||
r0 = returnFunc(ctx, userID, oldHash, newHash)
|
||||
} else {
|
||||
r0 = ret.Get(0).(bool)
|
||||
}
|
||||
if returnFunc, ok := ret.Get(1).(func(context.Context, uuid.UUID, string, string) error); ok {
|
||||
r1 = returnFunc(ctx, userID, oldHash, newHash)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MockRepository_UpdatePasswordHash_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'UpdatePasswordHash'
|
||||
type MockRepository_UpdatePasswordHash_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// UpdatePasswordHash is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - userID uuid.UUID
|
||||
// - oldHash string
|
||||
// - newHash string
|
||||
func (_e *MockRepository_Expecter) UpdatePasswordHash(ctx any, userID any, oldHash any, newHash any) *MockRepository_UpdatePasswordHash_Call {
|
||||
return &MockRepository_UpdatePasswordHash_Call{Call: _e.mock.On("UpdatePasswordHash", ctx, userID, oldHash, newHash)}
|
||||
}
|
||||
|
||||
func (_c *MockRepository_UpdatePasswordHash_Call) Run(run func(ctx context.Context, userID uuid.UUID, oldHash string, newHash string)) *MockRepository_UpdatePasswordHash_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
var arg0 context.Context
|
||||
if args[0] != nil {
|
||||
arg0 = args[0].(context.Context)
|
||||
}
|
||||
var arg1 uuid.UUID
|
||||
if args[1] != nil {
|
||||
arg1 = args[1].(uuid.UUID)
|
||||
}
|
||||
var arg2 string
|
||||
if args[2] != nil {
|
||||
arg2 = args[2].(string)
|
||||
}
|
||||
var arg3 string
|
||||
if args[3] != nil {
|
||||
arg3 = args[3].(string)
|
||||
}
|
||||
run(
|
||||
arg0,
|
||||
arg1,
|
||||
arg2,
|
||||
arg3,
|
||||
)
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_UpdatePasswordHash_Call) Return(b bool, err error) *MockRepository_UpdatePasswordHash_Call {
|
||||
_c.Call.Return(b, err)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *MockRepository_UpdatePasswordHash_Call) RunAndReturn(run func(ctx context.Context, userID uuid.UUID, oldHash string, newHash string) (bool, error)) *MockRepository_UpdatePasswordHash_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
|
||||
type Repository interface {
|
||||
Credentials(ctx context.Context, username string) (domain.UserCredentials, error)
|
||||
UpdatePasswordHash(ctx context.Context, userID uuid.UUID, oldHash, newHash string) (bool, error)
|
||||
Create(ctx context.Context, session domain.NewSession) (domain.Session, error)
|
||||
Resolve(ctx context.Context, tokenHash []byte) (domain.Session, error)
|
||||
Revoke(ctx context.Context, userID uuid.UUID, tokenHash []byte) error
|
||||
@@ -36,12 +38,38 @@ func NewService(sessions Repository, tokens Tokens, ttl time.Duration) *Service
|
||||
func (s *Service) Authenticate(ctx context.Context, username, plain string) (domain.User, error) {
|
||||
credentials, err := s.sessions.Credentials(ctx, username)
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrUnauthenticated) {
|
||||
// Spend the same derivation budget as a real credential check so
|
||||
// an unknown username does not answer measurably faster.
|
||||
if dummyErr := password.DummyVerify(plain); dummyErr != nil {
|
||||
return domain.User{}, fmt.Errorf("verify password: %w", dummyErr)
|
||||
}
|
||||
}
|
||||
return domain.User{}, fmt.Errorf("authenticate: %w", err)
|
||||
}
|
||||
|
||||
if !password.Verify(plain, credentials.PasswordHash) {
|
||||
verified, err := password.Verify(plain, credentials.PasswordHash)
|
||||
if err != nil {
|
||||
return domain.User{}, fmt.Errorf("verify password: %w", err)
|
||||
}
|
||||
if !verified.Match {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return credentials.User, nil
|
||||
}
|
||||
|
||||
@@ -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