def validate_sign(xml, cert=None, fingerprint=None, fingerprintalg='sha1', validatecert=False, debug=False, xpath=None, multicerts=None):
        """
        Validates a signature (Message or Assertion).

        :param xml: The element we should validate
        :type: string | Document

        :param cert: The pubic cert
        :type: string

        :param fingerprint: The fingerprint of the public cert
        :type: string

        :param fingerprintalg: The algorithm used to build the fingerprint
        :type: string

        :param validatecert: If true, will verify the signature and if the cert is valid.
        :type: bool

        :param debug: Activate the xmlsec debug
        :type: bool

        :param xpath: The xpath of the signed element
        :type: string

        :param multicerts: Multiple public certs
        :type: list

        :param raise_exceptions: Whether to return false on failure or raise an exception
        :type raise_exceptions: Boolean
        """
        if xml is None or xml == '':
            raise Exception('Empty string supplied as input')
        elif isinstance(xml, etree._Element):
            elem = xml
        elif isinstance(xml, Document):
            xml = xml.toxml()
            elem = fromstring(str(xml), forbid_dtd=True)
        elif isinstance(xml, Element):
            xml.setAttributeNS(
                unicode(OneLogin_Saml2_Constants.NS_SAMLP),
                'xmlns:samlp',
                unicode(OneLogin_Saml2_Constants.NS_SAMLP)
            )
            xml.setAttributeNS(
                unicode(OneLogin_Saml2_Constants.NS_SAML),
                'xmlns:saml',
                unicode(OneLogin_Saml2_Constants.NS_SAML)
            )
            xml = xml.toxml()
            elem = fromstring(str(xml), forbid_dtd=True)
        elif isinstance(xml, basestring):
            elem = fromstring(str(xml), forbid_dtd=True)
        else:
            raise Exception('Error parsing xml string')

        error_callback_method = None
        if debug:
            error_callback_method = print_xmlsec_errors
        xmlsec.set_error_callback(error_callback_method)

        xmlsec.addIDs(elem, ["ID"])

        if xpath:
            signature_nodes = OneLogin_Saml2_Utils.query(elem, xpath)
        else:
            signature_nodes = OneLogin_Saml2_Utils.query(elem, OneLogin_Saml2_Utils.RESPONSE_SIGNATURE_XPATH)

            if len(signature_nodes) == 0:
                signature_nodes = OneLogin_Saml2_Utils.query(elem, OneLogin_Saml2_Utils.ASSERTION_SIGNATURE_XPATH)

        if len(signature_nodes) == 1:
            signature_node = signature_nodes[0]

            if not multicerts:
                return OneLogin_Saml2_Utils.validate_node_sign(signature_node, elem, cert, fingerprint, fingerprintalg, validatecert, debug, raise_exceptions=True)
            else:
                # If multiple certs are provided, I may ignore cert and
                # fingerprint provided by the method and just check the
                # certs multicerts
                fingerprint = fingerprintalg = None
                for cert in multicerts:
                    if OneLogin_Saml2_Utils.validate_node_sign(signature_node, elem, cert, fingerprint, fingerprintalg, validatecert, False, raise_exceptions=False):
                        return True
                raise OneLogin_Saml2_ValidationError('Signature validation failed. SAML Response rejected.')
        else:
            raise OneLogin_Saml2_ValidationError('Expected exactly one signature node; got {}.'.format(len(signature_nodes)), OneLogin_Saml2_ValidationError.WRONG_NUMBER_OF_SIGNATURES)
def validate_sign ( xml , cert = None , fingerprint = None , fingerprintalg = 'sha1' , validatecert = False , debug = False , xpath = None , multicerts = None ) : if xml is None or xml == '' : raise Exception ( 'Empty string supplied as input' ) elif isinstance ( xml , etree . _Element ) : elem = xml elif isinstance ( xml , Document ) : xml = xml . toxml ( ) elem = fromstring ( str ( xml ) , forbid_dtd = True ) elif isinstance ( xml , Element ) : xml . setAttributeNS ( unicode ( OneLogin_Saml2_Constants . NS_SAMLP ) , 'xmlns:samlp' , unicode ( OneLogin_Saml2_Constants . NS_SAMLP ) ) xml . setAttributeNS ( unicode ( OneLogin_Saml2_Constants . NS_SAML ) , 'xmlns:saml' , unicode ( OneLogin_Saml2_Constants . NS_SAML ) ) xml = xml . toxml ( ) elem = fromstring ( str ( xml ) , forbid_dtd = True ) elif isinstance ( xml , basestring ) : elem = fromstring ( str ( xml ) , forbid_dtd = True ) else : raise Exception ( 'Error parsing xml string' ) error_callback_method = None if debug : error_callback_method = print_xmlsec_errors xmlsec . set_error_callback ( error_callback_method ) xmlsec . addIDs ( elem , [ "ID" ] ) if xpath : signature_nodes = OneLogin_Saml2_Utils . query ( elem , xpath ) else : signature_nodes = OneLogin_Saml2_Utils . query ( elem , OneLogin_Saml2_Utils . RESPONSE_SIGNATURE_XPATH ) if len ( signature_nodes ) == 0 : signature_nodes = OneLogin_Saml2_Utils . query ( elem , OneLogin_Saml2_Utils . ASSERTION_SIGNATURE_XPATH ) if len ( signature_nodes ) == 1 : signature_node = signature_nodes [ 0 ] if not multicerts : return OneLogin_Saml2_Utils . validate_node_sign ( signature_node , elem , cert , fingerprint , fingerprintalg , validatecert , debug , raise_exceptions = True ) else : fingerprint = fingerprintalg = None for cert in multicerts : if OneLogin_Saml2_Utils . validate_node_sign ( signature_node , elem , cert , fingerprint , fingerprintalg , validatecert , False , raise_exceptions = False ) : return True raise OneLogin_Saml2_ValidationError ( 'Signature validation failed. SAML Response rejected.' ) else : raise OneLogin_Saml2_ValidationError ( 'Expected exactly one signature node; got {}.' . format ( len ( signature_nodes ) ) , OneLogin_Saml2_ValidationError . WRONG_NUMBER_OF_SIGNATURES )
Validates a signature (Message or Assertion).

        :param xml: The element we should validate
        :type: string | Document

        :param cert: The pubic cert
        :type: string

        :param fingerprint: The fingerprint of the public cert
        :type: string

        :param fingerprintalg: The algorithm used to build the fingerprint
        :type: string

        :param validatecert: If true, will verify the signature and if the cert is valid.
        :type: bool

        :param debug: Activate the xmlsec debug
        :type: bool

        :param xpath: The xpath of the signed element
        :type: string

        :param multicerts: Multiple public certs
        :type: list

        :param raise_exceptions: Whether to return false on failure or raise an exception
        :type raise_exceptions: Boolean