middleware

package
v2.40.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2023 License: Apache-2.0 Imports: 6 Imported by: 3

Documentation

Overview

Copyright 2016 Wenhui Shen <www.webx.top>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	AlgorithmHS256 = "HS256"
)

Algorithims

Variables

View Source
var (
	// DefaultJWTConfig is the default JWT auth middleware config.
	DefaultJWTConfig = JWTConfig{
		Skipper:       echo.DefaultSkipper,
		SigningMethod: AlgorithmHS256,
		ContextKey:    "jwtUser",
		TokenLookup:   "header:" + echo.HeaderAuthorization,
		Claims:        jwt.MapClaims{},
		OnErrorAbort:  true,
		// contains filtered or unexported fields
	}
)
View Source
var (
	ErrJWTMissing = echo.NewHTTPError(http.StatusBadRequest, "missing or malformed jwt")
)

Errors

Functions

func BuildMapSignedString added in v1.6.0

func BuildMapSignedString(claims jwt.MapClaims, mySigningKey interface{}) (string, error)

BuildMapSignedString example: github.com/golang-jwt/jwt/example_test.go

func BuildSignedString added in v1.6.0

func BuildSignedString(claims jwt.Claims, mySigningKey interface{}) (string, error)

BuildSignedString example: github.com/golang-jwt/jwt/example_test.go

func BuildStandardSignedString

func BuildStandardSignedString(claims *jwt.StandardClaims, mySigningKey interface{}) (string, error)

BuildStandardSignedString example: github.com/golang-jwt/jwt/example_test.go

func JWT

func JWT(key []byte) echo.MiddlewareFuncd

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

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

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

func JWTWithConfig

func JWTWithConfig(config JWTConfig) echo.MiddlewareFuncd

JWTWithConfig returns a JWT auth middleware with config. See: `JWT()`.

Types

type JWTConfig

type JWTConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper echo.Skipper `json:"-"`

	// Signing key to validate token.
	// Required.
	SigningKey interface{} `json:"signing_key"`

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

	// Context key to store user information from the token into context.
	// Optional. Default value "user".
	ContextKey string `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>"
	TokenLookup string `json:"token_lookup"`

	OnErrorAbort bool `json:"on_error_abort"`
	// contains filtered or unexported fields
}

JWTConfig defines the config for JWT middleware.

func (*JWTConfig) ErrorHandler added in v1.6.0

func (j *JWTConfig) ErrorHandler() func(c echo.Context, err error)

func (*JWTConfig) FallbackExtractor added in v1.6.0

func (j *JWTConfig) FallbackExtractor() func(c echo.Context) (string, error)

func (*JWTConfig) SetErrorHandler added in v1.6.0

func (j *JWTConfig) SetErrorHandler(errorHandler func(c echo.Context, err error)) *JWTConfig

func (*JWTConfig) SetFallbackExtractor added in v1.6.0

func (j *JWTConfig) SetFallbackExtractor(extractor func(c echo.Context) (string, error)) *JWTConfig

func (*JWTConfig) SetTokenPreprocessor added in v1.6.0

func (j *JWTConfig) SetTokenPreprocessor(tokenPreprocessor func(c echo.Context, token string) (string, error)) *JWTConfig

Jump to

Keyboard shortcuts

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