Example #1
0
 def get_fault(self, reply):
     """
     Extract the fault from the specified SOAP reply.  If I{faults} is True, an
     exception is raised.  Otherwise, the I{unmarshalled} fault L{Object} is
     returned.  This method is called when the server raises a I{web fault}.
     @param reply: A SOAP reply message.
     @type reply: str
     @return: A fault object.
     @rtype: tuple ( L{Element}, L{Object} )
     """
     _reply = self.replyfilter(reply)
     sax = Parser()
     faultroot = sax.parse(string=_reply)
     soapenv = faultroot.getChild('Envelope')
     soapbody = soapenv.getChild('Body')
     fault = soapbody.getChild('Fault')
     unmarshaller = self.unmarshaller(False)
     if fault:
         p = unmarshaller.process(fault)
         if self.options().faults:
             raise WebFault(p, faultroot)
         return (True, faultroot, p.detail)
     else:
         #p = unmarshaller.process(soapbody)
         #if self.options().faults:
             #raise WebFault(p, faultroot)
         return (False, faultroot, reply)
Example #2
0
	def extract_auth_tokens_on_premise(self, resp_content):
		fix_suds()
		from suds.sax.parser import Parser
		p = Parser()
		doc = p.parse(string=resp_content)

		created = (self.now - timedelta(minutes=1)).isoformat()
		expires = (self.now + timedelta(minutes=60)).isoformat()
		rst_resp = doc.childAtPath('Envelope/Body/RequestSecurityTokenResponseCollection/RequestSecurityTokenResponse')
		key_ident = rst_resp.childAtPath('RequestedAttachedReference/SecurityTokenReference/KeyIdentifier').text
		binary_secret = rst_resp.childAtPath('RequestedProofToken/BinarySecret').text
		signature, signature_digest = self.generate_hmac_signature(binary_secret, created, expires)

		enc_data = rst_resp.childAtPath('RequestedSecurityToken/EncryptedData')
		key_ciphertext = enc_data.childAtPath('KeyInfo/EncryptedKey/CipherData/CipherValue').text
		token_ciphertext = enc_data.childAtPath('CipherData/CipherValue').text
		x509_info = enc_data.childAtPath('KeyInfo/EncryptedKey/KeyInfo/SecurityTokenReference/X509Data/X509IssuerSerial')
		issuer_name_x509 = x509_info.childAtPath('X509IssuerName').text
		serial_number_x509 = x509_info.childAtPath('X509SerialNumber').text

		context = {
			'key_ciphertext': key_ciphertext,
			'token_ciphertext': token_ciphertext,
			'key_ident': key_ident,
			'created': created,
			'expires': expires,
			'issuer_name_x509': issuer_name_x509,
			'serial_number_x509': serial_number_x509,
			'signature_digest': signature_digest,
			'signature': signature,
		}
		return context
Example #3
0
 def get(self, mangled):
     """Override this to prevent attempted purges."""
     fp = self.getf(mangled)
     if fp is None:
         return None
     p = Parser()
     return p.parse(fp)
Example #4
0
 def get(self, mangled):
     """Override this to prevent attempted purges."""
     fp = self.getf(mangled)
     if fp is None:
         return None
     p = Parser()
     return p.parse(fp)
Example #5
0
    def get_user(self, user_id):
        """
        Returns data of the user with id == `user_id` as a dict of type:
        {
            'firstname': ...,
            'lastname': ...,
            'internalemailaddress': ...,
            'systemuserid': ...,
        }
        """
        response = self.make_retrieve_soap_request(
            'systemuser', user_id, ['firstname', 'lastname', 'internalemailaddress']
        )

        parser = Parser()
        doc = parser.parse(string=response.content)

        attrs_el = doc.childAtPath('Envelope/Body/RetrieveResponse/RetrieveResult/Attributes')
        data = {}
        for attr_el in attrs_el:
            key = attr_el.getChild('key').text
            value = attr_el.getChild('value').text
            data[key] = value

        return data
Example #6
0
 def get_fault(self, reply):
     """
     Extract the fault from the specified soap reply.  If I{faults} is True, an
     exception is raised.  Otherwise, the I{unmarshalled} fault L{Object} is
     returned.  This method is called when the server raises a I{web fault}.
     @param reply: A soap reply message.
     @type reply: str
     @return: A fault object.
     @rtype: tuple ( L{Element}, L{Object} )
     """
     reply = self.replyfilter(reply)
     sax = Parser()
     faultroot = sax.parse(string=reply)
     soapenv = faultroot.getChild('Envelope')
     if soapenv is None:
         # If there isn't an <Envelope>, then we probably got a regular 500 error page (HTML) back. Not sure what to do
         # in this case, let's throw a generic exception (non-WebFault) for now.
         raise ServerErrorMissingSoapEnvelope(faultroot)
     soapbody = soapenv.getChild('Body')
     fault = soapbody.getChild('Fault')
     unmarshaller = self.unmarshaller(False)
     p = unmarshaller.process(fault)
     if self.options().faults:
         raise WebFault(p, faultroot)
     return (faultroot, p.detail)
