jwt_encode is not working #282
-
The func Jwt_encode(data map[string]interface{}, secret_string string) string {
new_data := jwt.MapClaims{}
for key, value := range data {
new_data[key] = value
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, new_data)
token_string, _ := token.SignedString(secret_string)
return token_string
} package main
import (
"log"
"testing"
"github.com/yingshaoxo/gopython/jwt_tool"
)
func Test_jwt(t *testing.T) {
secret := "no way"
data := make(map[string]interface{})
data["user"] = "yingshaoxo"
jwt_string := jwt_tool.Jwt_encode(data, secret)
log.Println(jwt_string)
new_data, _ := jwt_tool.Jwt_decode(jwt_string, secret)
log.Println(data)
log.Println(new_data)
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You are using Lines 16 to 29 in 0d2f0d4 Note, that you are intentionally ignoring the error of |
Beta Was this translation helpful? Give feedback.
-
Alright, thanks, now I've solved this problem: func Jwt_encode(data map[string]interface{}, secret_string string) string {
new_data := jwt.MapClaims{}
for key, value := range data {
new_data[key] = value
}
secret_string_bytes := string_tool.String_to_bytes(secret_string)
token := jwt.NewWithClaims(jwt.SigningMethodHS256, new_data)
token_string, _ := token.SignedString(secret_string_bytes)
return token_string
} |
Beta Was this translation helpful? Give feedback.
You are using
string
as the key for an HMAC. The signing method expects a[]byte
as seen in the README "The HMAC signing method (HS256,HS384,HS512) expect []byte values for signing and validation" or in the example:jwt/example_test.go
Lines 16 to 29 in 0d2f0d4