Skip to content

Commit

Permalink
Rename file
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelobelli committed Dec 28, 2018
1 parent 19083c8 commit 8969952
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
39 changes: 0 additions & 39 deletions PyNFSe/base/certificado.py

This file was deleted.

33 changes: 33 additions & 0 deletions PyNFSe/base/certificate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from tempfile import NamedTemporaryFile
from typing import Tuple

from OpenSSL import crypto


def get_certificate(cfx_certificate_filepath: str, password: str) -> Tuple:
with open(cfx_certificate_filepath, 'rb') as cert_pfx:
pkcs12 = crypto.load_pkcs12(cert_pfx.read(), password.encode())

cert = crypto.dump_certificate(crypto.FILETYPE_PEM, pkcs12.get_certificate())
key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkcs12.get_privatekey())

cert_ca = b''

if pkcs12.get_ca_certificates():
for ca in pkcs12.get_ca_certificates():
cert_ca = crypto.dump_certificate(crypto.FILETYPE_PEM, ca) + cert_ca

cert_ca = cert + cert_ca

cert_file = _create_temp_file(cert_ca)
key_file = _create_temp_file(key)

return cert_ca, cert_file, key, key_file


def _create_temp_file(content: bytes) -> NamedTemporaryFile:
temp_file = NamedTemporaryFile()
temp_file.write(content)
temp_file.seek(0)

return temp_file
4 changes: 2 additions & 2 deletions PyNFSe/nfse/pr/curitiba/_facade.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PyNFSe.base.certificado import certificado as c
from PyNFSe.base.certificate import get_certificate
from PyNFSe.base.nfse_signer import NFSeSigner
from PyNFSe.nfse.pr.curitiba import serializacao as s
from PyNFSe.nfse.pr.curitiba.comunicacao import Comunicacao
Expand All @@ -11,7 +11,7 @@ def __init__(self, certificado_pfx, senha, producao=False):
url_homologacao = 'https://pilotoisscuritiba.curitiba.pr.gov.br/nfse_ws/NfseWs.asmx?WSDL'
url_producao = 'https://isscuritiba.curitiba.pr.gov.br/Iss.NfseWebService/nfsews.asmx?WSDL'

self.cert, self.cert_file, self.key, self.key_file = c(certificado_pfx, senha)
self.cert, self.cert_file, self.key, self.key_file = get_certificate(certificado_pfx, senha)
url_ambiente = url_producao if producao else url_homologacao
cert_file_and_key_file = (self.cert_file.name, self.key_file.name)

Expand Down
4 changes: 2 additions & 2 deletions tests/base/test_nfse_signer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import pytest
from PyNFSe.base.certificado import certificado
from PyNFSe.base.certificate import get_certificate
from PyNFSe.base.nfse_signer import NFSeSigner


@pytest.fixture
def signer(certificate_file_path, certificate_password):
namespace = '{http://isscuritiba.curitiba.pr.gov.br/iss/nfse.xsd}'
cert, _, key, _ = certificado(certificate_file_path, certificate_password)
cert, _, key, _ = get_certificate(certificate_file_path, certificate_password)
return NFSeSigner(cert, key, namespace)


Expand Down

0 comments on commit 8969952

Please sign in to comment.