-
-
Notifications
You must be signed in to change notification settings - Fork 276
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hash existing API keys, and do checks against the hash
- Loading branch information
Showing
5 changed files
with
61 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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,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); | ||
} | ||
} |
This file contains 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 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 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 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