mvantellingen / python-zeep

A Python SOAP client
http://docs.python-zeep.org
Other
1.89k stars 587 forks source link

How to add SOAP-ENV:mustUnderstand=“1” in <wsse:security> header using Zeep #1248

Open JayantJain720 opened 3 years ago

JayantJain720 commented 3 years ago

I want my XML request to look like this -

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" SOAP-ENV:mustUnderstand="1">

However the output I'm getting from zeep is like this -

<soap-env:Header>
    <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">

How can I include xmlns:wsu and SOAP-ENV:mustUnderstand="1" in the xml generated by zeep?

My code is -


from lxml import etree
from zeep.wsse import BinarySignature
import xmlsec
from zeep.transports import Transport

client = zp.Client('http://locahost:8085/soap-server/gor/request/MasterData.wsdl',wsse=BinarySignature(key_file='private.pem',certfile='cert.pem', digest_method=xmlsec.constants.TransformSha256))

transport  = Transport()
factory = client.type_factory('ns0')

# product_type = client.get_type('ns0:PRODUCTMASTERDATA')
barcode = client.get_type('ns0:BARCODEDATA')(MPN_CAGE_BARCODE="test123")
description = client.get_type('ns0:DESCRIPTIONDATA')(PRDTXT='Test SKU', LANGU='EN')
dimensions = client.get_type('ns0:DIMENSIONDATA')(LAENG_LENGTH=10, BREIT_WIDTH= 10, HOEHE_HEIGHT= 10, MEABM_UNIT= 'INCH')
level_type = client.get_type('ns0:LEVELTYPEDATA')(QUANTITY=1,BARCODE_LIST=barcode, DIMENSION=dimensions, BRGEW_WEIGHT={'_value_1':"2",'GEWEI_TC_UNIT': "OUNCE"}, MEINS_LABEL='ITEM')
item_config = client.get_type('ns0:ITEMCONFIGDATA')(LEVEL_TYPE=level_type)
metadata = factory.METADATA(MESSAGEID='123456',CREA_DATE="2021-07-06+05:30",CREA_TIME="14:57:06.839+05:30", RECEIVERID="43567", SENDERID="89743894")
masterdata = factory.PRODUCTMASTERDATA(METADATA=metadata, DESCRIPTION=description, TAG_LIST={'CHARG_TAG_NAME':{'_value_1':"23223",'MANDATORY':'true'}}, ITEM_CONFIG=item_config, MATNR_PRODUCT_SKU='334423')
product_list = factory.PRODUCTLISTDATA(PRODUCT_MASTER=masterdata, LGNUM='12')

node = client.create_message(client.service, 'PRODUCT', PRODUCT_MASTER=masterdata, LGNUM='12')
x = etree.tostring(node)
print(x)
client.service.PRODUCT(PRODUCT_MASTER=masterdata, LGNUM='12')```
gdemonda commented 3 years ago

Hello @JayantJain720,

there you go :

`from zeep import xsd

security_header = xsd.Element('{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Security', xsd.ComplexType([xsd.Attribute('mustUnderstand', xsd.String())]))

security_header_value = security_header (mustUnderstand='true')

client.service.PRODUCT(PRODUCT_MASTER=masterdata, LGNUM='12', _soapheaders=[security_header_value]) ` May I ask you a question regarding the BinarySignature ? In my case it appears that I don't have both certificate and key file? Where did you get them ?

Thanks,

JayantJain720 commented 3 years ago

Hi @gdemonda , Thanks for the reply.

You can generate the certificate and key file using openssl - https://stackoverflow.com/questions/10175812/how-to-generate-a-self-signed-ssl-certificate-using-openssl

Or you can ask the client to provide them. Hope this helps.