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 ( 169160...61089b )
by Freek
02:33
created

SslCertificate::containsDomain()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
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 download(): Downloader
13
    {
14
        return new Downloader();
15
    }
16
17
    public static function createForHostName(string $url, int $timeout = 30): SslCertificate
18
    {
19
        $sslCertificate = Downloader::downloadCertificateFromUrl($url, $timeout);
20
21
        return $sslCertificate;
22
    }
23
24
    public function __construct(array $rawCertificateFields)
25
    {
26
        $this->rawCertificateFields = $rawCertificateFields;
27
    }
28
29
    public function getRawCertificateFields(): array
30
    {
31
        return $this->rawCertificateFields;
32
    }
33
34
    public function getIssuer(): string
35
    {
36
        return $this->rawCertificateFields['issuer']['CN'] ?? '';
37
    }
38
39
    public function getDomain(): string
40
    {
41
        return $this->rawCertificateFields['subject']['CN'] ?? '';
42
    }
43
44
    public function getSignatureAlgorithm(): string
45
    {
46
        return $this->rawCertificateFields['signatureTypeSN'] ?? '';
47
    }
48
49
    public function getAdditionalDomains(): array
50
    {
51
        $additionalDomains = explode(', ', $this->rawCertificateFields['extensions']['subjectAltName'] ?? '');
52
53
        return array_map(function (string $domain) {
54
            return str_replace('DNS:', '', $domain);
55
        }, $additionalDomains);
56
    }
57
58
    public function validFromDate(): Carbon
59
    {
60
        return Carbon::createFromTimestampUTC($this->rawCertificateFields['validFrom_time_t']);
61
    }
62
63
    public function expirationDate(): Carbon
64
    {
65
        return Carbon::createFromTimestampUTC($this->rawCertificateFields['validTo_time_t']);
66
    }
67
68
    public function isExpired(): bool
69
    {
70
        return $this->expirationDate()->isPast();
71
    }
72
73
    public function isValid(string $url = null)
74
    {
75
        if (! Carbon::now()->between($this->validFromDate(), $this->expirationDate())) {
76
            return false;
77
        }
78
79
        if (! empty($url)) {
80
            return $this->appliesToUrl($url ?? $this->getDomain());
81
        }
82
83
        return true;
84
    }
85
86
    public function isSelfSigned(): bool
87
    {
88
        return $this->getIssuer() === $this->getDomain();
89
    }
90
91
    public function usesSha1Hash(): bool
92
    {
93
        $certificateFields = $this->getRawCertificateFields();
94
95
        if ($certificateFields['signatureTypeSN'] === 'RSA-SHA1') {
96
            return true;
97
        }
98
99
        if ($certificateFields['signatureTypeLN'] === 'sha1WithRSAEncryption') {
100
            return true;
101
        }
102
103
        return false;
104
    }
105
106
    public function isValidUntil(Carbon $carbon, string $url = null): bool
107
    {
108
        if ($this->expirationDate()->lte($carbon)) {
109
            return false;
110
        }
111
112
        return $this->isValid($url);
113
    }
114
115
    public function daysUntilExpirationDate(): int
116
    {
117
        $endDate = $this->expirationDate();
118
119
        $interval = Carbon::now()->diff($endDate);
120
121
        return (int) $interval->format('%r%a');
122
    }
123
124
    public function getDomains(): array
125
    {
126
        return array_filter(array_merge([$this->getDomain()], $this->getAdditionalDomains()));
127
    }
128
129
    public function appliesToUrl(string $url): bool
130
    {
131
        $host = (new Url($url))->getHostName();
132
133
        $certificateHosts = $this->getDomains();
134
135
        foreach ($certificateHosts as $certificateHost) {
136
            if ($host === $certificateHost) {
137
                return true;
138
            }
139
140
            if ($this->wildcardHostCoversHost($certificateHost, $host)) {
141
                return true;
142
            }
143
        }
144
145
        return false;
146
    }
147
148
    protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool
149
    {
150
        if ($host === $wildcardHost) {
151
            return true;
152
        }
153
154
        if (! starts_with($wildcardHost, '*')) {
155
            return false;
156
        }
157
158
        $wildcardHostWithoutWildcard = substr($wildcardHost, 2);
159
160
        return substr_count($wildcardHost, '.') >= substr_count($host, '.') && ends_with($host, $wildcardHostWithoutWildcard);
161
    }
162
163
    public function getRawCertificateFieldsJson(): string
164
    {
165
        return json_encode($this->getRawCertificateFields());
166
    }
167
168
    public function getHash(): string
169
    {
170
        return md5($this->getRawCertificateFieldsJson());
171
    }
172
173
    public function __toString(): string
174
    {
175
        return $this->getRawCertificateFieldsJson();
176
    }
177
178
    public function containsDomain(string $domain): bool
179
    {
180
        $certificateHosts = $this->getDomains();
181
182
        foreach ($certificateHosts as $certificateHost) {
183
            if (str_contains($certificateHost, $domain)) {
184
                return true;
185
            }
186
        }
187
188
        return false;
189
    }
190
}
191