jwt

package
v0.0.0-...-1b77c7e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Index

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

func FromHeader(r *http.Request) (string, error)

FromHeader is a "TokenExtractor" that takes a give request and extracts the JWT token from the Authorization header.

func OnError

func OnError(w http.ResponseWriter, r *http.Request, err error)

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 New

func New(config Config) *JWT

func (*JWT) HandleJWT

func (j *JWT) HandleJWT(w http.ResponseWriter, r *http.Request) error

func (*JWT) Handler

func (j *JWT) Handler(h http.Handler) http.Handler

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

type TokenExtractor func(r *http.Request) (string, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL