middlewares

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnauthorized     = errors.New("invalid token format, expected 'Bearer <token>'")
	ErrIdentityNotFound = errors.New("identity not found")
)

Functions

func Auth

func Auth(authenticator auth.Authenticator) func(http.Handler) http.Handler

Auth adds the identity to the context retrieving it from the authenticator

func AuthzFromBody

func AuthzFromBody[T ObjectScopeProvider](
	object auth.ObjectType,
	action auth.Action,
	authorizer auth.Authorizer,
) func(http.Handler) http.Handler

AuthzFromBody middleware authorizes using the decoded body through the extractor pattern T must implement AuthTargetScopeProvider to provide its own target scope

func AuthzFromExtractor

func AuthzFromExtractor(
	object auth.ObjectType,
	action auth.Action,
	authorizer auth.Authorizer,
	extractor ObjectScopeExtractor,
) func(http.Handler) http.Handler

AuthzFromExtractor is the base authorization middleware that uses a scope extractor function to get the authorization target scope from the request

func AuthzFromID

func AuthzFromID(
	object auth.ObjectType,
	action auth.Action,
	authorizer auth.Authorizer,
	loader ObjectScopeLoader,
) func(http.Handler) http.Handler

AuthzFromID authorizes using a resource ID through the extractor pattern

func AuthzSimple

func AuthzSimple(
	object auth.ObjectType,
	action auth.Action,
	authorizer auth.Authorizer,
) func(http.Handler) http.Handler

AuthzSimple authorizes without resource-specific scope through the extractor pattern

func DecodeBody

func DecodeBody[T any]() func(http.Handler) http.Handler

DecodeBody is middleware that decodes the request body into a struct and stores it in the request context for later middlewares and handlers

func ID

func ID(next http.Handler) http.Handler

ID extracts and validates the UUID from URL paths with /{id} format

func MustGetBody

func MustGetBody[T any](ctx context.Context) T

MustGetBody retrieves and casts the decoded body to a specific type

func MustGetID

func MustGetID(ctx context.Context) properties.UUID

MustGetID retrieves the UUID from the request context

func MustHaveRoles

func MustHaveRoles(roles ...auth.Role) func(http.Handler) http.Handler

MustHaveRoles creates a middleware that ensures the authenticated user has at least one of the required roles

Types

type ObjectScopeExtractor

type ObjectScopeExtractor func(r *http.Request) (auth.ObjectScope, error)

ObjectScopeExtractor defines a function type that extracts the auth target scope from a request

func BodyScopeExtractor

func BodyScopeExtractor[T ObjectScopeProvider]() ObjectScopeExtractor

BodyScopeExtractor creates an extractor that gets scope from the request body

func IDScopeExtractor

func IDScopeExtractor(loader ObjectScopeLoader) ObjectScopeExtractor

IDScopeExtractor creates an extractor that gets scope from a resource ID using a retriever

func SimpleScopeExtractor

func SimpleScopeExtractor() ObjectScopeExtractor

SimpleScopeExtractor creates an extractor that always returns empty scope

type ObjectScopeLoader

type ObjectScopeLoader func(ctx context.Context, id properties.UUID) (auth.ObjectScope, error)

ObjectScopeLoader defines a function type that retrieves the authorization scope for a resource ID

type ObjectScopeProvider

type ObjectScopeProvider interface {
	ObjectScope() (auth.ObjectScope, error)
}

ObjectScopeProvider defines an interface for types that can provide their own auth target scope

Jump to

Keyboard shortcuts

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