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

apikeys

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package apikeys issues API keys to accounts and authenticates API requests with them.

A key is the app's prefix, an underscore and 32 random bytes in base64url, such as acme_V3kz…. Only the SHA-256 of the whole key is stored, with its last characters for display. The raw key is shown once, when it is created, and never logged. Lookup is by hash, so changing the prefix never invalidates existing keys.

templ: version: v0.3.1070

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotFound means the account has no key with that ID.
	ErrNotFound = errors.New("apikeys: key not found")
	// ErrInvalidKey means a presented key is malformed, unknown or revoked.
	ErrInvalidKey = errors.New("apikeys: invalid key")
	// ErrInvalidName means a key name is empty, too long or contains control characters.
	ErrInvalidName = errors.New("apikeys: a key name needs 1 to 64 printable characters")
	// ErrActive means a key must be revoked before it can be deleted.
	ErrActive = errors.New("apikeys: revoke the key before deleting it")
)
View Source
var Migrations = app.Migrations{Namespace: "apikeys", Files: sub(migrationFiles, "migrations")}

Migrations create the api_keys table. List them after accounts.Migrations.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Prefix starts every new key, such as "acme" for acme_… keys. Letters, digits and
	// underscores, starting with a letter.
	Prefix string
}

Config configures the module.

type Key

type Key struct {
	ID        string
	AccountID string
	Name      string
	// Prefix is the module's current prefix, for display.
	Prefix     string
	Suffix     string
	CreatedAt  time.Time
	LastUsedAt *time.Time
	RevokedAt  *time.Time
}

Key describes an issued key. It never holds the raw key.

func Current

func Current(c *gin.Context) Key

Current returns the key that authenticated the request. It panics on routes without RequireKey, which is a programming error.

func (Key) Display

func (k Key) Display() string

Display returns a recognisable, non-secret form of the key, such as acme_••••x7Qa9z.

type Keys

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

Keys is the API keys module.

func New

func New(a *app.App, cfg Config) (*Keys, error)

New returns the API keys module. Keys belong to accounts, and a foreign key deletes them with their account.

func (*Keys) Authenticate

func (k *Keys) Authenticate(ctx context.Context, raw string) (Key, error)

Authenticate returns the active key matching raw. It records when the key was last used, at most every five minutes.

func (*Keys) Create

func (k *Keys) Create(ctx context.Context, accountID, name string) (string, Key, error)

Create issues a key and returns it raw, once. Store or show it immediately.

func (*Keys) Delete

func (k *Keys) Delete(ctx context.Context, accountID, keyID string) error

Delete removes a revoked key from the account's list. An active key returns ErrActive; a key that no longer exists counts as deleted.

func (*Keys) List

func (k *Keys) List(ctx context.Context, accountID string) ([]Key, error)

List returns an account's keys, newest first, revoked ones included.

func (*Keys) LockActive

func (k *Keys) LockActive(ctx context.Context, tx *sql.Tx, accountID, keyID string) error

LockActive checks inside tx that the key still authenticates for the account, and keeps it from being revoked until tx ends. Run it in the transaction that spends credit for a request, so a key revoked meanwhile cannot spend. It returns ErrInvalidKey for a revoked or unknown key.

func (*Keys) Mount

func (k *Keys) Mount(r gin.IRouter, layout ui.Layout)

Mount adds the keys page to r, a group at the page's path that uses accounts.RequireSignIn, such as /account/api-keys:

  • GET "" lists the account's keys.
  • GET "/list" renders the list alone, for refreshes after a change.
  • POST "" creates a key and shows it once.
  • POST "/:id" renames ("action=rename" and "name"), revokes ("action=revoke") or deletes ("action=delete" and the key's name as "confirm_name") a key. Only revoked keys can be deleted.

func (*Keys) Rename

func (k *Keys) Rename(ctx context.Context, accountID, keyID, name string) error

Rename renames an active key. A revoked or unknown key returns ErrNotFound.

func (*Keys) RequireKey

func (k *Keys) RequireKey(c *gin.Context)

RequireKey protects API routes. It accepts exactly one "Authorization: Bearer <key>" header, ignores cookies, and answers 401 with a JSON error otherwise. The header is removed once the key is checked, so handlers cannot log or forward it.

func (*Keys) Revoke

func (k *Keys) Revoke(ctx context.Context, accountID, keyID string) error

Revoke stops a key from authenticating. Revoking a revoked key changes nothing. An unknown key returns ErrNotFound.

Jump to

Keyboard shortcuts

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