python - 哪个 Python JOSE 库支持嵌套 JWT(签名+加密)?

标签 python jwt python-jose jose

我查看了 python-jose 和 jose,但似乎都不支持加密已签名的 JWT。例如,“jose”库支持单独签名和加密,无需嵌套。

我是否遗漏了什么,比如在库外嵌套 JWT 可能相当容易?如果是这样,请分享实现此目的的技巧,以便结果的格式正确。

最佳答案

jwcrypto支持嵌套的 JWS 和 JWE。

签名然后加密:

# Load your RSA pub and private keys
pubKey = jwk.JWK().from_pyca(serializedPublicKey)
privateKey = jwk.JWK().from_pyca(serializedPrivateKey)

# your JWT claims go here
claims = {
    # JWT claims in JSON format
          }
# sign the JWT
# specify algorithm needed for JWS
header = {
          u'alg' : 'RS256', 
          'customSigHeader':'customHeaderContent'
          }
# generate JWT
T = jwt.JWT(header, claims)
# sign the JWT with a private key
T.make_signed_token(privateKey)
# serialize it
signed_token = T.serialize(compact=True)

# JWE algorithm in the header
eprot = {
    'alg': "RSA-OAEP", 
    'enc': "A128CBC-HS256",
     'customEncHeader':'customHeaderContent'
     }   
E = jwe.JWE(signed_token, json_encode(eprot))
# encrypt with a public key
E.add_recipient(pubKey)#
# serialize it
encrypted_signed_token = E.serialize(compact=True)

解密和验证签名:

#Decrypt and Verify signature
E = jwe.JWE()
# deserialize and decrypt
E.deserialize(encrypted_signed_token, key=privateKey)
raw_payload = E.payload
# verify signature
S = jws.JWS()
S.deserialize(raw_payload, key=pubKey)
final_payload = S.payload

关于python - 哪个 Python JOSE 库支持嵌套 JWT(签名+加密)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289672/

相关文章:

python - 使用 pyJWT 和 Python 解码 Apple 的 id_token(登录)

node.js - 为什么我的 jwt token 在背面而不是正面返回 null

python - lambda 函数中的 python 和 python-jose 错误

python - 设置 AnchoredOffsetbox 的线宽和面颜色?

python - 如何在 Selenium Python 中编写功能/集成测试

Azure Active Directory RBAC 未返回不记名 token 中的角色

python - CryptographyDeprecationWarning : int_from_bytes is deprecated, 改为使用 int.from_bytes

python - 当我尝试对带有掩码的图像进行 bitwise_and 时,python 出现 OpenCV 错误

python - 如何使用 pandas.date_range() 获取在指定开始日期和结束日期之间具有 n 个指定时间段(相等)的时间序列