Skip to content
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

Apptoken v3: imrpove token handling on external password change #11390

Merged
merged 4 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public function tryLogin($user, $password, $redirect_url, $remember_login = true
// requires https://github.com/owncloud/core/pull/24616
$this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]);
$this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, IToken::REMEMBER);
$this->userSession->updateTokens($loginResult->getUID(), $password);

// User has successfully logged in, now remove the password reset link, when it is available
$this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword');
Expand Down
53 changes: 53 additions & 0 deletions core/Migrations/Version15000Date20180926101451.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Roeland Jago Douma <[email protected]>
*
* @author Roeland Jago Douma <[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\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;

class Version15000Date20180926101451 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) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();

$table = $schema->getTable('authtoken');
$table->addColumn('password_invalid','boolean', [
'notnull' => true,
'default' => false,
]);

return $schema;
}

}
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@
'OC\\Core\\Migrations\\Version14000Date20180626223656' => $baseDir . '/core/Migrations/Version14000Date20180626223656.php',
'OC\\Core\\Migrations\\Version14000Date20180710092004' => $baseDir . '/core/Migrations/Version14000Date20180710092004.php',
'OC\\Core\\Migrations\\Version14000Date20180712153140' => $baseDir . '/core/Migrations/Version14000Date20180712153140.php',
'OC\\Core\\Migrations\\Version15000Date20180926101451' => $baseDir . '/core/Migrations/Version15000Date20180926101451.php',
'OC\\DB\\Adapter' => $baseDir . '/lib/private/DB/Adapter.php',
'OC\\DB\\AdapterMySQL' => $baseDir . '/lib/private/DB/AdapterMySQL.php',
'OC\\DB\\AdapterOCI8' => $baseDir . '/lib/private/DB/AdapterOCI8.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Core\\Migrations\\Version14000Date20180626223656' => __DIR__ . '/../../..' . '/core/Migrations/Version14000Date20180626223656.php',
'OC\\Core\\Migrations\\Version14000Date20180710092004' => __DIR__ . '/../../..' . '/core/Migrations/Version14000Date20180710092004.php',
'OC\\Core\\Migrations\\Version14000Date20180712153140' => __DIR__ . '/../../..' . '/core/Migrations/Version14000Date20180712153140.php',
'OC\\Core\\Migrations\\Version15000Date20180926101451' => __DIR__ . '/../../..' . '/core/Migrations/Version15000Date20180926101451.php',
'OC\\DB\\Adapter' => __DIR__ . '/../../..' . '/lib/private/DB/Adapter.php',
'OC\\DB\\AdapterMySQL' => __DIR__ . '/../../..' . '/lib/private/DB/AdapterMySQL.php',
'OC\\DB\\AdapterOCI8' => __DIR__ . '/../../..' . '/lib/private/DB/AdapterOCI8.php',
Expand Down
12 changes: 12 additions & 0 deletions lib/private/Authentication/Token/DefaultTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,16 @@ private function decryptPassword(string $password, string $token): string {
}
}

public function markPasswordInvalid(IToken $token, string $tokenId) {
if (!($token instanceof DefaultToken)) {
throw new InvalidTokenException();
}

//No need to mark as invalid. We just invalide default tokens
$this->invalidateToken($tokenId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't check the concrete class here …

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should be consitent yes. However I do check it in the PublicKeyTokenProvider since we use a function that is only available on this function always works. Anyway let me be consistent ;)

}

public function updatePasswords(string $uid, string $password) {
// Nothing to do here
}
}
16 changes: 16 additions & 0 deletions lib/private/Authentication/Token/IProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,20 @@ public function setPassword(IToken $token, string $tokenId, string $password);
* @return IToken
*/
public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken;

/**
* Marks a token as having an invalid password.
*
* @param IToken $token
* @param string $tokenId
*/
public function markPasswordInvalid(IToken $token, string $tokenId);

/**
* Update all the passwords of $uid if required
*
* @param string $uid
* @param string $password
*/
public function updatePasswords(string $uid, string $password);
}
12 changes: 12 additions & 0 deletions lib/private/Authentication/Token/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,16 @@ private function getProvider(IToken $token): IProvider {
}
throw new InvalidTokenException();
}


public function markPasswordInvalid(IToken $token, string $tokenId) {
$this->getProvider($token)->markPasswordInvalid($token, $tokenId);
}

