apikeys

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const PermissionEReaderBrowser = "ereader_browser"

PermissionEReaderBrowser is the permission for accessing the eReader browser UI.

View Source
const PermissionKoboSync = "kobo_sync"

PermissionKoboSync is the permission for Kobo sync API access.

Variables

View Source
var ErrNotFound = errors.New("api key not found")

Functions

func RegisterRoutes

func RegisterRoutes(e *echo.Echo, db *bun.DB, authMiddleware *auth.Middleware)

RegisterRoutes registers API key management routes.

Types

type APIKey

type APIKey struct {
	bun.BaseModel `bun:"table:api_keys,alias:ak" json:"-"`

	ID             string     `bun:"id,pk" json:"id"`
	UserID         int        `bun:"user_id,notnull" json:"userId"`
	Name           string     `bun:"name,notnull" json:"name"`
	Key            string     `bun:"key,notnull,unique" json:"key"`
	CreatedAt      time.Time  `bun:"created_at,notnull" json:"createdAt"`
	UpdatedAt      time.Time  `bun:"updated_at,notnull" json:"updatedAt"`
	LastAccessedAt *time.Time `bun:"last_accessed_at" json:"lastAccessedAt"`

	Permissions []*APIKeyPermission `bun:"rel:has-many,join:id=api_key_id" json:"permissions"`
}

APIKey represents a user's API key for programmatic access.

func (*APIKey) HasPermission

func (ak *APIKey) HasPermission(permission string) bool

HasPermission checks if the API key has a specific permission.

func (*APIKey) PermissionStrings

func (ak *APIKey) PermissionStrings() []string

PermissionStrings returns a list of permission strings.

type APIKeyPermission

type APIKeyPermission struct {
	bun.BaseModel `bun:"table:api_key_permissions,alias:akp" json:"-"`

	ID         string    `bun:"id,pk" json:"id"`
	APIKeyID   string    `bun:"api_key_id,notnull" json:"apiKeyId"`
	Permission string    `bun:"permission,notnull" json:"permission"`
	CreatedAt  time.Time `bun:"created_at,notnull" json:"createdAt"`
}

APIKeyPermission represents a permission granted to an API key.

type APIKeyShortURL

type APIKeyShortURL struct {
	bun.BaseModel `bun:"table:api_key_short_urls,alias:aksu" json:"-"`

	ID        string    `bun:"id,pk" json:"id"`
	APIKeyID  string    `bun:"api_key_id,notnull" json:"apiKeyId"`
	ShortCode string    `bun:"short_code,notnull,unique" json:"shortCode"`
	ExpiresAt time.Time `bun:"expires_at,notnull" json:"expiresAt"`
	CreatedAt time.Time `bun:"created_at,notnull" json:"createdAt"`

	APIKey *APIKey `bun:"rel:belongs-to,join:api_key_id=id" json:"-"`
}

APIKeyShortURL represents a temporary short URL for eReader setup.

type CreateRequest

type CreateRequest struct {
	Name string `json:"name"`
}

CreateRequest is the payload for creating an API key.

type Service

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

func NewService

func NewService(db *bun.DB) *Service

func (*Service) AddPermission

func (s *Service) AddPermission(ctx context.Context, userID int, keyID string, permission string) (*APIKey, error)

AddPermission adds a permission to an API key.

func (*Service) ClearKoboSyncHistory

func (s *Service) ClearKoboSyncHistory(ctx context.Context, userID int, keyID string) error

ClearKoboSyncHistory deletes all Kobo sync points for an API key, forcing a fresh sync.

func (*Service) Create

func (s *Service) Create(ctx context.Context, userID int, name string) (*APIKey, error)

Create creates a new API key for a user.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, userID int, keyID string) error

Delete removes an API key (only if owned by the user).

func (*Service) GenerateShortURL

func (s *Service) GenerateShortURL(ctx context.Context, userID int, keyID string) (*APIKeyShortURL, error)

GenerateShortURL creates a temporary short URL for an API key.

func (*Service) GetByKey

func (s *Service) GetByKey(ctx context.Context, key string) (*APIKey, error)

GetByKey retrieves an API key by its key value.

func (*Service) List

func (s *Service) List(ctx context.Context, userID int) ([]*APIKey, error)

List returns all API keys for a user.

func (*Service) RemovePermission

func (s *Service) RemovePermission(ctx context.Context, userID int, keyID string, permission string) (*APIKey, error)

RemovePermission removes a permission from an API key.

func (*Service) ResolveShortCode

func (s *Service) ResolveShortCode(ctx context.Context, shortCode string) (*APIKey, error)

ResolveShortCode looks up a short code and returns the associated API key.

func (*Service) TouchLastAccessed

func (s *Service) TouchLastAccessed(ctx context.Context, keyID string) error

TouchLastAccessed updates the last_accessed_at timestamp for an API key.

func (*Service) UpdateName

func (s *Service) UpdateName(ctx context.Context, userID int, keyID string, name string) (*APIKey, error)

UpdateName updates an API key's name.

type UpdateNameRequest

type UpdateNameRequest struct {
	Name string `json:"name"`
}

UpdateNameRequest is the payload for updating an API key's name.

Jump to

Keyboard shortcuts

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