Example #7
0
    def invoke(self, args, kwargs):
        """
        Send the required soap message to invoke the specified method
        @param args: A list of args for the method invoked.
        @type args: list
        @param kwargs: Named (keyword) args for the method invoked.
        @type kwargs: dict
        @return: The result of the method invocation.
        @rtype: I{builtin} or I{subclass of} L{Object}
        """
        simulation = kwargs[self.injkey]
        msg = simulation.get('msg')
        reply = simulation.get('reply')
        fault = simulation.get('fault')
        if msg is None:
            binding = self.method.binding.input
            soapenv = binding.get_message(self.method, args, kwargs)
            self.last_sent(soapenv)

            if reply is not None:
                return self.__reply(reply, args, kwargs)
            if fault is not None:
                return self.__fault(fault)
            raise Exception('(reply|fault) expected when msg=None')
        sax = Parser()
        msg = sax.parse(string=msg)
        return self.send(msg)
Example #8
0
    def extract_auth_tokens_on_premise(self, resp_content):
        fix_suds()
        from suds.sax.parser import Parser
        p = Parser()
        doc = p.parse(string=resp_content)

        created = (self.now - timedelta(minutes=1)).isoformat()
        expires = (self.now + timedelta(minutes=60)).isoformat()
        rst_resp = doc.childAtPath('Envelope/Body/RequestSecurityTokenResponseCollection/RequestSecurityTokenResponse')
        key_ident = rst_resp.childAtPath('RequestedAttachedReference/SecurityTokenReference/KeyIdentifier').text
        binary_secret = rst_resp.childAtPath('RequestedProofToken/BinarySecret').text
        signature, signature_digest = self.generate_hmac_signature(binary_secret, created, expires)

        enc_data = rst_resp.childAtPath('RequestedSecurityToken/EncryptedData')
        key_ciphertext = enc_data.childAtPath('KeyInfo/EncryptedKey/CipherData/CipherValue').text
        token_ciphertext = enc_data.childAtPath('CipherData/CipherValue').text
        x509_info = enc_data.childAtPath('KeyInfo/EncryptedKey/KeyInfo/SecurityTokenReference/X509Data/X509IssuerSerial')
        issuer_name_x509 = x509_info.childAtPath('X509IssuerName').text
        serial_number_x509 = x509_info.childAtPath('X509SerialNumber').text

        context = {
            'key_ciphertext': key_ciphertext,
            'token_ciphertext': token_ciphertext,
            'key_ident': key_ident,
            'created': created,
            'expires': expires,
            'issuer_name_x509': issuer_name_x509,
            'serial_number_x509': serial_number_x509,
            'signature_digest': signature_digest,
            'signature': signature,
        }
        return context
Example #9
0
def cdata():
    xml = '<a><![CDATA[<b>This is my &amp;&lt;tag&gt;</b>]]></a>'
    p = Parser()
    d = p.parse(string=xml)
    print d
    a = d.root()
    print a.getText()
Example #10
0
 def get_fault(self, reply):
     """
     Extract the fault from the specified soap reply.  If I{faults} is True, an
     exception is raised.  Otherwise, the I{unmarshalled} fault L{Object} is
     returned.  This method is called when the server raises a I{web fault}.
     @param reply: A soap reply message.
     @type reply: str
     @return: A fault object.
     @rtype: tuple ( L{Element}, L{Object} )
     """
     reply = self.replyfilter(reply)
     sax = Parser()
     faultroot = sax.parse(string=reply)
     soapenv = faultroot.getChild('Envelope')
     if soapenv is None:
         # If there isn't an <Envelope>, then we probably got a regular 500 error page (HTML) back. Not sure what to do
         # in this case, let's throw a generic exception (non-WebFault) for now.
         raise ServerErrorMissingSoapEnvelope(faultroot)
     soapbody = soapenv.getChild('Body')
     fault = soapbody.getChild('Fault')
     unmarshaller = self.unmarshaller(False)
     p = unmarshaller.process(fault)
     if self.options().faults:
         raise WebFault(p, faultroot)
     return (faultroot, p.detail)
Example #11
0
 def get_fault(self, reply):
     """
     Extract the fault from the specified soap reply.  If I{faults} is True, an
     exception is raised.  Otherwise, the I{unmarshalled} fault L{Object} is
     returned.  This method is called when the server raises a I{web fault}.
     @param reply: A soap reply message.
     @type reply: str
     @return: A fault object.
     @rtype: tuple ( L{Element}, L{Object} )
     """
     reply = self.replyfilter(reply)
     sax = Parser()
     faultroot = sax.parse(string=reply)
     soapenv = faultroot.getChild('Envelope')
     soapbody = soapenv.getChild('Body')
     fault = soapbody.getChild('Fault')
     unmarshaller = self.unmarshaller(False)
     p = unmarshaller.process(fault)
     if self.options().faults:
         raise WebFault(p, faultroot)
     try:
         detail = p.detail
     except AttributeError:
         try:
             detail = p.faultstring
         except AttributeError:
             detail = "Unknown Error"
     return (faultroot, detail)
