【发布时间】:2022-01-01 19:10:24
【问题描述】:
我正在尝试使用 python 中的公钥验证 idToken。
我首先将 JWK 令牌转换为 PEM,但是当我调用“解码”函数时,我看到“签名验证失败”异常。我错过了什么?
# Long string goes here - this is the token to verify
myToken = 'ezFraWQiXXX.YYYYYYYY.ZZZZZZZZ'
# JWK Token
webkey = {
"alg": "RS256",
"e": "AQAB",
"kid": "d9FzOfniXuHf2sF3opIKZb0sW8Nuaa0d5d+AXXXXXXXX=",
"kty": "RSA",
"n": "nQwBvRlZKdXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX4HcenyO_WASyjr6korLEHxh8XXXXXXXXXXXX",
"use": "sig"
}
# Converting JWK to PEM
public_key = jwt.algorithms.RSAAlgorithm.from_jwk(webkey)
pubk_bytes = public_key.public_bytes(encoding=serialization.Encoding.PEM,format=serialization.PublicFormat.SubjectPublicKeyInfo)
# This is where I get the "signature verification failed" exception
claim = jwt.decode(myToken, pubk_bytes, algorithms=['RS256']) # <<-- ideally this should decode the token for me
【问题讨论】:
-
我无法用发布的代码和我自己的数据重现问题。发布 test 数据:JWT 和 JWK。也许密钥不匹配,所以也将私钥发布到公钥。
标签: python oauth-2.0 jwt rsa jwk