Documentation ¶
Index ¶
- Constants
- Variables
- func FromAuthHeader(ctx iris.Context) (string, error)
- func GenTokenHS256(secret string, id string, expireIn time.Duration, issuer string) string
- func OnError(ctx iris.Context, err error)
- type Claims
- type Config
- type MapClaims
- type Middleware
- type RegisteredClaims
- type Token
- type TokenExtractor
Constants ¶
const ( // DefaultContextKey jwt DefaultContextKey = "jwt" // 默认的解析后放在 iris header里面的已验证的 user_id 的key 值 DefaultUserIDKey = "REMOTE_USER" )
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。
func GenTokenHS256 ¶ added in v0.1.2
生成HS256 token @scret: 密钥 @id: 用户id @expireIn: 过期时间(秒) @issuer: 签发者
Types ¶
type Claims ¶
type Claims = jwt.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 // 解析后放在 iris header里面的已验证的 user_id 的key 值, 默认为 "REMOTE_USER", 与django保持一致 UserIDKey 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 ¶
type MapClaims = jwt.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(secret string) *Middleware
func New ¶
func New(cfg ...Config) *Middleware
New constructs a new Secure instance with supplied options.
新建一个中间件实例
type RegisteredClaims ¶
type RegisteredClaims = jwt.RegisteredClaims
type Token ¶
type Token = jwt.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