Example #12
0
    def extract_auth_tokens_online(self, resp_content):
        fix_suds()
        from suds.sax.parser import Parser
        p = Parser()
        doc = p.parse(string=resp_content)

        rst_encrypted_data = doc.childAtPath(
            'Envelope/Body/RequestSecurityTokenResponse/RequestedSecurityToken/EncryptedData'
        )

        token_ciphertext = rst_encrypted_data.childAtPath(
            'CipherData/CipherValue').text

        encrypted_key = rst_encrypted_data.childAtPath('KeyInfo/EncryptedKey')
        key_ident = encrypted_key.childAtPath(
            'KeyInfo/SecurityTokenReference/KeyIdentifier').text
        key_ciphertext = encrypted_key.childAtPath(
            'CipherData/CipherValue').text

        # raise CrmAuthenticationError("KeyIdentifier or CipherValue not found
        # in", resp_content)
        context = {
            'key_ciphertext': key_ciphertext,
            'token_ciphertext': token_ciphertext,
            'key_ident': key_ident,
        }
        return context
Example #13
0
 def get_fault(self, reply):
     """
     Extract the fault from the specified SOAP reply.  If I{faults} is True, an
     exception is raised.  Otherwise, the I{unmarshalled} fault L{Object} is
     returned.  This method is called when the server raises a I{web fault}.
     @param reply: A SOAP reply message.
     @type reply: str
     @return: A fault object.
     @rtype: tuple ( L{Element}, L{Object} )
     """
     _reply = self.replyfilter(reply)
     sax = Parser()
     faultroot = sax.parse(string=_reply)
     soapenv = faultroot.getChild('Envelope')
     soapbody = soapenv.getChild('Body')
     fault = soapbody.getChild('Fault')
     unmarshaller = self.unmarshaller(False)
     if fault:
         p = unmarshaller.process(fault)
         if self.options().faults:
             raise WebFault(p, faultroot)
         return (True, faultroot, p.detail)
     else:
         #p = unmarshaller.process(soapbody)
         #if self.options().faults:
         #raise WebFault(p, faultroot)
         return (False, faultroot, reply)
Example #14
0
    def extract_users(self, resp_content):
        fix_suds()
        from suds.sax.parser import Parser
        p = Parser()
        doc = p.parse(string=resp_content)

        users = []
        user_elements = doc.childAtPath(
            'Envelope/Body/RetrieveMultipleResponse/RetrieveMultipleResult/Entities'
        )
        for user in user_elements.children:
            user_info = {}
            attributes = user.childrenAtPath(
                'Attributes/KeyValuePairOfstringanyType')
            for attr in attributes:
                if attr.childAtPath('key').text == 'systemuserid':
                    user_info['id'] = attr.childAtPath('value').text
                elif attr.childAtPath('key').text == 'internalemailaddress':
                    user_info['internalemailaddress'] = attr.childAtPath(
                        'value').text
                elif attr.childAtPath('key').text == 'fullname':
                    fullname = attr.childAtPath('value').text
                    user_info['last_name'] = ' '.join(fullname.split()[-1:])
                    user_info['first_name'] = ' '.join(fullname.split()[:-1])
            users.append(user_info)
        return users
Example #15
0
 def get_reply(self, method, reply):
     """
     Process the I{reply} for the specified I{method} by sax parsing the I{reply}
     and then unmarshalling into python object(s).
     @param method: The name of the invoked method.
     @type method: str
     @param reply: The reply XML received after invoking the specified method.
     @type reply: str
     @return: The unmarshalled reply.  The returned value is an L{Object} for a
         I{list} depending on whether the service returns a single object or a 
         collection.
     @rtype: tuple ( L{Element}, L{Object} )
     """
     reply = self.replyfilter(reply)
     sax = Parser()
     replyroot = sax.parse(string=reply)
     soapenv = replyroot.getChild('Envelope')
     soapenv.promotePrefixes()
     soapbody = soapenv.getChild('Body')
     soapbody = self.multiref.process(soapbody)
     nodes = self.replycontent(method, soapbody)
     rtypes = self.returned_types(method)
     if len(rtypes) > 1:
         result = self.replycomposite(rtypes, nodes)
         return (replyroot, result)
     if len(rtypes) == 1:
         if rtypes[0].unbounded():
             result = self.replylist(rtypes[0], nodes)
             return (replyroot, result)
         if len(nodes):
             unmarshaller = self.unmarshaller()
             resolved = rtypes[0].resolve(nobuiltin=True)
             result = unmarshaller.process(nodes[0], resolved)
             return (replyroot, result)
     return (replyroot, None)
