Passed
Push — master ( c87855...432701 )
by Olivier
02:29
created

asyncua.crypto.certificate_handler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A CertificateHandler.__init__() 0 2 1
A CertificateHandler.trust_certificate() 0 8 3
A CertificateHandler.__contains__() 0 4 1
1
from asyncua.crypto import uacrypto
2
import sys
3
import logging
4
sys.path.append('..')
5
6
7
class CertificateHandler:
8
    def __init__(self):
9
        self._trusted_certificates = {}
10
11
    async def trust_certificate(self, certificate_path: str, format: str = None, label: str = None):
12
        certificate = await uacrypto.load_certificate(certificate_path, format)
13
        if label is None:
14
            label = certificate_path
15
        if label in self._trusted_certificates:
16
            logging.warning(f"certificate with label {label} "
17
                            f"attempted to be added multiple times, only the last version will be kept.")
18
        self._trusted_certificates[label] = uacrypto.der_from_x509(certificate)
19
20
    def __contains__(self, certificate):
21
        return any(certificate == prospective_cert
22
                   for prospective_cert
23
                   in self._trusted_certificates.values())
24