Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error handling to examples #312

Merged
merged 8 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/jwt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ func verifyToken() error {
return data, nil
})

// Print an error if we can't parse for some reason
oxisto marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("couldn't parse token: %w", err)
}

// 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
if err != nil {
return fmt.Errorf("couldn't parse token: %w", err)
}

// Is token invalid?
if !token.Valid {
return fmt.Errorf("token is invalid")
Expand Down Expand Up @@ -274,7 +274,7 @@ func showToken() error {
}

token, err := jwt.Parse(string(tokData), nil)
if token == nil {
if err != nil || token == nil {
return fmt.Errorf("malformed token: %w", err)
}

Expand Down
13 changes: 13 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jwt_test
import (
"errors"
"fmt"
"log"
"time"

"github.com/golang-jwt/jwt/v5"
Expand Down Expand Up @@ -86,6 +87,9 @@ func ExampleParseWithClaims_customClaimsType() {
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte("AllYourBase"), nil
})
if err != nil {
oxisto marked this conversation as resolved.
Show resolved Hide resolved
log.Fatal(err)
}

if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
fmt.Printf("%v %v", claims.Foo, claims.RegisteredClaims.Issuer)
Expand All @@ -109,6 +113,9 @@ func ExampleParseWithClaims_validationOptions() {
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte("AllYourBase"), nil
}, jwt.WithLeeway(5*time.Second))
if err != nil {
log.Fatal(err)
}

if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
fmt.Printf("%v %v", claims.Foo, claims.RegisteredClaims.Issuer)
Expand Down Expand Up @@ -144,6 +151,9 @@ func ExampleParseWithClaims_customValidation() {
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte("AllYourBase"), nil
}, jwt.WithLeeway(5*time.Second))
if err != nil {
log.Fatal(err)
}

if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid {
fmt.Printf("%v %v", claims.Foo, claims.RegisteredClaims.Issuer)
Expand All @@ -162,6 +172,9 @@ func ExampleParse_errorChecking() {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return []byte("AllYourBase"), nil
})
if err != nil {
log.Fatal(err)
}

if token.Valid {
fmt.Println("You look nice today")
Expand Down
4 changes: 4 additions & 0 deletions hmac_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package jwt_test

import (
"fmt"
"log"
"os"
"time"

Expand Down Expand Up @@ -56,6 +57,9 @@ func ExampleParse_hmac() {
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
return hmacSampleSecret, nil
})
if err != nil {
log.Fatal(err)
}

if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
fmt.Println(claims["foo"], claims["nbf"])
Expand Down
4 changes: 1 addition & 3 deletions http_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ func Example_getTokenViaHTTP() {
"user": {"test"},
"pass": {"known"},
})
if err != nil {
fatal(err)
}
fatal(err)

if res.StatusCode != 200 {
fmt.Println("Unexpected status code", res.StatusCode)
Expand Down