|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Patchlevel\EventSourcing\Cryptography; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Connection; |
| 8 | +use Doctrine\DBAL\Schema\Schema; |
| 9 | +use Doctrine\DBAL\Types\Type; |
| 10 | +use Doctrine\DBAL\Types\Types; |
| 11 | +use Patchlevel\EventSourcing\Schema\DoctrineHelper; |
| 12 | +use Patchlevel\EventSourcing\Schema\DoctrineSchemaConfigurator; |
| 13 | +use Patchlevel\Hydrator\Extension\Cryptography\Cipher\CipherKey; |
| 14 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\CipherKeyNotExists; |
| 15 | +use Patchlevel\Hydrator\Extension\Cryptography\Store\CipherKeyStore; |
| 16 | + |
| 17 | +use function base64_decode; |
| 18 | +use function base64_encode; |
| 19 | + |
| 20 | +/** |
| 21 | + * @phpstan-type Row = array{ |
| 22 | + * id: non-empty-string, |
| 23 | + * subject_id: non-empty-string, |
| 24 | + * crypto_key: non-empty-string, |
| 25 | + * crypto_method: non-empty-string, |
| 26 | + * created_at: non-empty-string |
| 27 | + * } |
| 28 | + */ |
| 29 | +final class ExtensionDoctrineCipherKeyStore implements CipherKeyStore, DoctrineSchemaConfigurator |
| 30 | +{ |
| 31 | + private Type $dateTimeType; |
| 32 | + |
| 33 | + public function __construct( |
| 34 | + private readonly Connection $connection, |
| 35 | + private readonly string $tableName = 'cryptography_keys', |
| 36 | + ) { |
| 37 | + $this->dateTimeType = Type::getType(Types::DATETIMETZ_IMMUTABLE); |
| 38 | + } |
| 39 | + |
| 40 | + public function get(string $id): CipherKey |
| 41 | + { |
| 42 | + /** @var Row|false $result */ |
| 43 | + $result = $this->connection->fetchAssociative( |
| 44 | + "SELECT * FROM {$this->tableName} WHERE id = :id", |
| 45 | + ['id' => $id], |
| 46 | + ); |
| 47 | + |
| 48 | + if ($result === false) { |
| 49 | + throw CipherKeyNotExists::forKeyId($id); |
| 50 | + } |
| 51 | + |
| 52 | + return new CipherKey( |
| 53 | + $result['id'], |
| 54 | + $result['subject_id'], |
| 55 | + base64_decode($result['crypto_key']), |
| 56 | + $result['crypto_method'], |
| 57 | + $this->dateTimeType->convertToPHPValue($result['created_at'], $this->connection->getDatabasePlatform()), |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public function currentKeyFor(string $subjectId): CipherKey |
| 62 | + { |
| 63 | + /** @var Row|false $result */ |
| 64 | + $result = $this->connection->fetchAssociative( |
| 65 | + "SELECT * FROM {$this->tableName} WHERE subject_id = :subject_id", |
| 66 | + ['subject_id' => $subjectId], |
| 67 | + ); |
| 68 | + |
| 69 | + if ($result === false) { |
| 70 | + throw CipherKeyNotExists::forSubjectId($subjectId); |
| 71 | + } |
| 72 | + |
| 73 | + return new CipherKey( |
| 74 | + $result['id'], |
| 75 | + base64_decode($result['crypto_key']), |
| 76 | + $result['crypto_method'], |
| 77 | + base64_decode($result['crypto_iv']), |
| 78 | + $this->dateTimeType->convertToPHPValue($result['created_at'], $this->connection->getDatabasePlatform()), |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + public function store(CipherKey $key): void |
| 83 | + { |
| 84 | + $this->connection->insert($this->tableName, [ |
| 85 | + 'id' => $key->id, |
| 86 | + 'subject_id' => $key->subjectId, |
| 87 | + 'crypto_key' => base64_encode($key->key), |
| 88 | + 'crypto_method' => $key->method, |
| 89 | + 'created_at' => $this->dateTimeType->convertToDatabaseValue($key->createdAt, $this->connection->getDatabasePlatform()), |
| 90 | + ]); |
| 91 | + } |
| 92 | + |
| 93 | + public function remove(string $id): void |
| 94 | + { |
| 95 | + $this->connection->delete($this->tableName, ['id' => $id]); |
| 96 | + } |
| 97 | + |
| 98 | + public function removeWithSubjectId(string $subjectId): void |
| 99 | + { |
| 100 | + $this->connection->delete($this->tableName, ['subject_id' => $subjectId]); |
| 101 | + } |
| 102 | + |
| 103 | + public function configureSchema(Schema $schema, Connection $connection): void |
| 104 | + { |
| 105 | + if (!DoctrineHelper::sameDatabase($this->connection, $connection)) { |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + $table = $schema->createTable($this->tableName); |
| 110 | + $table->addColumn('id', 'string') |
| 111 | + ->setNotnull(true) |
| 112 | + ->setLength(255); |
| 113 | + $table->addColumn('subject_id', 'string') |
| 114 | + ->setNotnull(true) |
| 115 | + ->setLength(255); |
| 116 | + $table->addColumn('crypto_key', 'string') |
| 117 | + ->setNotnull(true) |
| 118 | + ->setLength(255); |
| 119 | + $table->addColumn('crypto_method', 'string') |
| 120 | + ->setNotnull(true) |
| 121 | + ->setLength(255); |
| 122 | + $table->addColumn('created_at', 'datetimetz_immutable') |
| 123 | + ->setNotnull(true); |
| 124 | + $table->setPrimaryKey(['id']); |
| 125 | + $table->addIndex(['subject_id']); |
| 126 | + } |
| 127 | +} |
0 commit comments