Skip to content

Commit 5cf4dfd

Browse files
Guido Gröönmrts
authored andcommitted
Use nullsafe operator
1 parent 0f5ae2b commit 5cf4dfd

8 files changed

+22
-62
lines changed

src/validator/AuthTokenValidatorBuilder.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ public function __construct(LoggerInterface $logger = null)
5555
public function withSiteOrigin(Uri $origin): AuthTokenValidatorBuilder
5656
{
5757
$this->configuration->setSiteOrigin($origin);
58-
if ($this->logger) {
59-
$this->logger->debug("Origin set to " . $this->configuration->getSiteOrigin()->jsonSerialize());
60-
}
58+
$this->logger?->debug("Origin set to " . $this->configuration->getSiteOrigin()->jsonSerialize());
6159
return $this;
6260
}
6361

@@ -77,9 +75,7 @@ public function withSiteOrigin(Uri $origin): AuthTokenValidatorBuilder
7775
public function withTrustedCertificateAuthorities(X509 ...$certificates): AuthTokenValidatorBuilder
7876
{
7977
array_push($this->configuration->getTrustedCACertificates(), ...$certificates);
80-
if ($this->logger) {
81-
$this->logger->debug("Trusted intermediate certificate authorities set to " . json_encode(X509Collection::getSubjectDNs(null, ...$this->configuration->getTrustedCACertificates())));
82-
}
78+
$this->logger?->debug("Trusted intermediate certificate authorities set to " . json_encode(X509Collection::getSubjectDNs(null, ...$this->configuration->getTrustedCACertificates())));
8379
return $this;
8480
}
8581

@@ -96,9 +92,7 @@ public function withTrustedCertificateAuthorities(X509 ...$certificates): AuthTo
9692
public function withDisallowedCertificatePolicies(string ...$policies): AuthTokenValidatorBuilder
9793
{
9894
array_push($this->configuration->getDisallowedSubjectCertificatePolicies(), ...$policies);
99-
if ($this->logger) {
100-
$this->logger->debug("Disallowed subject certificate policies set to " . json_encode($this->configuration->getDisallowedSubjectCertificatePolicies()));
101-
}
95+
$this->logger?->debug("Disallowed subject certificate policies set to " . json_encode($this->configuration->getDisallowedSubjectCertificatePolicies()));
10296
return $this;
10397
}
10498

@@ -114,9 +108,7 @@ public function withDisallowedCertificatePolicies(string ...$policies): AuthToke
114108
public function withoutUserCertificateRevocationCheckWithOcsp(): AuthTokenValidatorBuilder
115109
{
116110
$this->configuration->setUserCertificateRevocationCheckWithOcspDisabled();
117-
if ($this->logger) {
118-
$this->logger->warning("User certificate revocation check with OCSP is disabled, you should turn off the revocation check only in exceptional circumstances");
119-
}
111+
$this->logger?->warning("User certificate revocation check with OCSP is disabled, you should turn off the revocation check only in exceptional circumstances");
120112
return $this;
121113
}
122114

@@ -131,9 +123,7 @@ public function withoutUserCertificateRevocationCheckWithOcsp(): AuthTokenValida
131123
public function withOcspRequestTimeout(int $ocspRequestTimeout): AuthTokenValidatorBuilder
132124
{
133125
$this->configuration->setOcspRequestTimeout($ocspRequestTimeout);
134-
if ($this->logger) {
135-
$this->logger->debug("OCSP request timeout set to " . $ocspRequestTimeout);
136-
}
126+
$this->logger?->debug("OCSP request timeout set to " . $ocspRequestTimeout);
137127
return $this;
138128
}
139129

@@ -151,19 +141,15 @@ public function withNonceDisabledOcspUrls(URI ...$uris): AuthTokenValidatorBuild
151141
foreach ($uris as $uri) {
152142
$this->configuration->getNonceDisabledOcspUrls()->pushItem($uri);
153143
}
154-
if ($this->logger) {
155-
$this->logger->debug("OCSP URLs for which the nonce protocol extension is disabled set to " . implode(", ", $this->configuration->getNonceDisabledOcspUrls()->getUrlsArray()));
156-
}
144+
$this->logger?->debug("OCSP URLs for which the nonce protocol extension is disabled set to " . implode(", ", $this->configuration->getNonceDisabledOcspUrls()->getUrlsArray()));
157145

158146
return $this;
159147
}
160148

161149
public function withDesignatedOcspServiceConfiguration(DesignatedOcspServiceConfiguration $serviceConfiguration): AuthTokenValidatorBuilder
162150
{
163151
$this->configuration->setDesignatedOcspServiceConfiguration($serviceConfiguration);
164-
if ($this->logger) {
165-
$this->logger->debug("Using designated OCSP service configuration");
166-
}
152+
$this->logger?->debug("Using designated OCSP service configuration");
167153
return $this;
168154
}
169155

src/validator/AuthTokenValidatorImpl.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,33 +104,25 @@ private function validateTokenLength(string $authToken): void
104104

105105
public function parse(string $authToken): WebEidAuthToken
106106
{
107-
if ($this->logger) {
108-
$this->logger->info("Starting token parsing");
109-
}
107+
$this->logger?->info("Starting token parsing");
110108

111109
try {
112110
$this->validateTokenLength($authToken);
113111
return new WebEidAuthToken($authToken);
114112
} catch (Throwable $e) {
115-
if ($this->logger) {
116-
$this->logger->warning("Token parsing was interrupted: " . $e->getMessage());
117-
}
113+
$this->logger?->warning("Token parsing was interrupted: " . $e->getMessage());
118114
throw $e;
119115
}
120116
}
121117

