Documentation
¶
Overview ¶
Package helper provides the request, header and HTTP error helpers of the relay proxy.
Index ¶
- Constants
- Variables
- func APIKey(r *http.Request) string
- func APIKeyFromHeaders(header func(string) string) string
- func AuthenticateFlagSet(ctx context.Context, flagsetManager service.FlagSetResolver, apiKey string) (context.Context, bool)
- func AuthenticateFlagSetName(ctx context.Context, flagsetManager service.FlagSetResolver, apiKey string) (context.Context, bool)
- func BindBody(r *http.Request, target any) error
- func BindRawBody(contentType string, raw []byte, target any) error
- func DecodeJSON(data []byte, target any) error
- func Handle(h func(http.ResponseWriter, *http.Request) error, exposeError bool) http.Handler
- func IsJSON(contentType string) bool
- func RequestFlagSetName(ctx context.Context, flagsetManager service.FlagSetResolver, apiKey string) (string, error)
- func WriteError(w http.ResponseWriter, r *http.Request, err error, exposeError bool)
- func WriteJSON(w http.ResponseWriter, code int, v any) error
- type ErrorBody
- type HTTPError
- func FlagSet(flagsetManager service.FlagSetResolver, apiKey string) (service.FlagSet, *HTTPError)
- func NewHTTPError(code int, message string) *HTTPError
- func RequestFlagSet(ctx context.Context, flagsetManager service.FlagSetResolver, apiKey string) (service.FlagSet, *HTTPError)
- func WrapHTTPError(code int, err error) *HTTPError
- type StatusError
Constants ¶
const XAPIKeyHeader = "X-Api-Key" //nolint:gosec // G101: the name of a header, not a credential
XAPIKeyHeader defines the header name for passing API keys. This is not a credential itself, but a standard HTTP header key.
Variables ¶
var ( ErrNotFound = StatusError(http.StatusNotFound) )
Predefined errors of the statuses used without a message of their own. Their message is the text of the status.
Functions ¶
func APIKey ¶
APIKey extracts the API key from the request headers. It checks headers in the following order of precedence: 1. X-API-Key header (raw value) 2. Authorization header (with "Bearer " prefix removed if present) Returns an empty string if no API key is found.
func APIKeyFromHeaders ¶ added in v0.2.0
APIKeyFromHeaders is APIKey for a source that reads a header by its name.
func AuthenticateFlagSet ¶
func AuthenticateFlagSet( ctx context.Context, flagsetManager service.FlagSetResolver, apiKey string, ) (context.Context, bool)
AuthenticateFlagSet resolves the flag set of the API key of the request. It returns the context to give to the handler, which carries the flag set, and false if the key does not route to a flag set. The accepted keys and the routing of the flag sets are one table: authentication and handler use the flag set resolved here, so a reload that swaps the routing in between can not make an authenticated request fail.
func AuthenticateFlagSetName ¶
func AuthenticateFlagSetName( ctx context.Context, flagsetManager service.FlagSetResolver, apiKey string, ) (context.Context, bool)
AuthenticateFlagSetName is AuthenticateFlagSet for the endpoints that only need the name of the flag set.
func BindBody ¶ added in v0.2.0
BindBody decodes the JSON body of the request in target. An empty body leaves target untouched, another content type than JSON is refused with 415, and a body that can not be read or decoded with 400.
func BindRawBody ¶ added in v0.2.0
BindRawBody is BindBody for a body that was read already, and checked against its content type.
func DecodeJSON ¶ added in v0.2.0
DecodeJSON decodes a JSON document in target. A failure is a 400.
func Handle ¶ added in v0.2.0
Handle makes a handler that returns the error to send a http.Handler: the error is sent as the response, and kept for the access log.
func IsJSON ¶ added in v0.2.0
IsJSON reports whether the content type is application/json, whatever its parameters.
func RequestFlagSetName ¶
func RequestFlagSetName(ctx context.Context, flagsetManager service.FlagSetResolver, apiKey string) (string, error)
RequestFlagSetName returns the name of the flag set of the request, like RequestFlagSet. The name is added to the logs of the request when it is looked up here: the authentication did it when it resolved the name.
func WriteError ¶ added in v0.2.0
WriteError sends the error as the response. It sends nothing for a HEAD request but the status.
Types ¶
type ErrorBody ¶ added in v0.2.0
type ErrorBody struct {
Message string `example:"Not Found" json:"message"`
Error string `example:"the wrapped error, only at the debug level" json:"error,omitempty"`
}
ErrorBody is the JSON body sent for an error.
func ErrorResponse ¶ added in v0.2.0
ErrorResponse returns the status code and the body to send to the client for err: the status of the error when it has one (500 otherwise) and its message. exposeError adds the wrapped error to the body.
type HTTPError ¶ added in v0.2.0
HTTPError is an error that carries the status code of the response and the message sent to the client. The body sent for it is {"message": ...}, plus {"error": ...} when the relay proxy runs with the debug level.
func FlagSet ¶
FlagSet retrieves the flagset for the given API key from the resolver This layer ensure that the resolver is initialized and that the API key is valid.
func NewHTTPError ¶ added in v0.2.0
NewHTTPError returns an HTTPError with the given status code and message.
func RequestFlagSet ¶
func RequestFlagSet( ctx context.Context, flagsetManager service.FlagSetResolver, apiKey string, ) (service.FlagSet, *HTTPError)
RequestFlagSet returns the flag set of the request: the one resolved by the authentication when there is one, or else the one of the API key of the request (authentication disabled, default mode).
func WrapHTTPError ¶ added in v0.2.0
WrapHTTPError returns an HTTPError with the status code, the text of the status as message and err as the wrapped error (exposed in the body at the debug level only).
func (*HTTPError) ContentType ¶ added in v0.2.0
ContentType keeps the content type of the negotiation (huma defaults an error to application/problem+json).
func (*HTTPError) StatusCode ¶ added in v0.2.0
StatusCode returns the status code of the response.
type StatusError ¶ added in v0.2.0
type StatusError int
StatusError is the error of a status that has no HTTPError: the body is the text of the status.
func (StatusError) Error ¶ added in v0.2.0
func (s StatusError) Error() string
Error returns the text of the status.
func (StatusError) StatusCode ¶ added in v0.2.0
func (s StatusError) StatusCode() int
StatusCode returns the status code.