-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
perf: move remember login tokens out of oc_preferences #64203
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
Open
cristianscheid
wants to merge
5
commits into
master
Choose a base branch
from
perf/61728/remember-login-token
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b985d88
perf: move remember login tokens out of oc_preferences
cristianscheid e2e44f5
fix: bump version.php to trigger DB upgrades on dev instances
cristianscheid 76a517d
refactor: port RememberLoginToken to new entity system and use snowfl…
cristianscheid 55e716d
fix(remember-login-token): avoid unecessary insert when migrating fro…
cristianscheid 9c2d598
fix(session): guard usages of RememberLoginTokenMapper
cristianscheid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Core\Migrations; | ||
|
|
||
| use Closure; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\Attributes\AddIndex; | ||
| use OCP\Migration\Attributes\CreateTable; | ||
| use OCP\Migration\Attributes\IndexType; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
| use Override; | ||
|
|
||
| #[CreateTable( | ||
| table: 'remember_login_tokens', | ||
| columns: ['uid', 'token'], | ||
| description: 'New table to store remember login tokens, replacing the login_token entries kept in oc_preferences', | ||
| )] | ||
| #[AddIndex(table: 'remember_login_tokens', type: IndexType::PRIMARY)] | ||
| #[AddIndex(table: 'remember_login_tokens', type: IndexType::UNIQUE, description: 'Allows to search on token')] | ||
| #[AddIndex(table: 'remember_login_tokens', type: IndexType::INDEX, description: 'Allows to search on user ID')] | ||
| class Version36000Date20260908184209 extends SimpleMigrationStep { | ||
|
|
||
| #[Override] | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
|
|
||
| if (!$schema->hasTable('remember_login_tokens')) { | ||
| $table = $schema->createTable('remember_login_tokens'); | ||
| $table->addColumn('id', Types::BIGINT, [ | ||
| 'notnull' => true, | ||
| 'length' => 20, | ||
| 'unsigned' => true, | ||
| ]); | ||
| $table->addColumn('uid', Types::STRING, [ | ||
| 'notnull' => true, | ||
| 'length' => 64, | ||
| ]); | ||
| $table->addColumn('token', Types::STRING, [ | ||
| 'notnull' => true, | ||
| 'length' => 200, | ||
| ]); | ||
| $table->setPrimaryKey(['id']); | ||
| $table->addUniqueIndex(['token'], 'remember_login_tokens_token'); | ||
| $table->addIndex(['uid'], 'remember_login_tokens_uid'); | ||
|
|
||
| return $schema; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
lib/private/Authentication/RememberLogin/RememberLoginToken.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Authentication\RememberLogin; | ||
|
|
||
| use OCP\AppFramework\ORM\Attribute\Column; | ||
| use OCP\AppFramework\ORM\Attribute\Entity; | ||
| use OCP\AppFramework\ORM\Attribute\Id; | ||
| use OCP\DB\Schema\ColumnType; | ||
| use OCP\Snowflake\ISnowflakeGenerator; | ||
|
|
||
| #[Entity(name: 'remember_login_tokens')] | ||
| final class RememberLoginToken { | ||
| #[Id(generatorClass: ISnowflakeGenerator::class)] | ||
| #[Column(name: 'id', type: ColumnType::Bigint)] | ||
| public ?string $id = null; | ||
|
|
||
| #[Column(name: 'uid', type: ColumnType::String, length: 64)] | ||
| public string $uid; | ||
|
|
||
| #[Column(name: 'token', type: ColumnType::String, length: 200)] | ||
| public string $token; | ||
| } |
92 changes: 92 additions & 0 deletions
92
lib/private/Authentication/RememberLogin/RememberLoginTokenMapper.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OC\Authentication\RememberLogin; | ||
|
|
||
| use OC\AppFramework\ORM\EntityManager; | ||
| use OCP\AppFramework\Db\DoesNotExistException; | ||
| use OCP\AppFramework\ORM\Repository; | ||
| use OCP\IConfig; | ||
| use OCP\IDBConnection; | ||
| use OCP\Snowflake\ISnowflakeGenerator; | ||
| use Override; | ||
|
|
||
| /** | ||
| * @template-extends Repository<RememberLoginToken> | ||
| */ | ||
| class RememberLoginTokenMapper extends Repository { | ||
| public const string entityClass = RememberLoginToken::class; | ||
|
|
||
| public function __construct( | ||
| IDBConnection $connection, | ||
| EntityManager $entityManager, | ||
| private readonly ISnowflakeGenerator $snowflakeGenerator, | ||
| private readonly IConfig $config, | ||
| ) { | ||
| /** @psalm-suppress InternalMethod */ | ||
| parent::__construct($connection, $entityManager); | ||
| } | ||
|
|
||
| #[Override] | ||
| public function insert(object $entity): object { | ||
| /** @var RememberLoginToken $entity */ | ||
| $entity->token = $this->hashToken($entity->token); | ||
|
|
||
| return parent::insert($entity); | ||
| } | ||
|
|
||
| /** | ||
| * @throws DoesNotExistException | ||
| */ | ||
| public function findByToken(string $token): RememberLoginToken { | ||
| return $this->findOneBy(['token' => $this->hashToken($token)]); | ||
| } | ||
|
|
||
| public function deleteByToken(string $token): int { | ||
| return $this->deleteBy(['token' => $this->hashToken($token)]); | ||
| } | ||
|
|
||
| /** | ||
| * Removes every remembered login token for given user | ||
| */ | ||
| public function deleteByUid(string $uid): int { | ||
| return $this->deleteBy(['uid' => $uid]); | ||
| } | ||
|
|
||
| /** | ||
| * Updates old token with the new one and generates a new snowflake ID, | ||
| * refreshing the creation timestamp encoded in it | ||
| * | ||
| * @return int Number of updated rows | ||
| */ | ||
| public function rotateToken(string $oldToken, string $newToken): int { | ||
| $qb = $this->connection->getQueryBuilder(); | ||
| $qb->update($this->getTableName()) | ||
| ->set('id', $qb->createNamedParameter($this->snowflakeGenerator->nextId())) | ||
| ->set('token', $qb->createNamedParameter($this->hashToken($newToken))) | ||
| ->where($qb->expr()->eq('token', $qb->createNamedParameter($this->hashToken($oldToken)))); | ||
|
|
||
| return $qb->executeStatement(); | ||
| } | ||
|
|
||
| /** | ||
| * Removes every remembered login token older than the given timestamp | ||
| */ | ||
| public function deleteOlderThan(int $timestamp): int { | ||
| $qb = $this->connection->getQueryBuilder(); | ||
| $qb->delete($this->getTableName()) | ||
| ->where($qb->expr()->lt('id', $qb->createNamedParameter($this->snowflakeGenerator->minForTimeId($timestamp)))); | ||
|
|
||
| return $qb->executeStatement(); | ||
| } | ||
|
|
||
| private function hashToken(string $token): string { | ||
| return hash('sha512', $token . $this->config->getSystemValueString('secret')); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is only needed if you're editing an existing table
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this supposed to be used as metadata? saw the same pattern being used in other migrations where new tables are created, like:
https://github.com/nextcloud/server/blob/master/core/Migrations/Version34000Date20260521110333.php
https://github.com/nextcloud/server/blob/master/core/Migrations/Version30000Date20240429122720.php
https://docs.nextcloud.com/server/stable/developer_manual/basics/storage/migrations.html#migrations-and-metadata
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example in the docs show it mentioning the modified column on another table than the one it's creating, and nothing about the columns/indexes of the new table.