package jwt

Import Path
	github.com/dgrijalva/jwt-go (on go.dev)

Dependency Relation
	imports 17 packages, and imported by one package

Involved Source Files claims.go Package jwt is a Go implementation of JSON Web Tokens: http://self-issued.info/docs/draft-jones-json-web-token.html See README.md for more info. ecdsa.go ecdsa_utils.go errors.go hmac.go map_claims.go none.go parser.go rsa.go rsa_pss.go rsa_utils.go signing_method.go token.go
Code Examples { mySigningKey := []byte("AllYourBase") type MyCustomClaims struct { Foo string `json:"foo"` jwt.StandardClaims } claims := MyCustomClaims{ "bar", jwt.StandardClaims{ ExpiresAt: 15000, Issuer: "test", }, } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) ss, err := token.SignedString(mySigningKey) fmt.Printf("%v %v", ss, err) } { mySigningKey := []byte("AllYourBase") claims := &jwt.StandardClaims{ ExpiresAt: 15000, Issuer: "test", } token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) ss, err := token.SignedString(mySigningKey) fmt.Printf("%v %v", ss, err) } { tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.HE7fK0xOQwFEr4WDgRWj4teRPZ6i3GLwD5YCm6Pwu_c" type MyCustomClaims struct { Foo string `json:"foo"` jwt.StandardClaims } at(time.Unix(0, 0), func() { token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) { return []byte("AllYourBase"), nil }) if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid { fmt.Printf("%v %v", claims.Foo, claims.StandardClaims.ExpiresAt) } else { fmt.Println(err) } }) } { // Token from another example. This token is expired var tokenString = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.HE7fK0xOQwFEr4WDgRWj4teRPZ6i3GLwD5YCm6Pwu_c" token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { return []byte("AllYourBase"), nil }) if token.Valid { fmt.Println("You look nice today") } else if ve, ok := err.(*jwt.ValidationError); ok { if ve.Errors&jwt.ValidationErrorMalformed != 0 { fmt.Println("That's not even a token") } else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 { fmt.Println("Timing is everything") } else { fmt.Println("Couldn't handle this token:", err) } } else { fmt.Println("Couldn't handle this token:", err) } }
Package-Level Type Names (total 14, in which 12 are exported)
/* sort exporteds by: | */
For a type to be a Claims object, it must just have a Valid method that determines if the token is invalid for any supported reason ( T) Valid() error MapClaims StandardClaims func NewWithClaims(method SigningMethod, claims Claims) *Token func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) func (*Parser).ParseUnverified(tokenString string, claims Claims) (token *Token, parts []string, err error) func (*Parser).ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error)
Parse methods use this 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. func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) func (*Parser).Parse(tokenString string, keyFunc Keyfunc) (*Token, error) func (*Parser).ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error)
Claims type that uses the map[string]interface{} for JSON decoding This is the default claims type if you don't supply one Validates time based claims "exp, iat, nbf". There is no accounting for clock skew. As well, if any of the above claims are not in the token, it will still be considered a valid claim. Compares the aud claim against cmp. If required is false, this method will return true if the value matches or is unset Compares the exp claim against cmp. If required is false, this method will return true if the value matches or is unset Compares the iat claim against cmp. If required is false, this method will return true if the value matches or is unset Compares the iss claim against cmp. If required is false, this method will return true if the value matches or is unset Compares the nbf claim against cmp. If required is false, this method will return true if the value matches or is unset T : Claims
// Skip claims validation during token parsing // Use JSON Number format in JSON decoder // If populated, only these methods will be considered valid Parse, validate, and return a token. keyFunc will receive the parsed token and should return the key for validating. If everything is kosher, err will be nil WARNING: Don't use this method unless you know what you're doing This method parses the token but doesn't validate the signature. It's only ever useful in cases where you know the signature is valid (because it has been checked previously in the stack) and you want to extract values from it. (*T) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error)
Implement SigningMethod to add new methods for signing or verifying tokens. // returns the alg identifier for this method (example: 'HS256') // Returns encoded signature or error // Returns nil if signature is valid *SigningMethodECDSA *SigningMethodHMAC *SigningMethodRSA *SigningMethodRSAPSS func GetSigningMethod(alg string) (method SigningMethod) func New(method SigningMethod) *Token func NewWithClaims(method SigningMethod, claims Claims) *Token
Implements the ECDSA family of signing methods signing methods Expects *ecdsa.PrivateKey for signing and *ecdsa.PublicKey for verification CurveBits int Hash crypto.Hash KeySize int Name string (*T) Alg() string Implements the Sign method from SigningMethod For this signing method, key must be an ecdsa.PrivateKey struct Implements the Verify method from SigningMethod For this verify method, key must be an ecdsa.PublicKey struct *T : SigningMethod var SigningMethodES256 *SigningMethodECDSA var SigningMethodES384 *SigningMethodECDSA var SigningMethodES512 *SigningMethodECDSA
Implements the HMAC-SHA family of signing methods signing methods Expects key type of []byte for both signing and validation Hash crypto.Hash Name string (*T) Alg() string Implements the Sign method from SigningMethod for this signing method. Key must be []byte Verify the signature of HSXXX tokens. Returns nil if the signature is valid. *T : SigningMethod var SigningMethodHS256 *SigningMethodHMAC var SigningMethodHS384 *SigningMethodHMAC var SigningMethodHS512 *SigningMethodHMAC
Implements the RSA family of signing methods signing methods Expects *rsa.PrivateKey for signing and *rsa.PublicKey for validation Hash crypto.Hash Name string (*T) Alg() string Implements the Sign method from SigningMethod For this signing method, must be an *rsa.PrivateKey structure. Implements the Verify method from SigningMethod For this signing method, must be an *rsa.PublicKey structure. *T : SigningMethod var SigningMethodRS256 *SigningMethodRSA var SigningMethodRS384 *SigningMethodRSA var SigningMethodRS512 *SigningMethodRSA
Implements the RSAPSS family of signing methods signing methods Options *rsa.PSSOptions SigningMethodRSA *SigningMethodRSA SigningMethodRSA.Hash crypto.Hash SigningMethodRSA.Name string ( T) Alg() string Implements the Sign method from SigningMethod For this signing method, key must be an rsa.PrivateKey struct Implements the Verify method from SigningMethod For this verify method, key must be an rsa.PublicKey struct *T : SigningMethod var SigningMethodPS256 *SigningMethodRSAPSS var SigningMethodPS384 *SigningMethodRSAPSS var SigningMethodPS512 *SigningMethodRSAPSS
Structured version of Claims Section, as referenced at https://tools.ietf.org/html/rfc7519#section-4.1 See examples for how to use this with your own claim types Audience string ExpiresAt int64 Id string IssuedAt int64 Issuer string NotBefore int64 Subject string Validates time based claims "exp, iat, nbf". There is no accounting for clock skew. As well, if any of the above claims are not in the token, it will still be considered a valid claim. Compares the aud claim against cmp. If required is false, this method will return true if the value matches or is unset Compares the exp claim against cmp. If required is false, this method will return true if the value matches or is unset Compares the iat claim against cmp. If required is false, this method will return true if the value matches or is unset Compares the iss claim against cmp. If required is false, this method will return true if the value matches or is unset Compares the nbf claim against cmp. If required is false, this method will return true if the value matches or is unset T : Claims
A JWT Token. Different fields will be used depending on whether you're creating or parsing/verifying a token. // The second segment of the token // The first segment of the token // The signing method used or to be used // The raw token. Populated when you Parse a token // The third segment of the token. Populated when you Parse a token // Is the token valid? Populated when you Parse/Verify a token Get the complete, signed token Generate the signing string. This is the most expensive part of the whole deal. Unless you need this for something special, just go straight for the SignedString. func New(method SigningMethod) *Token func NewWithClaims(method SigningMethod, claims Claims) *Token func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) func (*Parser).Parse(tokenString string, keyFunc Keyfunc) (*Token, error) func (*Parser).ParseUnverified(tokenString string, claims Claims) (token *Token, parts []string, err error) func (*Parser).ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error)
The error from Parse if token is not valid // bitfield. see ValidationError... constants // stores the error returned by external dependencies, i.e.: KeyFunc Validation error is an error type T : error func NewValidationError(errorText string, errorFlags uint32) *ValidationError
Package-Level Functions (total 24, in which 14 are exported)
Decode JWT specific base64url encoding with padding stripped
Encode JWT specific base64url encoding with padding stripped
Get a signing method from an "alg" string
Create a new Token. Takes a signing method
Helper for constructing a ValidationError with a string error message
func NewWithClaims(method SigningMethod, claims Claims) *Token
Parse, validate, and return a token. keyFunc will receive the parsed token and should return the key for validating. If everything is kosher, err will be nil
Parse PEM encoded Elliptic Curve Private Key Structure
Parse PEM encoded PKCS1 or PKCS8 public key
Parse PEM encoded PKCS1 or PKCS8 private key
Parse PEM encoded PKCS1 or PKCS8 private key protected with password
Parse PEM encoded PKCS1 or PKCS8 public key
func ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error)
Register the "alg" name and a factory function for signing method. This is typically done during init() in the method's implementation
Package-Level Variables (total 27, in which 25 are exported)
Sadly this is missing from crypto/ecdsa compared to crypto/rsa
Error constants
Error constants
Error constants
Specific instances for HS256 and company
Specific instances for EC256 and company
Specific instances for EC256 and company
Specific instances for EC256 and company
Specific instances for HS256 and company
Specific instances for HS256 and company
Specific instances for HS256 and company
Implements the none signing method. This is required by the spec but you probably should never use it.
Specific instances for RS/PS and company
Specific instances for RS/PS and company
Specific instances for RS/PS and company
Specific instances for RS256 and company
Specific instances for RS256 and company
Specific instances for RS256 and company
TimeFunc provides the current time when parsing token to validate "exp" claim (expiration time). You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
Package-Level Constants (total 11, all are exported)
const UnsafeAllowNoneSignatureType unsafeNoneMagicConstant = "none signing method allowed"
Standard Claim validation errors
The errors that might occur when parsing and validating a token
The errors that might occur when parsing and validating a token
The errors that might occur when parsing and validating a token
The errors that might occur when parsing and validating a token
The errors that might occur when parsing and validating a token
The errors that might occur when parsing and validating a token
The errors that might occur when parsing and validating a token
The errors that might occur when parsing and validating a token
The errors that might occur when parsing and validating a token