Example #16
0
def cdata():
    xml = '<a><![CDATA[<b>This is my &amp;&lt;tag&gt;</b>]]></a>'
    p = Parser()
    d = p.parse(string=xml)
    print d
    a = d.root()
    print a.getText()
Example #17
0
 def get(self, id):
     try:
         fp = self.getf(id)
         if fp is None:
             return None
         p = Parser()
         return p.parse(fp)
     except Exception:
         self.purge(id)
Example #18
0
 def get(self, id):
     try:
         fp = FileCache.getf(self, id)
         if fp is None:
             return None
         p = Parser()
         return p.parse(fp)
     except Exception:
         FileCache.purge(self, id)
Example #19
0
 def get(self, id):
     try:
         fp = FileCache.getf(self, id)
         if fp is None:
             return None
         p = Parser()
         return p.parse(fp)
     except:
         FileCache.purge(self, id)
Example #20
0
    def extract_adfs_url(self, resp_content):
        fix_suds()
        from suds.sax.parser import Parser
        p = Parser()
        doc = p.parse(string=resp_content)

        all_policies = doc.childAtPath('definitions/Policy/ExactlyOne/All')
        url = all_policies.childAtPath('AuthenticationPolicy/SecureTokenService/Identifier').text
        return url.replace('http:', 'https:')
Example #21
0
 def get(self, id):
     try:
         fp = self.getf(id)
         if fp is None:
             return None
         p = Parser()
         return p.parse(fp)
     except Exception:
         self.purge(id)
Example #22
0
	def extract_adfs_url(self, resp_content):
		fix_suds()
		from suds.sax.parser import Parser
		p = Parser()
		doc = p.parse(string=resp_content)

		all_policies = doc.childAtPath('definitions/Policy/ExactlyOne/All')
		url = all_policies.childAtPath('AuthenticationPolicy/SecureTokenService/Identifier').text
		return url.replace('http:', 'https:')
def marshall_response(vim, response):
    from suds.sax.parser import Parser
    from suds.bindings.document import Document
    parser = Parser()
    document = parser.parse(string=response)
    obj = document.getChildren()[0]
    binding = Document(vim.client.wsdl)
    unmarshaller = binding.unmarshaller()
    marshalled_obj = unmarshaller.process(obj, None)
    return vim._parse_object_content(marshalled_obj)
Example #24
0
 def get_response_path(response, xml_path):
     """
     Formats the response
     :param response:
     :return: The xml path given
     """
     if response and xml_path:
         sax = Parser()
         res = sax.parse(string=response)
         return res._Document__root.childrenAtPath(xml_path)
Example #25
0
    def sending(self, context):
        '''Signs XML before sending'''

        signature_template = '''
            <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
              <SignedInfo>
              <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
              <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
              <Reference URI="#%(REFERENCE_ID)s">
                <Transforms>
                  <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                  <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue></DigestValue>
              </Reference>
              </SignedInfo>
              <SignatureValue />
              <KeyInfo>
                <X509Data>
                  <X509Certificate />
                </X509Data>
              </KeyInfo>
            </Signature>
        '''

        envelope_element = Parser().parse(string=context.envelope).root()
        envelope_element.refitPrefixes()

        body = envelope_element.getChild('Body')
        payload = body[0]

        qname = payload.qname()
        if 'Echo' in qname:
            return

        reference_id = "refId:%s" % uuid4()
        payload.set('Id', reference_id)
        signature_template %= {'REFERENCE_ID': reference_id}

        signature_element = Parser().parse(string=signature_template).root()
        payload.append(signature_element)

        envelope = self.DTD_TEST_ID % qname
        envelope += envelope_element.str()
        envelope = envelope.encode('utf-8')

        signer = XMLDSIG()
        signer.load_key(self.key_path,
                        password=self.key_passphrase,
                        cert_path=self.cert_path)
        context.envelope = signer.sign(envelope)
        context.envelope = self.RE_DTD_TEST.sub('', context.envelope)
Example #26
0
 def download(self):
     url = self.location
     try:
         if '://' not in url:
             url = urljoin(self.schema.baseurl, url)
         transport = self.schema.options.transport
         root = Parser(transport).parse(url=url).root()
         root.set('url', url)
         return self.schema.instance(root, url)
     except TransportError:
         msg = 'imported schema (%s) at (%s), failed' % (self.ns[1], url)
         log.error('%s, %s', self.id, msg, exc_info=True)
         raise Exception(msg)
Example #27
0
    def get_whoami(self, resp_content):
        fix_suds()
        from suds.sax.parser import Parser
        p = Parser()
        doc = p.parse(string=resp_content)

        id = ''
        results = doc.childAtPath('Envelope/Body/ExecuteResponse/ExecuteResult/Results')
        for result in results.children:
            if result.childAtPath('key').text == 'UserId':
                id = result.childAtPath('value').text

        return id
