jwtauth

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package jwtauth

@author: xwc1125

Package jwtauth

@author: xwc1125

Package jwtauth

@author: xwc1125

Package jwtauth

@author: xwc1125

Package jwtauth

@author: xwc1125

Package jwtauth

@author: xwc1125

Package jwtauth

@author: xwc1125

Index

Constants

View Source
const (
	DefaultJwtContextKey = "key-jwt"
	AuthorizationKEY     = "Authorization"
)

Variables

View Source
var (
	TokenExactFailed           string = "token不存在或header设置不正确"
	TokenExpire                string = "回话已过期"
	TokenCreateFailed          string = "生成token错误"
	TokenParseFailed           string = "token解析错误"
	TokenParseFailedAndEmpty   string = "解析错误,token为空"
	TokenParseFailedAndInvalid string = "解析错误,token无效"
	TokenUnsupportedOptions           = errors.New("unsupported options")

	ErrTokenIllegal = &response.StatusErr{480, "err.ErrTokenIllegal", "Illegal token", ""}
	ErrTokenReplace = &response.StatusErr{481, "err.TokenReplace", "Other clients logged in", ""}
	ErrTokenExpired = &response.StatusErr{482, "err.TokenExpired", "Token expired", ""}
)
View Source
var (
	Issuer = "key-casbins-jwt"
)
View Source
var (
	JwtHandlerPool = &sync.Pool{New: func() interface{} {
		return &JwtHandler{}
	}}
)

Functions

func CheckPermission

func CheckPermission(ctx contextx.Context, config JWTConfig) bool

func GenerateRefreshToken

func GenerateRefreshToken(config JWTConfig, token string) (string, error)

GenerateRefreshToken 生成刷新token

func GenerateToken

func GenerateToken(config JWTConfig, userId int64, userName string, extraData MapClaims) (string, error)

GenerateToken 生成token

func GetUserId

func GetUserId(config JWTConfig, token string) (int64, bool)

func LoadRSAPrivateKey

func LoadRSAPrivateKey(prvFile string) (*rsa.PrivateKey, error)

LoadRSAPrivateKey 加载rsa私钥

func LoadRSAPublicKey

func LoadRSAPublicKey(pubFile string) (*rsa.PublicKey, error)

LoadRSAPublicKey 加载rsa公钥

func TokenFormat

func TokenFormat(token string) string

TokenFormat 格式化token

func TokenToMapClaims

func TokenToMapClaims(token *jwt.Token) jwt.MapClaims

TokenToMapClaims jwt token 转mapClaims

Types

type JWTConfig

type JWTConfig struct {
	AuthorizationKey string `json:"authorization_key" mapstructure:"authorization_key" yaml:"authorization_key"` // 请求中header中token key
	ParamTokenKey    string `json:"param_token_key" mapstructure:"param_token_key" yaml:"param_token_key"`       // 请求中params中token key
	CookieTokenKey   string `json:"cookie_token_key" mapstructure:"cookie_token_key" yaml:"cookie_token_key"`    // 请求中cookie中token key
	JwtContextKey    string `json:"jwt_context_key" mapstructure:"jwt_context_key" yaml:"jwt_context_key"`       // 缓存token的key

	PriKeyPath          string   `json:"pri_key_path" mapstructure:"pri_key_path"`                                                   // rsa私钥
	PubKeyPath          string   `json:"pub_key_path" mapstructure:"pub_key_path"`                                                   // rsa公钥
	Secret              string   `json:"secret" mapstructure:"secret"`                                                               // token 密钥
	Timeout             int64    `json:"timeout" mapstructure:"timeout"`                                                             // token 过期时间 单位:秒
	RefreshTimeout      int64    `json:"refresh_timeout" mapstructure:"refresh_timeout" yaml:"refresh_timeout"`                      // refreshToken 过期时间 单位:秒
	IgnoreURLs          []string `json:"ignore_urls" mapstructure:"ignore_urls"`                                                     // 忽略的url
	OnlineKey           string   `json:"online_key" mapstructure:"online_key"`                                                       // 在线标识
	EnableAuthOnOptions bool     `json:"enable_auth_on_options" mapstructure:"enable_auth_on_options" yaml:"enable_auth_on_options"` // 是否启动OPTIONS方法的所有请求都将使用身份验证
	Debug               bool     `json:"debug" mapstructure:"debug" yaml:"debug"`                                                    // 是否启动debug,进行日志输出
}

