Skip to content

Commit

Permalink
Merge pull request #41927 from nextcloud/perf/login-with-email-token
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusknorr authored Dec 5, 2023
2 parents 991f529 + 0ccf84b commit 6c52242
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
11 changes: 11 additions & 0 deletions core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use OC\Authentication\Notifications\Notifier as AuthenticationNotifier;
use OC\Core\Listener\BeforeTemplateRenderedListener;
use OC\Core\Notification\CoreNotifier;
use OC\SystemConfig;
use OC\TagManager;
use OCP\AppFramework\App;
use OCP\AppFramework\Http\Events\BeforeLoginTemplateRenderedEvent;
Expand Down Expand Up @@ -81,6 +82,7 @@ public function __construct() {
$notificationManager->registerNotifierService(AuthenticationNotifier::class);

$eventDispatcher->addListener(AddMissingIndicesEvent::class, function (AddMissingIndicesEvent $event) {
$dbType = $this->getContainer()->get(SystemConfig::class)->getSystemValue('dbtype', 'sqlite');
$event->addMissingIndex(
'share',
'share_with_index',
Expand Down Expand Up @@ -237,6 +239,15 @@ public function __construct() {
['appid', 'configkey']
);

if ($dbType !== 'oci') {
$event->addMissingIndex(
'preferences',
'preferences_configvalue',
['configvalue'],
['lengths' => [80]]
);
}

$event->addMissingIndex(
'mounts',
'mounts_class_index',
Expand Down
4 changes: 4 additions & 0 deletions core/Migrations/Version13000Date20170718121200.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*/
namespace OC\Core\Migrations;

use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use OCP\DB\ISchemaWrapper;
use OCP\DB\Types;
Expand Down Expand Up @@ -332,6 +333,9 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
]);
$table->setPrimaryKey(['userid', 'appid', 'configkey']);
$table->addIndex(['appid', 'configkey'], 'preferences_app_key');
if (!$this->connection->getDatabasePlatform() instanceof OraclePlatform) {
$table->addIndex(['configvalue'], 'preferences_configvalue', [], ['lengths' => [80]]);
}
}

if (!$schema->hasTable('properties')) {
Expand Down
13 changes: 11 additions & 2 deletions lib/private/AllConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*/
namespace OC;

use Doctrine\DBAL\Platforms\OraclePlatform;
use OCP\Cache\CappedMemoryCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
Expand Down Expand Up @@ -490,12 +491,15 @@ public function getUsersForUserValue($appName, $key, $value) {
$this->fixDIInit();

$qb = $this->connection->getQueryBuilder();
$configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform)
? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)
: 'configvalue';
$result = $qb->select('userid')
->from('preferences')
->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq(
$qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR),
$configValueColumn,
$qb->createNamedParameter($value, IQueryBuilder::PARAM_STR))
)->orderBy('userid')
->executeQuery();
Expand Down Expand Up @@ -524,13 +528,18 @@ public function getUsersForUserValueCaseInsensitive($appName, $key, $value) {
// Email address is always stored lowercase in the database
return $this->getUsersForUserValue($appName, $key, strtolower($value));
}

$qb = $this->connection->getQueryBuilder();
$configValueColumn = ($this->connection->getDatabasePlatform() instanceof OraclePlatform)
? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)
: 'configvalue';

$result = $qb->select('userid')
->from('preferences')
->where($qb->expr()->eq('appid', $qb->createNamedParameter($appName, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key, IQueryBuilder::PARAM_STR)))
->andWhere($qb->expr()->eq(
$qb->func()->lower($qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR)),
$qb->func()->lower($configValueColumn),
$qb->createNamedParameter(strtolower($value), IQueryBuilder::PARAM_STR))
)->orderBy('userid')
->executeQuery();
Expand Down
13 changes: 11 additions & 2 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,17 @@ public function logClientIn($user,
$this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password);
return false;
}
$users = $this->manager->getByEmail($user);
if (!(\count($users) === 1 && $this->login($users[0]->getUID(), $password))) {

if ($isTokenPassword) {
$dbToken = $this->tokenProvider->getToken($password);
$userFromToken = $this->manager->get($dbToken->getUID());
$isValidEmailLogin = $userFromToken->getEMailAddress() === $user;
} else {
$users = $this->manager->getByEmail($user);
$isValidEmailLogin = (\count($users) === 1 && $this->login($users[0]->getUID(), $password));
}

if (!$isValidEmailLogin) {
$this->handleLoginFailed($throttler, $currentDelay, $remoteAddress, $user, $password);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/User/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ public function testLogClientInThrottlerEmail() {

$userSession->expects($this->once())
->method('isTokenPassword')
->willReturn(true);
->willReturn(false);
$userSession->expects($this->once())
->method('login')
->with('john@foo.bar', 'I-AM-AN-PASSWORD')
Expand Down

0 comments on commit 6c52242

Please sign in to comment.