feat(repository): refactor query/mutation ports with capability-safe
interfaces - refactor: split UserRepository into AuthUserRepository and AdminUserRepository capabilities - refactor: split SessionRepository into AuthSessionRepository and repository-owned CLI/admin methods - refactor: replace generic Repository Update/Delete with operation-specific params and ownership predicates - refactor: replace CredentialRepository generic CRUD with passkey-specific methods - refactor: replace FileRepository generic Create/Update/Delete with UploadedFileParams, DirectoryParams, and owned soft-delete - refactor: remove repository fields from WebApp struct; repositories are now composition-time wiring only - feat: add domain error kinds ErrParentNotFound, ErrParentNotDir, ErrDirectoryNotEmpty, ErrInvalidMove - feat: add CredentialTypeAppPasskey constant - feat: add testutil.SetUserAdmin for test fixture setup that bypasses production service ports - test: add architecture test banning GORM Save in repository package - test: add capability interface contract tests ensuring each service receives the minimal interface - test: add blockingStorage helper for concurrent promotion tests - test: add preserved DSN parameter test for sqliteImmediateDSN - docs: update architecture decisions with repository write rules and capability separation - docs: update roadmap to clarify atomic single-use refresh sessions - docs: add -race test target to development docs
This commit is contained in:
@@ -6,18 +6,33 @@ import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
"github.com/dhao2001/mygo/internal/model"
|
||||
)
|
||||
|
||||
// SessionRepository provides access to refresh token sessions.
|
||||
// RefreshSessionParams contains the fields persisted for a refresh session.
|
||||
type RefreshSessionParams struct {
|
||||
ID string
|
||||
UserID string
|
||||
TokenHash string
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// AuthSessionRepository exposes only session operations required by AuthService.
|
||||
type AuthSessionRepository interface {
|
||||
CreateRefreshSession(ctx context.Context, params RefreshSessionParams) error
|
||||
ConsumeRefreshSession(ctx context.Context, tokenHash string) (*model.Session, error)
|
||||
RevokeRefreshSession(ctx context.Context, tokenHash string) error
|
||||
}
|
||||
|
||||
// SessionRepository is the composition-time union of session capabilities.
|
||||
type SessionRepository interface {
|
||||
Create(ctx context.Context, session *model.Session) error
|
||||
AuthSessionRepository
|
||||
FindByID(ctx context.Context, id string) (*model.Session, error)
|
||||
FindByTokenHash(ctx context.Context, tokenHash string) (*model.Session, error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
DeleteByUserID(ctx context.Context, userID string) error
|
||||
DeleteExpired(ctx context.Context) (int64, error)
|
||||
DeleteSessionsByUserID(ctx context.Context, userID string) error
|
||||
DeleteExpiredSessions(ctx context.Context) (int64, error)
|
||||
}
|
||||
|
||||
type sessionRepository struct {
|
||||
@@ -29,7 +44,13 @@ func NewSessionRepository(db *gorm.DB) SessionRepository {
|
||||
return &sessionRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *sessionRepository) Create(ctx context.Context, session *model.Session) error {
|
||||
func (r *sessionRepository) CreateRefreshSession(ctx context.Context, params RefreshSessionParams) error {
|
||||
session := &model.Session{
|
||||
ID: params.ID,
|
||||
UserID: params.UserID,
|
||||
TokenHash: params.TokenHash,
|
||||
ExpiresAt: params.ExpiresAt,
|
||||
}
|
||||
result := r.db.WithContext(ctx).Create(session)
|
||||
if result.Error != nil {
|
||||
if isDuplicateKeyError(result.Error) {
|
||||
@@ -64,15 +85,39 @@ func (r *sessionRepository) FindByTokenHash(ctx context.Context, tokenHash strin
|
||||
return &session, nil
|
||||
}
|
||||
|
||||
func (r *sessionRepository) Delete(ctx context.Context, id string) error {
|
||||
result := r.db.WithContext(ctx).Delete(&model.Session{}, "id = ?", id)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
func (r *sessionRepository) ConsumeRefreshSession(ctx context.Context, tokenHash string) (*model.Session, error) {
|
||||
var consumed model.Session
|
||||
err := r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
result := tx.Clauses(clause.Locking{Strength: clause.LockingStrengthUpdate}).
|
||||
First(&consumed, "token_hash = ?", tokenHash)
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return model.ErrNotFound
|
||||
}
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
result = tx.Delete(&model.Session{}, "id = ?", consumed.ID)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
return model.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
return &consumed, nil
|
||||
}
|
||||
|
||||
func (r *sessionRepository) DeleteByUserID(ctx context.Context, userID string) error {
|
||||
func (r *sessionRepository) RevokeRefreshSession(ctx context.Context, tokenHash string) error {
|
||||
result := r.db.WithContext(ctx).Delete(&model.Session{}, "token_hash = ?", tokenHash)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func (r *sessionRepository) DeleteSessionsByUserID(ctx context.Context, userID string) error {
|
||||
result := r.db.WithContext(ctx).Delete(&model.Session{}, "user_id = ?", userID)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
@@ -80,7 +125,7 @@ func (r *sessionRepository) DeleteByUserID(ctx context.Context, userID string) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *sessionRepository) DeleteExpired(ctx context.Context) (int64, error) {
|
||||
func (r *sessionRepository) DeleteExpiredSessions(ctx context.Context) (int64, error) {
|
||||
result := r.db.WithContext(ctx).Delete(&model.Session{}, "expires_at < ?", time.Now())
|
||||
if result.Error != nil {
|
||||
return 0, result.Error
|
||||
|
||||
Reference in New Issue
Block a user