auth

package
v0.29.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package auth holds the authentication types and the request-scoped authentication state that both the elephantine root package and the rpc package need.

They live here rather than in the root package because the root package uses the Connect interceptors in rpc, and rpc needs the caller's AuthInfo. The types are declared once here and aliased by both packages, so elephantine.AuthInfo and rpc.AuthInfo are the same type.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAuthorization = errors.New("no authorization provided")

ErrNoAuthorization is used to communicate that authorization was completely missing, rather than being invalid, expired, or malformed.

Functions

func ClientIDFromContext

func ClientIDFromContext(ctx context.Context) string

ClientIDFromContext returns the client id of the caller a request authenticated as, or the empty string for an anonymous or unauthenticated request.

func SetInfo

func SetInfo(ctx context.Context, info *Info) context.Context

SetInfo creates a child context with the given authentication information.

Types

type Info

type Info struct {
	Token  string
	Claims JWTClaims
}

Info is used to add authentication information to a request context. It is exposed as elephantine.AuthInfo and rpc.AuthInfo.

func GetInfo

func GetInfo(ctx context.Context) (*Info, bool)

GetInfo returns the authentication information for the given context.

func (*Info) ClientID

func (i *Info) ClientID() string

ClientID returns the id of the application the token was issued to: the "client_id" claim, or the authorized party ("azp") for a token that carries no client id, which is the shape of a token a user was issued through an application. It is the empty string for an anonymous caller, and is safe to call on a nil Info.

type JWTClaims

type JWTClaims struct {
	jwt.RegisteredClaims

	OriginalSub string `json:"-"`

	Name            string   `json:"sub_name"`
	Email           string   `json:"email"`
	Scope           string   `json:"scope"`
	AuthorizedParty string   `json:"azp"`
	ClientID        string   `json:"client_id"`
	Units           []string `json:"units,omitempty"`
	Org             string   `json:"org"`
}

JWTClaims defines the claims that the elephant services understand.

func (JWTClaims) HasAnyScope

func (c JWTClaims) HasAnyScope(names ...string) bool

HasAnyScope returns true if the Scope claim contains any of the named scopes.

func (JWTClaims) HasScope

func (c JWTClaims) HasScope(name string) bool

HasScope returns true if the Scope claim contains the named scope.

type Parser

type Parser interface {
	// AuthInfoFromHeader extracts the Info from a HTTP Authorization
	// header, then validates the bearer token. Return ErrNoAuthorization
	// if no authorization information was provided.
	AuthInfoFromHeader(authorization string) (*Info, error)
	// AuthInfoFromToken validates a bearer token and returns the Info.
	// Useful when we have already extracted the token from header and/or
	// query parameter.
	AuthInfoFromToken(token string) (*Info, error)
	// ValidateTokenWithClaims validates a bearer token and returns the raw
	// token object. Useful if you need to do custom claims
	// deserialization.
	ValidateTokenWithClaims(token string, claims jwt.Claims) (*jwt.Token, error)
}

Parser validates bearer tokens and turns them into Info.

Jump to

Keyboard shortcuts

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