auth

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingJwtToken        = errors.New("JWT token is missing")
	ErrMissingKeyFunc         = errors.New("keyFunc is missing")
	ErrTokenInvalid           = errors.New("Token is invalid")
	ErrTokenExpired           = errors.New("JWT token has expired")
	ErrTokenParseFail         = errors.New("Fail to parse JWT token ")
	ErrUnSupportSigningMethod = errors.New("Wrong signing method")
	ErrWrongContext           = errors.New("Wrong context for middleware")
	ErrNeedTokenProvider      = errors.New("Token provider is missing")
	ErrSignToken              = errors.New("Can not sign token.Is the key correct?")
	ErrGetKey                 = errors.New("Can not get key while signing token")
)

Functions

func FromContext added in v0.0.11

func FromContext(ctx context.Context) (token jwt.Claims, ok bool)

FromContext extract auth info from context

func GenerateSignature

func GenerateSignature(message string, secret string) string

GenerateSignature 生成签名

func GenerateToken added in v0.0.11

func GenerateToken(key string, opts ...Option) (string, error)

func New

func New(config JwtConfig) (*jwtMiddleware, error)

New 创建一个新的 jwtMiddleware 实例

func NewContext added in v0.0.11

func NewContext(ctx context.Context, info jwt.Claims) context.Context

NewContext put auth info into context

func ParseJwtToken added in v0.0.11

func ParseJwtToken(jwtToken string, opts ...Option) (jwt.Claims, error)

ParseJwtToken 解析和验证令牌

func VerifySignature

func VerifySignature(ak, sk, timeStr, sign string) bool

VerifySignature 验证签名

Types

type JwtConfig

type JwtConfig struct {
	SigningKey    interface{} // 用于签名的密钥
	TokenLookup   string      // 定义如何查找令牌
	SigningMethod string      // 签名方法
	ErrHandler    func(w http.ResponseWriter, r *http.Request, err error)
}

JwtConfig 定义了 JWT 中间件的配置

type Option added in v0.0.11

type Option func(*options)

Option is jwt option.

func WithClaims added in v0.0.11

func WithClaims(f func() jwt.Claims) Option

func WithKeyFunc added in v0.0.11

func WithKeyFunc(f jwt.Keyfunc) Option

func WithSigningMethod added in v0.0.11

func WithSigningMethod(method jwt.SigningMethod) Option

type TokenClaims

type TokenClaims struct {
	Username string `json:"username"`
	// 在这里添加你想包含的其他声明
	jwt.RegisteredClaims
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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