Documentation
¶
Overview ¶
Package auth provides framework-independent authentication and authorization.
Index ¶
- Variables
- func MatchesAnyPath(patterns []string, path string) bool
- func MatchesPath(permission, path string) bool
- func ParseBearerToken(header string) (string, error)
- func RequireAdministrator(principal Principal) error
- func WithPrincipal(ctx context.Context, principal Principal) context.Context
- type Permission
- type Principal
- type Service
- type TokenAuthenticator
- type UserConfig
Constants ¶
This section is empty.
Variables ¶
var ( // ErrMissingBearerToken indicates that a request has no usable bearer token. ErrMissingBearerToken = errors.New("missing bearer token") // ErrInvalidBearerToken indicates that a bearer token is not configured. ErrInvalidBearerToken = errors.New("invalid bearer token") // ErrPermissionDenied indicates that a principal cannot access a resource. ErrPermissionDenied = errors.New("permission denied") // ErrAdministratorRequired indicates that a resource is restricted to administrators. ErrAdministratorRequired = errors.New("administrator permission required") )
Functions ¶
func MatchesAnyPath ¶
MatchesAnyPath reports whether a path matches one of the configured patterns.
func MatchesPath ¶
MatchesPath matches complete path segments, including *, **, and :parameter.
func ParseBearerToken ¶
ParseBearerToken extracts a token from an HTTP Authorization header.
func RequireAdministrator ¶
RequireAdministrator checks whether a principal is an administrator.
Types ¶
type Permission ¶
type Permission string
Permission grants access to an HTTP method and path pattern.
type Principal ¶
type Principal struct {
ID string
Permissions []Permission
IsAdmin bool
}
Principal is the authenticated request identity.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service authenticates immutable, statically configured identities.
func NewService ¶
func NewService(users []UserConfig) *Service
NewService constructs an authentication service from static user configuration.
func (*Service) Authenticate ¶
Authenticate returns the principal associated with a raw bearer token. The token digest is compared against every configured digest in constant time, mirroring TokenAuthenticator, so authentication latency does not depend on the token contents.
func (*Service) AuthenticateBearer ¶
AuthenticateBearer authenticates an HTTP Authorization header.
type TokenAuthenticator ¶
type TokenAuthenticator struct {
// contains filtered or unexported fields
}
TokenAuthenticator authenticates bearer tokens without creating identities.
func NewTokenAuthenticator ¶
func NewTokenAuthenticator(tokens []string) *TokenAuthenticator
NewTokenAuthenticator snapshots the accepted bearer tokens.
func (*TokenAuthenticator) AuthenticateBearer ¶
func (a *TokenAuthenticator) AuthenticateBearer(header string) error
AuthenticateBearer validates an HTTP Authorization header.