Documentation ¶
Index ¶
- Constants
- func IsTokenExpiredError(err error) bool
- type JWT
- func (j *JWT) New(method jwt.SigningMethod, claims jwt.MapClaims, options ...jwt.TokenOption) (string, error)
- func (j *JWT) NewComplexly(method jwt.SigningMethod, claims jwt.MapClaims, options ...jwt.TokenOption) (cipherText string, err error)
- func (j *JWT) Parse(tokenString string, keyFunc jwt.Keyfunc, options ...jwt.ParserOption) (jwt.MapClaims, error)
- func (j *JWT) ParseComplexly(cipherText string, keyFunc jwt.Keyfunc, options ...jwt.ParserOption) (jwt.MapClaims, error)
Constants ¶
View Source
const ( // KeyNotBefore 生效时间 KeyNotBefore = "nbf" // KeyExpirationTime 过期时间 KeyExpirationTime = "exp" // KeyIssuedAt 签发时间 KeyIssuedAt = "iat" // KeyAudience 受众 KeyAudience = "aud" // KeyIssuer 签发人 KeyIssuer = "iss" // KeySubject 主题 KeySubject = "sub" // KeyJwtId 编号 KeyJwtId = "jti" )
Variables ¶
This section is empty.
Functions ¶
func IsTokenExpiredError ¶
Types ¶
type JWT ¶
type JWT struct { Key []byte // contains filtered or unexported fields }
func (*JWT) New ¶
func (j *JWT) New(method jwt.SigningMethod, claims jwt.MapClaims, options ...jwt.TokenOption) (string, error)
New
@param Key 密钥 @param method e.g. jwt.SigningMethodHS256 || jwt.SigningMethodHS384 || jwt.SigningMethodHS512
func (*JWT) NewComplexly ¶
func (*JWT) Parse ¶
func (j *JWT) Parse(tokenString string, keyFunc jwt.Keyfunc, options ...jwt.ParserOption) (jwt.MapClaims, error)
Parse
PS: 如果 token 过期(根据"exp",有的话),会返回 error(可以通过 IsTokenExpiredError 判断).
@param keyFunc e.g.
func(token *jwt.Token) (interface{}, error) { // Don't forget to validate the alg is what you expect: if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) } // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") return Key, nil }
func (*JWT) ParseComplexly ¶
Click to show internal directories.
Click to hide internal directories.