Skip to content

Commit

Permalink
Added validator callback + roles only on care providers (#22)
Browse files Browse the repository at this point in the history
* Added validator callback

* fix phpci
  • Loading branch information
jaytaph authored Feb 20, 2023
1 parent 60af540 commit fea60bb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 26 deletions.
24 changes: 21 additions & 3 deletions src/UziValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,26 @@ class UziValidator
protected array $allowedRoles;
protected array $caCerts = [];

/** @var callable|null */
protected $validatorCallback = null;

/**
* @param callable|null $validatorCallback
*/
public function __construct(
UziReader $reader,
bool $strictCaCheck,
array $allowedTypes,
array $allowedRoles,
array $caCerts = []
array $caCerts = [],
$validatorCallback = null
) {
$this->reader = $reader;
$this->strictCAcheck = $strictCaCheck;
$this->allowedTypes = $allowedTypes;
$this->allowedRoles = $allowedRoles;
$this->reader = $reader;
$this->caCerts = $caCerts;
$this->validatorCallback = $validatorCallback;
}

public function isValid(Request $request): bool
Expand Down Expand Up @@ -94,8 +102,18 @@ public function validate(Request $request): void
if (!in_array($uziInfo->getCardType(), $this->allowedTypes)) {
throw new UziAllowedTypeException('UZI card type not allowed');
}
if (!in_array(substr($uziInfo->getRole(), 0, 3), $this->allowedRoles)) {

// Check roles for care provider
if (
$uziInfo->getCardType() === UziConstants::UZI_TYPE_CARE_PROVIDER &&
!in_array(substr($uziInfo->getRole(), 0, 3), $this->allowedRoles)
) {
throw new UziAllowedRoleException("UZI card role not allowed");
}

// If a specific callback is set, call it
if ($this->validatorCallback && call_user_func($this->validatorCallback, $uziInfo) === false) {
throw new UziException('Uzi certificate validation failed (callback returned false)');
}
}
}
2 changes: 1 addition & 1 deletion tests/UziReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function testCheckValidCert(): void
$uziInfo = $uzi->getDataFromRequest($request);

$this->assertEquals('00000000', $uziInfo->getAgbCode());
$this->assertEquals('N', $uziInfo->getCardType());
$this->assertEquals('Z', $uziInfo->getCardType());
$this->assertEquals('john', $uziInfo->getGivenName());
$this->assertEquals('2.16.528.1.1003.1.3.5.5.2', $uziInfo->getOidCa());
$this->assertEquals('30.015', $uziInfo->getRole());
Expand Down
26 changes: 23 additions & 3 deletions tests/UziValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function testNotAllowedType(): void
$this->expectExceptionMessage("UZI card type not allowed");

$reader = new UziReader();
$validator = new UziValidator($reader, true, [UziConstants::UZI_TYPE_CARE_PROVIDER], []);
$validator = new UziValidator($reader, true, [UziConstants::UZI_TYPE_SERVER], []);
$validator->validate($request);
}

Expand All @@ -140,7 +140,7 @@ public function testNotAllowedRole(): void
$validator = new UziValidator(
$reader,
true,
[UziConstants::UZI_TYPE_NAMED_EMPLOYEE],
[UziConstants::UZI_TYPE_CARE_PROVIDER],
[UziConstants::UZI_ROLE_PHARMACIST]
);
$validator->validate($request);
Expand All @@ -156,9 +156,29 @@ public function testIsValid(): void
$validator = new UziValidator(
$reader,
true,
[UziConstants::UZI_TYPE_NAMED_EMPLOYEE],
[UziConstants::UZI_TYPE_CARE_PROVIDER],
[UziConstants::UZI_ROLE_NURSE]
);
$this->assertTrue($validator->isValid($request));
}

public function testCallback(): void
{
$request = new Request();
$request->server->set('SSL_CLIENT_VERIFY', "SUCCESS");
$request->server->set('SSL_CLIENT_CERT', file_get_contents(__DIR__ . '/certs/mock-011-correct.cert'));

$reader = new UziReader();
$validator = new UziValidator(
$reader,
true,
[UziConstants::UZI_TYPE_CARE_PROVIDER],
[UziConstants::UZI_ROLE_NURSE],
[],
function (UziUser $info) {
return false;
}
);
$this->assertFalse($validator->isValid($request));
}
}
2 changes: 1 addition & 1 deletion tests/certs/generate-mock-certs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ openssl req -x509 \
-out mock-011-correct.cert \
-days 3650 \
-subj "/C=NL/O=MockTest Cert/title=physician/SN=doe-12345678/GN=john/CN=john doe-12345678" \
-addext "subjectAltName = otherName:2.5.5.5;IA5STRING:2.16.528.1.1003.1.3.5.5.2-1-12345678-N-90000111-30.015-00000000"
-addext "subjectAltName = otherName:2.5.5.5;IA5STRING:2.16.528.1.1003.1.3.5.5.2-1-12345678-Z-90000111-30.015-00000000"

openssl req -x509 \
-nodes \
Expand Down
36 changes: 18 additions & 18 deletions tests/certs/mock-011-correct.cert
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
-----BEGIN CERTIFICATE-----
MIIELjCCAxagAwIBAgIUf9554etXemdCzHdgH2oNbNp8k6QwDQYJKoZIhvcNAQEL
MIIELjCCAxagAwIBAgIUQqLpwzodS5Qo1bPUFHMb4KD10k0wDQYJKoZIhvcNAQEL
BQAwezELMAkGA1UEBhMCTkwxFjAUBgNVBAoMDU1vY2tUZXN0IENlcnQxEjAQBgNV
BAwMCXBoeXNpY2lhbjEVMBMGA1UEBAwMZG9lLTEyMzQ1Njc4MQ0wCwYDVQQqDARq
b2huMRowGAYDVQQDDBFqb2huIGRvZS0xMjM0NTY3ODAeFw0yMTAxMjAxMDE5MDBa
Fw0zMTAxMTgxMDE5MDBaMHsxCzAJBgNVBAYTAk5MMRYwFAYDVQQKDA1Nb2NrVGVz
b2huMRowGAYDVQQDDBFqb2huIGRvZS0xMjM0NTY3ODAeFw0yMzAyMjAxMDM4MTJa
Fw0zMzAyMTcxMDM4MTJaMHsxCzAJBgNVBAYTAk5MMRYwFAYDVQQKDA1Nb2NrVGVz
dCBDZXJ0MRIwEAYDVQQMDAlwaHlzaWNpYW4xFTATBgNVBAQMDGRvZS0xMjM0NTY3
ODENMAsGA1UEKgwEam9objEaMBgGA1UEAwwRam9obiBkb2UtMTIzNDU2NzgwggEi
MA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDHbd5sIURqR5CWE+8o3cb2COIZ
wKGEcyn+wERcl8bbGyJxTrW885L98i+5gSSaOxqKbS9z7CcundYNuKu7zNM0rh/G
hv7CA8AX4ubHm9YKIhox45d+icxsKbd4baBsp5Ex5U7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