auth

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 22 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

Index

Constants

View Source
const (
	// MethodOIDC identifies a principal established from a JWT issued by an OIDC provider
	// or OAuth2 authorization server.
	MethodOIDC = "oidc"
	// MethodSigV1 identifies a principal established from a Bluelink Signature V1 header.
	MethodSigV1 = "sigv1"
	// MethodAPIKey identifies a principal established from a configured API key.
	MethodAPIKey = "apikey"
)

Authentication methods a principal can be established by.

View Source
const (
	// BluelinkAPIKeyHeaderName is the name of the header
	// used to pass the API key for authentication.
	BluelinkAPIKeyHeaderName = "Bluelink-Api-Key"
)

Variables

This section is empty.

Functions

func ContextWithPrincipal added in v0.9.0

func ContextWithPrincipal(ctx context.Context, principal *Principal) context.Context

ContextWithPrincipal returns a context carrying the principal a request authenticated as.

Types

type Checker

type Checker interface {
	// Check carries out an authentication check
	// using the provided request headers.
	// It returns an error of the `auth.Error` type
	// if the check fails, or the principal the request
	// authenticated as if it succeeds.
	// Other error types should be considered
	// as internal server errors.
	//
	// The principal is the only record of who acted as the credential it was
	// established from is discarded once the check has passed.
	Check(ctx context.Context, headers http.Header) (*Principal, error)
}

Checker provides an interface for carrying out an authentication check given a set of request headers.

func LoadJWTService

func LoadJWTService(config *core.AuthConfig) (Checker, error)

LoadJWTService loads a new instance of an auth.Checker that validates JSON Web Tokens (JWTs). This will use configuration from the provided auth.Config to wire up an appropriate JWT validator. This will make requests to the issuer URL as a part of the load process to determine how the JWKS URI should be configured.

func NewAPIKeyService

func NewAPIKeyService(config *core.AuthConfig) Checker

NewAPIKeyService creates a new service that checks if the provided API key is valid against a list of known API keys.

func NewMultiAuthChecker

func NewMultiAuthChecker(checkers ...Checker) Checker

NewMultiAuthChecker creates a new instance of a Checker that combines multiple authenication methods where they are applied in the order that they are configured. As soon as a checker succeeds (returns nil instead of an error), any subsequent checkers are skipped. If all checkers fail, the last error is returned.

func NewSigV1Service

func NewSigV1Service(
	keyPairs map[string]string,
	clock core.Clock,
	options *sigv1.VerifyOptions,
) Checker

NewSigV1Service creates a new instance of the Bluelink Signature V1 service. It takes a map of key pairs (key ID -> secret key), a clock for time verification, and options for customising configuration such as clock skew. Options can be nil, in which case default options will be used.

type Error

type Error struct {
	ChildErr error
}

Error is a type that represents an error that occurs during authentication. It is used to distinguish between authentication errors and other types of errors such as network or implementation errors.

func (*Error) Error

func (e *Error) Error() string

type JWKSProvider

type JWKSProvider interface {
	KeyFunc(ctx context.Context) (any, error)
}

JWKSProvider provides an interface for a provider that can provide a JSON Web Key Set (JWKS) for validating JSON Web Tokens (JWTs).

type JWTValidator

type JWTValidator interface {
	ValidateToken(ctx context.Context, tokenString string) (interface{}, error)
}

JWTValidator provides an interface for a token validator that is compatible with the auth0 JWT middleware.

type Middleware

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

func NewMiddleware

func NewMiddleware(
	checker Checker,
	excludeRoutes []*mux.Route,
	logger core.Logger,
) (*Middleware, error)

NewMiddleware creates a new middleware carries out authentication checks using the provided Checker that uses HTTP headers as the source of authentication data.

func (*Middleware) Middleware

func (m *Middleware) Middleware(next http.Handler) http.Handler

type Principal added in v0.9.0

type Principal struct {
	// Method is the authentication method the principal was established by.
	Method string
	// Subject identifies the principal within that method.
	// This is a JWT's subject claim, a Signature V1 key ID, or an API key's fingerprint.
	//
	// Never the credential itself. An API key's subject is a digest prefix, so that a
	// record naming the key cannot be replayed as the key.
	Subject string
	// Name is a friendly name for the principal where the method carries one.
	Name string
	// Claims holds the raw claims of a JWT, for an audit sink to record.
	Claims map[string]any
	// OnBehalfOf is an end-user identity asserted by a trusted fronting service.
	OnBehalfOf string
}

Principal identifies who made a request.

Attribution is the engine's to establish even though authorization is not, the engine is the only thing that sees the credential, and it is discarded once the check has passed, so nothing above the engine could work out who acted.

func PrincipalFromContext added in v0.9.0

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

PrincipalFromContext returns the principal a request authenticated as, and whether one was established.

Absent for a route that authentication is excluded from, so a caller has to handle a request that has no principal rather than assuming one.

Jump to

Keyboard shortcuts

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