Completed
Push — develop ( c97899...e1148a )
by Chris
05:11
created

SSL::generateKeys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
/**
3
* @author Chris Hilsdon <[email protected]>
4
*/
5
namespace Cloudflare;
6
7
class SSL extends Base
8
{
9
    private $config = [
10
        'digest_alg' => 'sha512',
11
        'private_key_bits' => 4096,
12
        'private_key_type' => OPENSSL_KEYTYPE_RSA,
13
    ];
14
15
    protected $baseUrl = 'certificates';
16
17
    protected $zone;
18
19
    public function __construct(Cloudflare $cloudflare, $zone)
20
    {
21
        parent::__construct($cloudflare);
22
        $cfzone = new Zone($this->CF);
23
        $zone = $cfzone->getDetails($zone);
24
        $this->zone = $zone;
25
    }
26
27
    private function getZoneDomain()
28
    {
29
        return $this->zone->result->name;
30
    }
31
32
    private function getZoneId()
33
    {
34
        return $this->zone->result->id;
35
    }
36
37 View Code Duplication
    public function listOriginCerts()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
    {
39
        $id = $this->getZoneId();
40
        $this->makeRequest($this->baseUrl . '?zone_id=' . $id, 'GET', null, true);
41
42
        $response = $this->getResponse();
43
44
        if ($response->success !== true) {
45
            return false;
46
        }
47
48
        return $response->result;
49
    }
50
51
    public function revokeCert($id)
52
    {
53
        $this->makeRequest($this->baseUrl . '/' . $id, 'DELETE', null, true);
54
        $response = $this->getResponse();
55
56
        return $response->success;
57
    }
58
59
    public function getNewOriginCert()
60
    {
61
        $domain = $this->getZoneDomain();
62
        $csr = $this->createCSR($domain);
63
        $cert = $this->requestNewOriginCert($csr, $domain);
64
        $csr['cert'] = $cert;
65
66
        return $csr;
67
    }
68
69
    private function requestNewOriginCert($csr, $domain)
70
    {
71
        $data = [
72
            'hostnames' => [
73
                $domain,
74
                '*.' . $domain
75
            ],
76
            'requested_validity' => 5475,
77
            'request_type' => 'origin-rsa',
78
            'csr' => $csr['csr']
79
        ];
80
        $this->makeRequest('certificates', 'POST', $data, true);
81
82
        $response = $this->getResponse();
83
84
        if ($response->success !== true) {
85
            return false;
86
        }
87
88
        return [
89
            'id' => $response->result->id,
90
            'certificate' => $response->result->certificate
91
        ];
92
    }
93
94
    private function createCSR($domain)
95
    {
96
        $this->domain = $domain;
0 ignored issues
show
Bug introduced by
The property domain does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
97
        $keys = $this->generateKeys();
98
99
        $csr = openssl_csr_new(['commonName' => $domain], $privkey, $this->config);
100
        openssl_csr_export($csr, $csrString);
101
102
        $keys['csr'] = $csrString;;
103
104
        return $keys;
105
    }
106
107
    private function generateKeys()
108
    {
109
        // Create the private and public key
110
        $res = openssl_pkey_new($this->config);
111
112
        // Extract the private key from $res to $privKey
113
        openssl_pkey_export($res, $privKey);
114
115
        // Extract the public key from $res to $pubKey
116
        $pubKey = openssl_pkey_get_details($res);
117
        $pubKey = $pubKey["key"];
118
119
        return [
120
            'pubKey' => $pubKey,
121
            'privKey' => $privKey
122
        ];
123
    }
124
}
125