Passed
Push — main ( 78d355...42a745 )
by Sat CFDI
01:49
created

satdigitalinvoice.utils.cert_info()   A

Complexity

Conditions 2

Size

Total Lines 14
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 14
rs 9.8
c 0
b 0
f 0
cc 2
nop 1
1
import base64
2
import logging
3
import random
4
from datetime import datetime
5
from uuid import UUID
6
7
from satcfdi import Signer
8
9
10
def to_uuid(s):
11
    try:
12
        return UUID(s)
13
    except ValueError:
14
        return None
15
16
17
def random_string():
18
    hash = random.randbytes(32)
19
    res = base64.urlsafe_b64encode(hash).decode()
20
    return res.rstrip("=")
21
22
23
def add_file_handler():
24
    fh = logging.FileHandler(
25
        f'.data/errors.log',
26
        mode='a'
27
    )
28
    fh.setLevel(logging.ERROR)
29
    formatter = logging.Formatter('%(asctime)s - %(message)s')
30
    fh.setFormatter(formatter)
31
    logging.root.addHandler(fh)
32
33
34
def convert_ans1_date(ans1_date):
35
    return datetime.strptime(ans1_date.decode('utf-8'), '%Y%m%d%H%M%SZ')
36
37
38
def cert_info(signer: Signer):
39
    if signer:
40
        return {
41
            "NoCertificado": signer.certificate_number,
42
            "Tipo": str(signer.type),
43
44
            "organizationName": signer.certificate.get_subject().O,
45
            "x500UniqueIdentifier": signer.certificate.get_subject().x500UniqueIdentifier,
46
            "serialNumber": signer.certificate.get_subject().serialNumber,
47
            "organizationUnitName": signer.certificate.get_subject().OU,
48
            "emailAddress": signer.certificate.get_subject().emailAddress,
49
50
            "notAfter": convert_ans1_date(signer.certificate.get_notAfter()),
51
            "notBefore": convert_ans1_date(signer.certificate.get_notBefore()),
52
        }