Documentation
¶
Overview ¶
Package repository provides database repository interfaces and implementations for the auth service. These interfaces abstract database operations to support multiple database backends through GORM.
Index ¶
- Variables
- type ClientCredential
- type ClientCredentialCreateParams
- type ClientCredentialRepository
- type DeletionRepository
- type DeletionResult
- type GormClientCredentialRepository
- func (r *GormClientCredentialRepository) Create(ctx context.Context, params ClientCredentialCreateParams) (*ClientCredential, error)
- func (r *GormClientCredentialRepository) Deactivate(ctx context.Context, id, ownerUUID uuid.UUID) error
- func (r *GormClientCredentialRepository) Delete(ctx context.Context, id, ownerUUID uuid.UUID) error
- func (r *GormClientCredentialRepository) GetByClientID(ctx context.Context, clientID string) (*ClientCredential, error)
- func (r *GormClientCredentialRepository) ListByOwner(ctx context.Context, ownerUUID uuid.UUID) ([]*ClientCredential, error)
- func (r *GormClientCredentialRepository) UpdateLastUsed(ctx context.Context, id uuid.UUID) error
- type GormDeletionRepository
- func (r *GormDeletionRepository) DeleteGroupAndData(ctx context.Context, internalUUID string) (*GroupDeletionResult, error)
- func (r *GormDeletionRepository) DeleteUserAndData(ctx context.Context, userEmail string) (*DeletionResult, error)
- func (r *GormDeletionRepository) DeleteUserByInternalUUID(ctx context.Context, internalUUID string) (*DeletionResult, error)
- func (r *GormDeletionRepository) TransferOwnership(ctx context.Context, sourceUserUUID, targetUserUUID string) (*TransferResult, error)
- type GormUserRepository
- func (r *GormUserRepository) Create(ctx context.Context, user *User) (*User, error)
- func (r *GormUserRepository) Delete(ctx context.Context, id string) error
- func (r *GormUserRepository) GetByAnyProviderID(ctx context.Context, providerUserID string) (*User, error)
- func (r *GormUserRepository) GetByEmail(ctx context.Context, email string) (*User, error)
- func (r *GormUserRepository) GetByID(ctx context.Context, id string) (*User, error)
- func (r *GormUserRepository) GetByProviderAndEmail(ctx context.Context, provider, email string) (*User, error)
- func (r *GormUserRepository) GetByProviderID(ctx context.Context, provider, providerUserID string) (*User, error)
- func (r *GormUserRepository) GetPrimaryProviderID(ctx context.Context, userID string) (string, error)
- func (r *GormUserRepository) GetProviders(ctx context.Context, userID string) ([]UserProvider, error)
- func (r *GormUserRepository) Update(ctx context.Context, user *User) error
- type GroupDeletionResult
- type TransferResult
- type User
- type UserProvider
- type UserRepository
Constants ¶
This section is empty.
Variables ¶
var ( ErrUserNotFound = fmt.Errorf("user: %w", dberrors.ErrNotFound) ErrClientCredentialNotFound = fmt.Errorf("client credential: %w", dberrors.ErrNotFound) ErrGroupNotFound = fmt.Errorf("group: %w", dberrors.ErrNotFound) )
Common errors returned by repositories. Each wraps the corresponding dberrors sentinel so handlers can check either the entity-specific error or the generic category.
Functions ¶
This section is empty.
Types ¶
type ClientCredential ¶
type ClientCredential struct {
ID uuid.UUID
OwnerUUID uuid.UUID
ClientID string
ClientSecretHash string
Name string
Description string
IsActive bool
DirectWrite bool
AddonID string // empty when unlinked (#883)
LastUsedAt *time.Time
CreatedAt time.Time
ModifiedAt time.Time
ExpiresAt *time.Time
}
ClientCredential represents an OAuth 2.0 client credential SEM@bb016c3822e5987a6d2abf81bf6fcf80682851a4: machine-to-machine OAuth client credential with hashed secret, activity metadata, direct_write flag, and addon link (pure)
type ClientCredentialCreateParams ¶
type ClientCredentialCreateParams struct {
OwnerUUID uuid.UUID
ClientID string
ClientSecretHash string
Name string
Description string
DirectWrite bool
AddonID string
ExpiresAt *time.Time
}
ClientCredentialCreateParams contains parameters for creating a new client credential SEM@bb016c3822e5987a6d2abf81bf6fcf80682851a4: parameters for registering a new client credential in the repository, including direct_write and addon link (pure)
type ClientCredentialRepository ¶
type ClientCredentialRepository interface {
// Create creates a new client credential
Create(ctx context.Context, params ClientCredentialCreateParams) (*ClientCredential, error)
// GetByClientID retrieves an active client credential by client ID
GetByClientID(ctx context.Context, clientID string) (*ClientCredential, error)
// ListByOwner retrieves all client credentials owned by a user
ListByOwner(ctx context.Context, ownerUUID uuid.UUID) ([]*ClientCredential, error)
// UpdateLastUsed updates the last_used_at timestamp for a client credential
UpdateLastUsed(ctx context.Context, id uuid.UUID) error
// Deactivate deactivates a client credential (soft delete)
Deactivate(ctx context.Context, id, ownerUUID uuid.UUID) error
// Delete permanently deletes a client credential
Delete(ctx context.Context, id, ownerUUID uuid.UUID) error
}
ClientCredentialRepository handles client credential operations SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: interface for managing OAuth client credentials including creation, lookup, and revocation
type DeletionRepository ¶
type DeletionRepository interface {
// DeleteUserAndData deletes a user by email and handles ownership transfer for threat models.
// Used by the self-deletion flow (DELETE /me) where identity comes from JWT email.
DeleteUserAndData(ctx context.Context, userEmail string) (*DeletionResult, error)
// DeleteUserByInternalUUID deletes a user by internal UUID and handles ownership transfer.
// Used by admin deletion to avoid multi-hop identity resolution that can target the wrong user.
DeleteUserByInternalUUID(ctx context.Context, internalUUID string) (*DeletionResult, error)
// DeleteGroupAndData deletes a group by internal UUID and handles threat model cleanup
// Uses internal_uuid for precise identification to avoid issues with duplicate group_names
DeleteGroupAndData(ctx context.Context, internalUUID string) (*GroupDeletionResult, error)
// TransferOwnership transfers all owned threat models and survey responses
// from sourceUserUUID to targetUserUUID within a single transaction.
// The source user is downgraded to "writer" role on all transferred items.
TransferOwnership(ctx context.Context, sourceUserUUID, targetUserUUID string) (*TransferResult, error)
}
DeletionRepository handles user and group deletion with data cleanup SEM@aaa9664f0ee44fef65807ddde317d73afdfcf8eb: interface for deleting users and groups with cascading ownership transfer (reads DB)
type DeletionResult ¶
DeletionResult contains statistics about user/group deletion operations SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: statistics returned after deleting a user, reporting threat model transfers and deletions (pure)
type GormClientCredentialRepository ¶
type GormClientCredentialRepository struct {
// contains filtered or unexported fields
}
GormClientCredentialRepository implements ClientCredentialRepository using GORM SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: GORM-backed store for client credential CRUD operations (reads DB)
func NewGormClientCredentialRepository ¶
func NewGormClientCredentialRepository(db *gorm.DB) *GormClientCredentialRepository
NewGormClientCredentialRepository creates a new GORM-backed client credential repository SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: build a GORM client credential repository wrapping the given database connection (pure)
func (*GormClientCredentialRepository) Create ¶
func (r *GormClientCredentialRepository) Create(ctx context.Context, params ClientCredentialCreateParams) (*ClientCredential, error)
Create creates a new client credential SEM@690b6a91dd88122c76b34cde3e9c1b6e4e5d7715: store a new client credential record and return the persisted entity (reads DB)
func (*GormClientCredentialRepository) Deactivate ¶
func (r *GormClientCredentialRepository) Deactivate(ctx context.Context, id, ownerUUID uuid.UUID) error
Deactivate deactivates a client credential (soft delete) SEM@3b9af7c655cdfe5497882bbc367b72fd757569a9: soft-delete a client credential by marking it inactive, scoped to owner (reads DB)
func (*GormClientCredentialRepository) Delete ¶
Delete permanently deletes a client credential SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: permanently delete a client credential record scoped to its owner (reads DB)
func (*GormClientCredentialRepository) GetByClientID ¶
func (r *GormClientCredentialRepository) GetByClientID(ctx context.Context, clientID string) (*ClientCredential, error)
GetByClientID retrieves an active client credential by client ID SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: fetch an active client credential by client ID; error if not found (reads DB)
func (*GormClientCredentialRepository) ListByOwner ¶
func (r *GormClientCredentialRepository) ListByOwner(ctx context.Context, ownerUUID uuid.UUID) ([]*ClientCredential, error)
ListByOwner retrieves all client credentials owned by a user SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: list all client credentials owned by a user, ordered by creation time (reads DB)
func (*GormClientCredentialRepository) UpdateLastUsed ¶
UpdateLastUsed updates the last_used_at timestamp for a client credential SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: update the last-used timestamp for a client credential by ID (reads DB)
type GormDeletionRepository ¶
type GormDeletionRepository struct {
// contains filtered or unexported fields
}
GormDeletionRepository implements DeletionRepository using GORM SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: GORM-backed repository for cascading deletion of users, groups, and their owned data
func NewGormDeletionRepository ¶
func NewGormDeletionRepository(db *gorm.DB) *GormDeletionRepository
NewGormDeletionRepository creates a new GORM-backed deletion repository SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: build a GormDeletionRepository backed by the given GORM DB connection (pure)
func (*GormDeletionRepository) DeleteGroupAndData ¶
func (r *GormDeletionRepository) DeleteGroupAndData(ctx context.Context, internalUUID string) (*GroupDeletionResult, error)
DeleteGroupAndData deletes a group by internal UUID and handles threat model cleanup Uses internal_uuid for precise identification to avoid issues with duplicate group_names SEM@d0742bff5d3b93b3ab7b22df0377398a720a8d9c: delete a non-built-in group and cascade-delete or transfer its owned threat models (reads DB)
func (*GormDeletionRepository) DeleteUserAndData ¶
func (r *GormDeletionRepository) DeleteUserAndData(ctx context.Context, userEmail string) (*DeletionResult, error)
DeleteUserAndData deletes a user by email and handles ownership transfer for threat models. Used by the self-deletion flow (DELETE /me) where identity comes from JWT email. SEM@d0742bff5d3b93b3ab7b22df0377398a720a8d9c: delete a user by email and all owned data in a retryable transaction (reads DB)
func (*GormDeletionRepository) DeleteUserByInternalUUID ¶
func (r *GormDeletionRepository) DeleteUserByInternalUUID(ctx context.Context, internalUUID string) (*DeletionResult, error)
DeleteUserByInternalUUID deletes a user by internal UUID and handles ownership transfer. Used by admin deletion to avoid multi-hop identity resolution (UUID→provider→email→UUID) that can target the wrong user when duplicate provider/email records exist. SEM@d0742bff5d3b93b3ab7b22df0377398a720a8d9c: delete a user by internal UUID and all owned data in a retryable transaction (reads DB)
func (*GormDeletionRepository) TransferOwnership ¶
func (r *GormDeletionRepository) TransferOwnership(ctx context.Context, sourceUserUUID, targetUserUUID string) (*TransferResult, error)
TransferOwnership transfers all owned threat models and survey responses from sourceUserUUID to targetUserUUID within a single transaction. The source user is downgraded to "writer" role on all transferred items. SEM@d0742bff5d3b93b3ab7b22df0377398a720a8d9c: transfer all owned threat models and survey responses from one user to another, downgrading the source to writer (mutates DB)
type GormUserRepository ¶
type GormUserRepository struct {
// contains filtered or unexported fields
}
GormUserRepository implements UserRepository using GORM SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: GORM-backed implementation of the user repository interface
func NewGormUserRepository ¶
func NewGormUserRepository(db *gorm.DB) *GormUserRepository
NewGormUserRepository creates a new GORM-backed user repository SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: build a GORM user repository bound to the given database connection (pure)
func (*GormUserRepository) Create ¶
Create creates a new user SEM@b01ccb8e475aed5b956de76b96fe25b3de6076d0: create a user via retryable transaction, generating UUID/timestamps (writes DB)
func (*GormUserRepository) Delete ¶
func (r *GormUserRepository) Delete(ctx context.Context, id string) error
Delete deletes a user by internal UUID SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: delete a user by internal UUID, returning an error if not found (mutates shared state)
func (*GormUserRepository) GetByAnyProviderID ¶
func (r *GormUserRepository) GetByAnyProviderID(ctx context.Context, providerUserID string) (*User, error)
GetByAnyProviderID retrieves a user by provider user ID across all providers SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: fetch a user by provider user ID across all OAuth providers (reads DB)
func (*GormUserRepository) GetByEmail ¶
GetByEmail retrieves a user by email address SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: fetch a user by email address (reads DB)
func (*GormUserRepository) GetByID ¶
GetByID retrieves a user by internal UUID SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: fetch a user by internal UUID (reads DB)
func (*GormUserRepository) GetByProviderAndEmail ¶
func (r *GormUserRepository) GetByProviderAndEmail(ctx context.Context, provider, email string) (*User, error)
GetByProviderAndEmail retrieves a user by provider and email address SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: fetch a user matching both provider name and email address (reads DB)
func (*GormUserRepository) GetByProviderID ¶
func (r *GormUserRepository) GetByProviderID(ctx context.Context, provider, providerUserID string) (*User, error)
GetByProviderID retrieves a user by provider and provider user ID SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: fetch a user by OAuth provider and provider user ID (reads DB)
func (*GormUserRepository) GetPrimaryProviderID ¶
func (r *GormUserRepository) GetPrimaryProviderID(ctx context.Context, userID string) (string, error)
GetPrimaryProviderID returns the provider user ID for a user SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: fetch the primary OAuth provider user ID for a given user (reads DB)
func (*GormUserRepository) GetProviders ¶
func (r *GormUserRepository) GetProviders(ctx context.Context, userID string) ([]UserProvider, error)
GetProviders returns the OAuth providers for a user Note: In the current architecture, each user has exactly one provider SEM@2dccb03396c9b3e288e2242edb54c418635c3e08: list the OAuth providers associated with a user (reads DB)
type GroupDeletionResult ¶
type GroupDeletionResult struct {
ThreatModelsDeleted int
ThreatModelsRetained int
GroupName string
}
GroupDeletionResult contains statistics about group deletion operations SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: statistics returned after deleting a group, reporting affected threat model counts (pure)
type TransferResult ¶
TransferResult contains statistics about an ownership transfer operation SEM@36c1f84217ecf3f5087ad65186cd974b9b4df275: identifiers of threat models and survey responses transferred during an ownership transfer (pure)
type User ¶
type User struct {
InternalUUID string
Provider string
ProviderUserID string
Email string
Name string
EmailVerified bool
AccessToken *string
RefreshToken *string
TokenExpiry *time.Time
CreatedAt time.Time
ModifiedAt time.Time
LastLogin *time.Time
Automation *bool
}
User represents a user entity for repository operations SEM@24dcbaf59ea6bfe4e66c3f1fbc4863c809cfdc0e: repository model for a user account with OAuth provider identity and token fields (pure)
type UserProvider ¶
type UserProvider struct {
ID string
UserID string
Provider string
ProviderUserID string
Email string
IsPrimary bool
CreatedAt time.Time
LastLogin time.Time
}
UserProvider represents a user's OAuth provider information SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: OAuth provider linkage record associating a user to a specific provider identity (pure)
type UserRepository ¶
type UserRepository interface {
// GetByEmail retrieves a user by email address
GetByEmail(ctx context.Context, email string) (*User, error)
// GetByID retrieves a user by internal UUID
GetByID(ctx context.Context, id string) (*User, error)
// GetByProviderID retrieves a user by provider and provider user ID
GetByProviderID(ctx context.Context, provider, providerUserID string) (*User, error)
// GetByProviderAndEmail retrieves a user by provider and email address
GetByProviderAndEmail(ctx context.Context, provider, email string) (*User, error)
// GetByAnyProviderID retrieves a user by provider user ID across all providers
GetByAnyProviderID(ctx context.Context, providerUserID string) (*User, error)
// GetProviders returns the OAuth providers for a user
GetProviders(ctx context.Context, userID string) ([]UserProvider, error)
// GetPrimaryProviderID returns the primary provider user ID for a user
GetPrimaryProviderID(ctx context.Context, userID string) (string, error)
// Create creates a new user
Create(ctx context.Context, user *User) (*User, error)
// Update updates an existing user
Update(ctx context.Context, user *User) error
// Delete deletes a user by internal UUID
Delete(ctx context.Context, id string) error
}
UserRepository handles user CRUD operations SEM@b4b216a8ad19c2ca17d1d9e7466281e90c7b2f41: interface for user CRUD and provider-lookup operations against the backing store