From e7ad1706a2d121d98db57d0fca5230ef1ca9d854 Mon Sep 17 00:00:00 2001 From: Chris Gibson Date: Sat, 2 Aug 2025 01:07:30 +0100 Subject: [PATCH 1/2] Update example to include the New() function rather than the NewWithClaims() function --- hmac_example_test.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/hmac_example_test.go b/hmac_example_test.go index dafdd673..180e6dc1 100644 --- a/hmac_example_test.go +++ b/hmac_example_test.go @@ -4,7 +4,6 @@ import ( "fmt" "log" "os" - "time" "github.com/golang-jwt/jwt/v5" ) @@ -25,18 +24,17 @@ func init() { // Example creating, signing, and encoding a JWT token using the HMAC signing method func ExampleNew_hmac() { - // Create a new token object, specifying signing method and the claims - // you would like it to contain. - token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ - "foo": "bar", - "nbf": time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix(), - }) + // Create a new token object, specifying signing method + token := jwt.New(jwt.SigningMethodHS256) + + // Optionally, add claims to the token object + token.Claims = jwt.MapClaims{"aud": "someone"} // Sign and get the complete encoded token as a string using the secret tokenString, err := token.SignedString(hmacSampleSecret) fmt.Println(tokenString, err) - // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJuYmYiOjE0NDQ0Nzg0MDB9.u1riaD1rW97opCoAuRCTy4w58Br-Zk-bh7vLiRIsrpU + // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJzb21lb25lIn0.F0J37fS_ENgyQg9nxIE0fMS-DX3A11SMh4bvZ7MzD4M } // Example parsing and validating a token using the HMAC signing method From 8db555b927ed4959b03e75ce034421a4878f9e4a Mon Sep 17 00:00:00 2001 From: Chris Gibson Date: Sun, 3 Aug 2025 13:32:43 +0100 Subject: [PATCH 2/2] Change func name to ExampleNewWithClaims --- hmac_example_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/hmac_example_test.go b/hmac_example_test.go index 180e6dc1..179d9c1d 100644 --- a/hmac_example_test.go +++ b/hmac_example_test.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "time" "github.com/golang-jwt/jwt/v5" ) @@ -23,18 +24,18 @@ func init() { } // Example creating, signing, and encoding a JWT token using the HMAC signing method -func ExampleNew_hmac() { - // Create a new token object, specifying signing method - token := jwt.New(jwt.SigningMethodHS256) - - // Optionally, add claims to the token object - token.Claims = jwt.MapClaims{"aud": "someone"} - +func ExampleNewWithClaims_hmac() { + // Create a new token object, specifying signing method and the claims + // you would like it to contain. + token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ + "foo": "bar", + "nbf": time.Date(2015, 10, 10, 12, 0, 0, 0, time.UTC).Unix(), + }) // Sign and get the complete encoded token as a string using the secret tokenString, err := token.SignedString(hmacSampleSecret) fmt.Println(tokenString, err) - // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJzb21lb25lIn0.F0J37fS_ENgyQg9nxIE0fMS-DX3A11SMh4bvZ7MzD4M + // Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJuYmYiOjE0NDQ0Nzg0MDB9.u1riaD1rW97opCoAuRCTy4w58Br-Zk-bh7vLiRIsrpU } // Example parsing and validating a token using the HMAC signing method