0

我使用该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.

我怎样才能得到这个工作?

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

4

1 回答 1

0

看起来更多的是与服务器交互的问题(example.com上面)。您应该查看其 API 文档。

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

headers = {
    # ...
    "Authorization": "Bearer {signed_jwt}"
}
于 2022-01-12T08:58:49.283 回答