Example #28
0
	def get_whoami(self, resp_content):
		fix_suds()
		from suds.sax.parser import Parser
		p = Parser()
		doc = p.parse(string=resp_content)

		id = ''
		results = doc.childAtPath('Envelope/Body/ExecuteResponse/ExecuteResult/Results')
		for result in results.children:
			if result.childAtPath('key').text == 'UserId':
				id = result.childAtPath('value').text

		return id
Example #29
0
 def __init__(self, wsdl):
     """
     @param wsdl: A wsdl.
     @type wsdl: L{wsdl.Definitions}
     """
     self.wsdl = wsdl
     self.schema = wsdl.schema
     self.options = Options()
     self.parser = Parser()
     self.multiref = MultiRef()
     self.xcodecs = (
         Unmarshaller(self.schema),
         Marshaller(self.schema),
     )
Example #30
0
 def download(self, url):
     """
     Download the docuemnt.
     @param url: A document url.
     @type url: str.
     @return: A file pointer to the docuemnt.
     @rtype: file-like
     """
     store = DocumentStore()
     fp = store.open(url)
     if fp is None:
         fp = self.options.transport.open(Request(url))
     sax = Parser()
     return sax.parse(file=fp)
Example #31
0
 def download(self):
     """ download the schema """
     url = self.location
     try:
         if '://' not in url:
             url = urljoin(self.schema.baseurl, url)
         transport = self.schema.options.transport
         root = Parser(transport).parse(url=url).root()
         root.set('url', url)
         return self.schema.instance(root, url)
     except TransportError:
         msg = 'imported schema (%s) at (%s), failed' % (self.ns[1], url)
         log.error('%s, %s', self.id, msg, exc_info=True)
         raise Exception(msg)
Example #32
0
 def download(self, url):
     """
     Download the docuemnt.
     @param url: A document url.
     @type url: str.
     @return: A file pointer to the docuemnt.
     @rtype: file-like
     """
     store = DocumentStore()
     fp = store.open(url)
     if fp is None:
         fp = self.options.transport.open(Request(url))
     sax = Parser()
     return sax.parse(file=fp)
Example #33
0
 def read_message(self, message):
     """
     Read soap message string
     @param message: The SOAP message - can be a request or a response
     @type message: str
     @return: Tuple of the raw parsed message and soap body
     """
     sax = Parser()
     messageroot = sax.parse(string=message)
     soapenv = messageroot.getChild('Envelope')
     soapenv.promotePrefixes()
     soapbody = soapenv.getChild('Body')
     self.detect_fault(soapbody)
     self.multiref.process(soapbody)
     return messageroot, soapbody
Example #34
0
	def _known_listitem_ref(self, listname, content_type, field, field_value, display_field='_ows_Title', fuzzy=False, max_dist=4):
		list_uuid = getattr(self, '%s_list_uuid' % listname)
		query = Parser().parse(string=str('<ns1:query><Query><Where><Eq><FieldRef Name="ContentType"/><Value Type="Text">%s</Value></Eq></Where></Query></ns1:query>' % content_type)).getChild('query') \
		        if content_type else None
		viewFields = ('ID', 'Title', field[5:] if field.startswith('_ows_') else field)

		return self.listitem_ref(list_uuid, query, viewFields, field, field_value, display_field=display_field, fuzzy=fuzzy, max_dist=max_dist)
Example #35
0
 def download(self, url):
     """
     Download the docuemnt.
     @param url: A document url.
     @type url: str.
     @return: A file pointer to the docuemnt.
     @rtype: file-like
     """
     store = DocumentStore()
     if url.startswith('http') or url.startswith('suds'):
         fp = store.open(url)
         if fp is None:
             fp = self.options.transport.open(Request(url))
     else:
         fp = open(url, 'r')
     sax = Parser()
     return sax.parse(file=fp)
Example #36
0
def download(reader, url):
    company = None
    info = url.split('/')
    filename = 'mocks/%s.xml' % info[-1].lower()

    # If the url contains the company name, take it and append it to response
    if len(info) == 3:
        company = info[0]
    with open(os.path.join(os.path.dirname(__file__), filename), 'r') as f:
        response = f.read()
        sax = Parser()
        result = sax.parse(string=response)
        if company:
            element = result.children[0].children[-1].children[0].children[0]
            location = element.get('location')
            element.set('location', '%s/%s' % (company, location))
        return result
Example #37
0
def download(reader, url):
    company = None
    info =  url.split('/')
    filename = 'mocks/%s.xml' % info[-1].lower()

    # If the url contains the company name, take it and append it to response
    if len(info) == 3:
        company = info[0]
    with open(os.path.join(os.path.dirname(__file__), filename), 'r') as f:
        response = f.read()
        sax = Parser()
        result = sax.parse(string=response)
        if company:
            element = result.children[0].children[-1].children[0].children[0]
            location = element.get('location')
            element.set('location', '%s/%s' %(company, location))
        return result
