Documentation
¶
Overview ¶
Package web holds the HTTP helpers shared by the servers: JSON in and out, CORS, bearer tokens.
Index ¶
- func BearerToken(r *http.Request) (string, bool)
- func CORS(h http.Handler, methods string) http.Handler
- func DecodeJSON(w http.ResponseWriter, r *http.Request, v any) bool
- func HashToken(token string) string
- func NewToken(prefix string) (token, hash string)
- func RandomString(n int) string
- func ServerError(w http.ResponseWriter, err error)
- func WriteError(w http.ResponseWriter, status int, msg string)
- func WriteJSON(w http.ResponseWriter, status int, v any)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BearerToken ¶
BearerToken returns the token from an "Authorization: Bearer" header.
func CORS ¶
CORS wraps h with permissive CORS headers and answers preflight requests. This is safe because authentication is always explicit (bearer tokens or HTTP Basic), never cookies, and Access-Control-Allow-Credentials is never sent.
func DecodeJSON ¶
DecodeJSON decodes a JSON request body of at most 1 MiB, rejecting unknown fields. On failure it writes a 400 and returns false.
func NewToken ¶
NewToken returns a new random token with the given prefix (which tells tokens of different services apart) and the hash to store.
func RandomString ¶
RandomString returns n random bytes, base64url-encoded.
func ServerError ¶
func ServerError(w http.ResponseWriter, err error)
ServerError logs err and writes a generic 500.
func WriteError ¶
func WriteError(w http.ResponseWriter, status int, msg string)
Types ¶
This section is empty.