Passed
Push — develop ( f8006a...41493b )
by Nikita
06:20
created

ClientCertificateRepository::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 23
nc 1
nop 0
dl 0
loc 32
rs 9.552
c 0
b 0
f 0
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Gameap\Models\ClientCertificate;
6
use Illuminate\Support\Facades\Storage;
7
8
class ClientCertificateRepository
9
{
10
    /**
11
     * @var ClientCertificate
12
     */
13
    protected $model;
14
15
    /**
16
     * ClientCertificateRepository constructor.
17
     * @param ClientCertificate $clientCertificate
18
     */
19
    public function __construct(ClientCertificate $clientCertificate)
20
    {
21
        $this->model = $clientCertificate;
22
    }
23
24
    /**
25
     * @param int $id
26
     * @return ClientCertificate
27
     */
28
    public function getFirstOrGenerate()
29
    {
30
        $clientCertificate = ClientCertificate::select()->first();
31
32
        if (empty($clientCertificate)) {
33
            $attributes = $this->generate();
34
            $clientCertificate = ClientCertificate::create($attributes);
35
        } else {
36
            // Fix. If client certificate exists in database but not exists certificates files.
37
            // Delete invalid files. Generate new certificates.
38
            if (! Storage::exists($clientCertificate->certificate) || ! Storage::exists($clientCertificate->private_key)) {
39
                if (Storage::exists($clientCertificate->certificate)) {
40
                    Storage::delete($clientCertificate->certificate);
41
                }
42
43
                if (Storage::exists($clientCertificate->private_key)) {
44
                    Storage::delete($clientCertificate->private_key);
45
                }
46
47
                $attributes = $this->generate();
48
49
                $clientCertificate->certificate = $attributes['certificate'];
50
                $clientCertificate->private_key = $attributes['private_key'];
51
                $clientCertificate->private_key_pass = $attributes['private_key_pass'];
52
                $clientCertificate->save();
53
            }
54
        }
55
56
        return $clientCertificate;
57
    }
58
59
    /**
60
     * Generate certificate
61
     * Return array with paths to certificates
62
     *
63
     * @return array
64
     */
65
    private function generate()
66
    {
67
        $certificateData = array(
68
            "countryName" => "RU",
69
            "stateOrProvinceName" => "GameAP",
70
            "localityName" => "GameAP",
71
            "organizationName" => "GameAP.ru",
72
            "organizationalUnitName" => "Development",
73
            "commonName" => "GameAP",
74
            "emailAddress" => "[email protected]"
75
        );
76
77
        $privateKey = openssl_pkey_new();
78
        $certificate = openssl_csr_new($certificateData, $privateKey);
79
        $certificate = openssl_csr_sign($certificate, null, $privateKey, 3650);
80
81
        openssl_x509_export($certificate, $pemCertificate);
82
        openssl_pkey_export($privateKey, $pemPrivateKey);
83
84
        Storage::makeDirectory('client_certificates');
85
86
        $timestamp = time();
87
        $certificateName = "client_certificates/client_{$timestamp}.crt";
88
        $privateKeyName = "client_certificates/client_{$timestamp}.key";
89
90
        Storage::put($certificateName, $pemCertificate);
91
        Storage::put($privateKeyName, $pemPrivateKey);
92
93
        return [
94
            'certificate' => $certificateName,
95
            'private_key' => $privateKeyName,
96
            'private_key_pass' => '',
97
        ];
98
    }
99
}