stdcrpcapikey

package
v0.0.265 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package stdcrpcapikey authenticates connect-go v2 RPCs with API keys that a database stores. A key is "<prefix><id>_<random>", such as "ak_<id>_<random>" with the default prefix: the prefix lets secret scanners and people tell a key apart, the ID finds the stored key without searching by its hash, and the random part, 130 bits, makes it unguessable. The database stores only the key's SHA-256 hash, so a leaked row does not leak the key, and a deleted row revokes the key at once.

New mints keys. NewServerInterceptor authenticates every RPC with the key in its "Authorization: Bearer <key>" header, before the request is decoded, and puts the Caller in the context. It finds the stored key with a Lookup the application provides, which typically calls a query that sqlc generates.

The package is generic over the type of a key's ID, KID, and of a user's ID, UID, such as uuid.UUID for both. A key's ID goes in the key as its text, so KID implements encoding.TextMarshaler, and *KID encoding.TextUnmarshaler; the text must not contain whitespace. The package only passes a UID on.

New and NewServerInterceptor take the same [Option]s: pass both the same WithPrefix, or neither, so the interceptor accepts the keys New mints.

An optional bootstrap key, WithBootstrapKey, belongs to no user and may only call the procedures it is given, such as those that create a system's first user and its key.

Index

Constants

View Source
const DefaultPrefix = "ak_"

DefaultPrefix starts every key unless WithPrefix gives another prefix.

Variables

This section is empty.

Functions

func New

func New[KID encoding.TextMarshaler](id KID, opts ...Option) (key, hash string, err error)

New mints a key for the stored key with ID id, and returns it with the hash to store, its SHA-256 in lower-case hex. It fails when id has no text, or when its MarshalText does. Of opts, it uses only WithPrefix.

func NewServerInterceptor

func NewServerInterceptor[KID, UID any, PKID interface {
	*KID
	encoding.TextUnmarshaler
}](lookup Lookup[KID, UID], opts ...Option) connect.ServerInterceptor

NewServerInterceptor returns the interceptor that authenticates every RPC with the key in its "Authorization: Bearer <key>" header, and puts the Caller in the context. It fails a call with UNAUTHENTICATED, the same error whichever way the key is wrong: no key, a key of another form, one no stored key has the ID or the hash of, and one that expired. Go infers its type arguments from lookup. Install it inside an interceptor that logs and maps errors, and before any that reads the request message, such as validation, so a call without a valid key is never decoded.

Types

type Caller

type Caller[UID any] struct {
	// UserID is the ID of the user; the zero UID for the bootstrap key.
	UserID UID
	// Bootstrap is true for a call with the bootstrap key.
	Bootstrap bool
}

Caller is who calls an RPC: a user, through one of its keys, or the bootstrap key, which is no user.

func CallerOf

func CallerOf[UID any](ctx context.Context) (Caller[UID], bool)

CallerOf returns the caller of the RPC that ctx belongs to, which the interceptor of NewServerInterceptor put there, and false outside such an RPC, or when the interceptor's UID is another type.

type Lookup

type Lookup[KID, UID any] func(ctx context.Context, id KID) (Row[UID], error)

Lookup finds the stored key with ID id, and fails with pgx.ErrNoRows when there is none, as a query that sqlc generates for pgx does. It decides how to reach the database, such as in a read-only transaction or on a pool.

type Option

type Option func(*options)

Option configures New and NewServerInterceptor.

func WithBootstrapKey

func WithBootstrapKey(key string, procedures ...string) Option

WithBootstrapKey makes NewServerInterceptor accept key as the bootstrap key, which is no user's and may call only procedures, such as "/acme.v1.UserService/CreateUser"; it fails every other call with PERMISSION_DENIED. An empty key leaves the bootstrap key out. New ignores it.

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix starts keys with prefix instead of DefaultPrefix, such as "acme_"; it is used as it is, so include a separator. Pass it to both New and NewServerInterceptor.

type Row

type Row[UID any] interface {
	// KeyUserID returns the ID of the user the key authenticates.
	KeyUserID() UID
	// KeyHash returns the hash that [New] returned with the key.
	KeyHash() string
	// KeyExpireTime returns when the key expires; nil when it does not.
	KeyExpireTime() *time.Time
}

Row is what the interceptor reads from a stored key. Give the row type that the query returns these methods, such as in a file next to the generated code.

Jump to

Keyboard shortcuts

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