Skip to content

Commit 0433eb3

Browse files
Mihkel Kivisildmrts
authored andcommitted
Harmonizing the PHP library with Java library
WE2-971 Signed-off-by: Mihkel Kivisild <[email protected]>
1 parent 4394c42 commit 0433eb3

File tree

11 files changed

+38
-38
lines changed

11 files changed

+38
-38
lines changed

example/src/Auth.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,13 @@ public function getNonce()
8888

8989
private function getPrincipalNameFromCertificate(X509 $userCertificate): string
9090
{
91+
$givenName = CertificateData::getSubjectGivenName($userCertificate);
9192
$surname = CertificateData::getSubjectSurname($userCertificate);
92-
$givenname = CertificateData::getSubjectGivenName($userCertificate);
93-
if ($surname && $givenname) {
94-
$principalName = $givenname . " " . $surname;
93+
if ($givenName && $surname) {
94+
return $givenName . " " . $surname;
9595
} else {
96-
$principalName = CertificateData::getSubjectCN($userCertificate);
96+
return CertificateData::getSubjectCN($userCertificate);
9797
}
98-
return $principalName;
9998
}
10099

101100
/**

src/authtoken/WebEidAuthToken.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,6 @@ class WebEidAuthToken
4848
* @var string Format
4949
*/
5050
private ?string $format = null;
51-
/**
52-
* @var string App version
53-
*/
54-
private ?string $appVersion = null;
5551

5652
public function __construct(string $authenticationTokenJSON)
5753
{
@@ -76,10 +72,6 @@ public function __construct(string $authenticationTokenJSON)
7672
if (isset($jsonDecoded['format'])) {
7773
$this->format = $this->filterString('format', $jsonDecoded['format']);
7874
}
79-
// appVersion
80-
if (isset($jsonDecoded['appVersion'])) {
81-
$this->appVersion = $this->filterString('appVersion', $jsonDecoded['appVersion']);
82-
}
8375
}
8476

8577
public function getUnverifiedCertificate(): ?string
@@ -102,11 +94,6 @@ public function getFormat(): ?string
10294
return $this->format;
10395
}
10496

