middleware

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2020 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CORS

func CORS(c *gin.Context)

CORS 全局允许跨域

func NewToken

func NewToken(entity AuthInterface, key interface{}) (string, error)

NewToken 根据传入的结构体(非空结构体)返回一个token

func VerifyAuth

func VerifyAuth(auth *Auth) gin.HandlerFunc

VerifyAuth 验证用户有效性

Types

type Auth

type Auth struct {
	// 在整个gin.Context 上线文中的 Get 操作的key名,可以获得 AuthEntity
	ContextKey string
	// JwtHeaderKey jwt token 在HTTP请求中的header名
	HeaderKey string
	// 默认为 false, 如果为 true , 将验证不通过后也会继续往下执行
	Continue bool
	// jwt 加密 key
	Key interface{}

	AuthEntity AuthInterface
	// contains filtered or unexported fields
}

Auth jwt认证对象

type AuthInterface

type AuthInterface interface {
	database.Table
	GetTopic() interface{}                         // 返回唯一信息
	FindByTopic(topic interface{}) AuthInterface   // 根据唯一信息标识获取数据信息, 比如根据用户id获取用户信息,需要注意传入的数据类型
	GetCheckData() string                          // 获取验证信息, jwt加密时, 改信息会一起进行加密, 解密时会解出来然后调用 Check 验证该信息的正确性, 如果是其他数据类型直接转string,比如是个结构体或者map, 直接转为json string
	Check(ctx *gin.Context, checkData string) bool // 验证信息
	ExpiredAt() int64                              // 返回过期时间,时间戳
}

AuthInterface 参与 jwt 数据表结构体需要实现这些接口

type CSRFToken

type CSRFToken struct {
	Domain     string `mapstructure:"domain"`
	Length     int    `mapstructure:"length"`
	SessionKey string `mapstructure:"session_key"`
	Duration   string `mapstructure:"duration"`
	Path       string `mapstructure:"path"`
	Secure     bool   `mapstructure:"secure"`
	HTTPOnly   bool   `mapstructure:"http_only"`
}

CSRFToken csrf token

func (CSRFToken) Handler

func (c CSRFToken) Handler(ctx *gin.Context)

Handler gin middleware

func (CSRFToken) Token

func (c CSRFToken) Token() string

Token get token

func (CSRFToken) Valid

func (c CSRFToken) Valid(ctx *gin.Context) error

Valid csrf token valid

type Claims

type Claims struct {
	ID        interface{} // 唯一id
	CheckData string      // 验证信息
	jwt.StandardClaims
}

Claims 生成token的结构体

func ParseToken

func ParseToken(sign string, keyFunc jwt.Keyfunc) (*Claims, error)

ParseToken 根据传入 token 得到 Claims 信息

Jump to

Keyboard shortcuts

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