// Token represents a JWT Token. Different fields will be used depending on
// whether you're creating or parsing/verifying a token.
typeTokenstruct{Rawstring// Raw contains the raw token. Populated when you [Parse] a token
MethodSigningMethod// Method is the signing method used or to be used
Headermap[string]interface{}// Header is the first segment of the token in decoded form
ClaimsClaims// Claims is the second segment of the token in decoded form
Signature[]byte// Signature is the third segment of the token in decoded form. Populated when you Parse a token
Validbool// Valid specifies if the token is valid. Populated when you Parse/Verify a token
}
以上為Token對象
我們再研究一下TokenOptions是什麽東西
1
2
3
// TokenOption is a reserved type, which provides some forward compatibility,
// if we ever want to introduce token creation-related options.
typeTokenOptionfunc(*Token)
MapClaims 是 Go 言語中的一個類型,通常用於處理 JSON Web Token (JWT) 的聲明(claims)。在 jwt-go 庫中,MapClaims 是一個基於地圖(map[string]interface{})的結構,用來存儲和處理 JWT 的聲明。
1
2
3
// MapClaims is a claims type that uses the map[string]interface{} for JSON
// decoding. This is the default claims type if you don't supply one
typeMapClaimsmap[string]interface{}
// New creates a new [Token] with the specified signing method and an empty map
// of claims. Additional options can be specified, but are currently unused.
funcNew(methodSigningMethod,opts...TokenOption)*Token{returnNewWithClaims(method,MapClaims{},opts...)}
// SignedString creates and returns a complete, signed JWT. The token is signed
// using the SigningMethod specified in the token. Please refer to
// https://golang-jwt.github.io/jwt/usage/signing_methods/#signing-methods-and-key-types
// for an overview of the different signing methods and their respective key
// types.
func(t*Token)SignedString(keyinterface{})(string,error){sstr,err:=t.SigningString()iferr!=nil{return"",err}sig,err:=t.Method.Sign(sstr,key)iferr!=nil{return"",err}returnsstr+"."+t.EncodeSegment(sig),nil}
// Parse parses, validates, verifies the signature and returns the parsed token.
// keyFunc will receive the parsed token and should return the cryptographic key
// for verifying the signature. The caller is strongly encouraged to set the
// WithValidMethods option to validate the 'alg' claim in the token matches the
// expected algorithm. For more details about the importance of validating the
// 'alg' claim, see
// https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
funcParse(tokenStringstring,keyFuncKeyfunc,options...ParserOption)(*Token,error){returnNewParser(options...).Parse(tokenString,keyFunc)}
我們來看看keyFunc這個結構體組成
1
2
3
4
5
6
7
8
9
// Keyfunc will be used by the Parse methods as a callback function to supply
// the key for verification. The function receives the parsed, but unverified
// Token. This allows you to use properties in the Header of the token (such as
// `kid`) to identify which key to use.
//
// The returned interface{} may be a single key or a VerificationKeySet containing
// multiple keys.
typeKeyfuncfunc(*Token)(interface{},error)