MigrateOauthTables.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Repair\Owncloud;
  7. use OC\DB\Connection;
  8. use OC\DB\SchemaWrapper;
  9. use OCP\DB\QueryBuilder\IQueryBuilder;
  10. use OCP\Migration\IOutput;
  11. use OCP\Migration\IRepairStep;
  12. class MigrateOauthTables implements IRepairStep {
  13. /** @var Connection */
  14. protected $db;
  15. /**
  16. * @param Connection $db
  17. */
  18. public function __construct(Connection $db) {
  19. $this->db = $db;
  20. }
  21. /**
  22. * @return string
  23. */
  24. public function getName() {
  25. return 'Migrate oauth2_clients table to nextcloud schema';
  26. }
  27. public function run(IOutput $output) {
  28. $schema = new SchemaWrapper($this->db);
  29. if (!$schema->hasTable('oauth2_clients')) {
  30. $output->info('oauth2_clients table does not exist.');
  31. return;
  32. }
  33. $output->info('Update the oauth2_access_tokens table schema.');
  34. $table = $schema->getTable('oauth2_access_tokens');
  35. if (!$table->hasColumn('hashed_code')) {
  36. $table->addColumn('hashed_code', 'string', [
  37. 'notnull' => true,
  38. 'length' => 128,
  39. ]);
  40. }
  41. if (!$table->hasColumn('encrypted_token')) {
  42. $table->addColumn('encrypted_token', 'string', [
  43. 'notnull' => true,
  44. 'length' => 786,
  45. ]);
  46. }
  47. if (!$table->hasIndex('oauth2_access_hash_idx')) {
  48. $table->addUniqueIndex(['hashed_code'], 'oauth2_access_hash_idx');
  49. }
  50. if (!$table->hasIndex('oauth2_access_client_id_idx')) {
  51. $table->addIndex(['client_id'], 'oauth2_access_client_id_idx');
  52. }
  53. $output->info('Update the oauth2_clients table schema.');
  54. $table = $schema->getTable('oauth2_clients');
  55. if ($table->getColumn('name')->getLength() !== 64) {
  56. // shorten existing values before resizing the column
  57. $qb = $this->db->getQueryBuilder();
  58. $qb->update('oauth2_clients')
  59. ->set('name', $qb->createParameter('shortenedName'))
  60. ->where($qb->expr()->eq('id', $qb->createParameter('theId')));
  61. $qbSelect = $this->db->getQueryBuilder();
  62. $qbSelect->select('id', 'name')
  63. ->from('oauth2_clients');
  64. $result = $qbSelect->executeQuery();
  65. while ($row = $result->fetch()) {
  66. $id = $row['id'];
  67. $shortenedName = mb_substr($row['name'], 0, 64);
  68. $qb->setParameter('theId', $id, IQueryBuilder::PARAM_INT);
  69. $qb->setParameter('shortenedName', $shortenedName, IQueryBuilder::PARAM_STR);
  70. $qb->executeStatement();
  71. }
  72. $result->closeCursor();
  73. // safely set the new column length
  74. $table->getColumn('name')->setLength(64);
  75. }
  76. if ($table->hasColumn('allow_subdomains')) {
  77. $table->dropColumn('allow_subdomains');
  78. }
  79. if ($table->hasColumn('trusted')) {
  80. $table->dropColumn('trusted');
  81. }
  82. if (!$schema->getTable('oauth2_clients')->hasColumn('client_identifier')) {
  83. $table->addColumn('client_identifier', 'string', [
  84. 'notnull' => true,
  85. 'length' => 64,
  86. 'default' => ''
  87. ]);
  88. $table->addIndex(['client_identifier'], 'oauth2_client_id_idx');
  89. }
  90. $this->db->migrateToSchema($schema->getWrappedSchema());
  91. // Regenerate schema after migrating to it
  92. $schema = new SchemaWrapper($this->db);
  93. if ($schema->getTable('oauth2_clients')->hasColumn('identifier')) {
  94. $output->info("Move identifier column's data to the new client_identifier column.");
  95. // 1. Fetch all [id, identifier] couple.
  96. $selectQuery = $this->db->getQueryBuilder();
  97. $selectQuery->select('id', 'identifier')->from('oauth2_clients');
  98. $result = $selectQuery->executeQuery();
  99. $identifiers = $result->fetchAll();
  100. $result->closeCursor();
  101. // 2. Insert them into the client_identifier column.
  102. foreach ($identifiers as ['id' => $id, 'identifier' => $clientIdentifier]) {
  103. $insertQuery = $this->db->getQueryBuilder();
  104. $insertQuery->update('oauth2_clients')
  105. ->set('client_identifier', $insertQuery->createNamedParameter($clientIdentifier, IQueryBuilder::PARAM_STR))
  106. ->where($insertQuery->expr()->eq('id', $insertQuery->createNamedParameter($id, IQueryBuilder::PARAM_INT)))
  107. ->executeStatement();
  108. }
  109. $output->info('Drop the identifier column.');
  110. $table = $schema->getTable('oauth2_clients');
  111. $table->dropColumn('identifier');
  112. $this->db->migrateToSchema($schema->getWrappedSchema());
  113. }
  114. $output->info('Delete clients (and their related access tokens) with the redirect_uri starting with oc:// or ending with *');
  115. // delete the access tokens
  116. $qbDeleteAccessTokens = $this->db->getQueryBuilder();
  117. $qbSelectClientId = $this->db->getQueryBuilder();
  118. $qbSelectClientId->select('id')
  119. ->from('oauth2_clients')
  120. ->where(
  121. $qbSelectClientId->expr()->iLike('redirect_uri', $qbDeleteAccessTokens->createNamedParameter('oc://%', IQueryBuilder::PARAM_STR))
  122. )
  123. ->orWhere(
  124. $qbSelectClientId->expr()->iLike('redirect_uri', $qbDeleteAccessTokens->createNamedParameter('%*', IQueryBuilder::PARAM_STR))
  125. );
  126. $qbDeleteAccessTokens->delete('oauth2_access_tokens')
  127. ->where(
  128. $qbSelectClientId->expr()->in('client_id', $qbDeleteAccessTokens->createFunction($qbSelectClientId->getSQL()), IQueryBuilder::PARAM_STR_ARRAY)
  129. );
  130. $qbDeleteAccessTokens->executeStatement();
  131. // delete the clients
  132. $qbDeleteClients = $this->db->getQueryBuilder();
  133. $qbDeleteClients->delete('oauth2_clients')
  134. ->where(
  135. $qbDeleteClients->expr()->iLike('redirect_uri', $qbDeleteClients->createNamedParameter('oc://%', IQueryBuilder::PARAM_STR))
  136. )
  137. ->orWhere(
  138. $qbDeleteClients->expr()->iLike('redirect_uri', $qbDeleteClients->createNamedParameter('%*', IQueryBuilder::PARAM_STR))
  139. );
  140. $qbDeleteClients->executeStatement();
  141. }
  142. }