auth

package
v0.0.0-...-b125894 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package auth provides framework-independent authentication and authorization.

Index

Constants

This section is empty.

Variables

View Source
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

func MatchesAnyPath(patterns []string, path string) bool

MatchesAnyPath reports whether a path matches one of the configured patterns.

func MatchesPath

func MatchesPath(permission, path string) bool

MatchesPath matches complete path segments, including *, **, and :parameter.

func ParseBearerToken

func ParseBearerToken(header string) (string, error)

ParseBearerToken extracts a token from an HTTP Authorization header.

func RequireAdministrator

func RequireAdministrator(principal Principal) error

RequireAdministrator checks whether a principal is an administrator.

func WithPrincipal

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

WithPrincipal returns a context carrying an authenticated principal snapshot.

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.

func PrincipalFromContext

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

PrincipalFromContext returns the authenticated principal, if present.

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

func (s *Service) Authenticate(token string) (Principal, bool)

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

func (s *Service) AuthenticateBearer(header string) (Principal, error)

AuthenticateBearer authenticates an HTTP Authorization header.

func (*Service) Authorize

func (s *Service) Authorize(principal Principal, method, path string) error

Authorize checks whether a principal may access an HTTP method and path.

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.

type UserConfig

type UserConfig struct {
	ID          string
	BearerToken string
	Permissions []string
	IsAdmin     bool
}

UserConfig defines one statically configured bearer-token identity.

Jump to

Keyboard shortcuts

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