Version011901Date20240829164356.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\OAuth2\Migration;
  8. use Closure;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\IDBConnection;
  11. use OCP\Migration\IOutput;
  12. use OCP\Migration\SimpleMigrationStep;
  13. use OCP\Security\ICrypto;
  14. class Version011901Date20240829164356 extends SimpleMigrationStep {
  15. public function __construct(
  16. private IDBConnection $connection,
  17. private ICrypto $crypto,
  18. ) {
  19. }
  20. public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
  21. $qbUpdate = $this->connection->getQueryBuilder();
  22. $qbUpdate->update('oauth2_clients')
  23. ->set('secret', $qbUpdate->createParameter('updateSecret'))
  24. ->where(
  25. $qbUpdate->expr()->eq('id', $qbUpdate->createParameter('updateId'))
  26. );
  27. $qbSelect = $this->connection->getQueryBuilder();
  28. $qbSelect->select('id', 'secret')
  29. ->from('oauth2_clients');
  30. $req = $qbSelect->executeQuery();
  31. while ($row = $req->fetch()) {
  32. $id = $row['id'];
  33. $storedEncryptedSecret = $row['secret'];
  34. $secret = $this->crypto->decrypt($storedEncryptedSecret);
  35. $hashedSecret = bin2hex($this->crypto->calculateHMAC($secret));
  36. $qbUpdate->setParameter('updateSecret', $hashedSecret, IQueryBuilder::PARAM_STR);
  37. $qbUpdate->setParameter('updateId', $id, IQueryBuilder::PARAM_INT);
  38. $qbUpdate->executeStatement();
  39. }
  40. $req->closeCursor();
  41. }
  42. }