gpt4 book ai didi

go - 无法使用 VALID key 验证有效 token

转载 作者:行者123 更新时间:2023-12-01 20:08:37 24 4
gpt4 key购买 nike

我用谷歌搜索了 2 页,但没有找到任何解决问题的方法。 token 和公钥都是有效的,

这是 jwt 的链接调试器

以及实现

package internal

import (
"fmt"
"log"
"net/http"

"github.com/dgrijalva/jwt-go"
)

const publicKey = `-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA1PlcNpfUNweGd3eSRLuF
Cp0kZpdsO9tzHCkKMxgaU3jvQI9BzWPdljJfxwEedmEWlJ9G/4oCLdB0w5JhYNoR
uOvs02/goDduPWlnr9cwRsGG3hFDbY8oJ4i9APj6jQFCTvP+yfI7e12SV6RDb58z
7qO4g1BJqoc8sXPk/VsFQMnNuTs7qLM1aWl3uZUXqSrCqJY92YerbDdH5J8LFEi7
aTL7opu1GZ8s75SRWpzg5AjdXGseEm5sjGpURSdmPq8hH7u6+AQBZPTy9ozsgX2V
2Hj0X8Q/AAzrh4mFpnVj8mT/Kny7OcG+yjW+Nwqnq2zFeuyQISQgAMzrnn2GClsi
KUET2yRV2+5l5VZOhLDc8mF6vU2L5aSwZ2zoTGEt/su44tMRXQwoBK99yS7R59pg
IV5x2wusPUmz0IS3y0b6PlLUb4BdMhBLBN1Wy+hIuN1HCptOeKrqvaCrA3ABP/9O
/xJUK8xJDPZS+YW47uJqxk1Hon4ql9dEjOb8Q1onuhC1BRKbWcx3qTOEGbxaT0hT
gX2cTZ8dsFB3iJkDAS0/Xx6nO+a/xgXsWluUkvmrfbYlVgcz3inDWRQ3hSGjvnHK
DJZQju2Vi+v/F9joKUKb8AYnkE1vkRoAgYQ84rU+VpEk+iVL7xtVP63noMUyqtha
aPMC597mD4QGFw7wcXcxCx0CAwEAAQ==
-----END PUBLIC KEY-----`

const testToken = `eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlYzI4ZWEwMzgyMjI3ZDM4MzA4ZWI1ZiIsImlhdCI6MTU5MDY5NDM2MCwiZXhwIjoxNTkwNzgwNzYwfQ.nkP8Ot8bdLy_HwJ05IEMcZY5rpkMGbnMjMRtNrNKhm9UX3yMCWra3mwqnWQSXLoqSucxm3ngF4bsuegxVxczdzFj34cjeAtJZ21YFlNYq6y7FoEsTB3HZ4SXkbLeiCireVVzj1-ynbE0u97wELtjZ8mhkD29CesxRUNtaMP39cZk_nPz62oQQVnUYTE7CrN13mmlkWHC0hGxk_qNe-x9XvGWbVdJlmguuA1IrS3X6xNCk17igcWD97B3yAWSMAryXtJMuMGIX8lC0nGKIUhg-eumeziSB1j12LIAvVlY3bd6zRHsTUJjr8dQpP6T-kZk1zUAZRcl9CFX0a8XWoXjNsK6-NngovvnxK2eM-_QYPtUjMBt0bN89hnYycfnvT8ItdUQwkEpBzqMyeU2Bc1YslJZE9oz0MJ3PZ5iuOV4yKULyOmVJyESs_ViOUzCrzPbfvPXMVmdpabn3j8FHS6X8ixoixjqtNHv1WItN8BMsm377cdB_QyxKLHgbrd_DfiCXKxC01szttPgF5B9-8mxvv7VpZuIpQ7-KnStrJgfRVKaD5dPIHoLRHKRgO-fZ0tLDB4D5hdj1zs32JUZUQ-uQew0p3lXpYmWku67n8U_WVXUD8YOPTD6GVMdiAB8cZl1hOMAs1tjicIhnE7VLwK1ogOAJVq6OTpuVmmD5ALRxyI`


func VerifyToken() bool {

claims := jwt.MapClaims{}

token, err := jwt.ParseWithClaims(testToken, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(publicKey), nil
})
log.Printf("%v", err)
log.Printf("%v", token.Valid)
for key, val := range claims {
fmt.Printf("Key: %v, value: %v\n", key, val)
}

return true
}


无论如何验证都失败了。但是,它确实适用于 Node.JS 库以及上述 jwt 调试器。

最佳答案

您正在尝试使用 PEM 编码的 key 验证 token 。您必须解码 key 并对其进行解析以进行验证。验证需要一个 key 对象,具体取决于签名算法:

token, err := jwt.ParseWithClaims(testToken, claims, func(token *jwt.Token) (interface{}, error) {
block, _ := pem.Decode([]byte(publicKey))
if block==nil {
// return error
}

return x509.ParsePKIXPublicKey(block.Bytes)
})

关于go - 无法使用 VALID key 验证有效 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62073850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com