Last active
February 22, 2019 14:24
-
-
Save EstebanFuentealba/d8f2e60b2b2f1bac13ba to your computer and use it in GitHub Desktop.
Autenticacion en Python - Servicio de Impuestos Internos - SII
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import libxml2 | |
import xmlsec | |
from SOAPpy import SOAPProxy | |
import xml.etree.ElementTree as ET | |
#pip install SOAPpy | |
#svn checkout svn://labs.libre-entreprise.org/svnroot/pyxmlsec | |
TEMPLATE_XML_SEMILLA = """<?xml version="1.0"?><getToken><item><Semilla>{semilla}</Semilla></item><Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><SignedInfo><CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/><SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><Reference URI=""><Transforms><Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></Transforms><DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><DigestValue/></Reference></SignedInfo><SignatureValue/><KeyInfo><KeyValue><RSAKeyValue><Modulus/><Exponent/></RSAKeyValue></KeyValue><X509Data><X509Certificate/></X509Data></KeyInfo></Signature></getToken>""" | |
class AutenticacionSII(object): | |
def __init__(self): | |
rutaCertificado = '/home/ubuntu/contribuyente1.p12' | |
claveCertificado = 'cert123' | |
xmlSemilla = self.ObtenerXMLSemilla() | |
semillaXmlFirmada = self.FirmarXMLSemilla(xmlSemilla, rutaCertificado, claveCertificado) | |
token = self.ObtenerToken(semillaXmlFirmada) | |
return token | |
def ObtenerXMLSemilla(self): | |
url = 'https://maullin.sii.cl/DTEWS/CrSeed.jws?WSDL' | |
namespace = 'urn:https://maullin.sii.cl/DTEWS/CrSeed.jws' | |
servicio = SOAPProxy(url, namespace) | |
raiz = ET.fromstring(servicio.getSeed()) | |
semilla = raiz[0][0].text | |
xmlSemilla =TEMPLATE_XML_SEMILLA.format(semilla = semilla) | |
return xmlSemilla | |
def FirmarXMLSemilla(self, xmlSemilla, rutaCertificado, claveCertificado): | |
libxml2.initParser() | |
libxml2.substituteEntitiesDefault(1) | |
xmlsec.init() | |
xmlsec.cryptoAppInit(None) | |
xmlsec.cryptoInit() | |
doc_xml = libxml2.parseMemory(xmlSemilla, len(xmlSemilla)) | |
nodo_firma = xmlsec.findNode(doc_xml.getRootElement(), | |
xmlsec.NodeSignature, | |
xmlsec.DSigNs,) | |
contexto_firma = xmlsec.DSigCtx() | |
llave = xmlsec.cryptoAppKeyLoad(filename=str(rutaCertificado), | |
format=xmlsec.KeyDataFormatPkcs12, | |
pwd=str(claveCertificado), | |
pwdCallback=None, | |
pwdCallbackCtx=None,) | |
contexto_firma.signKey = llave | |
contexto_firma.sign(nodo_firma) | |
return str(doc_xml) | |
def ObtenerToken(self, semillaXmlFirmada): | |
url = 'https://maullin.sii.cl/DTEWS/GetTokenFromSeed.jws?WSDL' | |
namespace = 'urn:https://maullin.sii.cl/DTEWS/GetTokenFromSeed.jws' | |
servicio = SOAPProxy(url, namespace) | |
raiz = ET.fromstring(servicio.getToken(semillaXmlFirmada)) | |
token = raiz[0][1].text | |
return token |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tengo este error
AttributeError: 'module' object has no attribute 'cryptoAppInit'