@staticmethod<EOL><INDENT>def add_x509_key_descriptors(metadata, cert=None, add_encryption=True):<DEDENT>
if cert is None or cert == '<STR_LIT>':<EOL><INDENT>return metadata<EOL><DEDENT>try:<EOL><INDENT>xml = parseString(metadata.encode('<STR_LIT:utf-8>'), forbid_dtd=True)<EOL><DEDENT>except Exception as e:<EOL><INDENT>raise Exception('<STR_LIT>' + e.message)<EOL><DEDENT>formated_cert = OneLogin_Saml2_Utils.format_cert(cert, False)<EOL>x509_certificate = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS, '<STR_LIT>')<EOL>content = xml.createTextNode(formated_cert)<EOL>x509_certificate.appendChild(content)<EOL>key_data = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS, '<STR_LIT>')<EOL>key_data.appendChild(x509_certificate)<EOL>key_info = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS, '<STR_LIT>')<EOL>key_info.appendChild(key_data)<EOL>key_descriptor = xml.createElementNS(OneLogin_Saml2_Constants.NS_DS, '<STR_LIT>')<EOL>entity_descriptor = xml.getElementsByTagName('<STR_LIT>')[<NUM_LIT:0>]<EOL>sp_sso_descriptor = entity_descriptor.getElementsByTagName('<STR_LIT>')[<NUM_LIT:0>]<EOL>sp_sso_descriptor.insertBefore(key_descriptor.cloneNode(True), sp_sso_descriptor.firstChild)<EOL>if add_encryption:<EOL><INDENT>sp_sso_descriptor.insertBefore(key_descriptor.cloneNode(True), sp_sso_descriptor.firstChild)<EOL><DEDENT>signing = xml.getElementsByTagName('<STR_LIT>')[<NUM_LIT:0>]<EOL>signing.setAttribute('<STR_LIT>', '<STR_LIT>')<EOL>signing.appendChild(key_info)<EOL>signing.setAttribute('<STR_LIT>', OneLogin_Saml2_Constants.NS_DS)<EOL>if add_encryption:<EOL><INDENT>encryption = xml.getElementsByTagName('<STR_LIT>')[<NUM_LIT:1>]<EOL>encryption.setAttribute('<STR_LIT>', '<STR_LIT>')<EOL>encryption.appendChild(key_info.cloneNode(True))<EOL>encryption.setAttribute('<STR_LIT>', OneLogin_Saml2_Constants.NS_DS)<EOL><DEDENT>return xml.toxml()<EOL>
Adds the x509 descriptors (sign/encryption) to the metadata
The same cert will be used for sign/encrypt

:param metadata: SAML Metadata XML
:type metadata: string

:param cert: x509 cert
:type cert: string

:param add_encryption: Determines if the KeyDescriptor[use="encryption"] should be added.
:type add_encryption: boolean

:returns: Metadata with KeyDescriptors
:rtype: string