Documentation ¶
Index ¶
- Constants
- Variables
- func ContextValueSetClaims(token *jwt.Token) (interface{}, error)
- func ContextValueSetToken(token *jwt.Token) (interface{}, error)
- func FromHeader(r *http.Request) (string, error)
- func OnError(w http.ResponseWriter, r *http.Request, err error)
- type Config
- type ContextValueFunc
- type ErrorHandler
- type JWT
- type PrintLogger
- type TokenExtractor
Constants ¶
View Source
const ( HeaderAuthorization = "Authorization" Bearer = "bearer" )
Variables ¶
View Source
var ( ErrorAuthHeaderFormat = errors.New("authorization header format must be Bearer {token}") ErrorTokenMissing = errors.New("authorization token is required, but not found") ErrorExtractingToken = errors.New("error extracting token") ErrorParsingToken = errors.New("error parsing jwt token") ErrorInvalidToken = errors.New("token is invalid") )
Functions ¶
func ContextValueSetClaims ¶
func ContextValueSetClaims(token *jwt.Token) (interface{}, error)
func ContextValueSetToken ¶
func ContextValueSetToken(token *jwt.Token) (interface{}, error)
func FromHeader ¶
FromHeader is a "TokenExtractor" that takes a give request and extracts the JWT token from the Authorization header.
Types ¶
type Config ¶
type Config struct { // Signing key to validate token. // Required if ValidationKeyGetter is nil. SigningKey interface{} // The function that will return the Key to validate the JWT. // It can be either a shared secret or a public key. // Required if SigningKey is nil. // First use this. ValidationKeyGetter jwt.Keyfunc // Signing method, used to verify tokens are signed with the specific signing algorithm. // Optional. Default: jwt.SigningMethodHS256. SigningMethod jwt.SigningMethod // The key name in the context where the user information // from the JWT will be stored. // Optional. Default: "user" ContextKey string // Claims are extendable claims data defining token content. // Optional. Default: jwt.MapClaims Claims jwt.Claims // Debug flag turns on debugging output // Optional. Default: false Debug bool // When set, all requests with the OPTIONS method will use authentication // Optional. Default: false EnableAuthOnOptions bool // A boolean indicating if the credentials are optional or not // Optional. Default: false CredentialsOptional bool // A function that extracts the token from the request // Optional. Default: FromAuthHeader (i.e., from Authorization header as bearer token) Extractor TokenExtractor // The function that will be called when there's an error validating the token // Optional. Default: OnError ErrorHandler ErrorHandler // The function that will be called before set value into context. // Used to customize the value will be stored into context. // Optional. Default: ContextValueSetClaims ContextValueFunc ContextValueFunc }
type ContextValueFunc ¶
type ContextValueFunc func(token *jwt.Token) (interface{}, error)
A function called before set value into context
type ErrorHandler ¶
type ErrorHandler func(w http.ResponseWriter, r *http.Request, err error)
A function called when an error is encountered
type JWT ¶
type JWT struct {
// contains filtered or unexported fields
}
func (*JWT) HandlerWithNext ¶
func (j *JWT) HandlerWithNext(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)
func (*JWT) SetLogger ¶
func (j *JWT) SetLogger(l PrintLogger)
type PrintLogger ¶
type PrintLogger interface {
Println(v ...interface{})
}
type TokenExtractor ¶
A function used to extract jwt token raw string from request.
func FromCookie ¶
func FromCookie(key string) TokenExtractor
FromCookie returns a function that extracts the token from the first cookie matched a special key.
func FromFirst ¶
func FromFirst(extractors ...TokenExtractor) TokenExtractor
FromFirst returns a function that runs multiple token extractors and takes the first token it finds.
func FromQuery ¶
func FromQuery(key string) TokenExtractor
FromQuery returns a function that extracts the token from the specified query string parameter.
Click to show internal directories.
Click to hide internal directories.