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
- Variables
- func CheckPermission(ctx contextx.Context, config JWTConfig) bool
- func GenerateRefreshToken(config JWTConfig, token string) (string, error)
- func GenerateToken(config JWTConfig, userId int64, userName string, extraData MapClaims) (string, error)
- func GetUserId(config JWTConfig, token string) (int64, bool)
- func LoadRSAPrivateKey(prvFile string) (*rsa.PrivateKey, error)
- func LoadRSAPublicKey(pubFile string) (*rsa.PublicKey, error)
- func TokenFormat(token string) string
- func TokenToMapClaims(token *jwt.Token) jwt.MapClaims
- type JWTConfig
- type JwtAuth
- type JwtClaims
- type JwtHandler
- func (j *JwtHandler) GenerateRefreshToken(claims JwtRefreshClaims) (string, error)
- func (j *JwtHandler) GenerateToken(claims JwtClaims) (string, error)
- func (j *JwtHandler) GetPrivateKey() *rsa.PrivateKey
- func (j *JwtHandler) GetPublicKey() *rsa.PublicKey
- func (j *JwtHandler) GetSecret() string
- func (j *JwtHandler) Release()
- func (j *JwtHandler) SetPrivateKey(key *rsa.PrivateKey)
- func (j *JwtHandler) SetPublicKey(key *rsa.PublicKey)
- func (j *JwtHandler) SetSecret(secret string)
- func (j *JwtHandler) ValidateRefreshToken(token string) (*JwtRefreshClaims, error)
- func (j *JwtHandler) ValidateToken(token string) (*JwtClaims, error)
- type JwtRefreshClaims
- type MapClaims
- type TokenExtractor
- type UserToken
Constants ¶
const ( DefaultJwtContextKey = "key-jwt" AuthorizationKEY = "Authorization" )
Variables ¶
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", ""} )
var (
Issuer = "key-casbins-jwt"
)
var ( JwtHandlerPool = &sync.Pool{New: func() interface{} { return &JwtHandler{} }} )
Functions ¶
func GenerateRefreshToken ¶
GenerateRefreshToken 生成刷新token
func GenerateToken ¶
func GenerateToken(config JWTConfig, userId int64, userName string, extraData MapClaims) (string, error)
GenerateToken 生成token
func LoadRSAPrivateKey ¶
func LoadRSAPrivateKey(prvFile string) (*rsa.PrivateKey, error)
LoadRSAPrivateKey 加载rsa私钥
func LoadRSAPublicKey ¶
LoadRSAPublicKey 加载rsa公钥
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配置信息
type JwtAuth ¶
type JwtAuth struct { JWTConfig JWTConfig *JwtHandler Extractor TokenExtractor // Extractor 从请求中提取令牌的函数 }
JwtAuth jwt auth
type JwtClaims ¶
type JwtClaims struct { UserToken jwt.StandardClaims }
JwtClaims jwt claims信息
func MapClaimsToJwt ¶
func MapClaimsToJwt(claims jwt.MapClaims) *JwtClaims
MapClaimsToJwt map转jwtClaims
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 TokenExtractor ¶
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中获取