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 ( d2eba2...51c228 )
by Freek
01:48
created

SslCertificate::isSelfSigned()   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 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 isValidUntil(Carbon $carbon, string $url = null): bool
92
    {
93
        if ($this->expirationDate()->lte($carbon)) {
94
            return false;
95
        }
96
97
        return $this->isValid($url);
98
    }
99
100
    public function daysUntilExpirationDate(): int
101
    {
102
        $endDate = $this->expirationDate();
103
104
        $interval = Carbon::now()->diff($endDate);
105
106
        return (int)$interval->format("%r%a");
107
    }
108
109
    public function getDomains(): array
110
    {
111
        return array_filter(array_merge([$this->getDomain()], $this->getAdditionalDomains()));
112
    }
113
114
    public function appliesToUrl(string $url): bool
115
    {
116
        $host = (new Url($url))->getHostName();
117
118
        $certificateHosts = $this->getDomains();
119
120
        foreach ($certificateHosts as $certificateHost) {
121
            if ($host === $certificateHost) {
122
                return true;
123
            }
124
125
            if ($this->wildcardHostCoversHost($certificateHost, $host)) {
126
                return true;
127
            }
128
        }
129
130
        return false;
131
    }
132
133
    protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool
134
    {
135
        if ($host === $wildcardHost) {
136
            return true;
137
        }
138
139
        if (! starts_with($wildcardHost, '*')) {
140
            return false;
141
        }
142
143
        $wildcardHostWithoutWildcard = substr($wildcardHost, 2);
144
145
        return substr_count($wildcardHost, '.') >= substr_count($host, '.') && ends_with($host, $wildcardHostWithoutWildcard);
146
    }
147
148
    public function getRawCertificateFieldsJson(): string
149
    {
150
        return json_encode($this->getRawCertificateFields());
151
    }
152
153
    public function getHash(): string
154
    {
155
        return md5($this->getRawCertificateFieldsJson());
156
    }
157
158
    public function __toString(): string
159
    {
160
        return $this->getRawCertificateFieldsJson();
161
    }
162
}
163