Skip to content

Commit 6d4dbe4

Browse files
authored
Merge pull request #38682 from nextcloud/backport/38577/stable25
[stable25] Improve oauth2 database migration from ownCloud
2 parents 943bcb4 + 3188f68 commit 6d4dbe4

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

lib/private/Repair/Owncloud/MigrateOauthTables.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,35 @@ public function run(IOutput $output) {
7878
$schema = new SchemaWrapper($this->db);
7979
$table = $schema->getTable('oauth2_clients');
8080
if ($table->getColumn('name')->getLength() !== 64) {
81+
// shorten existing values before resizing the column
82+
$qb = $this->db->getQueryBuilder();
83+
$qb->update('oauth2_clients')
84+
->set('name', $qb->createParameter('shortenedName'))
85+
->where($qb->expr()->eq('id', $qb->createParameter('theId')));
86+
87+
$qbSelect = $this->db->getQueryBuilder();
88+
$qbSelect->select('id', 'name')
89+
->from('oauth2_clients');
90+
91+
$result = $qbSelect->executeQuery();
92+
while ($row = $result->fetch()) {
93+
$id = $row['id'];
94+
$shortenedName = mb_substr($row['name'], 0, 64);
95+
$qb->setParameter('theId', $id, IQueryBuilder::PARAM_INT);
96+
$qb->setParameter('shortenedName', $shortenedName, IQueryBuilder::PARAM_STR);
97+
$qb->executeStatement();
98+
}
99+
$result->closeCursor();
100+
101+
// safely set the new column length
81102
$table->getColumn('name')->setLength(64);
82103
}
83104
if ($table->hasColumn('allow_subdomains')) {
84105
$table->dropColumn('allow_subdomains');
85106
}
107+
if ($table->hasColumn('trusted')) {
108+
$table->dropColumn('trusted');
109+
}
86110

87111
if (!$schema->getTable('oauth2_clients')->hasColumn('client_identifier')) {
88112
$table->addColumn('client_identifier', 'string', [
@@ -120,5 +144,36 @@ public function run(IOutput $output) {
120144
$table->dropColumn('identifier');
121145
$this->db->migrateToSchema($schema->getWrappedSchema());
122146
}
147+
148+
$output->info('Delete clients (and their related access tokens) with the redirect_uri starting with oc:// or ending with *');
149+
// delete the access tokens
150+
$qbDeleteAccessTokens = $this->db->getQueryBuilder();
151+
152+
$qbSelectClientId = $this->db->getQueryBuilder();
153+
$qbSelectClientId->select('id')
154+
->from('oauth2_clients')
155+
->where(
156+
$qbSelectClientId->expr()->iLike('redirect_uri', $qbDeleteAccessTokens->createNamedParameter('oc://%', IQueryBuilder::PARAM_STR))
157+
)
158+
->orWhere(
159+
$qbSelectClientId->expr()->iLike('redirect_uri', $qbDeleteAccessTokens->createNamedParameter('%*', IQueryBuilder::PARAM_STR))
160+
);
161+
162+
$qbDeleteAccessTokens->delete('oauth2_access_tokens')
163+
->where(
164+
$qbSelectClientId->expr()->in('client_id', $qbDeleteAccessTokens->createFunction($qbSelectClientId->getSQL()), IQueryBuilder::PARAM_STR_ARRAY)
165+
);
166+
$qbDeleteAccessTokens->executeStatement();
167+
168+
// delete the clients
169+
$qbDeleteClients = $this->db->getQueryBuilder();
170+
$qbDeleteClients->delete('oauth2_clients')
171+
->where(
172+
$qbDeleteClients->expr()->iLike('redirect_uri', $qbDeleteClients->createNamedParameter('oc://%', IQueryBuilder::PARAM_STR))
173+
)
174+
->orWhere(
175+
$qbDeleteClients->expr()->iLike('redirect_uri', $qbDeleteClients->createNamedParameter('%*', IQueryBuilder::PARAM_STR))
176+
);
177+
$qbDeleteClients->executeStatement();
123178
}
124179
}

0 commit comments

Comments
 (0)