Projekt

Obecné

Profil

Stáhnout (3.23 KB) Statistiky
| Větev: | Tag: | Revize:
1
from injector import inject
2

    
3
from src.dao.private_key_repository import PrivateKeyRepository
4
from src.model.private_key import PrivateKey
5
from src.services.cryptography import CryptographyService
6
from src.utils.logger import Logger
7

    
8

    
9
class KeyService:
10

    
11
    @inject
12
    def __init__(self, cryptography_service: CryptographyService, private_key_repository: PrivateKeyRepository):
13
        self.cryptography_service = cryptography_service
14
        self.private_key_repository = private_key_repository
15

    
16
    def create_new_key(self, passphrase="") -> PrivateKey:
17
        """
18
        Creates a new private key using the given passphrase.
19
        :param passphrase: Passphrase to be used when encrypting the PK
20
        :return: An instance of the <PrivateKey> class representing the generated PK
21
        """
22

    
23
        Logger.debug("Function launched.")
24

    
25
        # generate a new private key
26
        private_key_pem = self.cryptography_service.create_private_key(passphrase)
27

    
28
        # store generated PK and the passphrase in a wrapper
29
        private_key = PrivateKey(-1, private_key_pem, passphrase)
30

    
31
        # store the wrapper in the PK repository
32
        private_key_id = self.private_key_repository.create(private_key)
33

    
34
        # assign the generated ID to the wrapper
35
        private_key.private_key_id = private_key_id
36

    
37
        return private_key
38

    
39
    def get_key(self, unique_id):
40
        """
41
        Tries to fetch a PK using the given ID.
42
        :param unique_id: ID of the PK to be found
43
        :return:An instance of the required PK or `None`
44
        """
45

    
46
        Logger.debug("Function launched.")
47

    
48
        return self.private_key_repository.read(unique_id)
49

    
50
    def get_keys(self, unique_ids=None):
51
        """
52
        Tries to fetch all PKs in the repository. Exact PKs to be fetched can be specified using the `unique_ids`
53
        parameter. If `unique_ids` parameter is not passed then all PKs in the repository are returned.
54
        :param unique_ids: An array containing IDs of PKs to be fetched from the repository.
55
        :return: A list of instances of the PrivateKey class representing the PKs found
56
        """
57

    
58
        Logger.debug("Function launched.")
59

    
60
        if unique_ids is None:
61
            return self.private_key_repository.read_all()
62
        else:
63
            # TODO this is very inefficient
64
            return [self.private_key_repository.read(identifier) for identifier in unique_ids]
65

    
66
    def delete_key(self, unique_id):
67
        """
68
        Deletes a private key
69

    
70
        :param unique_id: ID of specific certificate to be deleted
71
        :return: `True` when the deletion was successful. `False` in other case
72
        """
73

    
74
        Logger.debug("Function launched.")
75

    
76
        return self.private_key_repository.delete(unique_id)
77

    
78
    def get_public_key(self, private_key: PrivateKey):
79
        """
80
        Extracts a public key from the given private key
81
        :param private_key: private key from which a public key should be extracted
82
        :return: a string containing the extracted public key in PEM format
83
        """
84

    
85
        Logger.debug("Function launched.")
86

    
87
        return self.cryptography_service.extract_public_key_from_private_key(private_key.private_key,
88
                                                                             private_key.password)
(4-4/4)