121 lines
2.9 KiB
Python
121 lines
2.9 KiB
Python
#!/usr/bin/env python
|
|
|
|
import getpass
|
|
import logging
|
|
|
|
from pathlib import Path
|
|
|
|
from .cfdi_cert import SATCertificate
|
|
from .sat import portal_sat
|
|
|
|
|
|
LOG_FORMAT = '%(asctime)s - %(levelname)s - %(message)s'
|
|
LOG_DATE = '%d/%m/%Y %H:%M:%S'
|
|
logging.addLevelName(logging.ERROR, '\033[1;41mERROR\033[1;0m')
|
|
logging.addLevelName(logging.DEBUG, '\x1b[33mDEBUG\033[1;0m')
|
|
logging.addLevelName(logging.INFO, '\x1b[32mINFO\033[1;0m')
|
|
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=LOG_DATE)
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def fiel_validate(args):
|
|
path_fiel = args.fiel_dir
|
|
if not path_fiel:
|
|
msg = 'El argumento -fd (fiel-dir) es requerido'
|
|
log.error(msg)
|
|
return
|
|
|
|
path_fiel = Path(path_fiel)
|
|
if not path_fiel.is_dir():
|
|
msg = 'La ruta no es un directorio'
|
|
log.error(msg)
|
|
return
|
|
|
|
path_fiel_cer = path_fiel / f'{args.fiel_nombre}.cer'
|
|
if not path_fiel_cer.exists():
|
|
msg = 'No se encontró el archivo CER'
|
|
log.error(msg)
|
|
return
|
|
|
|
path_fiel_key = path_fiel / f'{args.fiel_nombre}.key'
|
|
if not path_fiel_cer.exists():
|
|
msg = 'No se encontró el archivo KEY'
|
|
log.error(msg)
|
|
return
|
|
|
|
cer = path_fiel_cer.read_bytes()
|
|
key = path_fiel_key.read_bytes()
|
|
|
|
cert = SATCertificate(cer)
|
|
|
|
if not cert.is_valid:
|
|
log.error(cert.error)
|
|
return
|
|
|
|
password = getpass.getpass('Captura la contraseña de la FIEL: ')
|
|
|
|
cert.validate_key(key, password)
|
|
|
|
if not cert.is_valid:
|
|
log.error(cert.error)
|
|
return
|
|
|
|
if not cert.is_fiel:
|
|
msg = 'El certificado no es Fiel, no puedes usarlo para descargar.'
|
|
log.error(msg)
|
|
return
|
|
|
|
path_fiel_enc = path_fiel / f'{args.fiel_nombre}.enc'
|
|
path_fiel_enc.write_bytes(cert.key_enc)
|
|
|
|
msg = f'El certificado es válido.\n{cert}\n\tPuedes usarlo para descargar.\n'
|
|
log.info(msg)
|
|
|
|
return
|
|
|
|
|
|
def _get_certificate(path_fiel, name_fiel):
|
|
if not path_fiel:
|
|
msg = 'El argumento -fd (fiel-dir) es requerido'
|
|
log.error(msg)
|
|
return
|
|
|
|
path_fiel = Path(path_fiel)
|
|
path_fiel_cer = path_fiel / f'{name_fiel}.cer'
|
|
path_fiel_key = path_fiel / f'{name_fiel}.enc'
|
|
path_fiel_pem = path_fiel / f'{name_fiel}.pem'
|
|
|
|
cer = path_fiel_cer.read_bytes()
|
|
|
|
certificate = SATCertificate(cer)
|
|
if not certificate.is_valid:
|
|
log.error(certificate.error)
|
|
return
|
|
|
|
if path_fiel_key.exists():
|
|
certificate.key = path_fiel_key.read_bytes()
|
|
if path_fiel_pem.exists():
|
|
certificate.key_pem = path_fiel_pem.read_bytes()
|
|
|
|
return certificate
|
|
|
|
|
|
def _validate_arguments(args):
|
|
cert = _get_certificate(args.fiel_dir, args.fiel_nombre)
|
|
if cert is None:
|
|
return {}, None
|
|
|
|
data = {'ok': True}
|
|
|
|
return data, cert
|
|
|
|
|
|
def download(args):
|
|
data, cert = _validate_arguments(args)
|
|
if not data:
|
|
return
|
|
|
|
portal_sat(data, cert)
|
|
|
|
return
|