我开发了以下方法,它应该启用基于令牌的身份验证 (jwt)。应该使用异步过程来生成令牌。
源代码似乎适用于并包括签名令牌的生成。使用 查询令牌时遇到问题ParseWithClaims
。有人可以帮忙吗?
package controllers
import (
"crypto/rand"
rsaKeys "crypto/rsa"
"fmt"
jwtgo "github.com/dgrijalva/jwt-go"
"github.com/gofiber/fiber"
)
func Login(c *fiber.Ctx) error {
type TestClaims struct {
HAPP string `json:"happ"`
jwtgo.StandardClaims
}
currentPrivateKey, err := rsaKeys.GenerateKey(rand.Reader, 512)
claims := TestClaims{
"owa",
jwtgo.StandardClaims{
Issuer: "test",
ExpiresAt: 15000,
},
}
token := jwtgo.NewWithClaims(jwtgo.SigningMethodRS256, claims)
tokenSigned, err := token.SignedString(currentPrivateKey)
if err != nil {
fmt.Printf("Failed to sign in account %v", err)
}
//Issue is in this statement
_, errTest := jwtgo.ParseWithClaims(tokenSigned, &TestClaims{"owa", jwtgo.StandardClaims{}}, func(token *jwtgo.Token) (interface{}, error) {
return currentPrivateKey, nil
})
if errTest != nil {
fmt.Printf("Error Message: %v", errTest) //Does throw error: key is of invalid type
}
return c.JSON(fiber.Map{
"message": "success",
})
}