repository

package
v1.21.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var (
	ErrUserNotFound             = fmt.Errorf("user: %w", dberrors.ErrNotFound)
	ErrClientCredentialNotFound = fmt.Errorf("client credential: %w", dberrors.ErrNotFound)
	ErrGroupNotFound            = fmt.Errorf("group: %w", dberrors.ErrNotFound)
	ErrUnauthorized             = fmt.Errorf("unauthorized: %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@32e22d40fa43dfa14fa39b14713e41c740ebe026: domain record of a client credential with owner, hashed secret, and expiry

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@32e22d40fa43dfa14fa39b14713e41c740ebe026: parameters for creating a client credential

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

type DeletionResult struct {
	ThreatModelsTransferred int
	ThreatModelsDeleted     int
	UserEmail               string
}

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

Create creates a new client credential SEM@32e22d40fa43dfa14fa39b14713e41c740ebe026: store a new client credential with hashed secret and ownership (writes 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

func (r *GormClientCredentialRepository) Delete(ctx context.Context, id, ownerUUID uuid.UUID) error

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

func (r *GormClientCredentialRepository) UpdateLastUsed(ctx context.Context, id uuid.UUID) error

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@3b9af7c655cdfe5497882bbc367b72fd757569a9: transfer all threat model ownership from one user to another in a retryable transaction (writes 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

func (r *GormUserRepository) Create(ctx context.Context, user *User) (*User, error)

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

func (r *GormUserRepository) GetByEmail(ctx context.Context, email string) (*User, error)

GetByEmail retrieves a user by email address SEM@8077d4387088ee7e6e22cce2171ad54ee850e10b: fetch a user by email address (reads DB)

func (*GormUserRepository) GetByID

func (r *GormUserRepository) GetByID(ctx context.Context, id string) (*User, error)

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)

func (*GormUserRepository) Update

func (r *GormUserRepository) Update(ctx context.Context, user *User) error

Update updates an existing user SEM@2dccb03396c9b3e288e2242edb54c418635c3e08: update a user's mutable fields, preserving existing provider identity (mutates shared state)

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

type TransferResult struct {
	ThreatModelIDs    []string
	SurveyResponseIDs []string
}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL