Python sign SOAP request using BinarySecurityToken

Python sign SOAP request using BinarySecurityToken

Signing a SOAP request using a BinarySecurityToken involves creating a digital signature for the SOAP message using the token's credentials. To achieve this, you typically need to construct the SOAP envelope, generate the necessary XML structures for the security header, and sign the request accordingly. Here's a general outline of how to do it:

  1. Create the SOAP Envelope: Build the SOAP envelope with the required headers, body, and payload.

  2. Generate Security Header with BinarySecurityToken: Construct the security header that includes the BinarySecurityToken element containing your credentials. This is typically done using XML libraries such as xml.etree.ElementTree or third-party libraries like lxml.

  3. Sign the Request: Use a cryptographic library like cryptography to generate the digital signature of the entire SOAP message or specific parts of it. You will typically use the private key corresponding to the public key associated with the BinarySecurityToken.

  4. Include the Signature in the Security Header: Insert the generated digital signature into the security header.

  5. Send the Signed SOAP Request: Finally, send the signed SOAP request to the server.

Here's a simplified example using Python's built-in xml.etree.ElementTree and cryptography libraries:

import xml.etree.ElementTree as ET from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives import hashes from cryptography.hazmat.primitives.asymmetric import padding # Create SOAP envelope soap_envelope = ET.Element("soapenv:Envelope", xmlns="...", soapenv="...") body = ET.SubElement(soap_envelope, "soapenv:Body") # Generate BinarySecurityToken and Security header binary_security_token = ET.Element("wsse:BinarySecurityToken", ValueType="...", EncodingType="...", wsse="...") security = ET.Element("wsse:Security", mustUnderstand="1", soapenv="...") security.append(binary_security_token) soap_envelope.insert(0, security) # Convert private key to a usable format (replace with actual key data) private_key_data = b"..." private_key = serialization.load_pem_private_key(private_key_data, password=None, backend=default_backend()) # Serialize the SOAP request soap_request = ET.tostring(soap_envelope, encoding="unicode") # Sign the request signature = private_key.sign(soap_request.encode(), padding.PKCS1v15(), hashes.SHA256()) # Include the signature in the Security header signed_info = ET.Element("ds:SignedInfo", xmlns:ds="...") reference = ET.Element("ds:Reference", URI="#...", ds="...") digest_value = ET.Element("ds:DigestValue", ds="...") reference.append(digest_value) signed_info.append(reference) security.insert(1, signed_info) # Serialize the updated SOAP request signed_soap_request = ET.tostring(soap_envelope, encoding="unicode") # Now you can send the signed SOAP request to the server print(signed_soap_request) 

Please note that this example is a simplified illustration of the process and might need adjustments to work with your specific SOAP service and authentication requirements. Additionally, the libraries used may vary depending on your project's needs.

