guard

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 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 guard is what the services other machines may reach share: who a bearer credential speaks for (RFC 6750), and that a listener off loopback requires one.

Index

Constants

View Source
const SourceStaticToken = "static-token"

SourceStaticToken is the Principal.Source of a static token.

View Source
const StaticTokenSubject = "token"

StaticTokenSubject is the subject of the principal a static token speaks for.

Variables

This section is empty.

Functions

func IsB64Token

func IsB64Token(s string) bool

IsB64Token reports whether s is a b64token of RFC 6750 § 2.1.

func IsLoopbackAddr

func IsLoopbackAddr(addr net.Addr) bool

IsLoopbackAddr reports whether a bound address can be reached from this machine only.

func IsLoopbackHost

func IsLoopbackHost(host string) bool

IsLoopbackHost reports whether a host name or address is this machine's loopback interface.

func ListenAddress

func ListenAddress(spec string) (string, error)

ListenAddress reads a listening address as every service spells one: a port alone, or host:port whose host is empty, binds loopback (localhost).

func NoToken

func NoToken(env string) error

NoToken is why a listener other machines reach is refused with no token; env is the variable the command reads one from.

func WithPrincipal added in v0.13.0

func WithPrincipal(ctx context.Context, p Principal) context.Context

WithPrincipal is ctx carrying p, the principal a transport accepted the request for, to the request's handlers.

Types

type Authenticator added in v0.13.0

type Authenticator interface {
	// Verify classifies an Authorization header (RFC 6750 § 2.1) and names the
	// principal of an Accepted one; r is nil on a transport with no HTTP request.
	Verify(ctx context.Context, authorization string, r *http.Request) (Principal, Refusal)
	// Metadata is what the authenticator has the resource advertise.
	Metadata() ResourceMetadata
}

Authenticator decides who an Authorization header speaks for; a service asks the ones it is configured with through a Chain.

type Chain added in v0.13.0

type Chain []Authenticator

Chain asks authenticators in order and answers with the first verdict that is not NothingPresented, so each claims the credentials of its own kind.

func (Chain) Metadata added in v0.13.0

func (c Chain) Metadata() ResourceMetadata

Metadata is the authenticators' metadata together, in their order and with each value once.

func (Chain) Verify added in v0.13.0

func (c Chain) Verify(ctx context.Context, authorization string, r *http.Request) (Principal, Refusal)

Verify is the first claiming authenticator's verdict; a credential none claims is refused as its header reads (RFC 6750 § 3.1), a bearer token as Mismatch.

type Principal added in v0.13.0

type Principal struct {
	// Subject identifies the principal stably and is never empty: an OIDC
	// sub, an API key's subject, StaticTokenSubject.
	Subject string
	// Name is how the principal is displayed: an e-mail address, a key's label.
	Name string
	// Scopes are the scopes the credential grants (RFC 6749 § 3.3).
	Scopes []string
	// Expires is when the credential stops being accepted; zero is never.
	Expires time.Time
	// Source names the kind of authenticator that accepted the credential and
	// holds no colon, so that ID reads one way.
	Source string
}

Principal is who an accepted credential speaks for.

func PrincipalFrom added in v0.13.0

func PrincipalFrom(ctx context.Context) (Principal, bool)

PrincipalFrom is the principal the request of ctx was accepted for, to key what its caller owns by ID; there is none on a transport that authenticates nobody.

func (Principal) ID added in v0.13.0

func (p Principal) ID() string

ID identifies the principal across authenticators as "<source>:<subject>", a subject being unique only within its source; a service keys what a caller owns by it.

type Refusal

type Refusal int

Refusal is why an Authorization header was not accepted, or Accepted.

const (
	// Accepted is a header carrying a credential the server accepts.
	Accepted Refusal = iota
	// NothingPresented is a request with no Authorization header; from one
	// Authenticator of a Chain, also a credential of a kind it does not read.
	NothingPresented
	// NotBearer is an Authorization header of another scheme.
	NotBearer
	// Malformed is a Bearer header whose credentials are not a b64token.
	Malformed
	// Mismatch is a well-formed bearer token that is not this server's.
	Mismatch
	// Expired is a bearer token of this server's whose lifetime has ended.
	Expired
	// InsufficientScope is a valid bearer token without a scope the server requires.
	InsufficientScope
	// Unavailable is a bearer token the server cannot verify for now, what issued
	// it being out of reach; RFC 6750 § 3.1 has no error code for it.
	Unavailable
)

func Bearer added in v0.13.0

func Bearer(authorization string) (string, Refusal)

Bearer reads the bearer token of an Authorization header (RFC 6750 § 2.1): the b64token with Accepted, or why the header carries none.

func (Refusal) Challenge

func (r Refusal) Challenge(realm string) string

Challenge is the WWW-Authenticate value for a refusal under a realm.

func (Refusal) ChallengeParams

func (r Refusal) ChallengeParams() []string

ChallengeParams are the error parameters of a WWW-Authenticate challenge, none for a refusal with no error code.

func (Refusal) Code

func (r Refusal) Code() string

Code is the RFC 6750 § 3.1 error code, empty when nothing was presented and for Unavailable, which is not the client's error.

func (Refusal) Description

func (r Refusal) Description() string

Description says what was wrong, empty when nothing was presented.

func (Refusal) HTTPStatus

func (r Refusal) HTTPStatus() int

HTTPStatus is the status a refusal is answered with (RFC 6750 § 3.1): 400 for an unreadable header, 403 for a missing scope, 503 for Unavailable, 401 otherwise.

func (Refusal) Message

func (r Refusal) Message() string

Message is the refusal's text for the client.

func (Refusal) String

func (r Refusal) String() string

String names the refusal for a log line; it never carries what was presented.

type ResourceMetadata added in v0.13.0

type ResourceMetadata struct {
	// AuthorizationServers are the issuers of its tokens (authorization_servers).
	AuthorizationServers []string
	// ScopesSupported are the scopes a client may request (scopes_supported).
	ScopesSupported []string
}

ResourceMetadata is what an authenticator has the resource advertise in its protected resource metadata (RFC 9728 § 2).

type StaticToken added in v0.13.0

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

StaticToken authenticates the one token a service was given out of band (RFC 6750); it reads every bearer token, so it ends the Chain it is in.

func NewStaticToken added in v0.13.0

func NewStaticToken(token Token, scopes ...string) StaticToken

NewStaticToken is the authenticator of token, whose principal holds scopes: every scope the service defines, the token being its operator's.

func (StaticToken) Metadata added in v0.13.0

func (StaticToken) Metadata() ResourceMetadata

Metadata is empty: no authorization server issues a static token, and no scope is requested for one.

func (StaticToken) Verify added in v0.13.0

func (s StaticToken) Verify(_ context.Context, authorization string, _ *http.Request) (Principal, Refusal)

Verify accepts the token as StaticTokenSubject with no expiry and refuses anything else as Token.Check does; an unset token accepts nothing.

type Token

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

Token is the one access token a service accepts (RFC 6750), issued out of band by whoever runs it; the zero value is no token.

func New

func New(s string) (Token, error)

New reads a token as RFC 6750 § 2.1 spells one (b64token), so a client can send what the server was given; anything else is refused.

func Read

func Read(tokenFile, token, env string) (Token, string, error)

Read reads the token -token-file (one trailing line break dropped), -token or the environment variable env names, in that precedence; both flags set is refused, none and an empty variable is no token. The text is returned beside the token for a command that prints its configuration.

func (Token) Check

func (t Token) Check(authorization string) Refusal

Check classifies the Authorization header of a request (RFC 6750 § 2.1).

func (Token) Matches

func (t Token) Matches(presented string) bool

Matches compares a presented token in constant time.

func (Token) Set

func (t Token) Set() bool

Set reports whether a token is configured.

Directories

Path Synopsis
Package apikey authenticates the access keys a service hands people and CI: bearer tokens osk_<id>.<secret> (RFC 6750), checked against a file of hashed records.
Package apikey authenticates the access keys a service hands people and CI: bearer tokens osk_<id>.<secret> (RFC 6750), checked against a file of hashed records.
Package flags declares the command-line flags that configure a service's authenticators once, so every command reads the same ones and resolves them alike.
Package flags declares the command-line flags that configure a service's authenticators once, so every command reads the same ones and resolves them alike.
Package oidc authenticates the access tokens an OpenID provider issues (RFC 9068, OpenID Connect Discovery 1.0) as one member of a guard.Chain.
Package oidc authenticates the access tokens an OpenID provider issues (RFC 9068, OpenID Connect Discovery 1.0) as one member of a guard.Chain.

Jump to

Keyboard shortcuts

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