Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions library/EngineBlock/Corto/Model/Consent.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function explicitConsentWasGivenFor(ServiceProvider $serviceProvider): Co
// Consent disabled: treat as already given (stable — no upgrade needed)
return ConsentVersion::stable();
}
return $this->_hasStoredConsent($serviceProvider, ConsentType::TYPE_EXPLICIT);
return $this->_hasStoredConsent($serviceProvider, ConsentType::Explicit);
}

/**
Expand All @@ -102,7 +102,7 @@ public function explicitConsentWasGivenFor(ServiceProvider $serviceProvider): Co
* The caller must pass the ConsentVersion already retrieved by explicitConsentWasGivenFor or
* implicitConsentWasGivenFor to avoid a second identical DB query.
*/
public function upgradeAttributeHashFor(ServiceProvider $serviceProvider, string $consentType, ConsentVersion $consentVersion): void
public function upgradeAttributeHashFor(ServiceProvider $serviceProvider, ConsentType $consentType, ConsentVersion $consentVersion): void
{
if (!$this->_consentEnabled) {
return;
Expand All @@ -117,19 +117,19 @@ public function implicitConsentWasGivenFor(ServiceProvider $serviceProvider): Co
if (!$this->_consentEnabled) {
return ConsentVersion::stable();
}
return $this->_hasStoredConsent($serviceProvider, ConsentType::TYPE_IMPLICIT);
return $this->_hasStoredConsent($serviceProvider, ConsentType::Implicit);
}

public function giveExplicitConsentFor(ServiceProvider $serviceProvider): bool
{
return !$this->_consentEnabled ||
$this->_storeConsent($serviceProvider, ConsentType::TYPE_EXPLICIT);
$this->_storeConsent($serviceProvider, ConsentType::Explicit);
}

public function giveImplicitConsentFor(ServiceProvider $serviceProvider): bool
{
return !$this->_consentEnabled ||
$this->_storeConsent($serviceProvider, ConsentType::TYPE_IMPLICIT);
$this->_storeConsent($serviceProvider, ConsentType::Implicit);
}

public function countTotalConsent(): int
Expand Down Expand Up @@ -157,7 +157,7 @@ protected function _getStableAttributesHash($attributes): string
return $this->_hashService->getStableAttributesHash($attributes, $this->_mustStoreValues);
}

private function _storeConsent(ServiceProvider $serviceProvider, $consentType): bool
private function _storeConsent(ServiceProvider $serviceProvider, ConsentType $consentType): bool
{
$consentUuid = $this->_getConsentUid();
if (!is_string($consentUuid)) {
Expand All @@ -168,14 +168,14 @@ private function _storeConsent(ServiceProvider $serviceProvider, $consentType):
hashedUserId: sha1($consentUuid),
serviceId: $serviceProvider->entityId,
attributeStableHash: $this->_getStableAttributesHash($this->_responseAttributes),
consentType: $consentType,
consentType: $consentType->value,
attributeHash: $this->_getAttributesHash($this->_responseAttributes),
);

return $this->_hashService->storeConsentHash($parameters);
}

private function _updateConsent(ServiceProvider $serviceProvider, $consentType): bool
private function _updateConsent(ServiceProvider $serviceProvider, ConsentType $consentType): bool
{
$consentUid = $this->_getConsentUid();
if (!is_string($consentUid)) {
Expand All @@ -187,13 +187,13 @@ private function _updateConsent(ServiceProvider $serviceProvider, $consentType):
attributeHash: $this->_getAttributesHash($this->_responseAttributes),
hashedUserId: sha1($consentUid),
serviceId: $serviceProvider->entityId,
consentType: $consentType,
consentType: $consentType->value,
);

return $this->_hashService->updateConsentHash($parameters);
}

private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentType): ConsentVersion
private function _hasStoredConsent(ServiceProvider $serviceProvider, ConsentType $consentType): ConsentVersion
{
$consentUid = $this->_getConsentUid();
if (!is_string($consentUid)) {
Expand All @@ -205,7 +205,7 @@ private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentTyp
serviceId: $serviceProvider->entityId,
attributeHash: $this->_getAttributesHash($this->_responseAttributes),
attributeStableHash: $this->_getStableAttributesHash($this->_responseAttributes),
consentType: $consentType,
consentType: $consentType->value,
);
return $this->_hashService->retrieveConsentHash($query);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function serve($serviceName, Request $httpRequest)
if (!$explicitConsent->given()) {
$consentRepository->giveExplicitConsentFor($destinationMetadata);
} else {
$consentRepository->upgradeAttributeHashFor($destinationMetadata, ConsentType::TYPE_EXPLICIT, $explicitConsent);
$consentRepository->upgradeAttributeHashFor($destinationMetadata, ConsentType::Explicit, $explicitConsent);
}

$response->setConsent(Constants::CONSENT_OBTAINED);
Expand Down
4 changes: 2 additions & 2 deletions library/EngineBlock/Corto/Module/Service/ProvideConsent.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function serve($serviceName, Request $httpRequest)
if (!$implicitConsent->given()) {
$consentRepository->giveImplicitConsentFor($serviceProviderMetadata);
} else {
$consentRepository->upgradeAttributeHashFor($serviceProviderMetadata, ConsentType::TYPE_IMPLICIT, $implicitConsent);
$consentRepository->upgradeAttributeHashFor($serviceProviderMetadata, ConsentType::Implicit, $implicitConsent);
}

$response->setConsent(Constants::CONSENT_INAPPLICABLE);
Expand All @@ -170,7 +170,7 @@ public function serve($serviceName, Request $httpRequest)

$priorConsent = $consentRepository->explicitConsentWasGivenFor($serviceProviderMetadata);
if ($priorConsent->given()) {
$consentRepository->upgradeAttributeHashFor($serviceProviderMetadata, ConsentType::TYPE_EXPLICIT, $priorConsent);
$consentRepository->upgradeAttributeHashFor($serviceProviderMetadata, ConsentType::Explicit, $priorConsent);

$response->setConsent(Constants::CONSENT_PRIOR);

Expand Down
69 changes: 5 additions & 64 deletions src/OpenConext/EngineBlock/Authentication/Value/ConsentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,14 @@
namespace OpenConext\EngineBlock\Authentication\Value;

use JsonSerializable;
use OpenConext\EngineBlock\Assert\Assertion;

final class ConsentType implements JsonSerializable
enum ConsentType: string implements JsonSerializable
{
const TYPE_EXPLICIT = 'explicit';
const TYPE_IMPLICIT = 'implicit';
case Explicit = 'explicit';
case Implicit = 'implicit';

/**
* @var string
*/
private $consentType;

/**
* @return ConsentType
*/
public static function explicit()
{
return new self(self::TYPE_EXPLICIT);
}

/**
* @return ConsentType
*/
public static function implicit()
{
return new self(self::TYPE_IMPLICIT);
}

/**
* @param ConsentType::TYPE_EXPLICIT|ConsentType::TYPE_IMPLICIT $consentType
*
* @deprecated Use the implicit and explicit named constructors. Will be removed
* when Doctrine ORM is implemented.
*/
public function __construct($consentType)
{
Assertion::choice(
$consentType,
[self::TYPE_EXPLICIT, self::TYPE_IMPLICIT],
'ConsentType must be one of ConsentType::TYPE_EXPLICIT, ConsentType::TYPE_IMPLICIT'
);

$this->consentType = $consentType;
}

/**
* @param ConsentType $other
* @return bool
*/
public function equals(ConsentType $other)
{
return $this->consentType === $other->consentType;
}

/**
* @return string
*/
public function jsonSerialize(): mixed
{
return $this->consentType;
}

/**
* @return string
*/
public function __toString()
public function jsonSerialize(): string
{
return $this->consentType;
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function (array $row) use ($userId) {
$userId,
$row['service_id'],
new DateTime($row['consent_date']),
new ConsentType($row['consent_type']),
ConsentType::from($row['consent_type']),
$row['attribute_stable'] ?? $row['attribute']
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ public function test_no_previous_consent_given($consentType)
->once()
->andReturn(ConsentVersion::notGiven());
switch ($consentType) {
case ConsentType::TYPE_EXPLICIT:
case ConsentType::Explicit:
$this->assertFalse($this->consent->explicitConsentWasGivenFor($serviceProvider)->given());
break;
case ConsentType::TYPE_IMPLICIT:
case ConsentType::Implicit:
$this->assertFalse($this->consent->implicitConsentWasGivenFor($serviceProvider)->given());
break;
}
Expand All @@ -119,16 +119,16 @@ public function test_unstable_previous_consent_given($consentType)
serviceId: 'service-provider-entity-id',
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
attributeStableHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
consentType: $consentType,
consentType: $consentType->value,
))
->once()
->andReturn(ConsentVersion::unstable());

switch ($consentType) {
case ConsentType::TYPE_EXPLICIT:
case ConsentType::Explicit:
$this->assertTrue($this->consent->explicitConsentWasGivenFor($serviceProvider)->given());
break;
case ConsentType::TYPE_IMPLICIT:
case ConsentType::Implicit:
$this->assertTrue($this->consent->implicitConsentWasGivenFor($serviceProvider)->given());
break;
}
Expand All @@ -149,16 +149,16 @@ public function test_stable_consent_given($consentType)
serviceId: 'service-provider-entity-id',
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
attributeStableHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
consentType: $consentType,
consentType: $consentType->value,
))
->once()
->andReturn(ConsentVersion::stable());

switch ($consentType) {
case ConsentType::TYPE_EXPLICIT:
case ConsentType::Explicit:
$this->assertTrue($this->consent->explicitConsentWasGivenFor($serviceProvider)->given());
break;
case ConsentType::TYPE_IMPLICIT:
case ConsentType::Implicit:
$this->assertTrue($this->consent->implicitConsentWasGivenFor($serviceProvider)->given());
break;
}
Expand All @@ -184,16 +184,16 @@ public function test_give_consent_toggle_on_stores_only_stable_hash($consentType
hashedUserId: '0e54805079c56c2b1c1197a760af86ac337b7bac',
serviceId: 'service-provider-entity-id',
attributeStableHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
consentType: $consentType,
consentType: $consentType->value,
attributeHash: null,
))
->andReturn(true);

switch ($consentType) {
case ConsentType::TYPE_EXPLICIT:
case ConsentType::Explicit:
$this->assertTrue($this->consent->giveExplicitConsentFor($serviceProvider));
break;
case ConsentType::TYPE_IMPLICIT:
case ConsentType::Implicit:
$this->assertTrue($this->consent->giveImplicitConsentFor($serviceProvider));
break;
}
Expand All @@ -219,16 +219,16 @@ public function test_give_consent_toggle_off_stores_both_hashes($consentType)
hashedUserId: '0e54805079c56c2b1c1197a760af86ac337b7bac',
serviceId: 'service-provider-entity-id',
attributeStableHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
consentType: $consentType,
consentType: $consentType->value,
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
))
->andReturn(true);

switch ($consentType) {
case ConsentType::TYPE_EXPLICIT:
case ConsentType::Explicit:
$this->assertTrue($this->consent->giveExplicitConsentFor($serviceProvider));
break;
case ConsentType::TYPE_IMPLICIT:
case ConsentType::Implicit:
$this->assertTrue($this->consent->giveImplicitConsentFor($serviceProvider));
break;
}
Expand All @@ -254,7 +254,7 @@ public function test_upgrade_toggle_off_preserves_legacy_hash($consentType)
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
hashedUserId: '0e54805079c56c2b1c1197a760af86ac337b7bac',
serviceId: 'service-provider-entity-id',
consentType: $consentType,
consentType: $consentType->value,
clearLegacyHash: false,
))
->andReturn(true);
Expand Down Expand Up @@ -282,7 +282,7 @@ public function test_upgrade_toggle_on_clears_legacy_hash($consentType)
attributeHash: '8739602554c7f3241958e3cc9b57fdecb474d508',
hashedUserId: '0e54805079c56c2b1c1197a760af86ac337b7bac',
serviceId: 'service-provider-entity-id',
consentType: $consentType,
consentType: $consentType->value,
clearLegacyHash: true,
))
->andReturn(true);
Expand Down Expand Up @@ -354,7 +354,7 @@ public function test_store_consent_hash_sql_resets_deleted_at_on_duplicate(): vo

