feat: secure service connections and login

This commit is contained in:
2026-11-06 21:14:49 +03:00
parent cb51354889
commit 48918e8b19
59 changed files with 1980 additions and 316 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
)
const attachmentJobTopic = "attachment.jobs"
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"fmt"
"time"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
)
func (s *Store) RenewAttachmentJobLease(
+1 -1
View File
@@ -9,7 +9,7 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
)
func (s *Store) RecoverExpiredAttachmentJobs(
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"github.com/jackc/pgx/v5"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
)
const attachmentColumns = `
+1 -1
View File
@@ -1,6 +1,6 @@
package postgres
import "gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
import "gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
var (
ErrConflict = model.ErrConflict
+1 -1
View File
@@ -12,7 +12,7 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
)
const expiredIdempotencyCleanupBatch = 100
+1 -1
View File
@@ -8,7 +8,7 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
)
func (s *Store) ClaimOutboxEvents(
+14 -1
View File
@@ -10,7 +10,7 @@ import (
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
)
type Store struct {
@@ -100,6 +100,19 @@ func (s *Store) UserByUsername(ctx context.Context, username string) (model.User
return credentials, nil
}
func (s *Store) UpdatePasswordHash(ctx context.Context, userID, previousHash, passwordHash string) (bool, error) {
const query = `
UPDATE users
SET password_hash = $1
WHERE id = $2 AND password_hash = $3`
result, err := s.pool.Exec(ctx, query, passwordHash, userID, previousHash)
if err != nil {
return false, fmt.Errorf("update password hash: %w", err)
}
return result.RowsAffected() == 1, nil
}
func (s *Store) CreateChat(
ctx context.Context,
title string,
+25 -4
View File
@@ -15,10 +15,10 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/stretchr/testify/suite"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/database"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/postgres"
"gitea.privcloud.tech/dizi/mtusi2026lab6/internal/reconcile"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/database"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/model"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/postgres"
"gitea.privcloud.tech/dizi/mtusi2026lab7/internal/reconcile"
)
type PostgresSuite struct {
@@ -77,6 +77,27 @@ func (s *PostgresSuite) TestConcurrentUsernameCreation() {
}
}
func (s *PostgresSuite) TestPasswordHashUpdateUsesCompareAndSwap() {
username := uniqueUsername()
user, err := s.store.CreateUser(context.Background(), username, "Password user", "old-hash")
s.Require().NoError(err)
updated, err := s.store.UpdatePasswordHash(context.Background(), user.ID, "other-hash", "new-hash")
s.Require().NoError(err)
s.False(updated)
updated, err = s.store.UpdatePasswordHash(context.Background(), user.ID, "old-hash", "new-hash")
s.Require().NoError(err)
s.True(updated)
updated, err = s.store.UpdatePasswordHash(context.Background(), user.ID, "old-hash", "stale-hash")
s.Require().NoError(err)
s.False(updated)
credentials, err := s.store.UserByUsername(context.Background(), username)
s.Require().NoError(err)
s.Equal("new-hash", credentials.PasswordHash)
}
func (s *PostgresSuite) TestCreateChatRollsBackForUnknownMember() {
t := s.T()
pool := s.pool