Passed
Push — master ( e7e3c4...d0e289 )
by Rogier
01:26
created

DomainValidation::getFileValidationData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Rogierw\RwAcme\Endpoints;
4
5
use Rogierw\RwAcme\DTO\AccountData;
6
use Rogierw\RwAcme\DTO\DomainValidationData;
7
use Rogierw\RwAcme\DTO\OrderData;
8
use Rogierw\RwAcme\Http\Response;
9
use Rogierw\RwAcme\Support\Arr;
10
use Rogierw\RwAcme\Support\Base64;
11
12
class DomainValidation extends Endpoint
13
{
14
    const TYPE_HTTP = 'http-01';
15
    const TYPE_DNS = 'dns-01';
16
17
    /** @return DomainValidationData[] */
18
    public function status(OrderData $orderData, string $type = 'all'): array
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

18
    public function status(OrderData $orderData, /** @scrutinizer ignore-unused */ string $type = 'all'): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
19
    {
20
        $data = [];
21
22
        foreach ($orderData->domainValidationUrls as $domainValidationUrl) {
23
            $response = $this->client
24
                ->getHttpClient()
25
                ->post(
26
                    $domainValidationUrl,
27
                    $this->createKeyId($orderData->accountUrl, $domainValidationUrl)
28
                );
29
30
            if ($response->getHttpResponseCode() === 200) {
31
                $data[] = DomainValidationData::fromResponse($response);
32
            }
33
        }
34
35
        return $data;
36
    }
37
38
    public function getFileValidationData(DomainValidationData $domainValidation): array
39
    {
40
        $digest = $this->createDigest();
41
42
        return [
43
            'type'       => 'http',
44
            'identifier' => $domainValidation->identifier['value'],
45
            'filename'   => $domainValidation->file['token'],
46
            'content'    => $domainValidation->file['token'] . '.' . $digest,
47
        ];
48
    }
49
50
    public function start(AccountData $accountData, DomainValidationData $domainValidation): Response
51
    {
52
        $this->client->logger(
53
            'info',
54
            'Start HTTP challenge for ' . Arr::get($domainValidation->identifier, 'value', '')
55
        );
56
57
        $digest = $this->createDigest();
58
59
        $payload = [
60
            'keyAuthorization' => $domainValidation->file['token'] . '.' . $digest,
61
        ];
62
63
        $data = $this->createKeyId($accountData->url, $domainValidation->file['url'], $payload);
64
65
        return $this->client->getHttpClient()->post($domainValidation->file['url'], $data);
66
    }
67
68
    private function createDigest(): string
69
    {
70
        $privateKeyContent = file_get_contents($this->client->getAccountKeysPath() . 'private.pem');
71
        $privateKey = openssl_pkey_get_private($privateKeyContent);
72
73
        $details = openssl_pkey_get_details($privateKey);
0 ignored issues
show
Bug introduced by
It seems like $privateKey can also be of type false; however, parameter $key of openssl_pkey_get_details() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $details = openssl_pkey_get_details(/** @scrutinizer ignore-type */ $privateKey);
Loading history...
74
75
        $header = [
76
            'e'   => Base64::urlSafeEncode($details['rsa']['e']),
77
            'kty' => 'RSA',
78
            'n'   => Base64::urlSafeEncode($details['rsa']['n']),
79
        ];
80
81
        return Base64::urlSafeEncode(hash('sha256', json_encode($header), true));
82
    }
83
}
84