【问题标题】:Which Python JOSE library supports nested JWT (signed+encrypted)?哪个 Python JOSE 库支持嵌套 JWT(签名+加密)?
【发布时间】:2016-09-14 08:28:55
【问题描述】:

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

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

【问题讨论】:

  • 嵌套 JWT 是什么意思?您是否只是希望加密 JWE 的有效负载是签名的 JWT?
  • 他们只是签署了一个 JWT,然后在加密的 JWE 的有效负载中使用它。它不是 JOSE 规范的一部分,所以你需要自己做。

标签: python jwt python-jose jose


【解决方案1】:

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

【讨论】:

  • 如何从现有字符串创建 JWK(用于 AES 128 GCM 加密以获取 JWE 的 128 位字符串)?
猜你喜欢
  • 2016-02-27
  • 1970-01-01
  • 2018-10-17
  • 2016-09-16
  • 2021-10-02
  • 2019-05-09
  • 2018-03-15
  • 2020-09-04
  • 2017-05-11
相关资源
最近更新 更多