jwt

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2021 License: MIT Imports: 7 Imported by: 0

README

Gin JWT Auth Middleware

Documentation

Index

Constants

View Source
const AlgorithmHS256 = "HS256"

Variables

View Source
var (
	ErrJWTMissing = errors.New("missing or malformed jwt")
	ErrJWTInvalid = errors.New("invalid or expired jwt")
)
View Source
var (
	DefaultAuthConfig = AuthConfig{
		SigningMethod: AlgorithmHS256,
		ContextKey:    "user",
		TokenLookup:   "header:Authorization",
		AuthScheme:    "Bearer",
		Claims:        jwt.MapClaims{},
	}
)

Functions

func Auth

func Auth(key string) gin.HandlerFunc

Auth returns a JSON Web Token (Auth) auth middleware.

For valid token, it sets the user in context and calls next handler. For invalid token, it returns "401 - Unauthorized" error. For missing token, it returns "400 - Bad Request" error.

See: https://jwt.io/introduction See `AuthConfig.TokenLookup`

func AuthWithConfig

func AuthWithConfig(cfg AuthConfig) gin.HandlerFunc

Types

type AuthConfig

type AuthConfig struct {
	// SuccessHandler defines a function which is executed for a valid token.
	SuccessHandler SuccessHandler `yaml:"success_handler" json:"success_handler"`

	// ErrorHandler defines a function which is executed for an invalid token.
	// It may be used to define a custom Auth error.
	ErrorHandler ErrorHandler `yaml:"error_handler" json:"error_handler"`

	// ErrorHandlerWithContext is almost identical to ErrorHandler, but it's passed the current context.
	ErrorHandlerWithContext ErrorHandlerWithContext `yaml:"error_handler_with_context" json:"error_handler_with_context"`

	// Signing key to validate token. Used as fallback if SigningKeys has length 0.
	// Required. This or SigningKeys.
	SigningKey string `yaml:"signing_key" json:"signing_key"`

	// Map of signing keys to validate token with kid field usage.
	// Required. This or SigningKey.
	SigningKeys map[string]string `yaml:"signing_keys" json:"signing_keys"`

	// Signing method, used to check token signing method.
	// Optional. Default value HS256.
	SigningMethod string `default:"HS256" yaml:"signing_method" json:"signing_method"`

	// Context key to store user information from the token into context.
	// Optional. Default value "user".
	ContextKey string `default:"user" yaml:"context_key" json:"context_key"`

	// Claims are extendable claims data defining token content.
	// Optional. Default value jwt.MapClaims
	Claims jwt.Claims

	// TokenLookup is a string in the form of "<source>:<name>" that is used
	// to extract token from the request.
	// Optional. Default value "header:Authorization".
	// Possible values:
	// - "header:<name>"
	// - "query:<name>"
	// - "param:<name>"
	// - "cookie:<name>"
	// - "form:<name>"
	TokenLookup string `default:"header:Authorization" yaml:"token_lookup" json:"token_lookup"`

	// AuthScheme to be used in the Authorization header.
	// Optional. Default value "Bearer".
	AuthScheme string `default:"Bearer" yaml:"auth_scheme" json:"auth_scheme"`
	// contains filtered or unexported fields
}

type ErrorHandler

type ErrorHandler func(error) error

ErrorHandler defines a function which is executed for an invalid token.

type ErrorHandlerWithContext

type ErrorHandlerWithContext func(error, *gin.Context) error

ErrorHandlerWithContext is almost identical to ErrorHandler, but it's passed the current context.

type SuccessHandler

type SuccessHandler func(*gin.Context)

SuccessHandler defines a function which is executed for a valid token.

Jump to

Keyboard shortcuts

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