I would need your help using p12 certificate to authenticate my get request to remote server. Below I am attaching 2 codes one is working only in miniconda3 environment (not useful for me as later on I will need to deploy code on the server for the customer without miniconda, so I am developing in standard venv environment using python 3.10.1.
Working one in miniconda3 environment returning expected data response in JSON format without any error:
import uuid
import ssl
from OpenSSL import crypto
from cryptography.hazmat.primitives.serialization import pkcs12
import socket
import json
#Unique identifier of the request
id = str(uuid.uuid4())
print(id)
#Open file to save response
with open('folder/response.json', 'w') as f:
f.write("")
#Save unique id of the request for audit purpose
with open('folder/apitest_request_id.txt', 'a') as f:
f.write(id + '\n')
#
with open('folder/certificate.pem', 'rb') as f:
cert = f.read()
with open('folder/private_key.pem', 'rb') as f:
key = f.read()
#Connection
conn = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
conn.load_cert_chain(certfile='folder/certificate.pem', keyfile='folder/private_key.pem')
#Headers
headers = {
'accept': "application/json",
'X-Client-Id': "mysecret123456id",
'X-Request-Id': id
}
#Get request.
with conn.wrap_socket(socket.socket(), server_hostname='example.com') as ssock:
ssock.connect(('example.com', 443))
ssock.sendall('GET //ex/service/mock/accounts HTTP/1.1\r\n'.encode('utf-8'))
ssock.sendall('Host: example.com\r\n'.encode('utf-8'))
ssock.sendall('Accept: application/json\r\n'.encode('utf-8'))
ssock.sendall(('X-Client-Id: mysecret123456id\r\n').encode('utf-8'))
ssock.sendall(('X-Request-Id: {}\r\n'.format(id)).encode('utf-8'))
ssock.sendall('\r\n'.encode('utf-8'))
#processing response
data = ssock.recv(1024)
while data:
print(data.decode("utf-8"))
with open('folder/response.json', 'a') as f:
f.write(data.decode("utf-8"))
data = ssock.recv(1024)
#parse JSON from line 36.
with open("folder/response.json") as f:
lines = f.readlines()
json_str = "".join(lines[36:])
data = json.loads(json_str)
print(data)
*Now high level request using requests_pkcs12 python library (Why? I am trying to find different and more straightforward solution not dependent on miniconda3 environment as the solution need to be deployed on the webservice server later so it's not worth for me to stick to the solution that I can not easily replicate.)*
from requests_pkcs12 import get
headers = {
'accept': "application/json",
'X-Client-Id': "mysecret123456id",
'X-Request-Id': "8d156c92-4f35-4a0d-9a45-138bbbe5c6f10"
}
r = get('https://example.com:443/ex/service/mock/accounts, headers=headers, verify=False, pkcs12_filename='folder/cert_testing.p12', pkcs12_password='password')
print(r.text)
*Returned error: *
InsecureRequestWarning: Unverified HTTPS request is being made to host 'example.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html>