122118
public function validate(WebEidAuthToken $authToken, string $currentChallengeNonce): X509
123119
{
124-
if ($this->logger) {
125-
$this->logger->info("Starting token validation");
126-
}
120+
$this->logger?->info("Starting token validation");
127121

128122
try {
129123
return $this->validateToken($authToken, $currentChallengeNonce);
130124
} catch (Throwable $e) {
131-
if ($this->logger) {
132-
$this->logger->warning("Token validation was interrupted: " . $e->getMessage());
133-
}
125+
$this->logger?->warning("Token validation was interrupted: " . $e->getMessage());
134126
throw $e;
135127
}
136128
}

src/validator/certvalidators/SubjectCertificateExpiryValidator.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,8 @@ public function validate(X509 $subjectCertificate): void
4646
{
4747
$now = DefaultClock::getInstance()->now();
4848
CertificateValidator::trustedCACertificatesAreValidOnDate($this->trustedCertificates, $now);
49-
if ($this->logger) {
50-
$this->logger->debug("CA certificates are valid.");
51-
}
49+
$this->logger?->debug("CA certificates are valid.");
5250
CertificateValidator::certificateIsValidOnDate($subjectCertificate, $now, "User");
53-
if ($this->logger) {
54-
$this->logger->debug("User certificate is valid.");
55-
}
51+
$this->logger?->debug("User certificate is valid.");
5652
}
5753
}

src/validator/certvalidators/SubjectCertificateNotRevokedValidator.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,14 @@ public function validate(X509 $subjectCertificate): void
6060

6161
$ocspService = $this->ocspServiceProvider->getService($subjectCertificate);
6262

63-
if ($this->logger && !$ocspService->doesSupportNonce()) {
64-
$this->logger->debug("Disabling OCSP nonce extension");
63+
if (!$ocspService->doesSupportNonce()) {
64+
$this->logger?->debug("Disabling OCSP nonce extension");
6565
}
6666

6767
$certificateId = (new Ocsp())->generateCertificateId($subjectCertificate, $this->trustValidator->getSubjectCertificateIssuerCertificate());
6868
$request = (new OcspRequestBuilder())->withCertificateId($certificateId)->enableOcspNonce($ocspService->doesSupportNonce())->build();
6969

70-
if ($this->logger) {
71-
$this->logger->debug("Sending OCSP request");
72-
}
70+
$this->logger?->debug("Sending OCSP request");
7371

7472
$response = $this->ocspClient->request($ocspService->getAccessLocation(), $request->getEncodeDer());
7573

@@ -145,9 +143,7 @@ private function verifyOcspResponse(OcspResponse $response, OcspService $ocspSer
145143
// Now we can accept the signed response as valid and validate the certificate status.
146144
OcspResponseValidator::validateSubjectCertificateStatus($response);
147145

148-
if ($this->logger) {
149-
$this->logger->debug("OCSP check result is GOOD");
150-
}
146+
$this->logger?->debug("OCSP check result is GOOD");
151147
}
152148

153149
private static function checkNonce(OcspRequest $request, OcspBasicResponse $basicResponse): void

src/validator/certvalidators/SubjectCertificatePolicyValidator.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ public function __construct(array $disallowedSubjectCertificatePolicyIds, Logger
4141

4242
public function validate(X509 $subjectCertificate): void
4343
{
44-
if ($this->logger) {
45-
$this->logger->debug("Validating");
46-
}
44+
$this->logger?->debug("Validating");
4745

4846
// No need to validate
4947
if (count($this->disallowedSubjectCertificatePolicyIds) == 0) {
@@ -63,8 +61,6 @@ public function validate(X509 $subjectCertificate): void
6361
}
6462
}
6563

66-
if ($this->logger) {
67-
$this->logger->debug("User certificate does not contain disallowed policies.");
68-
}
64+
$this->logger?->debug("User certificate does not contain disallowed policies.");
6965
}
7066
}

src/validator/certvalidators/SubjectCertificatePurposeValidator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ public function validate(X509 $subjectCertificate): void
6060
throw new UserCertificateWrongPurposeException();
6161
}
6262

63-
if ($this->logger) {
64-
$this->logger->debug("User certificate can be used for client authentication.");
65-
}
63+
$this->logger?->debug("User certificate can be used for client authentication.");
6664
}
6765
}

src/validator/certvalidators/SubjectCertificateTrustedValidator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public function validate(X509 $subjectCertificate): void
4848
$this->trustedCACertificates
4949
);
5050

51-
if ($this->logger) {
52-
$this->logger->debug("Subject certificate is signed by a trusted CA");
53-
}
51+
$this->logger?->debug("Subject certificate is signed by a trusted CA");
5452
}
5553

5654
public function getSubjectCertificateIssuerCertificate(): X509

src/validator/ocsp/OcspClientImpl.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ public function request(Uri $uri, string $encodedOcspRequest): OcspResponse
7373
$response = new OcspResponse($result);
7474

7575
$responseJson = json_encode($response->getResponse(), JSON_INVALID_UTF8_IGNORE);
76-
if ($this->logger) {
77-
$this->logger->debug("OCSP response: " . $responseJson);
78-
}
76+
$this->logger?->debug("OCSP response: " . $responseJson);
7977

8078
if ($info["content_type"] !== self::OCSP_RESPONSE_TYPE) {
8179
throw new UserCertificateOCSPCheckFailedException("OCSP response content type is not " . self::OCSP_RESPONSE_TYPE);

0 commit comments

Comments
 (0)