-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Store encrypted OAuth2 client secrets #38398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright 2023, Julien Veyssier <[email protected]> | ||
| * | ||
| * @author Julien Veyssier <[email protected]> | ||
| * | ||
| * @license GNU AGPL version 3 or any later version | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU Affero General Public License as | ||
| * published by the Free Software Foundation, either version 3 of the | ||
| * License, or (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU Affero General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU Affero General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| * | ||
| */ | ||
| namespace OCA\OAuth2\Migration; | ||
|
|
||
| use Closure; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||
| use OCP\IDBConnection; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
| use OCP\Security\ICrypto; | ||
|
|
||
| class Version011601Date20230522143227 extends SimpleMigrationStep { | ||
|
|
||
| public function __construct( | ||
| private IDBConnection $connection, | ||
| private ICrypto $crypto, | ||
| ) { | ||
| } | ||
|
|
||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
|
|
||
| if ($schema->hasTable('oauth2_clients')) { | ||
| $table = $schema->getTable('oauth2_clients'); | ||
| if ($table->hasColumn('secret')) { | ||
| $column = $table->getColumn('secret'); | ||
| $column->setLength(256); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's widen to 512?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 Thanks a lot. |
||
| return $schema; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { | ||
|
||
| $qbUpdate = $this->connection->getQueryBuilder(); | ||
| $qbUpdate->update('oauth2_clients') | ||
| ->set('secret', $qbUpdate->createParameter('updateSecret')) | ||
| ->where( | ||
| $qbUpdate->expr()->eq('id', $qbUpdate->createParameter('updateId')) | ||
| ); | ||
|
|
||
| $qbSelect = $this->connection->getQueryBuilder(); | ||
| $qbSelect->select('id', 'secret') | ||
| ->from('oauth2_clients'); | ||
| $req = $qbSelect->executeQuery(); | ||
| while ($row = $req->fetch()) { | ||
| $id = $row['id']; | ||
| $secret = $row['secret']; | ||
| $encryptedSecret = $this->crypto->encrypt($secret); | ||
| $qbUpdate->setParameter('updateSecret', $encryptedSecret, IQueryBuilder::PARAM_STR); | ||
| $qbUpdate->setParameter('updateId', $id, IQueryBuilder::PARAM_INT); | ||
| $qbUpdate->executeStatement(); | ||
| } | ||
| $req->closeCursor(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.