2015-04-16 16:42:13 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-04-16 16:42:13 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-04-16 16:42:13 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption\Controller;
|
|
|
|
|
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
|
|
|
use OCA\Encryption\KeyManager;
|
|
|
|
use OCA\Encryption\Session;
|
2015-10-13 18:54:06 +03:00
|
|
|
use OCA\Encryption\Util;
|
2015-04-16 16:42:13 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
2015-04-20 18:44:34 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2015-04-16 16:42:13 +03:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
2015-08-25 16:05:55 +03:00
|
|
|
use OCP\ISession;
|
2015-04-16 16:42:13 +03:00
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
|
|
|
class SettingsController extends Controller {
|
|
|
|
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l;
|
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
|
|
|
|
|
|
|
/** @var KeyManager */
|
|
|
|
private $keyManager;
|
|
|
|
|
|
|
|
/** @var Crypt */
|
|
|
|
private $crypt;
|
|
|
|
|
|
|
|
/** @var Session */
|
|
|
|
private $session;
|
|
|
|
|
2015-08-25 16:05:55 +03:00
|
|
|
/** @var ISession */
|
|
|
|
private $ocSession;
|
|
|
|
|
2015-10-13 18:54:06 +03:00
|
|
|
/** @var Util */
|
|
|
|
private $util;
|
|
|
|
|
2015-04-16 16:42:13 +03:00
|
|
|
/**
|
|
|
|
* @param string $AppName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param IL10N $l10n
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
* @param IUserSession $userSession
|
|
|
|
* @param KeyManager $keyManager
|
|
|
|
* @param Crypt $crypt
|
|
|
|
* @param Session $session
|
2015-08-25 16:05:55 +03:00
|
|
|
* @param ISession $ocSession
|
2015-10-13 18:54:06 +03:00
|
|
|
* @param Util $util
|
2015-04-16 16:42:13 +03:00
|
|
|
*/
|
|
|
|
public function __construct($AppName,
|
|
|
|
IRequest $request,
|
|
|
|
IL10N $l10n,
|
|
|
|
IUserManager $userManager,
|
|
|
|
IUserSession $userSession,
|
|
|
|
KeyManager $keyManager,
|
|
|
|
Crypt $crypt,
|
2015-08-25 16:05:55 +03:00
|
|
|
Session $session,
|
2015-10-13 18:54:06 +03:00
|
|
|
ISession $ocSession,
|
|
|
|
Util $util
|
|
|
|
) {
|
2015-04-16 16:42:13 +03:00
|
|
|
parent::__construct($AppName, $request);
|
|
|
|
$this->l = $l10n;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
$this->keyManager = $keyManager;
|
|
|
|
$this->crypt = $crypt;
|
|
|
|
$this->session = $session;
|
2015-08-25 16:05:55 +03:00
|
|
|
$this->ocSession = $ocSession;
|
2015-10-13 18:54:06 +03:00
|
|
|
$this->util = $util;
|
2015-04-16 16:42:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @UseSession
|
|
|
|
*
|
|
|
|
* @param string $oldPassword
|
|
|
|
* @param string $newPassword
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function updatePrivateKeyPassword($oldPassword, $newPassword) {
|
|
|
|
$result = false;
|
|
|
|
$uid = $this->userSession->getUser()->getUID();
|
|
|
|
$errorMessage = $this->l->t('Could not update the private key password.');
|
|
|
|
|
|
|
|
//check if password is correct
|
|
|
|
$passwordCorrect = $this->userManager->checkPassword($uid, $newPassword);
|
2015-08-25 16:05:55 +03:00
|
|
|
if ($passwordCorrect === false) {
|
|
|
|
// if check with uid fails we need to check the password with the login name
|
|
|
|
// e.g. in the ldap case. For local user we need to check the password with
|
|
|
|
// the uid because in this case the login name is case insensitive
|
|
|
|
$loginName = $this->ocSession->get('loginname');
|
|
|
|
$passwordCorrect = $this->userManager->checkPassword($loginName, $newPassword);
|
|
|
|
}
|
2015-04-16 16:42:13 +03:00
|
|
|
|
|
|
|
if ($passwordCorrect !== false) {
|
|
|
|
$encryptedKey = $this->keyManager->getPrivateKey($uid);
|
2015-08-07 16:51:43 +03:00
|
|
|
$decryptedKey = $this->crypt->decryptPrivateKey($encryptedKey, $oldPassword, $uid);
|
2015-04-16 16:42:13 +03:00
|
|
|
|
|
|
|
if ($decryptedKey) {
|
2015-08-07 16:51:43 +03:00
|
|
|
$encryptedKey = $this->crypt->encryptPrivateKey($decryptedKey, $newPassword, $uid);
|
2015-04-16 16:42:13 +03:00
|
|
|
$header = $this->crypt->generateHeader();
|
|
|
|
if ($encryptedKey) {
|
|
|
|
$this->keyManager->setPrivateKey($uid, $header . $encryptedKey);
|
|
|
|
$this->session->setPrivateKey($decryptedKey);
|
|
|
|
$result = true;
|
|
|
|
}
|
|
|
|
} else {
|
2015-04-20 18:44:34 +03:00
|
|
|
$errorMessage = $this->l->t('The old password was not correct, please try again.');
|
2015-04-16 16:42:13 +03:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-20 18:44:34 +03:00
|
|
|
$errorMessage = $this->l->t('The current log-in password was not correct, please try again.');
|
2015-04-16 16:42:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($result === true) {
|
|
|
|
$this->session->setStatus(Session::INIT_SUCCESSFUL);
|
|
|
|
return new DataResponse(
|
2015-04-20 19:15:06 +03:00
|
|
|
['message' => (string) $this->l->t('Private key password successfully updated.')]
|
2015-04-16 16:42:13 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return new DataResponse(
|
2015-04-20 19:15:06 +03:00
|
|
|
['message' => (string) $errorMessage],
|
2015-04-20 18:44:34 +03:00
|
|
|
Http::STATUS_BAD_REQUEST
|
2015-04-16 16:42:13 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-10-13 18:54:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @UseSession
|
|
|
|
*
|
|
|
|
* @param bool $encryptHomeStorage
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function setEncryptHomeStorage($encryptHomeStorage) {
|
|
|
|
$this->util->setEncryptHomeStorage($encryptHomeStorage);
|
|
|
|
return new DataResponse();
|
|
|
|
}
|
2015-04-16 16:42:13 +03:00
|
|
|
}
|