签名以请求 jwt 在 python3 中的 API 上使用
Signing for requesting a jwt to use on an API in python3
我尝试生成一个用于连接到 transip api 的 jwt 令牌,但无法弄清楚如何生成作为签名来请求一个。我更愿意在 Python3 中进行。我应该怎么做?
Api documentation of transip 并查看身份验证部分
我确实写了一些失败的代码:
#!/usr/bin/env python3
import OpenSSL
key='''-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDIYjaYtIh8EDSu
wmVxRaXm6gtA9cRGds2juYvO+za8oQ+36OS4J35Hig/XE0Zr1hrfjGfnuy3bubrj
zB/w3Hg4Kl63oBel/2HSmaNczR12dMGZzMqyHHXyZKl/cYjzhbgAucSM/q+inbuL
1gK0O9+Ov2Uc8iMfeRBG/XBNo5QVVzWYF+BSmojobawGMIR/LKKKJL6HNzdcz5yr
PAWR0HfukpwuZcehEJca/TLgUf/FoBfRKm+tFVyVNYB6v2kvI0ASMMhsnsEnQ0CX
0mi9cmyXB6iB/5SnwKkXZIwPXwmyXvSwIdZiTU70QNlfGKrIGuv5+fDNd1+g7XDV
yjzS9HObAgMBAAECggEAIDSQu8hAK1hbbz71GNhtyogRGPam/gA4GrlGfBSp/nUQ
VqmKoQJP7GWHGDUT218E4KrrRDY0L51RKS10cLyuYbCFmpOTWW2LJlLlC5Q3yQMI
3pQoe6nRVwzQpRf1P9Zc9Vjl+xcv2T3ql5Xkx1zcGFAwiw9rj7JgaFhxRTkmIquh
oyHkMHW74hSVlx55HhDtc1uTK3aWnEhKJfj/dRmA5Hald3ixoaASsZWA3ZTr7B8R
b/2mAGml+/dEYuspbkIZW9Bryv7UlpT2E9/7SpV8c05Spehl12EfMcJhaFJDe1G4
kkCqbl8Uks+jm9/aWSUIUzcHuZDw1PAiVqPVVcyfkQKBgQDtHp89U1QZBuAXI3KM
FOeOFUD+Vb4oLhTyWVsWE6jJjxfGVyGtIRNw6rHrHumlN8Fpb4ijEdvoNQkPiKnl
HYGnUp7BkbvpgncjL6b4br3b9cXtcpDHdTD5dAir+j3sDrVvAG6YXnuyZONOXxXm
oVp/sd+DBArBQFqHgQICOxgNowKBgQDYVsaZEoa22tuhrVfo5UxoRq+hAnMN5cTv
iGof1Qt2t2BYVEjpz2YpVQ/ksCHbtSVxRzw00qW4xzHNzxJ4JkDAlerxQy+nqBCp
emWMADs2H/ALTvCFaEA+rGLe+MYf0ANaw94AbR44jKNcGqMrwwVQpC9CY3/C0A9L
pg1ssZ/xqQKBgFZrG6QRE4xPeipUq/GryLx6uIY5H6WrLc0pjc3c+l4DPan2pXpg
nKJBlvhW+tZRHLddg9HSt2/IrHWx3CF5gIBH1z46695twxfazSKr0Zwx1aH1aBiZ
eHDhvitXd2vp7Gv5H1V+0dwxcrpkYyn70mzJmek49uZ5msTZ2q6PdPO7AoGAapi3
Wo1KW6cTOWLUQilZsLfDqi4uytZAZ1ZsFCtBbsmEa4F8O9i5mfwTzLcMt9lWDa7v
94cjqRxdae9yRkly9nHoReC5Bn9FVny8tHMYud6axLesw89OeJMwVHV4CgzQ2lRQ
ex1JGswRYjyt0c5SPB3qO2gTd8ZVAw1a6AfNq6ECgYB/tndONcYppD/j3278+3Wy
EV/P0CEBVrvXboUyxkFykZ5CFYkpNEisQ4Imu9PUGCOqSxxOoXQ2TyyzIb9nY1g1
K2Xs9J9sOEoGmmWJ+RJkHiwNkAIAbuYaluqtRyqY0w2z19JZKRv1u9vuWP1+f5G6
+5aF0x9Xgt2Tvq3T1caywg==
-----END PRIVATE KEY-----'''
data=b'{"login": "test-user","nonce": "98475920834"}'
signature=OpenSSL.crypto.sign(key,data,'sha512')
print (signature)
我得到的错误是
Traceback (most recent call last):
File "./jwt.py", line 37, in <module>
signatue=OpenSSL.crypto.sign(key,data,'sha512')
File "/usr/local/lib/python3.7/dist-packages/OpenSSL/crypto.py", line 2825, in sign
length = _lib.EVP_PKEY_size(pkey._pkey)
AttributeError: 'bytes' object has no attribute '_pkey'
这是怎么回事???
(p.s。密钥不再有效,但仍可用于创建签名)
我确实找到了自己的解决方案。
#!/usr/bin/env python3
from OpenSSL import crypto
import base64
key_file = open("test.pem", "r")
key = key_file.read()
key_file.close()
print(key)
print()
if key.startswith('-----BEGIN '):
pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key)
else:
pkey = crypto.load_pkcs12(key).get_privatekey()
print (pkey)
print()
data = b'''{ "login": "usernam", "nonce": "98475920834" }'''
sign = crypto.sign(pkey, data, "sha512")
print (sign)
print()
data_base64 = base64.b64encode(sign)
print (data_base64.decode())
这对我有用。 :-)
我尝试生成一个用于连接到 transip api 的 jwt 令牌,但无法弄清楚如何生成作为签名来请求一个。我更愿意在 Python3 中进行。我应该怎么做?
Api documentation of transip 并查看身份验证部分
我确实写了一些失败的代码:
#!/usr/bin/env python3
import OpenSSL
key='''-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDIYjaYtIh8EDSu
wmVxRaXm6gtA9cRGds2juYvO+za8oQ+36OS4J35Hig/XE0Zr1hrfjGfnuy3bubrj
zB/w3Hg4Kl63oBel/2HSmaNczR12dMGZzMqyHHXyZKl/cYjzhbgAucSM/q+inbuL
1gK0O9+Ov2Uc8iMfeRBG/XBNo5QVVzWYF+BSmojobawGMIR/LKKKJL6HNzdcz5yr
PAWR0HfukpwuZcehEJca/TLgUf/FoBfRKm+tFVyVNYB6v2kvI0ASMMhsnsEnQ0CX
0mi9cmyXB6iB/5SnwKkXZIwPXwmyXvSwIdZiTU70QNlfGKrIGuv5+fDNd1+g7XDV
yjzS9HObAgMBAAECggEAIDSQu8hAK1hbbz71GNhtyogRGPam/gA4GrlGfBSp/nUQ
VqmKoQJP7GWHGDUT218E4KrrRDY0L51RKS10cLyuYbCFmpOTWW2LJlLlC5Q3yQMI
3pQoe6nRVwzQpRf1P9Zc9Vjl+xcv2T3ql5Xkx1zcGFAwiw9rj7JgaFhxRTkmIquh
oyHkMHW74hSVlx55HhDtc1uTK3aWnEhKJf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nonce": "98475920834"}'
signature=OpenSSL.crypto.sign(key,data,'sha512')
print (signature)
我得到的错误是
Traceback (most recent call last):
File "./jwt.py", line 37, in <module>
signatue=OpenSSL.crypto.sign(key,data,'sha512')
File "/usr/local/lib/python3.7/dist-packages/OpenSSL/crypto.py", line 2825, in sign
length = _lib.EVP_PKEY_size(pkey._pkey)
AttributeError: 'bytes' object has no attribute '_pkey'
这是怎么回事???
(p.s。密钥不再有效,但仍可用于创建签名)
我确实找到了自己的解决方案。
#!/usr/bin/env python3
from OpenSSL import crypto
import base64
key_file = open("test.pem", "r")
key = key_file.read()
key_file.close()
print(key)
print()
if key.startswith('-----BEGIN '):
pkey = crypto.load_privatekey(crypto.FILETYPE_PEM, key)
else:
pkey = crypto.load_pkcs12(key).get_privatekey()
print (pkey)
print()
data = b'''{ "login": "usernam", "nonce": "98475920834" }'''
sign = crypto.sign(pkey, data, "sha512")
print (sign)
print()
data_base64 = base64.b64encode(sign)
print (data_base64.decode())
这对我有用。 :-)