Token is a simple middleware that reads a token from the basic auth header. It assumes that the token is stored in the user name field.
The key function to the tokens package is ValidateTokens()
. ValidateTokens()
takes anything that implements TokenValidator
.
var (
validator = ValidatorFunc(func(token string) bool {
return token == "SECRET"
})
handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
})
)
// Verifies token == "SECRET" before calling to handler
validatedHandler := ValidateTokens(validator, handler)
log.Fatal(http.ListenAndServe(":8080", validatedHandler))
Use a function as the TokenValidator by casting a function to the appropriate signature.
ValidateTokens(func(token string) bool {
return token == "SECRET"
}, handler)