Projekt

Obecné

Profil

« Předchozí | Další » 

Revize cfda1725

Přidáno uživatelem Stanislav Král před asi 4 roky(ů)

Re #8573 - Implemented get_public_key_of_a_certificate method in CertController and added /api/certificates/<id>/public_key endpoint

Zobrazit rozdíly:

src/controllers/certificates_controller.py
30 30

  
31 31
E_NO_ISSUER_FOUND = {"success": False, "data": "No certificate authority with such unique ID exists."}
32 32
E_NO_CERTIFICATES_FOUND = {"success": False, "data": "No such certificate found."}
33
E_NO_CERT_PRIVATE_KEY_FOUND = {"success": False,
34
                               "data": "Internal server error (certificate's private key cannot be found)."}
33 35
E_NOT_JSON_FORMAT = {"success": False, "data": "The request must be JSON-formatted."}
34 36
E_CORRUPTED_DATABASE = {"success": False, "data": "Internal server error (corrupted database)."}
35 37
E_GENERAL_ERROR = {"success": False, "data": "Internal server error (unknown origin)."}
......
38 40

  
39 41
C_CREATED_SUCCESSFULLY = 201
40 42
C_BAD_REQUEST = 400
41
C_NO_DATA = 205                                                         # TODO related to 204 issue
43
C_NOT_FOUND = 404
44
C_NO_DATA = 205  # TODO related to 204 issue
42 45
C_INTERNAL_SERVER_ERROR = 500
43 46
C_SUCCESS = 200
44 47

  
......
341 344
            USAGE: {CertController.INVERSE_KEY_MAP[k]: v for k, v in c.usages.items()},
342 345
            CA: c_issuer.certificate_id
343 346
        }
347

  
348
    def get_public_key_of_a_certificate(self, id):
349
        """
350
        Get a private key used to sign a certificate in PEM format specified by certificate's ID
351

  
352
        :param id: ID of a certificate whose public key is to be queried
353
        :type id: dict | bytes
354

  
355
        :rtype: PemResponse
356
        """
357

  
358
        # try to parse the supplied ID
359
        try:
360
            v = int(id)
361
        except ValueError:
362
            return E_WRONG_PARAMETERS, C_BAD_REQUEST
363

  
364
        # find a certificate with using the given ID
365
        cert = self.certificate_service.get_certificate(v)
366

  
367
        if cert is None:
368
            return E_NO_CERTIFICATES_FOUND, C_NOT_FOUND
369
        else:
370
            # certificate exists, fetch it's private key
371
            private_key = self.key_service.get_key(cert.private_key_id)
372
            if cert is None:
373
                return E_NO_CERT_PRIVATE_KEY_FOUND, C_INTERNAL_SERVER_ERROR
374
            else:
375
                # TODO public key can be extracted from a certificate
376
                # private key fetched, extract a public key from it
377
                public_key = self.key_service.get_public_key(private_key)
378
                return {"success": True, "data": public_key}, C_SUCCESS

Také k dispozici: Unified diff