se usa POLICY_ID desde archivo local, actualizacion de commit [47fd230ff9]
FossilOrigin-Name: 190ab3228ef9e3fbfb4210ae74a8e332cf0aa9d6a0d476e561164bd76ae772f0
This commit is contained in:
parent
a9564f9a70
commit
417de535f3
@ -227,7 +227,7 @@ def sign_xml(private_key, passphrase, xmlfile, ssl=True, use_cache_policy=False,
|
|||||||
if use_cache_policy:
|
if use_cache_policy:
|
||||||
warnings.warn("xades using cache policy")
|
warnings.warn("xades using cache policy")
|
||||||
|
|
||||||
signer = fe.DianXMLExtensionSigner(private_key, passphrase=passphrase, mockpolicy=use_cache_policy)
|
signer = fe.DianXMLExtensionSigner(private_key, passphrase=passphrase, localpolicy=use_cache_policy)
|
||||||
document = open(xmlfile, 'r').read().encode('utf-8')
|
document = open(xmlfile, 'r').read().encode('utf-8')
|
||||||
with open(output, 'w') as f:
|
with open(output, 'w') as f:
|
||||||
f.write(signer.sign_xml_string(document))
|
f.write(signer.sign_xml_string(document))
|
||||||
@ -358,7 +358,7 @@ def sign_verify_xml(private_key, passphrase, xmlfile, ssl=True, use_cache_policy
|
|||||||
warnings.warn("xades using cache policy")
|
warnings.warn("xades using cache policy")
|
||||||
|
|
||||||
print("THIS ONLY WORKS FOR DOCUMENTS GENERATE WITH FACHO")
|
print("THIS ONLY WORKS FOR DOCUMENTS GENERATE WITH FACHO")
|
||||||
signer = fe.DianXMLExtensionSignerVerifier(private_key, passphrase=passphrase, mockpolicy=use_cache_policy)
|
signer = fe.DianXMLExtensionSignerVerifier(private_key, passphrase=passphrase, localpolicy=use_cache_policy)
|
||||||
document = open(xmlfile, 'r').read().encode('utf-8')
|
document = open(xmlfile, 'r').read().encode('utf-8')
|
||||||
|
|
||||||
if signer.verify_string(document):
|
if signer.verify_string(document):
|
||||||
|
@ -26,9 +26,8 @@ SCHEME_AGENCY_ATTRS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pwd = Path(__file__).parent
|
|
||||||
# RESOLUCION 0001: pagina 516
|
# RESOLUCION 0001: pagina 516
|
||||||
POLICY_ID = 'file://'+str(pwd)+'/data/dian/politicadefirmav2.pdf'
|
POLICY_ID = 'https://facturaelectronica.dian.gov.co/politicadefirma/v2/politicadefirmav2.pdf'
|
||||||
POLICY_NAME = u'Política de firma para facturas electrónicas de la República de Colombia.'
|
POLICY_NAME = u'Política de firma para facturas electrónicas de la República de Colombia.'
|
||||||
|
|
||||||
|
|
||||||
@ -62,6 +61,7 @@ def mock_xades_policy():
|
|||||||
with patch('xades.policy.urllib.urlopen') as mock:
|
with patch('xades.policy.urllib.urlopen') as mock:
|
||||||
class UrllibPolicyMock:
|
class UrllibPolicyMock:
|
||||||
def read(self):
|
def read(self):
|
||||||
|
# Usamos contenido de archivo local
|
||||||
cur_dir = os.path.dirname(os.path.abspath(__file__))
|
cur_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
data_dir = os.path.join(cur_dir, 'data', 'dian')
|
data_dir = os.path.join(cur_dir, 'data', 'dian')
|
||||||
policy_file = os.path.join(data_dir, 'politicadefirmav2.pdf')
|
policy_file = os.path.join(data_dir, 'politicadefirmav2.pdf')
|
||||||
@ -281,20 +281,20 @@ class DianXMLExtensionSoftwareSecurityCode(FachoXMLExtension):
|
|||||||
|
|
||||||
class DianXMLExtensionSigner:
|
class DianXMLExtensionSigner:
|
||||||
|
|
||||||
def __init__(self, pkcs12_path, passphrase=None, mockpolicy=False):
|
def __init__(self, pkcs12_path, passphrase=None, localpolicy=True):
|
||||||
self._pkcs12_data = open(pkcs12_path, 'rb').read()
|
self._pkcs12_data = open(pkcs12_path, 'rb').read()
|
||||||
self._passphrase = None
|
self._passphrase = None
|
||||||
self._mockpolicy = mockpolicy
|
self._localpolicy = localpolicy
|
||||||
if passphrase:
|
if passphrase:
|
||||||
self._passphrase = passphrase.encode('utf-8')
|
self._passphrase = passphrase.encode('utf-8')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_bytes(cls, data, passphrase=None, mockpolicy=False):
|
def from_bytes(cls, data, passphrase=None, localpolicy=True):
|
||||||
self = cls.__new__(cls)
|
self = cls.__new__(cls)
|
||||||
|
|
||||||
self._pkcs12_data = data
|
self._pkcs12_data = data
|
||||||
self._passphrase = None
|
self._passphrase = None
|
||||||
self._mockpolicy = mockpolicy
|
self._localpolicy = localpolicy
|
||||||
if passphrase:
|
if passphrase:
|
||||||
self._passphrase = passphrase.encode('utf-8')
|
self._passphrase = passphrase.encode('utf-8')
|
||||||
|
|
||||||
@ -360,7 +360,7 @@ class DianXMLExtensionSigner:
|
|||||||
ctx.load_pkcs12(OpenSSL.crypto.load_pkcs12(self._pkcs12_data,
|
ctx.load_pkcs12(OpenSSL.crypto.load_pkcs12(self._pkcs12_data,
|
||||||
self._passphrase))
|
self._passphrase))
|
||||||
|
|
||||||
if self._mockpolicy:
|
if self._localpolicy:
|
||||||
with mock_xades_policy():
|
with mock_xades_policy():
|
||||||
ctx.sign(signature)
|
ctx.sign(signature)
|
||||||
ctx.verify(signature)
|
ctx.verify(signature)
|
||||||
@ -480,10 +480,10 @@ class DianZIP:
|
|||||||
|
|
||||||
class DianXMLExtensionSignerVerifier:
|
class DianXMLExtensionSignerVerifier:
|
||||||
|
|
||||||
def __init__(self, pkcs12_path_or_bytes, passphrase=None, mockpolicy=False):
|
def __init__(self, pkcs12_path_or_bytes, passphrase=None, localpolicy=True):
|
||||||
self._pkcs12_path_or_bytes = pkcs12_path_or_bytes
|
self._pkcs12_path_or_bytes = pkcs12_path_or_bytes
|
||||||
self._passphrase = None
|
self._passphrase = None
|
||||||
self._mockpolicy = mockpolicy
|
self._localpolicy = localpolicy
|
||||||
if passphrase:
|
if passphrase:
|
||||||
self._passphrase = passphrase.encode('utf-8')
|
self._passphrase = passphrase.encode('utf-8')
|
||||||
|
|
||||||
@ -508,7 +508,7 @@ class DianXMLExtensionSignerVerifier:
|
|||||||
ctx.load_pkcs12(OpenSSL.crypto.load_pkcs12(pkcs12_data,
|
ctx.load_pkcs12(OpenSSL.crypto.load_pkcs12(pkcs12_data,
|
||||||
self._passphrase))
|
self._passphrase))
|
||||||
try:
|
try:
|
||||||
if self._mockpolicy:
|
if self._localpolicy:
|
||||||
with mock_xades_policy():
|
with mock_xades_policy():
|
||||||
ctx.verify(signature)
|
ctx.verify(signature)
|
||||||
else:
|
else:
|
||||||
|
@ -13,7 +13,7 @@ def DIANWriteSigned(xml, filename, private_key, passphrase, use_cache_policy=Fal
|
|||||||
if dian_signer is None:
|
if dian_signer is None:
|
||||||
dian_signer = fe.DianXMLExtensionSigner
|
dian_signer = fe.DianXMLExtensionSigner
|
||||||
|
|
||||||
signer = dian_signer(private_key, passphrase=passphrase, mockpolicy=use_cache_policy)
|
signer = dian_signer(private_key, passphrase=passphrase, localpolicy=use_cache_policy)
|
||||||
|
|
||||||
with open(filename, 'w') as f:
|
with open(filename, 'w') as f:
|
||||||
f.write(signer.sign_xml_string(document))
|
f.write(signer.sign_xml_string(document))
|
||||||
|
@ -284,8 +284,8 @@ class InformacionGeneral:
|
|||||||
|
|
||||||
class DianXMLExtensionSigner(fe.DianXMLExtensionSigner):
|
class DianXMLExtensionSigner(fe.DianXMLExtensionSigner):
|
||||||
|
|
||||||
def __init__(self, pkcs12_path, passphrase=None, mockpolicy=False):
|
def __init__(self, pkcs12_path, passphrase=None, localpolicy=True):
|
||||||
super().__init__(pkcs12_path, passphrase=passphrase, mockpolicy=mockpolicy)
|
super().__init__(pkcs12_path, passphrase=passphrase, localpolicy=localpolicy)
|
||||||
|
|
||||||
def _element_extension_content(self, fachoxml):
|
def _element_extension_content(self, fachoxml):
|
||||||
return fachoxml.builder.xpath(fachoxml.root, './ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent')
|
return fachoxml.builder.xpath(fachoxml.root, './ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent')
|
||||||
|
5
setup.py
5
setup.py
@ -21,11 +21,14 @@ requirements = ['Click>=6.0',
|
|||||||
'xmlsig==0.1.7',
|
'xmlsig==0.1.7',
|
||||||
'xades==0.2.2',
|
'xades==0.2.2',
|
||||||
'xmlsec==1.3.12',
|
'xmlsec==1.3.12',
|
||||||
|
# usamos esta dependencia en runtime
|
||||||
|
# para forzar uso de policy_id de archivo local
|
||||||
|
'mock>=2.0.0',
|
||||||
'xmlschema>=1.8']
|
'xmlschema>=1.8']
|
||||||
|
|
||||||
setup_requirements = ['pytest-runner', ]
|
setup_requirements = ['pytest-runner', ]
|
||||||
|
|
||||||
test_requirements = ['pytest', 'mock>=2.0.0']
|
test_requirements = ['pytest', ]
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
author="Jovany Leandro G.C",
|
author="Jovany Leandro G.C",
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
|
|
||||||
def mock_urlopen(m):
|
|
||||||
import urllib.request
|
|
||||||
def mock(url):
|
|
||||||
class FakeIO:
|
|
||||||
def read(*args):
|
|
||||||
return "X".encode('utf-8')
|
|
||||||
return FakeIO()
|
|
||||||
m.setattr(urllib.request, "urlopen", mock)
|
|
@ -19,10 +19,7 @@ def test_xmlsigned_build(monkeypatch):
|
|||||||
xml = fe.FeXML('Invoice',
|
xml = fe.FeXML('Invoice',
|
||||||
'http://www.dian.gov.co/contratos/facturaelectronica/v1')
|
'http://www.dian.gov.co/contratos/facturaelectronica/v1')
|
||||||
|
|
||||||
|
signer.sign_xml_element(xml.root)
|
||||||
with monkeypatch.context() as m:
|
|
||||||
helpers.mock_urlopen(m)
|
|
||||||
signer.sign_xml_element(xml.root)
|
|
||||||
|
|
||||||
elem = xml.find_or_create_element('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/ds:Signature')
|
elem = xml.find_or_create_element('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/ds:Signature')
|
||||||
|
|
||||||
@ -38,9 +35,7 @@ def test_xmlsigned_with_passphrase_build(monkeypatch):
|
|||||||
xml = fe.FeXML('Invoice',
|
xml = fe.FeXML('Invoice',
|
||||||
'http://www.dian.gov.co/contratos/facturaelectronica/v1')
|
'http://www.dian.gov.co/contratos/facturaelectronica/v1')
|
||||||
|
|
||||||
with monkeypatch.context() as m:
|
signer.sign_xml_element(xml.root)
|
||||||
helpers.mock_urlopen(m)
|
|
||||||
signer.sign_xml_element(xml.root)
|
|
||||||
|
|
||||||
elem = xml.find_or_create_element('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/ds:Signature')
|
elem = xml.find_or_create_element('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/ds:Signature')
|
||||||
|
|
||||||
@ -105,10 +100,7 @@ def test_xml_sign_dian(monkeypatch):
|
|||||||
xmlstring = xml.tostring()
|
xmlstring = xml.tostring()
|
||||||
print(xmlstring)
|
print(xmlstring)
|
||||||
signer = fe.DianXMLExtensionSigner('./tests/example.p12')
|
signer = fe.DianXMLExtensionSigner('./tests/example.p12')
|
||||||
|
xmlsigned = signer.sign_xml_string(xmlstring)
|
||||||
with monkeypatch.context() as m:
|
|
||||||
helpers.mock_urlopen(m)
|
|
||||||
xmlsigned = signer.sign_xml_string(xmlstring)
|
|
||||||
assert "Signature" in xmlsigned
|
assert "Signature" in xmlsigned
|
||||||
|
|
||||||
def test_xml_sign_dian_using_bytes(monkeypatch):
|
def test_xml_sign_dian_using_bytes(monkeypatch):
|
||||||
@ -122,7 +114,5 @@ def test_xml_sign_dian_using_bytes(monkeypatch):
|
|||||||
p12_data = open('./tests/example.p12', 'rb').read()
|
p12_data = open('./tests/example.p12', 'rb').read()
|
||||||
signer = fe.DianXMLExtensionSigner.from_bytes(p12_data)
|
signer = fe.DianXMLExtensionSigner.from_bytes(p12_data)
|
||||||
|
|
||||||
with monkeypatch.context() as m:
|
xmlsigned = signer.sign_xml_string(xmlstring)
|
||||||
helpers.mock_urlopen(m)
|
|
||||||
xmlsigned = signer.sign_xml_string(xmlstring)
|
|
||||||
assert "Signature" in xmlsigned
|
assert "Signature" in xmlsigned
|
||||||
|
@ -40,10 +40,7 @@ def test_invoicesimple_xml_signed(monkeypatch, simple_invoice):
|
|||||||
signer = fe.DianXMLExtensionSigner('./tests/example.p12')
|
signer = fe.DianXMLExtensionSigner('./tests/example.p12')
|
||||||
|
|
||||||
print(xml.tostring())
|
print(xml.tostring())
|
||||||
with monkeypatch.context() as m:
|
xml.add_extension(signer)
|
||||||
import helpers
|
|
||||||
helpers.mock_urlopen(m)
|
|
||||||
xml.add_extension(signer)
|
|
||||||
|
|
||||||
elem = xml.get_element('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension[2]/ext:ExtensionContent/ds:Signature')
|
elem = xml.get_element('/fe:Invoice/ext:UBLExtensions/ext:UBLExtension[2]/ext:ExtensionContent/ds:Signature')
|
||||||
assert elem.text is not None
|
assert elem.text is not None
|
||||||
|
@ -239,9 +239,7 @@ def test_nomina_xmlsign(monkeypatch):
|
|||||||
xml = nomina.toFachoXML()
|
xml = nomina.toFachoXML()
|
||||||
|
|
||||||
signer = fe.nomina.DianXMLExtensionSigner('./tests/example.p12')
|
signer = fe.nomina.DianXMLExtensionSigner('./tests/example.p12')
|
||||||
with monkeypatch.context() as m:
|
xml.add_extension(signer)
|
||||||
helpers.mock_urlopen(m)
|
|
||||||
xml.add_extension(signer)
|
|
||||||
|
|
||||||
print(xml.tostring())
|
print(xml.tostring())
|
||||||
elem = xml.get_element('/fe:NominaIndividual/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/ds:Signature')
|
elem = xml.get_element('/fe:NominaIndividual/ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent/ds:Signature')
|
||||||
|
Loading…
Reference in New Issue
Block a user