mirror of https://github.com/golang-jwt/jwt.git
Add error handling to examples (#312)
This commit is contained in:
parent
908d356713
commit
c776b83291
|
@ -150,20 +150,15 @@ func verifyToken() error {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// Print some debug data
|
|
||||||
if *flagDebug && token != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Header:\n%v\n", token.Header)
|
|
||||||
fmt.Fprintf(os.Stderr, "Claims:\n%v\n", token.Claims)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Print an error if we can't parse for some reason
|
// Print an error if we can't parse for some reason
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("couldn't parse token: %w", err)
|
return fmt.Errorf("couldn't parse token: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Is token invalid?
|
// Print some debug data
|
||||||
if !token.Valid {
|
if *flagDebug {
|
||||||
return fmt.Errorf("token is invalid")
|
fmt.Fprintf(os.Stderr, "Header:\n%v\n", token.Header)
|
||||||
|
fmt.Fprintf(os.Stderr, "Claims:\n%v\n", token.Claims)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the token details
|
// Print the token details
|
||||||
|
@ -279,7 +274,7 @@ func showToken() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := jwt.Parse(string(tokData), nil)
|
token, err := jwt.Parse(string(tokData), nil)
|
||||||
if token == nil {
|
if err != nil {
|
||||||
return fmt.Errorf("malformed token: %w", err)
|
return fmt.Errorf("malformed token: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package jwt_test
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/golang-jwt/jwt/v5"
|
"github.com/golang-jwt/jwt/v5"
|
||||||
|
@ -24,7 +25,7 @@ func ExampleNewWithClaims_registeredClaims() {
|
||||||
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
ss, err := token.SignedString(mySigningKey)
|
ss, err := token.SignedString(mySigningKey)
|
||||||
fmt.Printf("%v %v", ss, err)
|
fmt.Println(ss, err)
|
||||||
// Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.0XN_1Tpp9FszFOonIBpwha0c_SfnNI22DhTnjMshPg8 <nil>
|
// Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.0XN_1Tpp9FszFOonIBpwha0c_SfnNI22DhTnjMshPg8 <nil>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +68,7 @@ func ExampleNewWithClaims_customClaimsType() {
|
||||||
|
|
||||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||||
ss, err := token.SignedString(mySigningKey)
|
ss, err := token.SignedString(mySigningKey)
|
||||||
fmt.Printf("%v %v", ss, err)
|
fmt.Println(ss, err)
|
||||||
|
|
||||||
// Output: foo: bar
|
// Output: foo: bar
|
||||||
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.xVuY2FZ_MRXMIEgVQ7J-TFtaucVFRXUzHm9LmV41goM <nil>
|
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.xVuY2FZ_MRXMIEgVQ7J-TFtaucVFRXUzHm9LmV41goM <nil>
|
||||||
|
@ -86,11 +87,12 @@ func ExampleParseWithClaims_customClaimsType() {
|
||||||
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
return []byte("AllYourBase"), nil
|
return []byte("AllYourBase"), nil
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
|
log.Fatal(err)
|
||||||
fmt.Printf("%v %v", claims.Foo, claims.RegisteredClaims.Issuer)
|
} else if claims, ok := token.Claims.(*MyCustomClaims); ok {
|
||||||
|
fmt.Println(claims.Foo, claims.RegisteredClaims.Issuer)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err)
|
log.Fatal("unknown claims type, cannot proceed")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output: bar test
|
// Output: bar test
|
||||||
|
@ -109,11 +111,12 @@ func ExampleParseWithClaims_validationOptions() {
|
||||||
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
return []byte("AllYourBase"), nil
|
return []byte("AllYourBase"), nil
|
||||||
}, jwt.WithLeeway(5*time.Second))
|
}, jwt.WithLeeway(5*time.Second))
|
||||||
|
if err != nil {
|
||||||
if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
|
log.Fatal(err)
|
||||||
fmt.Printf("%v %v", claims.Foo, claims.RegisteredClaims.Issuer)
|
} else if claims, ok := token.Claims.(*MyCustomClaims); ok {
|
||||||
|
fmt.Println(claims.Foo, claims.RegisteredClaims.Issuer)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err)
|
log.Fatal("unknown claims type, cannot proceed")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output: bar test
|
// Output: bar test
|
||||||
|
@ -147,11 +150,12 @@ func ExampleParseWithClaims_customValidation() {
|
||||||
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
return []byte("AllYourBase"), nil
|
return []byte("AllYourBase"), nil
|
||||||
}, jwt.WithLeeway(5*time.Second))
|
}, jwt.WithLeeway(5*time.Second))
|
||||||
|
if err != nil {
|
||||||
if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
|
log.Fatal(err)
|
||||||
fmt.Printf("%v %v", claims.Foo, claims.RegisteredClaims.Issuer)
|
} else if claims, ok := token.Claims.(*MyCustomClaims); ok {
|
||||||
|
fmt.Println(claims.Foo, claims.RegisteredClaims.Issuer)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err)
|
log.Fatal("unknown claims type, cannot proceed")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output: bar test
|
// Output: bar test
|
||||||
|
|
|
@ -2,6 +2,7 @@ package jwt_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -56,8 +57,11 @@ func ExampleParse_hmac() {
|
||||||
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
|
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
|
||||||
return hmacSampleSecret, nil
|
return hmacSampleSecret, nil
|
||||||
})
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
if claims, ok := token.Claims.(jwt.MapClaims); ok {
|
||||||
fmt.Println(claims["foo"], claims["nbf"])
|
fmt.Println(claims["foo"], claims["nbf"])
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
|
@ -83,9 +83,7 @@ func Example_getTokenViaHTTP() {
|
||||||
"user": {"test"},
|
"user": {"test"},
|
||||||
"pass": {"known"},
|
"pass": {"known"},
|
||||||
})
|
})
|
||||||
if err != nil {
|
fatal(err)
|
||||||
fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if res.StatusCode != 200 {
|
if res.StatusCode != 200 {
|
||||||
fmt.Println("Unexpected status code", res.StatusCode)
|
fmt.Println("Unexpected status code", res.StatusCode)
|
||||||
|
|
Loading…
Reference in New Issue