Skip to content

Commit 2123b75

Browse files
authored
Atualização para suporte da nova versão signxml (#355)
* Fix update 4.0.0 of signxml * Fix X509Certificate at xml * Change order * Remove just whitespace * Sign autofill? * Fix lint errors
1 parent 81ea4a7 commit 2123b75

File tree

4 files changed

+10
-30
lines changed

4 files changed

+10
-30
lines changed

pynfe/entidades/certificado.py

+4-15
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,11 @@ def separar_arquivo(self, senha, caminho=False):
6464
(
6565
chave,
6666
cert,
67-
) = pkcs12.load_key_and_certificates(
68-
cert_conteudo, senha
69-
)[:2]
67+
) = pkcs12.load_key_and_certificates(cert_conteudo, senha)[:2]
7068
except Exception as e:
7169
if "invalid password" in str(e).lower():
7270
raise Exception(
73-
"Falha ao carregar certificado digital A1. Verifique a senha do"
74-
" certificado."
71+
"Falha ao carregar certificado digital A1. Verifique a senha do" " certificado."
7572
) from e
7673
else:
7774
raise Exception(
@@ -84,24 +81,16 @@ def separar_arquivo(self, senha, caminho=False):
8481
arqcert.write(cert.public_bytes(Encoding.PEM))
8582
with tempfile.NamedTemporaryFile(delete=False) as arqchave:
8683
arqchave.write(
87-
chave.private_bytes(
88-
Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()
89-
)
84+
chave.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())
9085
)
9186
self.arquivos_temp.append(arqchave.name)
9287
self.arquivos_temp.append(arqcert.name)
9388
return arqchave.name, arqcert.name
9489
else:
9590
# Certificado
9691
cert = cert.public_bytes(Encoding.PEM).decode("utf-8")
97-
cert = cert.replace("\n", "")
98-
cert = cert.replace("-----BEGIN CERTIFICATE-----", "")
99-
cert = cert.replace("-----END CERTIFICATE-----", "")
100-
10192
# Chave, string decodificada da chave privada
102-
chave = chave.private_bytes(
103-
Encoding.PEM, PrivateFormat.PKCS8, NoEncryption()
104-
)
93+
chave = chave.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())
10594

10695
return chave, cert
10796

pynfe/processamento/assinatura.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
from pynfe.utils import etree, remover_acentos, CustomXMLSigner
3-
from pynfe.utils.flags import NAMESPACE_SIG
42
import signxml
3+
54
from pynfe.entidades import CertificadoA1
5+
from pynfe.utils import CustomXMLSigner, etree, remover_acentos
66

77

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

3333
# retira acentos
34-
xml_str = remover_acentos(
35-
etree.tostring(xml, encoding="unicode", pretty_print=False)
36-
)
34+
xml_str = remover_acentos(etree.tostring(xml, encoding="unicode", pretty_print=False))
3735
xml = etree.fromstring(xml_str)
3836

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

5048
ref_uri = ("#%s" % reference) if reference else None
51-
signed_root = signer.sign(
52-
xml, key=self.key, cert=self.cert, reference_uri=ref_uri
53-
)
54-
55-
ns = {"ns": NAMESPACE_SIG}
56-
# coloca o certificado na tag X509Data/X509Certificate
57-
tagX509Data = signed_root.find(".//ns:X509Data", namespaces=ns)
58-
etree.SubElement(tagX509Data, "X509Certificate").text = self.cert
49+
signed_root = signer.sign(xml, key=self.key, cert=self.cert, reference_uri=ref_uri)
5950
if retorna_string:
6051
return etree.tostring(signed_root, encoding="unicode", pretty_print=False)
6152
else:

pynfe/utils/nfse/ginfes/servico_consultar_lote_rps_resposta_v03.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def _BuildAutomaton():
305305
)
306306

307307

308-
def _BuildAutomaton_():
308+
def _BuildAutomaton_(): # noqa
309309
# Remove this helper function from the namespace after it is invoked
310310
global _BuildAutomaton_
311311
del _BuildAutomaton_

pynfe/utils/nfse/ginfes/servico_consultar_nfse_resposta_v03.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def _BuildAutomaton():
294294
)
295295

296296

297-
def _BuildAutomaton_():
297+
def _BuildAutomaton_(): # noqa
298298
# Remove this helper function from the namespace after it is invoked
299299
global _BuildAutomaton_
300300
del _BuildAutomaton_

0 commit comments

Comments
 (0)