[RFR]: Added Origin CA Certificate endpoint (#123)
* Added Certificate endpoint * Implement all calls in /certificates endpoint * Added to travis more php versions * Fix package compatibility * Added CertificateTest * Added test for Certificate endpoint * Added more tests * Updated README Co-authored-by: haphan <thanhha.phan@myrepublic.net>
This commit is contained in:
58
src/Configurations/Certificate.php
Normal file
58
src/Configurations/Certificate.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Cloudflare\API\Configurations;
|
||||
|
||||
class Certificate implements Configurations
|
||||
{
|
||||
const ORIGIN_RSA = 'origin-rsa';
|
||||
const ORIGIN_ECC = 'origin-ecc';
|
||||
const KEYLESS_CERTIFICATE = 'keyless-certificate';
|
||||
|
||||
private $config = [];
|
||||
|
||||
public function getArray(): array
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Array of hostnames or wildcard names (e.g., *.example.com) bound to the certificate
|
||||
* Example: $hostnames = ["example.com", "foo.example.com"]
|
||||
* @param array $hostnames
|
||||
*/
|
||||
public function setHostnames(array $hostnames)
|
||||
{
|
||||
$this->config['hostnames'] = $hostnames;
|
||||
}
|
||||
|
||||
/**
|
||||
* The number of days for which the certificate should be valid
|
||||
* Default value: 5475
|
||||
* Valid values: 7, 30, 90, 365, 730, 1095, 5475
|
||||
* @param int $validity
|
||||
*/
|
||||
public function setRequestedValidity(int $validity)
|
||||
{
|
||||
$this->config['requested_validity'] = $validity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Signature type desired on certificate ("origin-rsa" (rsa), "origin-ecc" (ecdsa), or "keyless-certificate" (for Keyless SSL servers)
|
||||
* Valid values: origin-rsa, origin-ecc, keyless-certificate
|
||||
* @param string $type
|
||||
*/
|
||||
public function setRequestType(string $type)
|
||||
{
|
||||
$this->config['request_type'] = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* The Certificate Signing Request (CSR). Must be newline-encoded.
|
||||
*
|
||||
* @param string $csr
|
||||
*/
|
||||
public function setCsr(string $csr)
|
||||
{
|
||||
$this->config['csr'] = $csr;
|
||||
}
|
||||
}
|
||||
86
src/Endpoints/Certificates.php
Normal file
86
src/Endpoints/Certificates.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace Cloudflare\API\Endpoints;
|
||||
|
||||
use Cloudflare\API\Adapter\Adapter;
|
||||
use Cloudflare\API\Configurations\Certificate as CertificateConfig;
|
||||
use Cloudflare\API\Traits\BodyAccessorTrait;
|
||||
|
||||
class Certificates implements API
|
||||
{
|
||||
use BodyAccessorTrait;
|
||||
|
||||
private $adapter;
|
||||
|
||||
public function __construct(Adapter $adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* List all existing Origin CA certificates for a given zone
|
||||
*
|
||||
* @param string $zoneID
|
||||
* @return array
|
||||
*/
|
||||
public function listCertificates(string $zoneID): \stdClass
|
||||
{
|
||||
$certificates = $this->adapter->get('certificates', ['zone_id' => $zoneID]);
|
||||
$this->body = json_decode($certificates->getBody());
|
||||
|
||||
return (object)['result' => $this->body->result];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an existing Origin CA certificate by its serial number
|
||||
*
|
||||
* @param string $certificateID
|
||||
* @param string $zoneID
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCertificate(string $certificateID, string $zoneID)
|
||||
{
|
||||
$certificates = $this->adapter->get('certificates/'.$certificateID, ['zone_id' => $zoneID]);
|
||||
$this->body = json_decode($certificates->getBody());
|
||||
|
||||
return (object)['result' => $this->body->result];
|
||||
}
|
||||
|
||||
/**
|
||||
* Revoke an existing Origin CA certificate by its serial number
|
||||
*
|
||||
* @param string $certificateID
|
||||
* @param string $zoneID
|
||||
* @return bool
|
||||
*/
|
||||
public function revokeCertificate(string $certificateID, string $zoneID): bool
|
||||
{
|
||||
$certificates = $this->adapter->delete('certificates/'.$certificateID, ['zone_id' => $zoneID]);
|
||||
$this->body = json_decode($certificates->getBody());
|
||||
|
||||
if (isset($this->body->result->id)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an Origin CA certificate
|
||||
*
|
||||
* @param CertificateConfig $config
|
||||
* @return bool
|
||||
*/
|
||||
public function createCertificate(CertificateConfig $config): bool
|
||||
{
|
||||
$certificate = $this->adapter->post('certificates', $config->getArray());
|
||||
|
||||
$this->body = json_decode($certificate->getBody());
|
||||
|
||||
if (isset($this->body->result->id)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user