Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atualização para suporte da nova versão signxml #355

Merged
merged 6 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading