tagercito
tagercito

Reputation: 66

How to sign XML with python ( suds)

I've been trying to sign an XML object created by suds but I have no luck.

My current script looks like this.

from suds.client import Client
from suds.transport.http import HttpAuthenticated
from suds.transport import Reply, TransportError

import requests

class RequestsTransport(HttpAuthenticated):

    def __init__(self, **kwargs):
        self.cert = kwargs.pop('cert', None)
        HttpAuthenticated.__init__(self, **kwargs)

    def send(self, request):
        self.addcredentials(request)
        resp = requests.post(
            request.url,
            data=request.message,
            headers=request.headers,
            cert=self.cert,
            verify=True
        )
        result = Reply(resp.status_code, resp.headers, resp.content)
        return result

url = 'URL'
headers = {"Content-Type": "text/xml;charset=UTF-8",
           "SOAPAction": ""}
t = RequestsTransport(cert=("path to cert","path to key"))
client = Client(url, headers=headers, transport=t)

I create a method and then I need to sign it. I have a pem file for the public cert of the WSDL I am checking.

Also, if I dont sign the request I get :

suds.WebFault: Server raised fault: 'An error was discovered processing the header'

Upvotes: 1

Views: 1311

Answers (1)

tagercito
tagercito

Reputation: 66

I found that python-wsse ( https://py-wsse.readthedocs.io/en/latest/ ) works with suds like a charm.

    from suds.client import Client
    from suds.wsse import Security, Timestamp
    from wsse.suds import WssePlugin

    def get_client(our_keyfile_path, our_certfile_path, their_certfile_path):
        wsse = Security()
        wsse.tokens.append(Timestamp())

        return Client(
            wsdl_url,
            transport=transport,
            wsse=wsse,
            plugins=[
                WssePlugin(
                    keyfile=our_keyfile_path,
                    certfile=our_certfile_path,
                    their_certfile=their_certfile_path,
                ),
            ],
        )

Upvotes: 1

Related Questions