Example #38
0
def suds_unmarshall(data):
    try:
        from suds.sax.parser import Parser
        from suds.umx.basic import Basic
    except ImportError:
        print "ERROR:  Could not import SUDS."
        print "You must install SUDS for the '-t' option to work"
        print "https://fedorahosted.org/suds/"
        return None

    p = Parser()
    obj = None
    try:
        root = p.parse(string=data).root()
        umx = Basic()
        obj = umx.process(root)
    except Exception, e:
        print "SAX Excpetion:", e
Example #39
0
	def extract_entity(self, resp_content, attributesSet):
		fix_suds()
		from suds.sax.parser import Parser
		p = Parser()
		doc = p.parse(string=resp_content)

		entities = []
		entity_elements = doc.childAtPath('Envelope/Body/RetrieveMultipleResponse/RetrieveMultipleResult/Entities')
		for entity in entity_elements.children:
			entity_info = {}
			attributes = entity.childrenAtPath('Attributes/KeyValuePairOfstringanyType')
			for attr in attributes:
				for attrSet in attributesSet:
					if attr.childAtPath('key').text == attrSet[0]:
						entity_info[attrSet[1]] = attr.childAtPath(attrSet[2]).text
						break
			entities.append(entity_info)
		return entities
 def download(self, url):
     """
     Download the docuemnt.
     @param url: A document url.
     @type url: str.
     @return: A file pointer to the docuemnt.
     @rtype: file-like
     """
     store = DocumentStore()
     fp = store.open(url)
     if fp is None:
         fp = self.options.transport.open(Request(url))
     content = fp.read()
     fp.close()
     ctx = self.plugins.document.loaded(url=url, document=content)
     content = ctx.document
     sax = Parser()
     return sax.parse(string=content)
Example #41
0
 def __init__(self, wsdl):
     """
     @param wsdl: A wsdl.
     @type wsdl: L{wsdl.Definitions}
     """
     self.wsdl = wsdl
     self.schema = wsdl.schema
     self.options = Options()
     self.parser = Parser()
     self.multiref = MultiRef()
Example #42
0
 def open(self, url):
     """
     Open an XML document at the specified I{url}.
     First, the document attempted to be retrieved from
     the I{document cache}.  If not found, it is downloaded and
     parsed using the SAX parser.  The result is added to the
     document store for the next open().
     @param url: A document url.
     @type url: str.
     @return: The specified XML document.
     @rtype: I{Document}
     """
     d = self.options.cache.get(url)
     if d is None:
         fp = self.options.transport.open(Request(url))
         sax = Parser()
         d = sax.parse(file=fp)
         #self.options.cache.put(url, d)
     return d
 def __init__(self, wsdl_url=None, token=None, sandbox=False, _validate=True):
     es_kwargs = {
         'sandbox': sandbox,
     }
     if wsdl_url is not None:
         es_kwargs['wsdl_url'] = wsdl_url
     if token is not None:
         es_kwargs['token'] = token
     self.client = TradingAPI(**es_kwargs)
     self.saxparser = Parser()
     self._validate = _validate
Example #44
0
def _parse(string):
    """
    Parses the given XML document content and returns the resulting root XML
    element node. Returns None if the given XML content is empty.
    @param string: XML document content to parse.
    @type string: str
    @return: Resulting root XML element node or None.
    @rtype: L{Element}
    """
    if len(string) > 0:
        return Parser().parse(string=string)
Example #45
0
    def _extract_auth_tokens(self, resp_content):
        """
        Internal method which extracts the auth data from the content of a SOAP response, generates signatures
        and other related fields needed for the next SOAP request and returns a dict with all of them.
        """
        parser = Parser()
        doc = parser.parse(string=resp_content)

        now = get_request_now()
        creation_date = now.isoformat()
        expiration_date_dt = (now + datetime.timedelta(minutes=CDMS_EXPIRATION_TOKEN_DELTA))
        expiration_date = expiration_date_dt.isoformat()

        rst_resp = doc.childAtPath('Envelope/Body/RequestSecurityTokenResponseCollection/RequestSecurityTokenResponse')
        enc_data = rst_resp.childAtPath('RequestedSecurityToken/EncryptedData')

        key_identifier = rst_resp.childAtPath('RequestedAttachedReference/SecurityTokenReference/KeyIdentifier').text
        ciphertext_key = enc_data.childAtPath('KeyInfo/EncryptedKey/CipherData/CipherValue').text
        ciphertext_token = enc_data.childAtPath('CipherData/CipherValue').text

        x509_info = enc_data.childAtPath(
            'KeyInfo/EncryptedKey/KeyInfo/SecurityTokenReference/X509Data/X509IssuerSerial'
        )
        x509_issuer_name = x509_info.childAtPath('X509IssuerName').text
        x509_serial_number = x509_info.childAtPath('X509SerialNumber').text

        binary_secret = rst_resp.childAtPath('RequestedProofToken/BinarySecret').text
        signature, signature_digest = self._generate_hmac_signature(binary_secret, creation_date, expiration_date)

        return {
            'ciphertext_key': ciphertext_key,
            'ciphertext_token': ciphertext_token,
            'key_identifier': key_identifier,
            'creation_date': creation_date,
            'expiration_date': expiration_date,
            'expiration_date_dt': expiration_date_dt,
            'X509_issuer_name': x509_issuer_name,
            'X509_serial_number': x509_serial_number,
            'signature_digest': signature_digest,
            'signature': signature,
        }
