亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用x5c解析一組JWK并驗證JWT?

如何使用x5c解析一組JWK并驗證JWT?

Go
Helenr 2022-08-24 10:20:30
我想驗證 JSON Web 令牌。用于驗證的 JSON Web 密鑰可在此 URL 下使用。這些是具有 x509 證書 (x5c) 的 JWK。根據另一個問題的答案,嘗試了以下操作: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的示例。解決方案不必使用我在示例中使用的庫。謝謝!
查看完整描述

2 回答

?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

該錯誤 () 的原因是此 URL 中的 JWK 集無效。即使 JWK 包含它,它仍然必須包含該特定所需的其他公鑰成員,對于該 URL 中列出的 RSA 密鑰,這意味著具有 和 。required field e is missingx5cktyne



查看完整回答
反對 回復 2022-08-24
?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

作者 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,則不妨對 JWT 使用 http://github.com/lestrrat-go/jwx/jwt;)


查看完整回答
反對 回復 2022-08-24
  • 2 回答
  • 0 關注
  • 294 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號