repository

package
v1.21.30 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 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

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

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

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

type DeletionResult

type DeletionResult struct {
	ThreatModelsTransferred int
	ThreatModelsDeleted     int
	UserEmail               string
}

DeletionResult contains statistics about user/group deletion operations

type GormClientCredentialRepository

type GormClientCredentialRepository struct {
	// contains filtered or unexported fields
}

GormClientCredentialRepository implements ClientCredentialRepository using GORM

func NewGormClientCredentialRepository

func NewGormClientCredentialRepository(db *gorm.DB) *GormClientCredentialRepository

NewGormClientCredentialRepository creates a new GORM-backed client credential repository

func (*GormClientCredentialRepository) Create

Create creates a new client credential

func (*GormClientCredentialRepository) Deactivate

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

Deactivate deactivates a client credential (soft delete)

func (*GormClientCredentialRepository) Delete

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

Delete permanently deletes a client credential

func (*GormClientCredentialRepository) GetByClientID

func (r *GormClientCredentialRepository) GetByClientID(ctx context.Context, clientID string) (*ClientCredential, error)

GetByClientID retrieves an active client credential by client ID

func (*GormClientCredentialRepository) ListByOwner

func (r *GormClientCredentialRepository) ListByOwner(ctx context.Context, ownerUUID uuid.UUID) ([]*ClientCredential, error)

ListByOwner retrieves all client credentials owned by a user

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

type GormDeletionRepository

type GormDeletionRepository struct {
	// contains filtered or unexported fields
}

GormDeletionRepository implements DeletionRepository using GORM

func NewGormDeletionRepository

func NewGormDeletionRepository(db *gorm.DB) *GormDeletionRepository

NewGormDeletionRepository creates a new GORM-backed deletion repository

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

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.

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.

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.

type GormUserRepository

type GormUserRepository struct {
	// contains filtered or unexported fields
}

GormUserRepository implements UserRepository using GORM

func NewGormUserRepository

func NewGormUserRepository(db *gorm.DB) *GormUserRepository

NewGormUserRepository creates a new GORM-backed user repository

func (*GormUserRepository) Create

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

Create creates a new user

func (*GormUserRepository) Delete

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

Delete deletes a user by internal UUID

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

func (*GormUserRepository) GetByEmail

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

GetByEmail retrieves a user by email address

func (*GormUserRepository) GetByID

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

GetByID retrieves a user by internal UUID

func (*GormUserRepository) GetByProviderAndEmail

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

GetByProviderAndEmail retrieves a user by provider and email address

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

func (*GormUserRepository) GetPrimaryProviderID

func (r *GormUserRepository) GetPrimaryProviderID(ctx context.Context, userID string) (string, error)

GetPrimaryProviderID returns the provider user ID for a user

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

func (*GormUserRepository) Update

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

Update updates an existing user

type GroupDeletionResult

type GroupDeletionResult struct {
	ThreatModelsDeleted  int
	ThreatModelsRetained int
	GroupName            string
}

GroupDeletionResult contains statistics about group deletion operations

type TransferResult

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

TransferResult contains statistics about an ownership transfer operation

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

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

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

Jump to

Keyboard shortcuts

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