-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
PublickKeyTokenProvider: Fix password update routine with password hash #33898
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,57 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * @copyright Copyright (c) 2022 Marcel Klehr <[email protected]> | ||
| * | ||
| * @author Marcel Klehr <[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 OC\Core\Migrations; | ||
|
|
||
| use Closure; | ||
| use OCP\DB\ISchemaWrapper; | ||
| use OCP\DB\Types; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\SimpleMigrationStep; | ||
|
|
||
| class Version25000Date20220905140840 extends SimpleMigrationStep { | ||
|
|
||
| /** | ||
| * @param IOutput $output | ||
| * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` | ||
| * @param array $options | ||
| * @return null|ISchemaWrapper | ||
| */ | ||
| public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { | ||
| /** @var ISchemaWrapper $schema */ | ||
| $schema = $schemaClosure(); | ||
|
|
||
| $authTokenTable = $schema->getTable('authtoken'); | ||
| if (!$authTokenTable->hasColumn('password_hash')) { | ||
| $authTokenTable->addColumn('password_hash', Types::STRING, [ | ||
| 'notnull' => false, | ||
| 'length' => 255, | ||
| ]); | ||
| return $schema; | ||
| } | ||
| return null; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -41,6 +41,7 @@ | |||||
| use OCP\IConfig; | ||||||
| use OCP\IDBConnection; | ||||||
| use OCP\Security\ICrypto; | ||||||
| use OCP\Security\IHasher; | ||||||
| use Psr\Log\LoggerInterface; | ||||||
|
|
||||||
| class PublicKeyTokenProvider implements IProvider { | ||||||
|
|
@@ -66,12 +67,15 @@ class PublicKeyTokenProvider implements IProvider { | |||||
| /** @var CappedMemoryCache */ | ||||||
| private $cache; | ||||||
|
|
||||||
| private IHasher $hasher; | ||||||
|
|
||||||
| public function __construct(PublicKeyTokenMapper $mapper, | ||||||
| ICrypto $crypto, | ||||||
| IConfig $config, | ||||||
| IDBConnection $db, | ||||||
| LoggerInterface $logger, | ||||||
| ITimeFactory $time) { | ||||||
| ITimeFactory $time, | ||||||
| IHasher $hasher) { | ||||||
| $this->mapper = $mapper; | ||||||
| $this->crypto = $crypto; | ||||||
| $this->config = $config; | ||||||
|
|
@@ -80,6 +84,7 @@ public function __construct(PublicKeyTokenMapper $mapper, | |||||
| $this->time = $time; | ||||||
|
|
||||||
| $this->cache = new CappedMemoryCache(); | ||||||
| $this->hasher = $hasher; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
@@ -286,10 +291,15 @@ public function setPassword(IToken $token, string $tokenId, string $password) { | |||||
| foreach ($tokens as $t) { | ||||||
| $publicKey = $t->getPublicKey(); | ||||||
| $t->setPassword($this->encryptPassword($password, $publicKey)); | ||||||
| $t->setPasswordHash($this->hashPassword($password)); | ||||||
| $this->updateToken($t); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| private function hashPassword(string $password): string { | ||||||
| return $this->hasher->hash(sha1($password) . $password); | ||||||
juliusknorr marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
|
|
||||||
| public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken { | ||||||
| $this->cache->clear(); | ||||||
|
|
||||||
|
|
@@ -401,6 +411,7 @@ private function newToken(string $token, | |||||
| throw new \RuntimeException('Trying to save a password with more than 469 characters is not supported. If you want to use big passwords, disable the auth.storeCryptedPassword option in config.php'); | ||||||
| } | ||||||
| $dbToken->setPassword($this->encryptPassword($password, $publicKey)); | ||||||
| $dbToken->setPasswordHash($this->hashPassword($password)); | ||||||
| } | ||||||
|
|
||||||
| $dbToken->setName($name); | ||||||
|
|
@@ -435,11 +446,12 @@ public function updatePasswords(string $uid, string $password) { | |||||
|
|
||||||
| // Update the password for all tokens | ||||||
| $tokens = $this->mapper->getTokenByUser($uid); | ||||||
| $passwordHash = $this->hashPassword($password); | ||||||
| foreach ($tokens as $t) { | ||||||
| $publicKey = $t->getPublicKey(); | ||||||
| $encryptedPassword = $this->encryptPassword($password, $publicKey); | ||||||
| if ($t->getPassword() !== $encryptedPassword) { | ||||||
| $t->setPassword($encryptedPassword); | ||||||
| if ($t->getPasswordHash() === null || $this->hasher->verify(sha1($password) . $password, $t->getPasswordHash())) { | ||||||
|
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. Did you maybe mean:
Suggested change
So the password is updated everytime it does NOT match the old stored password?
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. But the actual problem is |
||||||
| $t->setPassword($this->encryptPassword($password, $publicKey)); | ||||||
| $t->setPasswordHash($passwordHash); | ||||||
| $t->setPasswordInvalid(false); | ||||||
| $this->updateToken($t); | ||||||
| } | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.