Documentation
¶
Index ¶
- Variables
- func AuthenticatedUserFromContext(ctx context.Context) (u *auth.User, ok bool)
- func AuthenticatedUserToContext(ctx context.Context, u *auth.User) context.Context
- func NewBearerTokenAuthenticationMiddleware(authenticator BearerTokenAuthenticator, authCtxKey any, optionalAuthCtxKey any, ...) func(http.Handler) http.Handler
- type BearerTokenAuthenticator
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingAuthHeader = errors.New("missing authorization header") ErrInvalidAuthHeader = errors.New("authorization header is malformed") )
Functions ¶
func AuthenticatedUserFromContext ¶
AuthenticatedUserFromContext attempts to get the user from the given context.
func AuthenticatedUserToContext ¶
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.