jwt-go icon indicating copy to clipboard operation
jwt-go copied to clipboard

looking for example of

Open ORESoftware opened this issue 4 years ago • 2 comments

I see this in the docs:

// 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)

I see no reference to hmacSampleSecret I have no idea how to generate that? can it be documented? ty

the docs for this are: https://godoc.org/github.com/dgrijalva/jwt-go#example-New--Hmac

ORESoftware avatar Mar 11 '20 20:03 ORESoftware

hmacSampleSecret is the secret you need to set. example: hmacSampleSecret := []byte("the secret key for you") or var hmacSampleSecret []byte if keyData, e := ioutil.ReadFile("test/hmacTestKeyFile"); e == nil { hmacSampleSecret = keyData }

ygj6 avatar Jul 15 '20 02:07 ygj6

For HS256 the secret is a bytes array of your choosing. As @ygj6 says, just pick one and use it as input to SignedString

friend0 avatar Jan 13 '21 22:01 friend0