GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 868cad...5c1c40 )
by Freek
02:07
created

SslCertificate::getSignatureAlgorithm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\SslCertificate;
4
5
use Carbon\Carbon;
6
7
class SslCertificate
8
{
9
    /** @var array */
10
    protected $rawCertificateFields = [];
11
12
    public static function createForHostName(string $url, int $timeout = 30): SslCertificate
13
    {
14
        $rawCertificateFields = Downloader::downloadCertificateFromUrl($url, $timeout);
15
16
        return new static($rawCertificateFields);
17
    }
18
19
    public function __construct(array $rawCertificateFields)
20
    {
21
        $this->rawCertificateFields = $rawCertificateFields;
22
    }
23
24
    public function getRawCertificateFields(): array
25
    {
26
        return $this->rawCertificateFields;
27
    }
28
29
    public function getIssuer(): string
30
    {
31
        return $this->rawCertificateFields['issuer']['CN'];
32
    }
33
34
    public function getDomain(): string
35
    {
36
        return $this->rawCertificateFields['subject']['CN'] ?? '';
37
    }
38
39
    public function getSignatureAlgorithm(): string
40
    {
41
        return $this->rawCertificateFields['signatureTypeSN'] ?? '';
42
    }
43
44
    public function getAdditionalDomains(): array
45
    {
46
        $additionalDomains = explode(', ', $this->rawCertificateFields['extensions']['subjectAltName'] ?? '');
47
48
        return array_map(function (string $domain) {
49
            return str_replace('DNS:', '', $domain);
50
        }, $additionalDomains);
51
    }
52
53
    public function validFromDate(): Carbon
54
    {
55
        return Carbon::createFromTimestampUTC($this->rawCertificateFields['validFrom_time_t']);
56
    }
57
58
    public function expirationDate(): Carbon
59
    {
60
        return Carbon::createFromTimestampUTC($this->rawCertificateFields['validTo_time_t']);
61
    }
62
63
    public function isExpired(): bool
64
    {
65
        return $this->expirationDate()->isPast();
66
    }
67
68
    public function isValid(string $url = null)
69
    {
70
        if (!Carbon::now()->between($this->validFromDate(), $this->expirationDate())) {
71
            return false;
72
        }
73
74
        if (!empty($url)) {
75
            return $this->appliesToUrl($url ?? $this->getDomain());
76
        }
77
78
        return true;
79
    }
80
81
    public function isValidUntil(Carbon $carbon, string $url = null): bool
82
    {
83
        if ($this->expirationDate()->gt($carbon)) {
84
            return false;
85
        }
86
87
        return $this->isValid($url);
88
    }
89
90
    public function appliesToUrl(string $url): bool
91
    {
92
        $host = (new Url($url))->getHostName();
93
94
        $certificateHosts = array_merge([$this->getDomain()], $this->getAdditionalDomains());
95
96
        foreach ($certificateHosts as $certificateHost) {
97
            if ($host === $certificateHost) {
98
                return true;
99
            }
100
101
            if ($this->wildcardHostCoversHost($certificateHost, $host)) {
102
                return true;
103
            }
104
        }
105
106
        return false;
107
    }
108
109
    protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool
110
    {
111
        if ($host === $wildcardHost) {
112
            return true;
113
        }
114
115
        if (!starts_with($wildcardHost, '*')) {
116
            return false;
117
        }
118
119
        $wildcardHostWithoutWildcard = substr($wildcardHost, 2);
120
121
        return ends_with($host, $wildcardHostWithoutWildcard);
122
    }
123
}
124