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

authn

package
v0.5.1 Latest Latest
Warning

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

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

Documentation

Overview

Package authn is the authentication boundary of the operator surface (ADR-0021). It defines the one principal every caller of the domain layer acts as, how that principal travels in a context to the domain-function boundary, and the Authenticator interface the surface authenticates through: one verifier per credential form (the password, an operator token, a session) over one session minter.

The implementation over the operator domain is operator.NewAuthenticator. Nothing outside internal/operator depends on how a credential is stored or checked.

Version 1 has one operator and binary authorization: a caller is authenticated or it is not. The principal therefore names the one operator and nothing more; it carries no record of which credential produced it, no role, and no scope.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoPrincipal = errors.New("authn: no authenticated principal")

ErrNoPrincipal is a domain function's refusal of a caller whose context carries no principal: it reached the domain layer without having been authenticated, which only a wiring fault can do.

View Source
var ErrUnsupportedForm = errors.New("authn: no verifier accepts this credential form")

ErrUnsupportedForm means no verifier accepts the credential's form.

Functions

func NewContext

func NewContext(ctx context.Context, p *Principal) context.Context

NewContext returns a context carrying p. The surface's authentication middleware and the command line's entry path are its callers; nothing else places a principal.

Types

type Authenticator

type Authenticator interface {
	// Verify resolves a credential to the principal through the verifier
	// for its form, or reports ErrUnsupportedForm when there is none.
	Verify(ctx context.Context, c Credential) (*Principal, error)
	Minter
}

Authenticator is the boundary the operator surface is written against: credential verification by form, over one session minter. The operator domain's implementation is operator.NewAuthenticator; a backend that verifies elsewhere replaces it without touching the surface.

type Chain

type Chain struct {
	Verifiers []Verifier
	Minter    Minter
}

Chain is an Authenticator composed from a list of verifiers over one minter. The first verifier whose form matches the credential decides.

func (*Chain) End

func (c *Chain) End(ctx context.Context, id string) error

End implements Minter.

func (*Chain) Mint

func (c *Chain) Mint(ctx context.Context) (MintedSession, error)

Mint implements Minter.

func (*Chain) Verify

func (c *Chain) Verify(ctx context.Context, cred Credential) (*Principal, error)

Verify implements Authenticator.

type Credential

type Credential struct {
	Form   Form
	Secret string
}

Credential is one presented credential: its form and its secret.

type Form

type Form int

Form names a credential form. Forms exist only at this boundary: a verifier is chosen by the form presented, and the principal it resolves never records which one it was.

const (
	// Password is the operator password, presented once to log in.
	Password Form = iota + 1
	// OperatorToken is a bearer operator token, presented by automation.
	OperatorToken
	// Session is a session identifier a login minted, presented by a
	// browser in its cookie.
	Session
)

type MintedSession

type MintedSession struct {
	ID       string
	Lifetime time.Duration
}

MintedSession is a session a login minted: the identifier the client holds, returned exactly once, and its fixed lifetime.

type Minter

type Minter interface {
	// Mint draws and persists a new session.
	Mint(ctx context.Context) (MintedSession, error)
	// End revokes a session. An identifier that no longer authenticates
	// is not an error: that is the outcome the caller wants.
	End(ctx context.Context, id string) error
}

Minter mints sessions and ends them. A session is minted only after a credential has been verified; the minter does not verify.

type Principal

type Principal struct {
	DisplayName *string
}

Principal is the authenticated operator as the domain layer sees it. Both operator credential forms resolve to it, and the command line acts as it; it carries no record of which credential, or which transport, produced it. DisplayName is the optional name set with the password; a surface renders a fallback when it is nil.

func FromContext

func FromContext(ctx context.Context) (*Principal, bool)

FromContext returns the principal ctx carries. It reports false when there is none, so a caller that reaches the domain layer without having been authenticated is told so rather than handed a zero principal.

type Verifier

type Verifier interface {
	// Form is the credential form this verifier accepts.
	Form() Form
	// Verify resolves secret to the principal, or refuses it.
	Verify(ctx context.Context, secret string) (*Principal, error)
}

Verifier checks one credential form and resolves it to the principal. A refusal is the verifier's own error, which callers match with errors.Is; a verifier never falls back to another form.

Jump to

Keyboard shortcuts

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