Skip to content

Commit

Permalink
pyopenssl dependent endesive now optional
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalyi authored and renatonlima committed Mar 15, 2022
1 parent 3e6c80f commit e79ae88
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pytz>=2016.7
cryptography==3.3.2
signxml==2.8.2
endesive==2.0.1
chardet==3.0.4
15 changes: 13 additions & 2 deletions src/erpbrasil/assinatura/assinatura.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# coding=utf-8
import logging
import signxml
from base64 import b64encode
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import padding
from endesive import pdf
from lxml import etree
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA
from Crypto.Signature import PKCS1_v1_5
from hashlib import sha1


_logger = logging.getLogger(__name__)


class Assinatura(object):

def __init__(self, certificado):
Expand Down Expand Up @@ -126,6 +128,15 @@ def assina_string(self, message):
return signature

def assina_pdf(self, arquivo, dados_assinatura, algoritmo='sha256'):
try:
from endesive import pdf
except ImportError:
_logger.info(
"assina_pdf requires the https://github.com/m32/endesive"
"package but it is not bundled by default"
"to avoid depending on pyopenssl which is deprecated"
)
return False
return pdf.cms.sign(
datau=arquivo,
udct=dados_assinatura,
Expand Down
25 changes: 23 additions & 2 deletions tests/test_erpbrasil_assinatura_pdf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# coding=utf-8

import logging
import os
import tempfile
from datetime import datetime
Expand All @@ -18,6 +17,8 @@

test_path = os.environ.get('test_path', 'tests/')

_logger = logging.getLogger(__name__)


def test_assinatura_nfe_pdf():
certificado = Certificado(certificado_nfe_caminho, certificado_nfe_senha, raise_expirado=False)
Expand All @@ -35,6 +36,16 @@ def test_assinatura_nfe_pdf():
'reason': 'Teste assinatura',
}

try:
from endesive import pdf
except ImportError:
_logger.info(
"skipping test because https://github.com/m32/endesive"
"package but it is not installed. It is not bundled by default"
"to avoid depending on pyopenssl which is deprecated."
)
return False

assinatura = assinador.assina_pdf(
arquivo=arquivo,
dados_assinatura=dados_assinatura,
Expand All @@ -61,6 +72,16 @@ def test_assinatura_multipla_pdf():
'reason': 'Teste Assinatura CPF',
}

try:
from endesive import pdf
except ImportError:
_logger.info(
"skipping test because https://github.com/m32/endesive"
"package but it is not installed. It is not bundled by default"
"to avoid depending on pyopenssl which is deprecated."
)
return False

assinatura1 = assinador_ecpf.assina_pdf(
arquivo=arquivo,
dados_assinatura=dados_assinatura,
Expand Down

0 comments on commit e79ae88

Please sign in to comment.