Version011601Date20230522143227.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\ISchemaWrapper;
  10. use OCP\DB\QueryBuilder\IQueryBuilder;
  11. use OCP\IDBConnection;
  12. use OCP\Migration\IOutput;
  13. use OCP\Migration\SimpleMigrationStep;
  14. use OCP\Security\ICrypto;
  15. class Version011601Date20230522143227 extends SimpleMigrationStep {
  16. public function __construct(
  17. private IDBConnection $connection,
  18. private ICrypto $crypto,
  19. ) {
  20. }
  21. public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
  22. /** @var ISchemaWrapper $schema */
  23. $schema = $schemaClosure();
  24. if ($schema->hasTable('oauth2_clients')) {
  25. $table = $schema->getTable('oauth2_clients');
  26. if ($table->hasColumn('secret')) {
  27. $column = $table->getColumn('secret');
  28. $column->setLength(512);
  29. return $schema;
  30. }
  31. }
  32. return null;
  33. }
  34. public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
  35. $qbUpdate = $this->connection->getQueryBuilder();
  36. $qbUpdate->update('oauth2_clients')
  37. ->set('secret', $qbUpdate->createParameter('updateSecret'))
  38. ->where(
  39. $qbUpdate->expr()->eq('id', $qbUpdate->createParameter('updateId'))
  40. );
  41. $qbSelect = $this->connection->getQueryBuilder();
  42. $qbSelect->select('id', 'secret')
  43. ->from('oauth2_clients');
  44. $req = $qbSelect->executeQuery();
  45. while ($row = $req->fetch()) {
  46. $id = $row['id'];
  47. $secret = $row['secret'];
  48. $encryptedSecret = $this->crypto->encrypt($secret);
  49. $qbUpdate->setParameter('updateSecret', $encryptedSecret, IQueryBuilder::PARAM_STR);
  50. $qbUpdate->setParameter('updateId', $id, IQueryBuilder::PARAM_INT);
  51. $qbUpdate->executeStatement();
  52. }
  53. $req->closeCursor();
  54. }
  55. }