Я пытаюсь реализовать скрипт python для входа через наш WebAPI, используя запросы и pycryptodome. Я использую эту функцию шифрования:
import base64
from Crypto.Cipher import PKCS1_v1_5, PKCS1_OAEP
from Crypto.PublicKey import RSA
def encrypt(pubkey: str, data: str):
"""
Main usage: encrypting login-password data
:param pubkey:
:param data:
:return:
"""
rsa_key = RSA.importKey(pubkey)
cipher = PKCS1_v1_5.new(rsa_key)
# I'm also tried this cipher, but no luck either:
# cipher = PKCS1_OAEP.new(rsa_key)
return base64.b64encode(cipher.encrypt(bytes(data, encoding="ascii")))
Вот как я пытаюсь связаться с сервером:
r = s.get(pubkey_endpoint, headers=headers)
assert_that(r.status_code).is_equal_to(200)
key = r.json().get("key")
assert_that(key).is_not_none()
encd_login = encrypt(key, test_users["login"]).decode("utf-8")
encd_pw = encrypt(key, test_users["pw"]).decode("utf-8")
params = {"grant_type": "Password",
"user_type": "customer",
"username": encd_login,
"password": encd_pw}
r = s.post(proto_and_host+"/auth/oauth/token", params)
data = dump.dump_all(r)
И вот что у меня есть:
< POST /auth/oauth/token HTTP/1.1
< Host: some.host.org
< User-Agent: python-requests/2.23.0
< Accept-Encoding: gzip, deflate
< Accept: */*
< Connection: keep-alive
< Cookie: b701e88d391f375c912cf44e1f88dda9=605744d6274314dea50910d8580287a8
< Content-Length: 424
< Content-Type: application/x-www-form-urlencoded
<
< grant_type=Password&user_type=customer&username=GoSYuXF0hKuejdaF%2FjRoLIcq94d8XNCOQ2I1j0qnurShg6EypASbWYebxvB2X3kFaBYmgFylq6Z6lIdZyYShG7yCQERQ7YNysfnooTbr%2F5vRKYdV3EzIPrHCvUChMXyF2ZZoTwrOqFcSrDSuYEWJfCj7JKdY6l6sGLleiCGCRnE%3D&password=dZWBlY3IzxTb2V3ES%2FROyoT7CjZtMhYLZHe40b7lKKkz8a9%2Bg3kyLD6SCOX5jHYJuF4dxo4HTb%2BsQQS2eGAHnBcLgIGTm4EO5aEFwW6O%2BFNE0Mq4bhkoYfNtpE83%2BhETwYdSvnpck14gNA8UMJIgK4Sk6FWdO%2FHyPR%2BobstPCNE%3D
> HTTP/1.1 401 Unauthorized
> Server: nginx
> Date: Fri, 24 Apr 2020 12:20:08 GMT
> Content-Type: application/json
> Transfer-Encoding: chunked
> Connection: keep-alive
> Expires: 0
> Cache-Control: no-cache, no-store, max-age=0, must-revalidate
> X-XSS-Protection: 1; mode=block
> Pragma: no-cache
> X-Frame-Options: DENY
> WWW-Authenticate: Basic realm="oauth2/client"
> X-Content-Type-Options: nosniff
> Strict-Transport-Security: max-age=31536000 ; includeSubDomains
> Set-Cookie: f4f642e55980c82d739473a37ee479b6=605744d6274314dea50910d8580287a8; path=/; HttpOnly
>
{"timestamp":"2020-04-24T12:20:08.425+0000","status":401,"error":"Unauthorized","message":"Unauthorized","path":"//oauth/token"}
Что я делаю не так?
PS: К сожалению, я не могу раскрыть многие детали.