def generate_name_id(value, sp_nq, sp_format=None, cert=None, debug=False, nq=None):
        """
        Generates a nameID.

        :param value: fingerprint
        :type: string

        :param sp_nq: SP Name Qualifier
        :type: string

        :param sp_format: SP Format
        :type: string

        :param cert: IdP Public Cert to encrypt the nameID
        :type: string

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

        :param nq: IDP Name Qualifier
        :type: string

        :returns: DOMElement | XMLSec nameID
        :rtype: string
        """
        doc = Document()
        name_id_container = doc.createElementNS(OneLogin_Saml2_Constants.NS_SAML, 'container')
        name_id_container.setAttribute("xmlns:saml", OneLogin_Saml2_Constants.NS_SAML)

        name_id = doc.createElement('saml:NameID')
        if sp_nq is not None:
            name_id.setAttribute('SPNameQualifier', sp_nq)
        if nq is not None:
            name_id.setAttribute('NameQualifier', nq)
        if sp_format is not None:
            name_id.setAttribute('Format', sp_format)
        name_id.appendChild(doc.createTextNode(value))
        name_id_container.appendChild(name_id)

        if cert is not None:
            xml = name_id_container.toxml()
            elem = fromstring(xml, forbid_dtd=True)

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

            # Load the public cert
            mngr = xmlsec.KeysMngr()
            file_cert = OneLogin_Saml2_Utils.write_temp_file(cert)
            key_data = xmlsec.Key.load(file_cert.name, xmlsec.KeyDataFormatCertPem, None)
            key_data.name = basename(file_cert.name)
            mngr.addKey(key_data)
            file_cert.close()

            # Prepare for encryption
            enc_data = EncData(xmlsec.TransformAes128Cbc, type=xmlsec.TypeEncElement)
            enc_data.ensureCipherValue()
            key_info = enc_data.ensureKeyInfo()
            # enc_key = key_info.addEncryptedKey(xmlsec.TransformRsaPkcs1)
            enc_key = key_info.addEncryptedKey(xmlsec.TransformRsaOaep)
            enc_key.ensureCipherValue()

            # Encrypt!
            enc_ctx = xmlsec.EncCtx(mngr)
            enc_ctx.encKey = xmlsec.Key.generate(xmlsec.KeyDataAes, 128, xmlsec.KeyDataTypeSession)

            edata = enc_ctx.encryptXml(enc_data, elem[0])

            newdoc = parseString(tostring(edata, encoding='unicode').encode('utf-8'), forbid_dtd=True)

            if newdoc.hasChildNodes():
                child = newdoc.firstChild
                child.removeAttribute('xmlns')
                child.removeAttribute('xmlns:saml')
                child.setAttribute('xmlns:xenc', OneLogin_Saml2_Constants.NS_XENC)
                child.setAttribute('xmlns:dsig', OneLogin_Saml2_Constants.NS_DS)

            nodes = newdoc.getElementsByTagName("*")
            for node in nodes:
                if node.tagName == 'ns0:KeyInfo':
                    node.tagName = 'dsig:KeyInfo'
                    node.removeAttribute('xmlns:ns0')
                    node.setAttribute('xmlns:dsig', OneLogin_Saml2_Constants.NS_DS)
                else:
                    node.tagName = 'xenc:' + node.tagName

            encrypted_id = newdoc.createElement('saml:EncryptedID')
            encrypted_data = newdoc.replaceChild(encrypted_id, newdoc.firstChild)
            encrypted_id.appendChild(encrypted_data)
            return newdoc.saveXML(encrypted_id)
        else:
            return doc.saveXML(name_id)
def generate_name_id ( value , sp_nq , sp_format = None , cert = None , debug = False , nq = None ) : doc = Document ( ) name_id_container = doc . createElementNS ( OneLogin_Saml2_Constants . NS_SAML , 'container' ) name_id_container . setAttribute ( "xmlns:saml" , OneLogin_Saml2_Constants . NS_SAML ) name_id = doc . createElement ( 'saml:NameID' ) if sp_nq is not None : name_id . setAttribute ( 'SPNameQualifier' , sp_nq ) if nq is not None : name_id . setAttribute ( 'NameQualifier' , nq ) if sp_format is not None : name_id . setAttribute ( 'Format' , sp_format ) name_id . appendChild ( doc . createTextNode ( value ) ) name_id_container . appendChild ( name_id ) if cert is not None : xml = name_id_container . toxml ( ) elem = fromstring ( xml , forbid_dtd = True ) error_callback_method = None if debug : error_callback_method = print_xmlsec_errors xmlsec . set_error_callback ( error_callback_method ) mngr = xmlsec . KeysMngr ( ) file_cert = OneLogin_Saml2_Utils . write_temp_file ( cert ) key_data = xmlsec . Key . load ( file_cert . name , xmlsec . KeyDataFormatCertPem , None ) key_data . name = basename ( file_cert . name ) mngr . addKey ( key_data ) file_cert . close ( ) enc_data = EncData ( xmlsec . TransformAes128Cbc , type = xmlsec . TypeEncElement ) enc_data . ensureCipherValue ( ) key_info = enc_data . ensureKeyInfo ( ) enc_key = key_info . addEncryptedKey ( xmlsec . TransformRsaOaep ) enc_key . ensureCipherValue ( ) enc_ctx = xmlsec . EncCtx ( mngr ) enc_ctx . encKey = xmlsec . Key . generate ( xmlsec . KeyDataAes , 128 , xmlsec . KeyDataTypeSession ) edata = enc_ctx . encryptXml ( enc_data , elem [ 0 ] ) newdoc = parseString ( tostring ( edata , encoding = 'unicode' ) . encode ( 'utf-8' ) , forbid_dtd = True ) if newdoc . hasChildNodes ( ) : child = newdoc . firstChild child . removeAttribute ( 'xmlns' ) child . removeAttribute ( 'xmlns:saml' ) child . setAttribute ( 'xmlns:xenc' , OneLogin_Saml2_Constants . NS_XENC ) child . setAttribute ( 'xmlns:dsig' , OneLogin_Saml2_Constants . NS_DS ) nodes = newdoc . getElementsByTagName ( "*" ) for node in nodes : if node . tagName == 'ns0:KeyInfo' : node . tagName = 'dsig:KeyInfo' node . removeAttribute ( 'xmlns:ns0' ) node . setAttribute ( 'xmlns:dsig' , OneLogin_Saml2_Constants . NS_DS ) else : node . tagName = 'xenc:' + node . tagName encrypted_id = newdoc . createElement ( 'saml:EncryptedID' ) encrypted_data = newdoc . replaceChild ( encrypted_id , newdoc . firstChild ) encrypted_id . appendChild ( encrypted_data ) return newdoc . saveXML ( encrypted_id ) else : return doc . saveXML ( name_id )
Generates a nameID.

        :param value: fingerprint
        :type: string

        :param sp_nq: SP Name Qualifier
        :type: string

        :param sp_format: SP Format
        :type: string

        :param cert: IdP Public Cert to encrypt the nameID
        :type: string

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

        :param nq: IDP Name Qualifier
        :type: string

        :returns: DOMElement | XMLSec nameID
        :rtype: string