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

auth

package
v1.96.0 Latest Latest
Warning

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

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

Documentation

Overview

Package auth carries the caller's identity through a request: the key it presented, the backup key every agent of a server shares or the admin key, and the agent id each agent declares.

Index

Constants

View Source
const (

	// AgentHeader carries the agent id the caller declares.
	AgentHeader = "goback-agent"
)

Variables

View Source
var ErrForbidden = errors.New("not allowed for this caller")

ErrForbidden is returned when the principal may not do what it asked.

View Source
var ErrUnauthenticated = errors.New("no caller identity")

ErrUnauthenticated is returned when a request carries no principal.

Functions

func Agent

func Agent(ctx context.Context) (string, bool)

Agent returns the agent id the incoming call declares, if any.

func Bearer

func Bearer(ctx context.Context) (string, bool)

Bearer returns the bearer token of the incoming call, if any.

func Stream

func Stream(authenticate Authenticator) grpc.StreamServerInterceptor

Stream runs authenticate before every streaming call.

func StreamInterceptor

func StreamInterceptor(secret string) grpc.StreamServerInterceptor

StreamInterceptor is UnaryInterceptor for streaming calls.

func Unary

func Unary(authenticate Authenticator) grpc.UnaryServerInterceptor

Unary runs authenticate before every unary call.

func UnaryInterceptor

func UnaryInterceptor(secret string) grpc.UnaryServerInterceptor

UnaryInterceptor rejects calls that do not present secret and attaches the declared agent as the principal of the ones that do.

func WithPrincipal

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

WithPrincipal attaches the principal to the context.

Types

type Authenticator

type Authenticator func(ctx context.Context) (context.Context, error)

Authenticator decides who an incoming call is from and returns the context to handle it with, or a status error refusing it.

func Keys added in v1.96.0

func Keys(backup, admin string) Authenticator

Keys authenticates callers presenting the backup or the admin key as bearer token and attaches the agent they declare as the principal, an Admin one for the admin key. An empty admin key admits no admin.

func LimitFailures added in v1.96.0

func LimitFailures(authenticate Authenticator, limit FailureLimit) Authenticator

LimitFailures refuses the calls of a peer address that has used up its failures with ResourceExhausted, without checking what key they carry, until the limit lets it try again. A failure is a call authenticate answers Unauthenticated.

func SharedSecret

func SharedSecret(secret string) Authenticator

SharedSecret authenticates callers presenting secret as bearer token and attaches the agent they declare as the principal.

type Credentials

type Credentials struct {
	Secret  string
	AgentID string
	// Plaintext allows the secret onto a connection nothing encrypts,
	// where anyone on the path can read it and everything it uploads.
	// It is for a store on the same machine, nothing further.
	Plaintext bool
}

Credentials sends the shared secret and the agent id with every call and refuses to do so over a plaintext connection unless Plaintext says otherwise.

func (Credentials) GetRequestMetadata

func (c Credentials) GetRequestMetadata(context.Context, ...string) (map[string]string, error)

GetRequestMetadata implements credentials.PerRPCCredentials.

func (Credentials) RequireTransportSecurity

func (c Credentials) RequireTransportSecurity() bool

RequireTransportSecurity implements credentials.PerRPCCredentials.

type FailureLimit added in v1.96.0

type FailureLimit struct {
	Burst int
	Every time.Duration
	// Forwarded names the peer by the last x-forwarded-for entry a call
	// carries, which is the one a proxy in front appended, rather than by
	// the proxy's own address.
	Forwarded bool
}

FailureLimit is how often a peer address may fail to authenticate: Burst times at once, then once every Every.

type Principal

type Principal struct {
	AgentID string
	Admin   bool
	// Bounded marks a caller that may only add to what the store keeps:
	// it escrows the store key only where no copy of it is kept, and
	// changes a set's retention only to a policy that keeps at least as
	// much.
	Bounded bool
}

Principal is who is calling: the agent id the caller declared, and whether it presented the admin key.

func FromContext

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

FromContext returns the request's principal, if any.

func Require

func Require(ctx context.Context) (*Principal, error)

Require returns the principal or ErrUnauthenticated.

func (*Principal) AuthorizeCommit

func (p *Principal) AuthorizeCommit(c *proto.Commit) error

AuthorizeCommit checks that a commit names the principal's agent.

Jump to

Keyboard shortcuts

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