added some examples of unpacking errors from the bitfield

This commit is contained in:
Dave Grijalva 2014-12-28 12:44:46 -08:00
parent db4251f9dd
commit d9679c1420
1 changed files with 22 additions and 0 deletions

View File

@ -28,3 +28,25 @@ func ExampleNew(mySigningKey string) (string, error) {
tokenString, err := token.SignedString(mySigningKey) tokenString, err := token.SignedString(mySigningKey)
return tokenString, err return tokenString, err
} }
func ExampleParse_errorChecking(myToken string, myLookupKey func(interface{}) (interface{}, error)) {
token, err := jwt.Parse(myToken, func(token *jwt.Token) (interface{}, error) {
return myLookupKey(token.Header["kid"])
})
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 {
// Token is either expired or not active yet
fmt.Println("Timing is everything")
} else {
fmt.Println("Couldn't handle this token:", err)
}
} else {
fmt.Println("Couldn't handle this token:", err)
}
}