-
Notifications
You must be signed in to change notification settings - Fork 3
legacy cryptography attribute metadata mapping #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/Extension/Cryptography/LegacyCryptographyMetadataEnricher.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Patchlevel\Hydrator\Extension\Cryptography; | ||
|
|
||
| use Patchlevel\Hydrator\Attribute\DataSubjectId; | ||
| use Patchlevel\Hydrator\Attribute\PersonalData; | ||
| use Patchlevel\Hydrator\Metadata\ClassMetadata; | ||
| use Patchlevel\Hydrator\Metadata\MetadataEnricher; | ||
| use ReflectionProperty; | ||
|
|
||
| use function array_key_exists; | ||
| use function in_array; | ||
|
|
||
| final class LegacyCryptographyMetadataEnricher implements MetadataEnricher | ||
| { | ||
| private const SUBJECT_ID = 'legacy'; | ||
|
|
||
| public function enrich(ClassMetadata $classMetadata): void | ||
| { | ||
| /** @var array<string, string> $subjectIdMapping */ | ||
| $subjectIdMapping = isset($classMetadata->extras[SubjectIdFieldMapping::class]) | ||
| ? $classMetadata->extras[SubjectIdFieldMapping::class]->nameToField | ||
| : []; | ||
DavidBadura marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| foreach ($classMetadata->properties as $property) { | ||
| $attributeReflectionList = $property->reflection->getAttributes(DataSubjectId::class); | ||
|
|
||
| if ($attributeReflectionList) { | ||
| if (array_key_exists(self::SUBJECT_ID, $subjectIdMapping)) { | ||
| throw new DuplicateSubjectIdIdentifier( | ||
| $classMetadata->className, | ||
| $classMetadata->propertyForField($subjectIdMapping[self::SUBJECT_ID])->propertyName, | ||
| $property->propertyName, | ||
| self::SUBJECT_ID, | ||
| ); | ||
| } | ||
|
|
||
| $subjectIdMapping[self::SUBJECT_ID] = $property->fieldName; | ||
| } | ||
|
|
||
| $sensitiveDataInfo = $this->sensitiveDataInfo($property->reflection); | ||
|
|
||
| if (!$sensitiveDataInfo) { | ||
| continue; | ||
| } | ||
|
|
||
| if (in_array($property->fieldName, $subjectIdMapping, true)) { | ||
| throw new SubjectIdAndSensitiveDataConflict($classMetadata->className, $property->propertyName); | ||
| } | ||
|
|
||
| if (isset($property->extras[SensitiveDataInfo::class])) { | ||
| throw new PersonalDataAndSensitiveDataOnSameProperty($classMetadata->className, $property->propertyName); | ||
| } | ||
|
|
||
| $property->extras[SensitiveDataInfo::class] = $sensitiveDataInfo; | ||
| } | ||
|
|
||
| if ($subjectIdMapping === []) { | ||
| return; | ||
| } | ||
|
|
||
| $classMetadata->extras[SubjectIdFieldMapping::class] = new SubjectIdFieldMapping($subjectIdMapping); | ||
| } | ||
|
|
||
| private function sensitiveDataInfo(ReflectionProperty $reflectionProperty): SensitiveDataInfo|null | ||
| { | ||
| $attributeReflectionList = $reflectionProperty->getAttributes(PersonalData::class); | ||
|
|
||
| if ($attributeReflectionList === []) { | ||
| return null; | ||
| } | ||
|
|
||
| $attribute = $attributeReflectionList[0]->newInstance(); | ||
|
|
||
| return new SensitiveDataInfo( | ||
| self::SUBJECT_ID, | ||
| $attribute->fallbackCallable !== null ? ($attribute->fallbackCallable)(...) : $attribute->fallback, | ||
| ); | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
src/Extension/Cryptography/PersonalDataAndSensitiveDataOnSameProperty.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Patchlevel\Hydrator\Extension\Cryptography; | ||
|
|
||
| use Patchlevel\Hydrator\Metadata\MetadataException; | ||
| use RuntimeException; | ||
|
|
||
| use function sprintf; | ||
|
|
||
| /** @experimental */ | ||
| final class PersonalDataAndSensitiveDataOnSameProperty extends RuntimeException implements MetadataException | ||
| { | ||
| /** @param class-string $class */ | ||
| public function __construct(string $class, string $property) | ||
| { | ||
| parent::__construct( | ||
| sprintf( | ||
| 'Personal data and sensitive data cannot be used on the same property %s::%s', | ||
| $class, | ||
| $property, | ||
| ), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
tests/Unit/Extension/Cryptography/CryptographyExtensionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Patchlevel\Hydrator\Tests\Unit\Extension\Cryptography; | ||
|
|
||
| use Patchlevel\Hydrator\Cryptography\PayloadCryptographer; | ||
| use Patchlevel\Hydrator\Extension\Cryptography\Cryptographer; | ||
| use Patchlevel\Hydrator\Extension\Cryptography\CryptographyExtension; | ||
| use Patchlevel\Hydrator\Extension\Cryptography\CryptographyMetadataEnricher; | ||
| use Patchlevel\Hydrator\Extension\Cryptography\CryptographyMiddleware; | ||
| use Patchlevel\Hydrator\Extension\Cryptography\LegacyCryptographyDecryptMiddleware; | ||
| use Patchlevel\Hydrator\Extension\Cryptography\LegacyCryptographyMetadataEnricher; | ||
| use Patchlevel\Hydrator\StackHydratorBuilder; | ||
| use PHPUnit\Framework\Attributes\CoversClass; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| #[CoversClass(CryptographyExtension::class)] | ||
| final class CryptographyExtensionTest extends TestCase | ||
| { | ||
| public function testConfigureWithoutLegacyOptions(): void | ||
| { | ||
| $builder = new StackHydratorBuilder(); | ||
| $cryptographer = $this->createMock(Cryptographer::class); | ||
|
|
||
| $extension = new CryptographyExtension($cryptographer); | ||
| $extension->configure($builder); | ||
|
|
||
| $middlewares = $builder->middlewares(); | ||
| self::assertCount(1, $middlewares); | ||
| self::assertInstanceOf(CryptographyMiddleware::class, $middlewares[0]); | ||
|
|
||
| $metadataEnrichers = $builder->metadataEnrichers(); | ||
| self::assertCount(1, $metadataEnrichers); | ||
| self::assertInstanceOf(CryptographyMetadataEnricher::class, $metadataEnrichers[0]); | ||
| } | ||
|
|
||
| public function testConfigureWithLegacyMetadataMapping(): void | ||
| { | ||
| $builder = new StackHydratorBuilder(); | ||
| $cryptographer = $this->createMock(Cryptographer::class); | ||
|
|
||
| $extension = new CryptographyExtension($cryptographer, legacyMetadataMapping: true); | ||
| $extension->configure($builder); | ||
|
|
||
| $metadataEnrichers = $builder->metadataEnrichers(); | ||
| self::assertCount(2, $metadataEnrichers); | ||
| self::assertInstanceOf(CryptographyMetadataEnricher::class, $metadataEnrichers[0]); | ||
| self::assertInstanceOf(LegacyCryptographyMetadataEnricher::class, $metadataEnrichers[1]); | ||
| } | ||
|
|
||
| public function testConfigureWithLegacyCryptographerAndMetadataMapping(): void | ||
| { | ||
| $builder = new StackHydratorBuilder(); | ||
| $cryptographer = $this->createMock(Cryptographer::class); | ||
| $legacyCryptographer = $this->createMock(PayloadCryptographer::class); | ||
|
|
||
| $extension = new CryptographyExtension($cryptographer, $legacyCryptographer, true); | ||
| $extension->configure($builder); | ||
|
|
||
| $middlewares = $builder->middlewares(); | ||
| self::assertCount(2, $middlewares); | ||
| self::assertInstanceOf(LegacyCryptographyDecryptMiddleware::class, $middlewares[0]); | ||
| self::assertInstanceOf(CryptographyMiddleware::class, $middlewares[1]); | ||
|
|
||
| $metadataEnrichers = $builder->metadataEnrichers(); | ||
| self::assertCount(2, $metadataEnrichers); | ||
| self::assertInstanceOf(CryptographyMetadataEnricher::class, $metadataEnrichers[0]); | ||
| self::assertInstanceOf(LegacyCryptographyMetadataEnricher::class, $metadataEnrichers[1]); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.