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

key is of invalid type: Still getting this error in 2019

Open fahdjamy opened this issue 6 years ago • 3 comments

It is 2019 December and this issue is still existing.

import ( "errors" "os" "time"

"github.com/dgrijalva/jwt-go"
"github.com/jinzhu/gorm"

)

`// CreateRefreshAndAccessToken creates a user access token and a jwt token func (u *User) GenerateTokens(user *models.User) (map[string]string, error) { var err error apiKey := os.Getenv("API_SECRET")

// Set claims
// This is the information which frontend can use
// The backend can also decode the token and get admin etc.
claims := jwt.MapClaims{}
claims["authorized"] = true
claims["user_id"] = user.ID
claims["isAdmin"] = user.IsAdmin
claims["isClient"] = user.IsClient
claims["isCustomer"] = user.IsCustomer
claims["email"] = user.Email
claims["exp"] = time.Now().Add(time.Hour * 1).Unix() //Token expires after 1 hour
token := jwt.NewWithClaims(jwt.SigningMethodES256, claims)

// create access_token
accessToken, err := token.SignedString([]byte(apiKey))

if err != nil {
	return nil, err
}

rfClaims := jwt.MapClaims{}
rfClaims["sub"] = 1
rfClaims["exp"] = time.Now().Add(time.Hour * 24).Unix()
// generate refresh token
rfToken := jwt.NewWithClaims(jwt.SigningMethodES256, rfClaims)

refreshToken, err := rfToken.SignedString([]byte(apiKey))
if err != nil {
	return nil, err
}

return map[string]string{"access_token": accessToken, "refresh_token": refreshToken}, nil

}`

I am still getting this and it is not descriptive enough.

I have tried this issue but the is no working solution. link

fahdjamy avatar Dec 21 '19 18:12 fahdjamy

I have this problem too.

XiaoLiu1020 avatar Jan 04 '20 06:01 XiaoLiu1020

Version 4 adds more detailed error messages as well as better documentation.

In the meantime, see the documentation for the signing method you're using. In your case: https://godoc.org/github.com/dgrijalva/jwt-go#SigningMethodECDSA

Expects *ecdsa.PrivateKey for signing and *ecdsa.PublicKey for verification

dgrijalva avatar Jan 07 '20 20:01 dgrijalva

No,No,No,Still getting this error in 2020~, this link working for me

timestee avatar Apr 25 '20 06:04 timestee