auth104

package
v0.28.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 21 Imported by: 1 module, 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 auth104 is the server side of BRC-103 mutual authentication over HTTP (BRC-104), as a provider's routes need it: the handshake at /.well-known/auth, and each authenticated request checked and its answer signed, the way the SDK's clients (authhttp in Go, AuthFetch in TypeScript) send and check them. It holds the hardening every bstore route shares: a handshake budget per address and for the route, checked before any signature work; bounded sessions; a budget of signed answers per session, taken before a request is verified; and a request sent twice refused before any work. go-sdk carries the client and the Peer but no HTTP server, so this is that server, built on the SDK's own payload encoding (authpayload) and nonce rules.

Index

Constants

View Source
const (
	DefaultMaxSessions   = 10000
	DefaultSessionTTL    = 10 * time.Minute
	DefaultMaxRemembered = 1 << 18
	DefaultMaxHandshake  = 16 << 10
)
View Source
const HandshakePath = "/.well-known/auth"

HandshakePath is where the SDK's clients send a handshake: the origin's /.well-known/auth.

View Source
const MaxAddresses = 10000

MaxAddresses is the most addresses with a bucket of their own.

Variables

View Source
var (
	DefaultHandshakes = Budget{Rate: 4, Burst: 8}
	DefaultPerAddress = Budget{Rate: 1, Burst: 4}
	DefaultResponses  = Budget{Rate: 5, Burst: 20}
)

The defaults, as the bstore host's terms route has them.

Functions

func AddressKey

func AddressKey(remote string) string

AddressKey is the key a remote address is budgeted under: an IPv4 address as it is (an IPv4-mapped IPv6 address as its IPv4 address), an IPv6 address by its /64, which is what one host is usually given. A port is dropped.

func Write

func Write(w http.ResponseWriter, r Reply)

Write writes r.

Types

type Budget

type Budget struct {
	Rate  float64
	Burst float64
}

Budget is a token bucket's rate and burst: Rate a second, at most Burst at once.

type Caller

type Caller struct {
	Key *ec.PublicKey
	Hex string
}

Caller is who sent a request: the identity key BRC-103 authenticated, or nil for a request without authentication.

type Config

type Config struct {
	Wallet Wallet
	// MaxSessions bounds the sessions kept, SessionTTL how long one idle is
	// kept, and MaxRemembered the request identifiers remembered across all
	// sessions (past it the least recently used session is forgotten whole).
	MaxSessions   int
	SessionTTL    time.Duration
	MaxRemembered int
	// Handshakes and PerAddress are the handshake budget; Responses the
	// signed answers one session is given.
	Handshakes Budget
	PerAddress Budget
	Responses  Budget
	// MaxHandshake bounds a handshake's body.
	MaxHandshake int64
	Now          func() time.Time
	// ClientAddr, when set, is the remote address a request is budgeted
	// under (a client behind a trusted proxy); RemoteAddr otherwise.
	ClientAddr func(*http.Request) string
	// Count, when set, is told each outcome: "handshake" with accepted,
	// failed, limited_address or limited_global; "request" with replayed,
	// limited, failed or accepted; "session_evicted" with cap, idle or
	// requests.
	Count func(what, result string)
}

Config is a server's settings. Zero fields take the defaults below.

type Handler

type Handler func(ctx context.Context, r *http.Request, body []byte, who *Caller) Reply

Handler answers one request whose body is already read and bounded.

type Handshakes

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

Handshakes is the handshake budget: a bucket for the route and one for each remote address, an address's checked first so that one address that floods spends its own budget and not the route's.

func NewHandshakes

func NewHandshakes(global, perAddress Budget, now func() time.Time) (*Handshakes, error)

NewHandshakes is a handshake budget.

func (*Handshakes) Take

func (h *Handshakes) Take(remote string) (ok bool, limit string, retry int)

Take takes one handshake for a remote address, or says which limit refused it ("address" or "global") and the seconds to wait.

type Keyed

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

Keyed is token buckets by key, at most Max of them: past it the least recently used is forgotten, which only ever gives a forgotten key a full bucket again, never more than Burst at once.

func NewKeyed

func NewKeyed(c Budget, max int, now func() time.Time) (*Keyed, error)

NewKeyed is a keyed budget of at most max keys.

func (*Keyed) Len

func (k *Keyed) Len() int

Len is how many keys have a bucket.

func (*Keyed) Take

func (k *Keyed) Take(key string) (bool, int)

Take takes one token from key's bucket, or says how many seconds until one is there.

type Reply

type Reply struct {
	Status  int
	Headers http.Header
	Body    []byte
}

Reply is an answer before it is written, signed when its request was authenticated.

func Error

func Error(status int, code, description string, headers ...string) Reply

Error is the JSON error reply a route answers.

func JSON

func JSON(status int, v any, headers ...string) Reply

JSON is a reply of v as JSON.

type Server

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

Server answers BRC-104 handshakes and authenticated requests.

func New

func New(ctx context.Context, cfg Config) (*Server, error)

New is a server over cfg.

func (*Server) Handshake

func (s *Server) Handshake(w http.ResponseWriter, r *http.Request)

Handshake answers a POST to /.well-known/auth. The budget is taken before the body is read and before any signature work.

func (*Server) Identity

func (s *Server) Identity() *ec.PublicKey

Identity is the server's identity key.

func (*Server) Serve

func (s *Server) Serve(w http.ResponseWriter, r *http.Request, maxBody int64, h Handler)

Serve reads a request's body up to maxBody bytes and answers it with h: unauthenticated when it carries no BRC-104 headers (who is nil), and otherwise only once it verifies, with its answer signed. A request that carries some authentication headers and not others is refused.

func (*Server) Sessions

func (s *Server) Sessions() int

Sessions is how many sessions are kept.

type Wallet

type Wallet interface {
	wallet.KeyOperations
}

Wallet is what the server signs and checks with: the SDK's ProtoWallet, or any BRC-100 wallet.

Jump to

Keyboard shortcuts

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