public function updatePasswords(string $uid, string $password) {
$this->defaultTokenProvider->updatePasswords($uid, $password);
$this->publicKeyTokenProvider->updatePasswords($uid, $password);
}


}
9 changes: 9 additions & 0 deletions lib/private/Authentication/Token/PublicKeyToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @method string getPublicKey()
* @method void setPublicKey(string $key)
* @method void setVersion(int $version)
* @method bool getPasswordInvalid()
*/
class PublicKeyToken extends Entity implements IToken {

Expand Down Expand Up @@ -90,6 +91,9 @@ class PublicKeyToken extends Entity implements IToken {
/** @var int */
protected $version;

/** @var bool */
protected $passwordInvalid;

public function __construct() {
$this->addType('uid', 'string');
$this->addType('loginName', 'string');
Expand All @@ -105,6 +109,7 @@ public function __construct() {
$this->addType('publicKey', 'string');
$this->addType('privateKey', 'string');
$this->addType('version', 'int');
$this->addType('passwordInvalid', 'bool');
}

public function getId(): int {
Expand Down Expand Up @@ -214,4 +219,8 @@ public function setExpires($expires) {
public function getExpires() {
return parent::getExpires();
}

public function setPasswordInvalid(bool $invalid) {
parent::setPasswordInvalid($invalid);
}
}
15 changes: 15 additions & 0 deletions lib/private/Authentication/Token/PublicKeyTokenMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,19 @@ public function deleteTempToken(PublicKeyToken $except) {

$qb->execute();
}

public function hasExpiredTokens(string $uid): bool {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('authtoken')
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
->andWhere($qb->expr()->eq('password_invalid', $qb->createNamedParameter(true), IQueryBuilder::PARAM_BOOL))
->setMaxResults(1);

$cursor = $qb->execute();
$data = $cursor->fetchAll();
$cursor->closeCursor();

return count($data) === 1;
}
}
26 changes: 26 additions & 0 deletions lib/private/Authentication/Token/PublicKeyTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,30 @@ private function newToken(string $token,

return $dbToken;
}

public function markPasswordInvalid(IToken $token, string $tokenId) {
if (!($token instanceof PublicKeyToken)) {
throw new InvalidTokenException();
}

$token->setPasswordInvalid(true);
$this->mapper->update($token);
}

public function updatePasswords(string $uid, string $password) {
if (!$this->mapper->hasExpiredTokens($uid)) {
// Nothing to do here
return;
}

// Update the password for all tokens
$tokens = $this->mapper->getTokenByUser($uid);
foreach ($tokens as $t) {
$publicKey = $t->getPublicKey();
$t->setPassword($this->encryptPassword($password, $publicKey));
$this->updateToken($t);
}
}


}
17 changes: 14 additions & 3 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,12 +694,19 @@ private function checkTokenCredentials(IToken $dbToken, $token) {
return true;
}

if ($this->manager->checkPassword($dbToken->getLoginName(), $pwd) === false
|| (!is_null($this->activeUser) && !$this->activeUser->isEnabled())) {
// Invalidate token if the user is no longer active
if (!is_null($this->activeUser) && !$this->activeUser->isEnabled()) {
$this->tokenProvider->invalidateToken($token);
// Password has changed or user was disabled -> log user out
return false;
}

// If the token password is no longer valid mark it as such
if ($this->manager->checkPassword($dbToken->getLoginName(), $pwd) === false) {
$this->tokenProvider->markPasswordInvalid($dbToken, $token);
// User is logged out
return false;
}

$dbToken->setLastCheck($now);
return true;
}
Expand Down Expand Up @@ -943,5 +950,9 @@ public function updateSessionTokenPassword($password) {
}
}

public function updateTokens(string $uid, string $password) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point we should discuss a refactoring of this user session class. This will soon be unmaintainable due to its complexity 🙈

$this->tokenProvider->updatePasswords($uid, $password);
}


}
19 changes: 19 additions & 0 deletions tests/lib/Authentication/Token/DefaultTokenProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OC\Authentication\Token\DefaultTokenProvider;
use OC\Authentication\Token\ExpiredTokenException;
use OC\Authentication\Token\IToken;
use OC\Authentication\Token\PublicKeyToken;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
Expand Down Expand Up @@ -532,4 +533,22 @@ public function testRotateNoPassword() {

$this->tokenProvider->rotate($token, 'oldtoken', 'newtoken');
}

public function testMarkPasswordInvalidInvalidToken() {
$token = $this->createMock(PublicKeyToken::class);

$this->expectException(InvalidTokenException::class);

$this->tokenProvider->markPasswordInvalid($token, 'tokenId');
}

public function testMarkPasswordInvalid() {
$token = $this->createMock(DefaultToken::class);

$this->mapper->expects($this->once())
->method('invalidate')
->with('0c7db0098fe8ddba6032b22719ec18867c69a1820fa36d71c28bf96d52843bdc44a112bd24093b049be5bb54769bcb72d67190a4a9690e51aac263cba38186fb');

$this->tokenProvider->markPasswordInvalid($token, 'tokenId');
}
}
41 changes: 41 additions & 0 deletions tests/lib/Authentication/Token/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,45 @@ public function testRotateConvertNoPassword() {

$this->assertSame($newToken, $this->manager->rotate($oldToken, 'oldId', 'newId'));
}

public function testMarkPasswordInvalidDefault() {
$token = $this->createMock(DefaultToken::class);

$this->defaultTokenProvider->expects($this->once())
->method('markPasswordInvalid')
->with($token, 'tokenId');
$this->publicKeyTokenProvider->expects($this->never())
->method($this->anything());

$this->manager->markPasswordInvalid($token, 'tokenId');
}

public function testMarkPasswordInvalidPublicKey() {
$token = $this->createMock(PublicKeyToken::class);

$this->publicKeyTokenProvider->expects($this->once())
->method('markPasswordInvalid')
->with($token, 'tokenId');
$this->defaultTokenProvider->expects($this->never())
->method($this->anything());

$this->manager->markPasswordInvalid($token, 'tokenId');
}

public function testMarkPasswordInvalidInvalidToken() {
$this->expectException(InvalidTokenException::class);

$this->manager->markPasswordInvalid($this->createMock(IToken::class), 'tokenId');
}

public function testUpdatePasswords() {
$this->defaultTokenProvider->expects($this->once())
->method('updatePasswords')
->with('uid', 'pass');
$this->publicKeyTokenProvider->expects($this->once())
->method('updatePasswords')
->with('uid', 'pass');

$this->manager->updatePasswords('uid', 'pass');
}
}
Loading