Help us shape the future of Go! Take the 2026 Go Developer Survey

apikeys

package
v0.37.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

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

ErrAPIKeyNotFound is returned by Revoke when no key has the given id.

Functions

func GenerateAPIKey

func GenerateAPIKey() (id, rawKey string, err error)

GenerateAPIKey returns a new (id, rawKey) pair. The rawKey is the secret the caller presents as a bearer token; the id is the public handle. Only the hash of rawKey is ever persisted.

Types

type APIKeyRecord

type APIKeyRecord struct {
	ID         string
	Label      string
	CreatedAt  time.Time
	LastUsedAt *time.Time
	RevokedAt  *time.Time
}

APIKeyRecord is the metadata for a single API key. It never carries the raw key or its hash — those never leave the store.

type APIKeyStore

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

APIKeyStore persists revocable API keys in Postgres. Keys are stored as SHA-256 hashes; the raw key is shown once at creation and never again.

func NewAPIKeyStore

func NewAPIKeyStore(db *sql.DB) *APIKeyStore

NewAPIKeyStore creates a Postgres-backed API key store. It shares the same *sql.DB (and therefore the same migrations) as the state store.

func (*APIKeyStore) Authenticate

func (s *APIKeyStore) Authenticate(ctx context.Context, rawToken string) (string, bool)

Authenticate reports whether rawToken matches a live (non-revoked) key. On success it returns the key id and bumps last_used_at on a best-effort basis — a failed bump never fails authentication. Implements the APIKeyAuthenticator interface consumed by the api package's BearerAuth.

func (*APIKeyStore) Create

func (s *APIKeyStore) Create(ctx context.Context, label string, now time.Time) (APIKeyRecord, string, error)

Create generates a new API key, stores its hash + metadata, and returns the record together with the raw key. The raw key is returned exactly once and cannot be recovered afterward.

func (*APIKeyStore) List

func (s *APIKeyStore) List(ctx context.Context) ([]APIKeyRecord, error)

List returns metadata for all keys (live and revoked), newest first. It never returns key hashes or raw keys.

func (*APIKeyStore) Revoke

func (s *APIKeyStore) Revoke(ctx context.Context, id string, now time.Time) error

Revoke marks the key with the given id revoked. Revoking an already-revoked key is idempotent (no error). Returns ErrAPIKeyNotFound if no key has that id.

Jump to

Keyboard shortcuts

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