forked from mtusi2026/mtusi2026lab7
feat: secure service connections and login
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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,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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user