apikeys

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidAPIKeyName is returned when the API key name is invalid.
	ErrInvalidAPIKeyName = errors.New("invalid API key name")

	// ErrAPIKeyNotFound is returned when the API key is not found.
	ErrAPIKeyNotFound = errors.New("API key not found")

	// ErrAPIKeyRevoked is returned when the API key is revoked.
	ErrAPIKeyRevoked = errors.New("API key is revoked")

	// ErrAPIKeyExpired is returned when the API key is expired.
	ErrAPIKeyExpired = errors.New("API key is expired")

	// ErrDuplicateAPIKeyName is returned when the API key name already exists for the user.
	ErrDuplicateAPIKeyName = errors.New("API key name already exists for this user")
)

Functions

func DeleteAPIKey

func DeleteAPIKey(ctx context.Context, db *gorm.DB, id string, userID uuid.UUID) error

DeleteAPIKey deletes an API key.

func MarkExpiredAPIKeys

func MarkExpiredAPIKeys(ctx context.Context, db *gorm.DB) error

MarkExpiredAPIKeys marks all expired API keys as expired.

func RevokeAPIKey

func RevokeAPIKey(ctx context.Context, db *gorm.DB, id string, userID uuid.UUID) error

RevokeAPIKey revokes an API key.

func UpdateAPIKeyLastUsed

func UpdateAPIKeyLastUsed(ctx context.Context, db *gorm.DB, id uuid.UUID) error

UpdateAPIKeyLastUsed updates the last used timestamp.

func UpdateAPIKeyState

func UpdateAPIKeyState(ctx context.Context, db *gorm.DB, id uuid.UUID, userID uuid.UUID, state string) error

UpdateAPIKeyState updates the state of an API key.

Types

type APIKey

type APIKey struct {
	ID         uuid.UUID  `json:"id"           gorm:"column:id;type:uuid;primaryKey"`
	UserID     uuid.UUID  `json:"user_id"      gorm:"column:user_id;type:uuid;not null"`
	Name       string     `json:"name"         gorm:"column:name;type:varchar(255);not null"`
	KeyHash    string     `json:"-"            gorm:"column:key_hash;type:varchar(255);unique;not null"`
	Scopes     []string   `json:"scopes"       gorm:"column:scopes;type:text[]"`
	ExpiresAt  *time.Time `json:"expires_at"   gorm:"column:expires_at;type:timestamp"`
	LastUsedAt *time.Time `json:"last_used_at" gorm:"column:last_used_at;type:timestamp"`
	RevokedAt  *time.Time `json:"revoked_at"   gorm:"column:revoked_at;type:timestamp"`
	State      string     `json:"state"        gorm:"column:state;type:varchar(50);not null;default:'active'"`
	CreatedAt  time.Time  `json:"created_at"   gorm:"autoCreateTime;column:created_at;not null"`
	UpdatedAt  time.Time  `json:"updated_at"   gorm:"autoUpdateTime;column:updated_at;not null"`
}

func CreateAPIKey

func CreateAPIKey(ctx context.Context, db *gorm.DB, apiKey *APIKey) (*APIKey, string, error)

CreateAPIKey creates a new API key.

func GetAPIKeyByHash

func GetAPIKeyByHash(ctx context.Context, db *gorm.DB, keyHash string) (*APIKey, error)

GetAPIKeyByHash retrieves an active API key by its hash.

func ListAPIKeys

func ListAPIKeys(ctx context.Context, tx *gorm.DB, params url.Values) ([]APIKey, error)

ListAPIKeys lists all API keys for a user.

func (*APIKey) BeforeCreate

func (a *APIKey) BeforeCreate(tx *gorm.DB) error

func (*APIKey) GetStatus

func (a *APIKey) GetStatus() APIKeyStatus

GetStatus returns the current status of the API key.

func (*APIKey) IsActive

func (a *APIKey) IsActive() bool

IsActive returns true if the API key is active.

func (*APIKey) TableName

func (a *APIKey) TableName() string

func (*APIKey) Validate

func (a *APIKey) Validate() error

Validate validates the API key data.

type APIKeyStatus

type APIKeyStatus string
const (
	StatusActive  APIKeyStatus = "active"
	StatusRevoked APIKeyStatus = "revoked"
	StatusExpired APIKeyStatus = "expired"
)

Jump to

Keyboard shortcuts

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