Examples

  1. "Python SOAP request with WS-Security and BinarySecurityToken"

    • Using the zeep library to sign a SOAP request with WS-Security and BinarySecurityToken.
    pip install zeep cryptography 
    from zeep import Client from zeep.wsse.signature import BinarySignature from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.hashes import SHA256 # Load a private key for signing with open("private_key.pem", "rb") as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None ) client = Client("http://example.com/service?wsdl") wsse = BinarySignature(private_key, None, SHA256()) # Add the WS-Security configuration client.transport.session.headers.update({ 'BinarySecurityToken': '...' # Your token }) result = client.service.SomeOperation(_soapheaders=[wsse]) print(result) 
  2. "How to sign a SOAP request in Python with WS-Security and RSA key"

    • Signing a SOAP request with RSA and WS-Security using zeep.
    from zeep import Client from zeep.wsse.signature import BinarySignature from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.hashes import SHA256 # Load the RSA private key with open("rsa_private_key.pem", "rb") as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None ) client = Client("http://example.com/soap?wsdl") wsse = BinarySignature(private_key, None, SHA256()) # Configure the client with WS-Security response = client.service.SomeSOAPOperation(_soapheaders=[wsse]) print(response) 
  3. "Python SOAP BinarySecurityToken with certificate"

    • Using a certificate for WS-Security in SOAP.
    from zeep import Client from zeep.wsse.signature import BinarySignature from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.hashes import SHA256 # Load a certificate with open("certificate.pem", "rb") as cert_file: cert_data = cert_file.read() with open("private_key.pem", "rb") as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None ) client = Client("http://example.com/service?wsdl") wsse = BinarySignature(private_key, cert_data, SHA256()) response = client.service.AnotherSOAPOperation(_soapheaders=[wsse]) print(response) 
  4. "Python signing SOAP request with WS-Security and X.509 certificate"

    • Using X.509 certificate to sign a SOAP request.
    from zeep import Client from zeep.wsse.signature import BinarySignature from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.hashes import SHA256 # Load X.509 certificate and private key with open("x509_cert.pem", "rb") as cert_file: cert_data = cert_file.read() with open("private_key.pem", "rb") as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None ) client = Client("http://example.com/service?wsdl") wsse = BinarySignature(private_key, cert_data, SHA256()) response = client.service.OperationWithX509(_soapheaders=[wsse]) print(response) 
  5. "Python SOAP WS-Security: signing with RSA and digesting with SHA256"

    • Applying RSA signatures to SOAP requests and using SHA256 for hashing.
    from zeep import Client from zeep.wsse.signature import BinarySignature from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.hashes import SHA256 # Load RSA private key with open("rsa_key.pem", "rb") as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None ) client = Client("http://example.com/wsdl") wsse = BinarySignature(private_key, None, SHA256()) # Send signed SOAP request result = client.service.DoSomething(_soapheaders=[wsse]) print(result) 
  6. "Python signing SOAP request with BinarySecurityToken and WS-Security"

    • Configuring a SOAP request with a BinarySecurityToken for signing.
    from zeep import Client from zeep.wsse.signature import BinarySignature from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.hashes import SHA256 # Load private key for signing with open("private_key.pem", "rb") as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None ) client = Client("http://example.com/service?wsdl") wsse = BinarySignature(private_key, None, SHA256()) # Prepare SOAP headers with BinarySecurityToken response = client.service.SampleOperation(_soapheaders=[wsse]) print(response) 
  7. "Python signing SOAP request with binary token and WS-Security"

    • Using a binary token for signing SOAP requests.
    from zeep import Client from zeep.wsse.signature import BinarySignature from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.hashes import SHA256 # Load the binary token for signing with open("binary_token.pem", "rb") as token_file: binary_token = token_file.read() with open("private_key.pem", "rb") as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None ) client = Client("http://example.com/wsdl") wsse = BinarySignature(private_key, binary_token, SHA256()) result = client.service.SomeOtherOperation(_soapheaders=[wsse]) print(result) 
  8. "Python BinarySecurityToken SOAP WS-Security with zeep"

    • Implementing SOAP requests with BinarySecurityToken using the zeep library.
    from zeep import Client from zeep.wsse.signature import BinarySignature from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.hashes import SHA256 # Load the binary security token with open("binary_security_token.pem", "rb") as token_file: binary_token = token_file.read() # Load the private key for signing with open("private_key.pem", "rb") as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None ) client = Client("http://example.com/service?wsdl") wsse = BinarySignature(private_key, binary_token, SHA256()) result = client.service.AnotherSOAPOperation(_soapheaders=[wsse]) print(result) 
  9. "Python signing SOAP request with BinarySecurityToken and RSA private key"

    • Using a BinarySecurityToken and RSA private key for signing a SOAP request.
    from zeep import Client from zeep.wsse.signature import BinarySignature from cryptography.hazmat.primitives.asymmetric import rsa from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.hashes import SHA256 # Load RSA private key and binary token with open("rsa_private_key.pem", "rb") as key_file: private_key = serialization.load_pem_private_key( key_file.read(), password=None ) with open("binary_token.pem", "rb") as token_file: binary_token = token_file.read() client = Client("http://example.com/soap?wsdl") wsse = BinarySignature(private_key, binary_token, SHA256()) # Send signed SOAP request result = client.service.SignatureOperation(_soapheaders=[wsse]) print(result) 
  10. "Python SOAP WS-Security with BinarySecurityToken and X.509 certificate"


More Tags

sql-scripts linker-errors jmx hyperlink sms-gateway karma-runner request-headers file-exists webservice-client entitymanager

More Python Questions

More Math Calculators

More Mortgage and Real Estate Calculators

More Entertainment Anecdotes Calculators

More Biochemistry Calculators