httpmiddlewares

package
v0.0.0-...-96aafe2 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingAuthHeader = errors.New("missing authorization header")
	ErrInvalidAuthHeader = errors.New("authorization header is malformed")
)

Functions

func AuthenticatedUserFromContext

func AuthenticatedUserFromContext(ctx context.Context) (u *auth.User, ok bool)

AuthenticatedUserFromContext attempts to get the user from the given context.

func AuthenticatedUserToContext

func AuthenticatedUserToContext(ctx context.Context, u *auth.User) context.Context

AuthenticatedUserToContext creates a new context with the user added to it.

func NewBearerTokenAuthenticationMiddleware

func NewBearerTokenAuthenticationMiddleware(authenticator BearerTokenAuthenticator,
	authCtxKey any, optionalAuthCtxKey any,
	errorFn func(context.Context, int, http.ResponseWriter, error)) func(http.Handler) http.Handler

NewBearerTokenAuthenticationMiddleware returns a middleware that can be used to authenticate requests. It detects if a route requires authentication by checking if a field (authCtxKey) is set in the request context. If the authCtxKey field is set and the Authorization header is present, the middleware authenticates the user and sets the authenticated user in the context. If both authCtxKey and optionalAuthCtxKey fields are set and the Authorization header is not present, it allows the request to proceed without authentication.

The errorFn parameter allows the caller to customize the error response returned when authentication fails. This makes the middleware more generic and adaptable to different error handling requirements.

It is the responsibility of the caller of this middleware to ensure that the `authCtxKey` is set in the request context whenever authentication is needed. This can be done using a wrapper middleware that knows about the OpenAPI generator's security semantics.

See https://github.com/oapi-codegen/oapi-codegen/issues/518 for details on the lack of per-endpoint middleware support.

Types

type BearerTokenAuthenticator

type BearerTokenAuthenticator interface {
	Authenticate(ctx context.Context, token string) (*auth.User, error)
}

Jump to

Keyboard shortcuts

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