Hash existing API keys, and do checks against the hash

This commit is contained in:
Alejandro Celaya
2024-11-05 23:23:06 +01:00
parent 9f6975119e
commit 1b9c8377ae
5 changed files with 61 additions and 10 deletions

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace ShlinkMigrations;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use function hash;
/**
* Hash API keys as SHA256
*/
final class Version20241105215309 extends AbstractMigration
{
public function up(Schema $schema): void
{
$keyColumnName = $this->connection->quoteIdentifier('key');
$qb = $this->connection->createQueryBuilder();
$qb->select($keyColumnName)
->from('api_keys');
$result = $qb->executeQuery();
$updateQb = $this->connection->createQueryBuilder();
$updateQb
->update('api_keys')
->set($keyColumnName, ':encryptedKey')
->where($updateQb->expr()->eq($keyColumnName, ':plainTextKey'));
while ($key = $result->fetchOne()) {
$updateQb->setParameters([
'encryptedKey' => hash('sha256', $key),
'plainTextKey' => $key,
])->executeStatement();
}
}
public function isTransactional(): bool
{
return ! ($this->connection->getDatabasePlatform() instanceof MySQLPlatform);
}
}