105-
public function getAppVersion(): ?string
106-
{
107-
return $this->appVersion;
108-
}
109-
11097
private function filterString(string $key, $data): string
11198
{
11299
$type = gettype($data);

src/certificate/CertificateData.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,14 @@ public static function getSubjectCountryCode(X509 $certificate): ?string
8080
/**
8181
* Get specified subject field from x509 certificate
8282
*
83-
* @return string
83+
* @return ?string
8484
*/
8585
private static function getField(X509 $certificate, string $fieldId): ?string
8686
{
8787
$result = $certificate->getSubjectDNProp($fieldId);
8888
if ($result) {
89-
return join(" ", $result);
90-
}
91-
else {
89+
return join(", ", $result);
90+
} else {
9291
return null;
9392
}
9493
}

src/validator/AuthTokenSignatureValidator.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ public function __construct(Uri $siteOrigin)
5454

5555
public function validate(string $algorithm, string $signature, $publicKey, string $currentChallengeNonce): void
5656
{
57-
$this->requireNotEmpty($algorithm, "algorithm");
58-
$this->requireNotEmpty($signature, "signature");
57+
if (empty($currentChallengeNonce)) {
58+
throw new ChallengeNullOrEmptyException();
59+
}
5960

6061
if (is_null($publicKey)) {
6162
throw new InvalidArgumentException("Public key is null");
6263
}
6364

64-
if (empty($currentChallengeNonce)) {
65-
throw new ChallengeNullOrEmptyException();
66-
}
65+
$this->requireNotEmpty($algorithm, "algorithm");
66+
$this->requireNotEmpty($signature, "signature");
6767

6868
if (!in_array($algorithm, self::ALLOWED_SIGNATURE_ALGORITHMS)) {
69-
throw new AuthTokenParseException("Invalid signature algorithm");
69+
throw new AuthTokenParseException("Unsupported signature algorithm");
7070
}
7171

7272
$decodedSignature = base64_decode($signature);

src/validator/ocsp/OcspRequestBuilder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace web_eid\web_eid_authtoken_validation_php\validator\ocsp;
2828

29+
use InvalidArgumentException;
2930
use web_eid\web_eid_authtoken_validation_php\ocsp\OcspRequest;
3031
use web_eid\web_eid_authtoken_validation_php\util\SecureRandom;
3132

@@ -58,6 +59,9 @@ public function enableOcspNonce(bool $ocspNonceEnabled): OcspRequestBuilder
5859
public function build(): OcspRequest
5960
{
6061
$ocspRequest = new OcspRequest();
62+
if (is_null($this->certificateId)) {
63+
throw new InvalidArgumentException("Certificate Id must not be null");
64+
}
6165
$ocspRequest->addCertificateId($this->certificateId);
6266

6367
if ($this->ocspNonceEnabled) {

src/validator/ocsp/OcspServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace web_eid\web_eid_authtoken_validation_php\validator\ocsp;
2828

29+
use InvalidArgumentException;
2930
use phpseclib3\File\X509;
3031
use web_eid\web_eid_authtoken_validation_php\validator\ocsp\service\AiaOcspService;
3132
use web_eid\web_eid_authtoken_validation_php\validator\ocsp\service\AiaOcspServiceConfiguration;
@@ -41,7 +42,8 @@ class OcspServiceProvider
4142
public function __construct(?DesignatedOcspServiceConfiguration $designatedOcspServiceConfiguration, AiaOcspServiceConfiguration $aiaOcspServiceConfiguration)
4243
{
4344
$this->designatedOcspService = !is_null($designatedOcspServiceConfiguration) ? new DesignatedOcspService($designatedOcspServiceConfiguration) : null;
44-
$this->aiaOcspServiceConfiguration = $aiaOcspServiceConfiguration;
45+
$this->aiaOcspServiceConfiguration = $aiaOcspServiceConfiguration ?? throw new InvalidArgumentException("AIA Ocsp Service Configuration must not be null");
46+
4547
}
4648

4749
/**

src/validator/ocsp/OcspUrl.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use phpseclib3\File\X509;
2929
use GuzzleHttp\Psr7\Uri;
3030
use Exception;
31+
use InvalidArgumentException;
3132

3233
final class OcspUrl
3334
{
@@ -43,6 +44,9 @@ public function __construct()
4344
*/
4445
public static function getOcspUri(X509 $certificate): ?Uri
4546
{
47+
if (is_null($certificate)) {
48+
throw new InvalidArgumentException("Certificate must not be null");
49+
}
4650
$authorityInformationAccess = $certificate->getExtension("id-pe-authorityInfoAccess");
4751
if ($authorityInformationAccess) {
4852
foreach ($authorityInformationAccess as $accessDescription) {

src/validator/ocsp/service/AiaOcspService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use DateTime;
3636
use web_eid\web_eid_authtoken_validation_php\validator\ocsp\OcspResponseValidator;
3737
use Exception;
38+
use InvalidArgumentException;
3839

3940
/**
4041
* An OCSP service that uses the responders from the Certificates' Authority Information Access (AIA) extension.
@@ -48,6 +49,9 @@ class AiaOcspService implements OcspService
4849

4950
public function __construct(AiaOcspServiceConfiguration $configuration, X509 $certificate)
5051
{
52+
if (is_null($configuration)) {
53+
throw new InvalidArgumentException("Configuration cannot be null");
54+
}
5155
$this->url = self::getOcspAiaUrlFromCertificate($certificate);
5256
$this->trustedCACertificates = $configuration->getTrustedCACertificates();
5357
$this->supportsNonce = !in_array($this->url->jsonSerialize(), $configuration->getNonceDisabledOcspUrls()->getUrlsArray());

tests/authtoken/WebEidAuthTokenTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function testValidateAuthTokenParameters(): void
4848
$this->assertEquals("RS256", $authToken->getAlgorithm());
4949
$this->assertEquals("HBjNXIaUskXbfhzYQHvwjKDUWfNu4yxXZh", $authToken->getSignature());
5050
$this->assertEquals("web-eid:1.0", $authToken->getFormat());
51-
$this->assertEquals("https://web-eid.eu/web-eid-app/releases/v2.0.0", $authToken->getAppVersion());
5251
}
5352

5453
public function testWhenNotAuthToken(): void

tests/certificate/CertificateDataTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,22 @@ public function testWhenOrganizationCertificateThenSubjectCNAndIdCodeAndCountryC
4949
$this->assertEquals("EE", CertificateData::getSubjectCountryCode($cert));
5050
}
5151

52-
public function testWhenOrganizationCertificateThenSubjectGivenNameAndSurnameAreNull(): void
52+
public function testWhenOrganizationCertificateThenSubjectGivenNameAndSurnameAreEmpty(): void
5353
{
5454
$cert = Certificates::getOrganizationCert();
55-
$this->assertEquals(null, CertificateData::getSubjectGivenName($cert));
56-
$this->assertEquals(null, CertificateData::getSubjectSurname($cert));
55+
$givenName = CertificateData::getSubjectGivenName($cert);
56+
$surname = CertificateData::getSubjectSurname($cert);
57+
$this->assertEmpty($givenName);
58+
$this->assertEmpty($surname);
5759
}
5860

5961
public function testWhenOrganizationCertificateThenSucceeds(): void
6062
{
6163
$cert = Certificates::getOrganizationCert();
64+
$givenName = CertificateData::getSubjectGivenName($cert);
6265
$surname = CertificateData::getSubjectSurname($cert);
63-
$givenname = CertificateData::getSubjectGivenName($cert);
64-
if ($surname && $givenname) {
65-
$principalName = $givenname . " " . $surname;
66+
if ($givenName && $surname) {
67+
$principalName = $givenName . " " . $surname;
6668
} else {
6769
$principalName = CertificateData::getSubjectCN($cert);
6870
}

0 commit comments

Comments
 (0)