Example #46
0
def basic():
    xml = "<a>Me &amp;&amp; &lt;b&gt;my&lt;/b&gt; shadow&apos;s &lt;i&gt;dog&lt;/i&gt; love to &apos;play&apos; and sing &quot;la,la,la&quot;;</a>"
    p = Parser()
    d = p.parse(string=xml)
    a = d.root()
    print 'A(parsed)=\n%s' % a
    assert str(a) == xml
    b = Element('a')
    b.setText('Me && <b>my</b> shadow\'s <i>dog</i> love to \'play\' and sing "la,la,la";')
    print 'B(encoded)=\n%s' % b
    assert str(b) == xml
    print 'A(text-decoded)=\n%s' % a.getText()
    print 'B(text-decoded)=\n%s' % b.getText()
    assert a.getText() == b.getText()
    print 'test pruning'
    j = Element('A')
    j.set('n', 1)
    j.append(Element('B'))
    print j
    j.prune()
    print j
Example #47
0
def basic():
    xml = "<a>Me &amp;&amp; &lt;b&gt;my&lt;/b&gt; shadow&apos;s &lt;i&gt;dog&lt;/i&gt; love to &apos;play&apos; and sing &quot;la,la,la&quot;;</a>"
    p = Parser()
    d = p.parse(string=xml)
    a = d.root()
    print 'A(parsed)=\n%s' % a
    assert str(a) == xml
    b = Element('a')
    b.setText('Me &&amp; &lt;b>my</b> shadow\'s <i>dog</i> love to \'play\' and sing "la,la,la";')
    print 'B(encoded)=\n%s' % b
    assert str(b) == xml
    print 'A(text-decoded)=\n%s' % a.getText()
    print 'B(text-decoded)=\n%s' % b.getText()
    assert a.getText() == b.getText()
    print 'test pruning'
    j = Element('A')
    j.set('n', 1)
    j.append(Element('B'))
    print j
    j.prune()
    print j
Example #48
0
    def parse_request(self, method_name, data):
        """"
        @args: method_name Short version of soap_action.
        @args: raw soap data (string)
        """
        root = Parser().parse(string=data)
        env = root.getChild('Envelope')
        env.promotePrefixes()
        body = env.getChild('Body')

        method = self.method = self._lookup(method_name)

        binding = method.binding.input
        # code extracted from "suds.bindings.binding.Binding.get_reply"
        body = binding.multiref.process(body)
        nodes = binding.replycontent(method, body)
        # Note: "method" has its input and output exchanged.
        # Therefore, "returned_types" in fact means "parameter_types".
        rtypes = binding.returned_types(method)
        params = binding.replycomposite(rtypes, nodes)
        # the normalization part does not seem to do anything usefull
        return method, params
Example #49
0
 def download(self, url):
     """
     Download the document.
     @param url: A document URL.
     @type url: str.
     @return: A file pointer to the document.
     @rtype: file-like
     """
     content = None
     store = self.options.documentStore
     if store is not None:
         content = store.open(url)
     if content is None:
         fp = self.options.transport.open(Request(url))
         try:
             content = fp.read()
         finally:
             fp.close()
     ctx = self.plugins.document.loaded(url=url, document=content)
     content = ctx.document
     sax = Parser()
     return sax.parse(string=content)
Example #50
0
 def download(self, url):
     """
     Download the document.
     @param url: A document URL.
     @type url: str.
     @return: A file pointer to the document.
     @rtype: file-like
     """
     content = None
     store = self.options.documentStore
     if store is not None:
         content = store.open(url)
     if content is None:
         fp = self.options.transport.open(Request(url))
         try:
             content = fp.read()
         finally:
             fp.close()
     ctx = self.plugins.document.loaded(url=url, document=content)
     content = ctx.document
     sax = Parser()
     return sax.parse(string=content)
Example #51
0
    def extract_auth_tokens_online(self, resp_content):
        fix_suds()
        from suds.sax.parser import Parser
        p = Parser()
        doc = p.parse(string=resp_content)

        rst_encrypted_data = doc.childAtPath('Envelope/Body/RequestSecurityTokenResponse/RequestedSecurityToken/EncryptedData')

        token_ciphertext = rst_encrypted_data.childAtPath('CipherData/CipherValue').text

        encrypted_key = rst_encrypted_data.childAtPath('KeyInfo/EncryptedKey')
        key_ident = encrypted_key.childAtPath('KeyInfo/SecurityTokenReference/KeyIdentifier').text
        key_ciphertext = encrypted_key.childAtPath('CipherData/CipherValue').text

        # raise CrmAuthenticationError("KeyIdentifier or CipherValue not found
        # in", resp_content)
        context = {
            'key_ciphertext': key_ciphertext,
            'token_ciphertext': token_ciphertext,
            'key_ident': key_ident,
        }
        return context
