【问题标题】:How to create a signed JWT token using python如何使用 python 创建签名的 JWT 令牌
【发布时间】:2022-01-12 22:19:56
【问题描述】:

我正在使用jwcrypto 库来创建签名的 JWT。要求是生成由 RSA 密钥的私有组件签名的 JWT。我采取了以下步骤

创建 JWK 密钥对

from jwcrypto import jwk,jwt
key = jwk.JWK.generate(
            kty='RSA', 
            size=2048, 
            kid='test',
            use='sig',
            e='AQAB',
            alg='RS256'
        )

private_key = key.export_private()
public_key = key.export_public(as_dict=True)

然后我将公钥发送到服务器并像这样创建签名的 JWT,可能做错了:

from datetime import datetime as dt

jwt_header = {
    'alg':'RS256',
    'kid':'test',
}

jwt_claims = {
    'iss':'767676',
    'sub':'test',
    'aud':'https://example.com',
    'token.aud': 'https://example.com',
    'iat':int(dt.now().timestamp()),
    'exp':int(dt.now().timestamp())+600
    
}

jwt_token = jwt.JWT(
        header = jwt_header,
        claims = jwt_claims,
    )
jwt_token.make_signed_token(key)
signed_jwt = jwt_token.serialize()

向服务器发送 JWT:

headers = {
    'Accept-Encoding':'gzip,deflate',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Host': 'test.example.com',
    'Connection': 'Keep-Alive',
    'User-Agent': 'TestApp/1.0.0'
    }

params = {
    'grant_type':'urn:ietf:params:oauth:grant-type:jwt-bearer',
    'assertion':signed_jwt,
    'client_id':'123456'
}

r = requests.post("https:example.com",headers=headers,params=params)

auth_data = r.json()

当我将签名的signed_jwt 传递给服务器时,我收到错误'Invalid Grant Type. Only jwt-bearer supported.

我怎样才能让它工作?

对于使用不同库的答案也很高兴

【问题讨论】:

  • 请发布您如何将令牌发送到服务器?包括请求标头。
  • @ToreNestenius 当然,我已经更新了
  • 您将它发布到什么样的服务器以及您想要实现什么? API 将不接受使用您自己的密钥签名的 JWT 令牌。 API 需要使用它信任的密钥对其进行签名。
  • @ToreNestenius 不确定是哪种服务器,它只是一个端点,应该使用我在第一步中发送的公钥对我进行身份验证,然后它返回一个访问令牌以访问另一个服务。我只是不确定我是否使用私钥正确签署了 JWT?
  • 在一个正常的用例中,您通过授权服务器进行身份验证,它为您颁发令牌,然后您将令牌发送到 API,然后 API 向身份验证服务器询问公钥以验证令牌签名。很少有人会接受你自己签名的令牌。

标签: python jwt jwk jwcrypto


【解决方案1】:

您与服务器的交互方式看起来更像是一个问题(上面的example.com)。你应该查看它的 API 文档。

通常,令牌作为标头发送,如下所示:

headers = {
    # ...
    "Authorization": "Bearer {signed_jwt}"
}

【讨论】:

  • 那么我签署 JWT 的方式正确吗?文档说 jwt 应该在请求正文中
  • The authentication service is a standard OAuth 2.0 token end-point, or authorisation service, which is configured to generate an OAuth2.0 JWT Bearer Token authorisation grant type. The authorisation service is an implementation of the OAuth Assertion Framework. In order for an app to be able to use the service the following specific parameter values must be provided in a service requests body: The value of the ‘grant_type’ parameter must be ‘urn:ietf:params:oauth:grant-type:jwt-bearer’. The value of ‘assertion’ must contain a single JWT signed by the software instance’s private key.
  • 本文也确认jwt作为参数发送ldapwiki.com/wiki/…
  • 如果令牌的目标是 API ,则由 API 定义如何将令牌传递给它。通常,与令牌一起使用的 API 使用身份验证承载标头,如此处所述 swagger.io/docs/specification/authentication/… 但您想对令牌做什么?看来您尝试将其传递回授权服务器?如果是,为什么?
  • @ToreNestenius 抱歉,我不清楚。有两个步骤,首先我将签名的 jwt 传递给验证我是谁的服务器。然后服务器向我发送一些访问令牌。第二步,然后我使用此访问令牌访问我想要使用的实际 API。是的,API 实际上定义了如何将访问令牌传递给它,并且它通过标题作为答案所说的。我只是停留在第一步
猜你喜欢
  • 2016-12-25
  • 2021-11-06
  • 1970-01-01
  • 2019-12-12
  • 2019-11-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
  • 2016-12-08
相关资源
最近更新 更多