我使用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.。
我该怎么做呢?
也很高兴得到一个使用不同库的答案
发布于 2022-01-12 08:58:49
看起来,您与服务器的交互方式更像是一个问题(上面的example.com)。您应该检查它的API文档。
通常,令牌作为标题发送,如下所示:
headers = {
# ...
"Authorization": "Bearer {signed_jwt}"
}https://stackoverflow.com/questions/70675306
复制相似问题