Example #52
0
    def extract_users(self, resp_content):
        fix_suds()
        from suds.sax.parser import Parser
        p = Parser()
        doc = p.parse(string=resp_content)

        users = []
        user_elements = doc.childAtPath('Envelope/Body/RetrieveMultipleResponse/RetrieveMultipleResult/Entities')
        for user in user_elements.children:
            user_info = {}
            attributes = user.childrenAtPath('Attributes/KeyValuePairOfstringanyType')
            for attr in attributes:
                if attr.childAtPath('key').text == 'systemuserid':
                    user_info['id'] = attr.childAtPath('value').text
                elif attr.childAtPath('key').text == 'internalemailaddress':
                    user_info['internalemailaddress'] = attr.childAtPath('value').text
                elif attr.childAtPath('key').text == 'fullname':
                    fullname = attr.childAtPath('value').text
                    user_info['last_name'] = ' '.join(fullname.split()[-1:])
                    user_info['first_name'] = ' '.join(fullname.split()[:-1])
            users.append(user_info)
        return users
Example #53
0
    def get_whoami(self):
        """
        Returns the ids of the logged in user as a dict of type:
        {
            'UserId': ...,
            'BusinessUnitId': ...,
            'OrganizationId': ...
        }
        """
        response = self.make_execute_soap_request('WhoAmI')

        parser = Parser()
        doc = parser.parse(string=response.content)

        results_el = doc.childAtPath('Envelope/Body/ExecuteResponse/ExecuteResult/Results')
        data = {}
        for result_el in results_el:
            key = result_el.getChild('key').text
            value = result_el.getChild('value').text
            data[key] = value

        return data
Example #54
0
    def parse_request(self, method_name, data):
        """"
        @args: method_name Short version of soap_action.
        @args: raw soap data (string)
        """
        root = Parser().parse(string=data)
        env = root.getChild('Envelope')
        env.promotePrefixes()
        body = env.getChild('Body')

        method = self.method = self._lookup(method_name)

        binding = method.binding.input
        # code extracted from "suds.bindings.binding.Binding.get_reply"
        body = binding.multiref.process(body)
        nodes = binding.replycontent(method, body)
        # Note: "method" has its input and output exchanged.
        # Therefore, "returned_types" in fact means "parameter_types".
        rtypes = binding.returned_types(method)
        params = binding.replycomposite(rtypes, nodes)
        # the normalization part does not seem to do anything usefull
        return method, params
Example #55
0
    def sending(self, context):
        '''Signs XML before sending'''

        signature_template = '''
            <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
              <SignedInfo>
              <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
              <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
              <Reference URI="#%(REFERENCE_ID)s">
                <Transforms>
                  <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                  <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                </Transforms>
                <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                <DigestValue></DigestValue>
              </Reference>
              </SignedInfo>
              <SignatureValue />
              <KeyInfo>
                <X509Data>
                  <X509Certificate />
                </X509Data>
              </KeyInfo>
            </Signature>
        '''

        envelope_element = Parser().parse(string=context.envelope).root()
        envelope_element.refitPrefixes()

        body = envelope_element.getChild('Body')
        payload = body[0]

        qname = payload.qname()
        if 'Echo' in qname:
            return

        reference_id = "refId:%s" % uuid4()
        payload.set('Id', reference_id)
        signature_template %= {'REFERENCE_ID': reference_id}

        signature_element = Parser().parse(string=signature_template).root()
        payload.append(signature_element)

        envelope = self.DTD_TEST_ID % qname
        envelope += envelope_element.str()
        envelope = envelope.encode('utf-8')

        signer = XMLDSIG()
        signer.load_key(self.key_path,
                        password=self.key_passphrase,
                        cert_path=self.cert_path)
        context.envelope = signer.sign(envelope)
        context.envelope = self.RE_DTD_TEST.sub('', context.envelope)
Example #56
0
 def open(self, url):
     """
     Open an XML document at the specified I{url}.
     First, the document attempted to be retrieved from
     the I{object cache}.  If not found, it is downloaded and
     parsed using the SAX parser.  The result is added to the
     cache for the next open().
     @param url: A document url.
     @type url: str.
     @return: The specified XML document.
     @rtype: I{Document}
     """
     cache = self.cache()
     id = self.mangle(url, 'document')
     d = cache.get(id)
     if d is None:
         d = self.download(url)
         cache.put(id, d)
     else:
         sax = Parser()
         d = sax.parse(string=d)
     self.plugins.document.parsed(url=url, document=d.root())
     return d