键的类型无效

huangapple go评论88阅读模式
英文:

key is of invalid type

问题

背景:我正在尝试使用Go语言重写此指南中的第一步

我有以下内容:

  • 一个JWT令牌(tokenString),使用RS256算法,可以在这里查看。
  • 一个密钥(key),采用SSH RFC-4253格式

我正在尝试使用该密钥验证JWT令牌。以下是我的代码:

import (
	"fmt"
	"github.com/golang-jwt/jwt/v4"
)

tokenString := "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE2MzQ3NzE5OTMsImV4cCI6MTY2NjMwNzk5MywiYXVkIjoiaHR0cHM6Ly9hcGkuYXV0aG9yaXpvbi5jb20vdjEvIiwiaXNzIjoiaHR0cHM6Ly9hdXRob3Jpem9uLmNvbS8iLCJzdWIiOiIzOGQ3ZjMxZTVjMzc0MGM5YjQwMmFlZTI4YzRhYjc3MCIsInBlZXJfdHlwZSI6ImNsaWVudCJ9.CNNNxkhN_xmpiiihrZS2fAJSaDCx9kpBXA9G8adh3BbGOs681381Jl63q9ZNgOXsf64ZLlVEtk6YirhaloZ05ZQIEGklXh98h7EPrvNWyAUwEzSHsoAUmmnuSG7EZn4xo7utNuk4UuFOwf63FwAJN6yU5X9gHwTXlf8JjGXAbFSj-QCmX-g1TfVRPwsGAJqM03thKVc-OiOuevJr04y1DnFx3kXiMeGgRMLpPzhYDgsUkCcyITIl1SIZXY_6ifwbPibqwpPy-r0bWi0hczNBJ_wxa3wElRm0Ib-XxldgpZNI3GD9RcTdzDW5jg5Upuf0ar8BKJDHjoWfGIoD93mRA4opT4gd5IMEU16w-d_UBPu0D96Q002kbq3Ou1_iKaS1APUKnCSfyfZK2o5W_OAYbD0FkkxpqTApRaoPws1a0yGpET6pYmQHUE2Jo-1ZH6uSnPf8kT1lwoyVJzs4ScOcjilApL4JXqV_SOGWA5WiFJG5qcigFlFWRYuVitjyXQTzsxIZnKaYsElf2KUOvXgeqHXGAsGsuYs-5fR7vQOdV02n_QOU0Kr_r13I3TP1OiaL2YSGxiNMqKmWM32KNYTAFYKrWoaz_TYSgrQSv1d96E0VgipxmAucM_-qw_sa1eVeUf8XLMGYl3x1Ssv8YY5eRPOWQ8FgAmAJq5MU1y11Dao"
key := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDqSnARw5EZWAMSW3njVBfi6GAHe0N4rp51Uv2k107p5ZpF3gNPU0GTAQyhRBrwydaoHpeoI9Q2D3/8ornVUF+os6hiOltp885VPYrVSt6aN4vPj3UbuVykOhXoEln9CEbUb0dIfKCsiugHu94as38SE6DtVeRuQlGqpczIHdNkaFVRIx4hzeRmUZ2CyyA+8/kJgODeePP6hfHvoL2s45AC/VoffGEfskn6byC1okrfh3Fnb+ouJJYWgTqrQoQSx45Mz+NBwpBfeucAar02zD5chi/dXkwULBEF2hX9tqySuSqq544bK+ECygROuNTMYMAHPEhki1iroyHmxrer8sW/cEQcaqixB2Vu07ZWJgh2tMekLrjvRBbobervsXaXFDzjWPLY1OGsZqvSxuRSfCiQk645dzL/mXDj484FQGQ6pY6P9Tnc2alDs6yhtGb5vQtW5uFDRyfVpCBdvZjvpRXWEPHTEbKygKIy04QEi+QvZcInm6idBD00oIefJtsLvD+ZGyNM2L29gVP4klJoHZuXcXX1mxZFLMKvSIB9nl0+KRoMhaPaqyInPu8Amlp1+fkQBwaHfaVJFCPbGzrf580t7flQEmtLBX8k+4eCs6g5sTCLXWaVotsTFKspKz0nb7B/f6WEIjDpkZ4Xcgzx9nQwPThoywZi+PfykZjoh4RvcQ== homiao@LCNC02F10MAMD6R"
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
	return []byte(key), nil
})

if err != nil {
	fmt.Printf("%v", err)
}

然而,我遇到了错误:

密钥类型无效

希望能得到帮助或其他可行的库!

英文:

Background: I am trying to rewrite Step 1 in this guide in Go language.

I have

I am trying to use this key to verify the JWT token. Here is my code

import (
	"fmt"
	"github.com/golang-jwt/jwt/v4"
)

tokenString := "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.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OhXoEln9CEbUb0dIfKCsiugHu94as38SE6DtVeRuQlGqpczIHdNkaFVRIx4hzeRmUZ2CyyA+8/kJgODeePP6hfHvoL2s45AC/VoffGEfskn6byC1okrfh3Fnb+ouJJYWgTqrQoQSx45Mz+NBwpBfeucAar02zD5chi/dXkwULBEF2hX9tqySuSqq544bK+ECygROuNTMYMAHPEhki1iroyHmxrer8sW/cEQcaqixB2Vu07ZWJgh2tMekLrjvRBbobervsXaXFDzjWPLY1OGsZqvSxuRSfCiQk645dzL/mXDj484FQGQ6pY6P9Tnc2alDs6yhtGb5vQtW5uFDRyfVpCBdvZjvpRXWEPHTEbKygKIy04QEi+QvZcInm6idBD00oIefJtsLvD+ZGyNM2L29gVP4klJoHZuXcXX1mxZFLMKvSIB9nl0+KRoMhaPaqyInPu8Amlp1+fkQBwaHfaVJFCPbGzrf580t7flQEmtLBX8k+4eCs6g5sTCLXWaVotsTFKspKz0nb7B/f6WEIjDpkZ4Xcgzx9nQwPThoywZi+PfykZjoh4RvcQ== homiao@LCNC02F10MAMD6R"
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
	return []byte(key), nil
})

if err != nil {
	fmt.Printf("%v", err)
}

However, I got error

> key is of invalid type

Any help or other libraries that work would be great!

答案1

得分: 2

我认为你需要在jwt.KeyFunc中返回一个*rsa.PublicKey而不是[]byte。根据你的密钥字符串,这只是一个猜测。

原来的代码:

token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
    return []byte(key), nil
})

尝试修改为:

token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
    return jwt.ParseRSAPublicKeyFromPEM([]byte(key))
})

你也可以根据签名方法使用switch case语句:

token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
    switch token.Method {
    case jwt.SigningMethodHS256.Alg():
        return []byte(key)
    case jwt.SigningMethodRS512.Alg():
        return jwt.ParseRSAPublicKeyFromPEM([]byte(key))

    // 其他签名方法...

    }
    ...
})
英文:

I'm think you'll have to return a *rsa.PublicKey instead of []byte from the jwt.KeyFunc. This is a guess looking at your key string

Instead of:

token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
    return []byte(key), nil
})

try:

token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
    return jwt.ParseRSAPublicKeyFromPEM([]byte(key))
})

you could also use a switch case on the signing method:

token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
    switch token.Method {
    case jwt.SigningMethodHS256.Alg():
        return []byte(key)
    case jwt.SigningMethodRS512.Alg():
        return jwt.ParseRSAPublicKeyFromPEM([]byte(key))

    // and so on...

    }
    ...
})

huangapple
  • 本文由 发表于 2021年10月21日 20:46:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/69662259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定