Skip to content

Commit

Permalink
Atualização para suporte da nova versão signxml (#355)
Browse files Browse the repository at this point in the history
* Fix update 4.0.0 of signxml

* Fix X509Certificate at xml

* Change order

* Remove just whitespace

* Sign autofill?

* Fix lint errors
  • Loading branch information
felps-dev authored Sep 2, 2024
1 parent 81ea4a7 commit 2123b75
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 30 deletions.
19 changes: 4 additions & 15 deletions pynfe/entidades/certificado.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@ def separar_arquivo(self, senha, caminho=False):
(
chave,
cert,
) = pkcs12.load_key_and_certificates(
cert_conteudo, senha
)[:2]
) = pkcs12.load_key_and_certificates(cert_conteudo, senha)[:2]
except Exception as e:
if "invalid password" in str(e).lower():
raise Exception(
"Falha ao carregar certificado digital A1. Verifique a senha do"
" certificado."
"Falha ao carregar certificado digital A1. Verifique a senha do" " certificado."
) from e
else:
raise Exception(
Expand All @@ -84,24 +81,16 @@ def separar_arquivo(self, senha, caminho=False):
arqcert.write(cert.public_bytes(Encoding.PEM))
with tempfile.NamedTemporaryFile(delete=False) as arqchave:
arqchave.write(
chave.private_bytes(
Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()
)
chave.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())
)
self.arquivos_temp.append(arqchave.name)
self.arquivos_temp.append(arqcert.name)
return arqchave.name, arqcert.name
else:
# Certificado
cert = cert.public_bytes(Encoding.PEM).decode("utf-8")
cert = cert.replace("\n", "")
cert = cert.replace("-----BEGIN CERTIFICATE-----", "")
cert = cert.replace("-----END CERTIFICATE-----", "")

# Chave, string decodificada da chave privada
chave = chave.private_bytes(
Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()
)
chave = chave.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())

return chave, cert

Expand Down
17 changes: 4 additions & 13 deletions pynfe/processamento/assinatura.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# -*- coding: utf-8 -*-
from pynfe.utils import etree, remover_acentos, CustomXMLSigner
from pynfe.utils.flags import NAMESPACE_SIG
import signxml

from pynfe.entidades import CertificadoA1
from pynfe.utils import CustomXMLSigner, etree, remover_acentos


class Assinatura(object):
Expand Down Expand Up @@ -31,9 +31,7 @@ def assinar(self, xml, retorna_string=False):
reference = xml.find(".//*[@Id]").attrib["Id"]

# retira acentos
xml_str = remover_acentos(
etree.tostring(xml, encoding="unicode", pretty_print=False)
)
xml_str = remover_acentos(etree.tostring(xml, encoding="unicode", pretty_print=False))
xml = etree.fromstring(xml_str)

signer = CustomXMLSigner(
Expand All @@ -48,14 +46,7 @@ def assinar(self, xml, retorna_string=False):
signer.namespaces = ns

ref_uri = ("#%s" % reference) if reference else None
signed_root = signer.sign(
xml, key=self.key, cert=self.cert, reference_uri=ref_uri
)

ns = {"ns": NAMESPACE_SIG}
# coloca o certificado na tag X509Data/X509Certificate
tagX509Data = signed_root.find(".//ns:X509Data", namespaces=ns)
etree.SubElement(tagX509Data, "X509Certificate").text = self.cert
signed_root = signer.sign(xml, key=self.key, cert=self.cert, reference_uri=ref_uri)
if retorna_string:
return etree.tostring(signed_root, encoding="unicode", pretty_print=False)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def _BuildAutomaton():
)


def _BuildAutomaton_():
def _BuildAutomaton_(): # noqa
# Remove this helper function from the namespace after it is invoked
global _BuildAutomaton_
del _BuildAutomaton_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def _BuildAutomaton():
)


def _BuildAutomaton_():
def _BuildAutomaton_(): # noqa
# Remove this helper function from the namespace after it is invoked
global _BuildAutomaton_
del _BuildAutomaton_
Expand Down

0 comments on commit 2123b75

Please sign in to comment.