|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * @copyright Copyright 2023, Julien Veyssier <julien-nc@posteo.net> |
| 7 | + * |
| 8 | + * @author B. Rederlechner <bernd.rederlechner@t-systems.com> |
| 9 | + * |
| 10 | + * @license GNU AGPL version 3 or any later version |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU Affero General Public License as |
| 14 | + * published by the Free Software Foundation, either version 3 of the |
| 15 | + * License, or (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU Affero General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU Affero General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + */ |
| 26 | +namespace OCA\UserOIDC\Migration; |
| 27 | + |
| 28 | +use Closure; |
| 29 | +use OCP\DB\ISchemaWrapper; |
| 30 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 31 | +use OCP\IDBConnection; |
| 32 | +use OCP\Migration\IOutput; |
| 33 | +use OCP\Migration\SimpleMigrationStep; |
| 34 | +use OCP\Security\ICrypto; |
| 35 | + |
| 36 | +class Version010304Date20230902125945 extends SimpleMigrationStep { |
| 37 | + |
| 38 | + /** |
| 39 | + * @var IDBConnection |
| 40 | + */ |
| 41 | + private $connection; |
| 42 | + /** |
| 43 | + * @var ICrypto |
| 44 | + */ |
| 45 | + private $crypto; |
| 46 | + |
| 47 | + public function __construct( |
| 48 | + IDBConnection $connection, |
| 49 | + ICrypto $crypto |
| 50 | + ) { |
| 51 | + $this->connection = $connection; |
| 52 | + $this->crypto = $crypto; |
| 53 | + } |
| 54 | + |
| 55 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
| 56 | + /** @var ISchemaWrapper $schema */ |
| 57 | + $schema = $schemaClosure(); |
| 58 | + $tableName = 'user_oidc_providers'; |
| 59 | + |
| 60 | + if ($schema->hasTable($tableName)) { |
| 61 | + $table = $schema->getTable($tableName); |
| 62 | + if ($table->hasColumn('bearer_secret')) { |
| 63 | + $column = $table->getColumn('bearer_secret'); |
| 64 | + $column->setLength(512); |
| 65 | + return $schema; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
| 73 | + $tableName = 'user_oidc_providers'; |
| 74 | + |
| 75 | + // update secrets in user_oidc_providers and user_oidc_id4me |
| 76 | + $qbUpdate = $this->connection->getQueryBuilder(); |
| 77 | + $qbUpdate->update($tableName) |
| 78 | + ->set('bearer_secret', $qbUpdate->createParameter('updateSecret')) |
| 79 | + ->where( |
| 80 | + $qbUpdate->expr()->eq('id', $qbUpdate->createParameter('updateId')) |
| 81 | + ); |
| 82 | + |
| 83 | + $qbSelect = $this->connection->getQueryBuilder(); |
| 84 | + $qbSelect->select('id', 'bearer_secret') |
| 85 | + ->from($tableName); |
| 86 | + $req = $qbSelect->executeQuery(); |
| 87 | + while ($row = $req->fetch()) { |
| 88 | + $id = $row['id']; |
| 89 | + $secret = $row['bearer_secret']; |
| 90 | + $encryptedSecret = $this->crypto->encrypt($secret); |
| 91 | + $qbUpdate->setParameter('updateSecret', $encryptedSecret, IQueryBuilder::PARAM_STR); |
| 92 | + $qbUpdate->setParameter('updateId', $id, IQueryBuilder::PARAM_INT); |
| 93 | + $qbUpdate->executeStatement(); |
| 94 | + } |
| 95 | + $req->closeCursor(); |
| 96 | + } |
| 97 | +} |
0 commit comments