如何解决如何用x5c解析一组JWK并验证JWT?
我想验证一个 JSON 网络令牌。用于验证的 JSON Web Key 在 this url 下可用。这些是带有 x509 证书 (x5c) 的 JWK。基于 answer to another question,尝试了以下操作:
import "github.com/dgrijalva/jwt-go"
import "github.com/lestrrat-go/jwx/jwk"
func verifyToken(tokenBytes []byte) {
token,err := jwt.Parse(string(tokenBytes),getKey)
if err != nil {
panic(err)
}
}
func getKey(token *jwt.Token) (interface{},error) {
set,err := jwk.Fetch(context.Background(),"https://shareduks.uks.attest.azure.net/certs")
if err != nil {
return nil,err
}
keyID,ok := token.Header["kid"].(string)
if !ok {
return nil,err
}
key,ok := set.LookupKeyID(keyID)
if !ok {
return nil,errors.New("could not find key with kid")
}
return key,nil
}
但我收到以下错误
panic: failed to parse JWK set: failed to unmarshal JWK set: failed to unmarshal key #1 (total 5) from multi-key JWK set: failed to unmarshal JSON into key (*jwk.rsaPublicKey): required field e is missing
我找不到使用 x5c 的示例。解决方案不必使用我在示例中使用的库。谢谢!
解决方法
该错误 (required field e is missing
) 的原因是来自 this url 的 JWK 集无效。即使 JWK 包含 x5c
,它仍然必须包含该特定 kty
的另一个 required public key members,对于该 URL 中列出的 RSA 密钥,这意味着具有 n
和 {{1 }}。
此处http://github.com/lestrrat-go/jwx的作者。
我还没有合并解析证书的能力,等待问题报告者的回应,但代码已经写好了https://github.com/lestrrat-go/jwx/compare/topic/issue-350
一旦发生变化,就可以执行一些手臂扭转并解析这些证书(伪代码):
data := ... read from that URL ...
rawSet := make(map[string]interface{})
if err := json.Unmarshal(data,&rawSet); err != nil {
...
}
// yikes
keys := rawset["keys"].([]interface{})
firstKey := keys[0].(map[string]interface{})
x5c := (firstKey["x5c"].([]interface{}))[0].(string)
// Decode from base64
cert,_ := base64.RawStdEncoding.DecodeString(x5c)
// turn the certificate into JWK (NOT YET MERGED)
key,_ := jwk.ParseKey(cert,jwk.WithPEM(true))
如果您需要能够将证书解析为 JWK,请在存储库中提交一个新问题,以便我可以跟踪更改。
此外,如果您要导入 http://github.com/lestrrat-go/jwx/jwk,您不妨将 http://github.com/lestrrat-go/jwx/jwt 用于 JWT ;)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。