Documentation
¶
Index ¶
Constants ¶
const (
// DefaultContextKey jwt
DefaultContextKey = "jwt"
)
Variables ¶
var ( // ErrTokenMissing is the error value that it's returned when // a token is not found based on the token extractor. ErrTokenMissing = errors.New("required authorization token not found") // ErrTokenInvalid is the error value that it's returned when // a token is not valid. ErrTokenInvalid = errors.New("token is invalid") // // ErrTokenExpired is the error value that it's returned when // // a token value is found and it's valid but it's expired. ErrTokenExpired = errors.New("token is expired") )
var ( // NewToken = jwt.New NewTokenWithClaims = jwt.NewWithClaims NewNumericDate = jwt.NewNumericDate )
Shortcuts to create a new Token.
var ( SigningMethodHS256 = jwt.SigningMethodHS256 SigningMethodHS384 = jwt.SigningMethodHS384 SigningMethodHS512 = jwt.SigningMethodHS512 )
HS256 and company.
var ( SigningMethodES256 = jwt.SigningMethodES256 SigningMethodES384 = jwt.SigningMethodES384 SigningMethodES512 = jwt.SigningMethodES512 )
ECDSA - EC256 and company.
Functions ¶
func FromAuthHeader ¶
FromAuthHeader 是 TokenExtractor 的一个实现,它接受一个上下文,并从 Authorization header 中提取 JWT token。
Types ¶
type Claims ¶
Claims must just have a Valid method that determines if the token is invalid for any supported reason.
A type alias for jwt.Claims.
type Config ¶
type Config struct {
// The function that will return the Key to validate the JWT.
// It can be either a shared secret or a public key.
// Default value: nil
ValidationKeyGetter jwt.Keyfunc
// The function that will be called when there's an error validating the token
// Default value:
ErrorHandler errorHandler
// 默认token抽取器
Extractor TokenExtractor
// The name of the property in the request where the user (&token) information
// from the JWT will be stored.
// 默认的存储在iris Context中的key, 默认值为"jwt"
ContextKey string
// A boolean indicating if the credentials are required or not
// Default value: false
CredentialsOptional bool
// When set, the middelware verifies that tokens are signed with the specific signing algorithm
// If the signing method is not constant the ValidationKeyGetter callback can be used to implement additional checks
// Important to avoid security issues described here: https://auth0.com/blog/2015/03/31/critical-vulnerabilities-in-json-web-token-libraries/
// Default: nil
SigningMethod jwt.SigningMethod
}
type MapClaims ¶
MapClaims type that uses the map[string]interface{} for JSON decoding This is the default claims type if you don't supply one
A type alias for jwt.MapClaims.
type Middleware ¶
type Middleware struct {
Config Config
}
func GetJwtMiddleware ¶
func GetJwtMiddleware() *Middleware
func New ¶
func New(cfg ...Config) *Middleware
New constructs a new Secure instance with supplied options.
新建一个中间件实例
type MyClaims ¶
type MyClaims struct {
*jwt.RegisteredClaims
// Username string `json:"username"`
Uid int `json:"uid"`
}
wanxiang的token payload
type RegisteredClaims ¶
type RegisteredClaims = jwt.RegisteredClaims
type Token ¶
Token for JWT. Different fields will be used depending on whether you're creating or parsing/verifying a token.
A type alias for jwt.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. 定义一个抽取器,接受一个上下文,返回token或者错误
func FromFirst ¶
func FromFirst(extractors ...TokenExtractor) TokenExtractor
按顺序查找多个token提取器,并返回第一个找到的token