apikey

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAPIKeyNotFound   = fmt.Errorf("%w: api key not found", shared.ErrNotFound)
	ErrAPIKeyNameExists = fmt.Errorf("%w: api key name already exists", shared.ErrAlreadyExists)
)

Functions

This section is empty.

Types

type APIKey

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

APIKey represents a tenant API key for programmatic access.

func NewAPIKey

func NewAPIKey(id, tenantID ID, name, keyHash, keyPrefix string) *APIKey

NewAPIKey creates a new API key entity.

func Reconstruct

func Reconstruct(
	id, tenantID ID,
	userID *ID,
	name, description, keyHash, keyPrefix string,
	scopes []string,
	rateLimit int,
	status Status,
	expiresAt, lastUsedAt *time.Time,
	lastUsedIP string,
	useCount int64,
	createdBy *ID,
	createdAt, updatedAt time.Time,
	revokedAt *time.Time,
	revokedBy *ID,
) *APIKey

Reconstruct creates an APIKey from stored data (database reconstruction).

func (*APIKey) CreatedAt

func (k *APIKey) CreatedAt() time.Time

func (*APIKey) CreatedBy

func (k *APIKey) CreatedBy() *ID

func (*APIKey) Description

func (k *APIKey) Description() string

func (*APIKey) ExpiresAt

func (k *APIKey) ExpiresAt() *time.Time

func (*APIKey) ID

func (k *APIKey) ID() ID

func (*APIKey) IsActive

func (k *APIKey) IsActive() bool

IsActive returns true if the key is active and not expired.

func (*APIKey) KeyHash

func (k *APIKey) KeyHash() string

func (*APIKey) KeyPrefix

func (k *APIKey) KeyPrefix() string

func (*APIKey) LastUsedAt

func (k *APIKey) LastUsedAt() *time.Time

func (*APIKey) LastUsedIP

func (k *APIKey) LastUsedIP() string

func (*APIKey) Name

func (k *APIKey) Name() string

func (*APIKey) RateLimit

func (k *APIKey) RateLimit() int

func (*APIKey) Revoke

func (k *APIKey) Revoke(revokedBy ID) error

Revoke marks the key as revoked.

func (*APIKey) RevokedAt

func (k *APIKey) RevokedAt() *time.Time

func (*APIKey) RevokedBy

func (k *APIKey) RevokedBy() *ID

func (*APIKey) Scopes

func (k *APIKey) Scopes() []string

func (*APIKey) SetCreatedBy

func (k *APIKey) SetCreatedBy(id ID)

func (*APIKey) SetDescription

func (k *APIKey) SetDescription(desc string)

func (*APIKey) SetExpiresAt

func (k *APIKey) SetExpiresAt(t *time.Time)

func (*APIKey) SetRateLimit

func (k *APIKey) SetRateLimit(limit int)

func (*APIKey) SetScopes

func (k *APIKey) SetScopes(scopes []string)

func (*APIKey) SetUserID

func (k *APIKey) SetUserID(id *ID)

func (*APIKey) Status

func (k *APIKey) Status() Status

func (*APIKey) TenantID

func (k *APIKey) TenantID() ID

func (*APIKey) UpdatedAt

func (k *APIKey) UpdatedAt() time.Time

func (*APIKey) UseCount

func (k *APIKey) UseCount() int64

func (*APIKey) UserID

func (k *APIKey) UserID() *ID

type Filter

type Filter struct {
	TenantID  *ID
	UserID    *ID
	Status    *Status
	Search    string
	Page      int
	PerPage   int
	SortBy    string
	SortOrder string
}

Filter represents filtering options for listing API keys.

type ID

type ID = shared.ID

ID is a type alias for shared.ID.

type ListResult

type ListResult struct {
	Data       []*APIKey
	Total      int64
	Page       int
	PerPage    int
	TotalPages int
}

ListResult represents a paginated list of API keys.

type Repository

type Repository interface {
	Create(ctx context.Context, key *APIKey) error
	GetByID(ctx context.Context, id, tenantID ID) (*APIKey, error)
	GetByHash(ctx context.Context, hash string) (*APIKey, error)
	List(ctx context.Context, filter Filter) (ListResult, error)
	Update(ctx context.Context, key *APIKey) error
	Delete(ctx context.Context, id, tenantID ID) error
}

Repository defines the interface for API key persistence.

type Status

type Status string

Status represents the API key status.

const (
	StatusActive  Status = "active"
	StatusExpired Status = "expired"
	StatusRevoked Status = "revoked"
)

func (Status) IsValid

func (s Status) IsValid() bool

IsValid returns true if the status is valid.

Jump to

Keyboard shortcuts

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