Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

jwt_encode is not working #282

Answered by oxisto
yingshaoxo asked this question in Q&A
Discussion options

The token_string returns nothing


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)
}
You must be logged in to vote

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

func ExampleNewWithClaims_registeredClaims() {
mySigningKey := []byte("AllYourBase")
// Create the Claims
claims := &jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Unix(1516239022, 0)),
Issuer: "test",
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := token.SignedString(mySigningKey)
fmt.Printf("%v %v", ss, err)
//Output: eyJhbGc...

Replies: 2 comments

Comment options

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

func ExampleNewWithClaims_registeredClaims() {
mySigningKey := []byte("AllYourBase")
// Create the Claims
claims := &jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Unix(1516239022, 0)),
Issuer: "test",
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
ss, err := token.SignedString(mySigningKey)
fmt.Printf("%v %v", ss, err)
//Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.0XN_1Tpp9FszFOonIBpwha0c_SfnNI22DhTnjMshPg8 <nil>
}

Note, that you are intentionally ignoring the error of SignedString. This error contains more information why it is failing and this case would you provide with an error that you are using the wrong key type for this signing algorithm.

You must be logged in to vote
0 replies
Answer selected by oxisto
Comment options

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
}
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #281 on March 05, 2023 08:18.

AltStyle によって変換されたページ (->オリジナル) /