public static function consentTypeProvider(): iterable
{
yield [ConsentType::TYPE_IMPLICIT];
yield [ConsentType::TYPE_EXPLICIT];
yield [ConsentType::Implicit];
yield [ConsentType::Explicit];
}
}
6 changes: 3 additions & 3 deletions tests/library/EngineBlock/Test/Corto/Model/ConsentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ public function testUpgradeAttributeHashSkippedWhenConsentDisabled()
$this->consentService->shouldNotReceive('retrieveConsentHash');
$this->consentService->shouldNotReceive('updateConsentHash');

$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::TYPE_EXPLICIT, ConsentVersion::stable());
$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::TYPE_IMPLICIT, ConsentVersion::stable());
$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::Explicit, ConsentVersion::stable());
$this->consentDisabled->upgradeAttributeHashFor($serviceProvider, ConsentType::Implicit, ConsentVersion::stable());
}

public function testConsentWriteToDatabase()
Expand Down Expand Up @@ -175,6 +175,6 @@ public function testNullNameIdReturnsNoConsentWithoutCallingRepository()
$this->assertFalse($consentWithNullUid->giveExplicitConsentFor($serviceProvider));
$this->assertFalse($consentWithNullUid->giveImplicitConsentFor($serviceProvider));
// upgradeAttributeHashFor should not throw when UID is null
$consentWithNullUid->upgradeAttributeHashFor($serviceProvider, ConsentType::TYPE_EXPLICIT, ConsentVersion::notGiven());
$consentWithNullUid->upgradeAttributeHashFor($serviceProvider, ConsentType::Explicit, ConsentVersion::notGiven());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function all_values_are_serialized_to_json()
{
$serviceProvider = $this->createServiceProvider();
$consentGivenOn = new DateTime('20080624 10:00:00');
$consentType = ConsentType::explicit();
$consentType = ConsentType::Explicit;

$consent = new Consent(
new ConsentModel(
Expand Down Expand Up @@ -135,7 +135,7 @@ public function test_display_name_of_organizations_works_as_intended(
) {
$serviceProvider = $this->createServiceProvider($organizations);
$consentGivenOn = new DateTime('20080624 10:00:00');
$consentType = ConsentType::explicit();
$consentType = ConsentType::Explicit;

$consent = new Consent(
new ConsentModel(
Expand Down Expand Up @@ -165,7 +165,7 @@ public function display_name_falls_back_to_name_if_display_name_is_empty()
$serviceProvider->nameNl = 'Name NL';

$consentGivenOn = new DateTime();
$consentType = ConsentType::explicit();
$consentType = ConsentType::Explicit;

$consent = new Consent(
new ConsentModel(
Expand Down Expand Up @@ -196,7 +196,7 @@ public function display_name_falls_back_to_entity_id_if_name_is_empty()
$serviceProvider->nameNl = '';

$consentGivenOn = new DateTime();
$consentType = ConsentType::explicit();
$consentType = ConsentType::Explicit;

$consent = new Consent(
new ConsentModel(
Expand Down
Loading
Loading