JWTConfig jwt配置信息

func (*JWTConfig) IsIgnore

func (c *JWTConfig) IsIgnore(reqPath, reqMethod string) bool

type JwtAuth

type JwtAuth struct {
	JWTConfig JWTConfig
	*JwtHandler
	Extractor TokenExtractor // Extractor 从请求中提取令牌的函数
}

JwtAuth jwt auth

func NewJwtAuth

func NewJwtAuth(config JWTConfig) *JwtAuth

NewJwtAuth jwt中间件配置

func (*JwtAuth) CheckJWT

func (m *JwtAuth) CheckJWT(ctx contextx.Context) (*UserToken, error)

CheckJWT 验证jwt

func (*JwtAuth) GenerateToken

func (m *JwtAuth) GenerateToken(userId int64, userName string, extraData MapClaims) (string, error)

GenerateToken 在登录成功的时候生成token

type JwtClaims

type JwtClaims struct {
	UserToken
	jwt.StandardClaims
}

JwtClaims jwt claims信息

func MapClaimsToJwt

func MapClaimsToJwt(claims jwt.MapClaims) *JwtClaims

MapClaimsToJwt map转jwtClaims

func ParseToken

func ParseToken(config JWTConfig, token string) (*JwtClaims, error)

ParseToken 解析token

type JwtHandler

type JwtHandler struct {
	// contains filtered or unexported fields
}

func NewJwtHandler

func NewJwtHandler() *JwtHandler

func (*JwtHandler) GenerateRefreshToken

func (j *JwtHandler) GenerateRefreshToken(claims JwtRefreshClaims) (string, error)

GenerateRefreshToken 生成刷新token

func (*JwtHandler) GenerateToken

func (j *JwtHandler) GenerateToken(claims JwtClaims) (string, error)

GenerateToken 生成token

func (*JwtHandler) GetPrivateKey

func (j *JwtHandler) GetPrivateKey() *rsa.PrivateKey

func (*JwtHandler) GetPublicKey

func (j *JwtHandler) GetPublicKey() *rsa.PublicKey

func (*JwtHandler) GetSecret

func (j *JwtHandler) GetSecret() string

func (*JwtHandler) Release

func (j *JwtHandler) Release()

func (*JwtHandler) SetPrivateKey

func (j *JwtHandler) SetPrivateKey(key *rsa.PrivateKey)

func (*JwtHandler) SetPublicKey

func (j *JwtHandler) SetPublicKey(key *rsa.PublicKey)

func (*JwtHandler) SetSecret

func (j *JwtHandler) SetSecret(secret string)

func (*JwtHandler) ValidateRefreshToken

func (j *JwtHandler) ValidateRefreshToken(token string) (*JwtRefreshClaims, error)

func (*JwtHandler) ValidateToken

func (j *JwtHandler) ValidateToken(token string) (*JwtClaims, error)

ValidateToken 验证token

type JwtRefreshClaims

type JwtRefreshClaims struct {
	Token string `json:"token"`
	jwt.StandardClaims
}

JwtRefreshClaims 刷新claims

func ParseRefreshToken

func ParseRefreshToken(config JWTConfig, token string) (*JwtRefreshClaims, error)

ParseRefreshToken 解析refresh token

type MapClaims

type MapClaims jwt.MapClaims

type TokenExtractor

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

TokenExtractor is a function that takes a context as input and returns either a token or an error. An error should only be returned if an attempt to specify a token was found, but the information was somehow incorrectly formed. In the case where a token is simply not present, this should not be treated as an error. An empty string should be returned in that case.

func FromAuthHeader

func FromAuthHeader(authKey string) TokenExtractor

FromAuthHeader 从Authorization header中获取

func FromCookie

func FromCookie(param string) TokenExtractor

FromCookie 从cookie中获取

func FromFirst

func FromFirst(extractors ...TokenExtractor) TokenExtractor

FromFirst 获取它找到的第一个令牌

func FromParameter

func FromParameter(tokenKey string) TokenExtractor

FromParameter 从params中获取

type UserToken

type UserToken struct {
	Uid       int64     `form:"uid"`      // UID
	Username  string    `form:"username"` // 用户名
	ExtraData MapClaims // 扩展内容
}

UserToken 用户信息

func Serve

func Serve(ctx contextx.Context, config JWTConfig) (userToken *UserToken, b bool)

Directories

Path Synopsis
Package jwtsession
Package jwtsession

Jump to

Keyboard shortcuts

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