Last active
December 12, 2017 02:45
-
-
Save jordanst3wart/522bc53016fc1b85f8c8cfb7f2a952ad to your computer and use it in GitHub Desktop.
Hello world
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import contextlib | |
import OpenSSL.crypto | |
import os | |
import requests | |
import ssl | |
import tempfile | |
# mostly from another gist | |
@contextlib.contextmanager | |
def pfx_to_pem(pfx_path, pfx_password): | |
''' Decrypts the .pfx file to be used with requests. ''' | |
with tempfile.NamedTemporaryFile(suffix='.pem') as t_pem: | |
f_pem = open(t_pem.name, 'wb') | |
pfx = open(pfx_path, 'rb').read() | |
p12 = OpenSSL.crypto.load_pkcs12(pfx, pfx_password) | |
f_pem.write(OpenSSL.crypto.dump_privatekey(OpenSSL.crypto.FILETYPE_PEM, p12.get_privatekey())) | |
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, p12.get_certificate())) | |
ca = p12.get_ca_certificates() | |
if ca is not None: | |
for cert in ca: | |
f_pem.write(OpenSSL.crypto.dump_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)) | |
f_pem.close() | |
yield t_pem.name | |
# could be environment variables | |
p12_file='someFile.p12' | |
password_p12='somePassword' | |
headers = { | |
'X-XSRF-Header': 'hi', | |
'Content-Type': 'application/json' | |
} | |
username='someUser' | |
password='somePassword' | |
host='example.com' | |
scheme='https' | |
port='8080' | |
endpoint='some/endpoint' | |
combined= scheme + '://' + host + ':' + port + '/' + endpoint | |
payload={} | |
with pfx_to_pem(p12_file,password_p12) as cert: | |
resp = requests.post(combined, cert=cert, data=payload, headers=headers, auth=(username, password), verify=False) | |
print(resp.status_code) | |
print(resp.text) | |
# print response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment