123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace OCA\Encryption\Controller;
- use OCA\Encryption\Crypto\Crypt;
- use OCA\Encryption\KeyManager;
- use OCA\Encryption\Session;
- use OCA\Encryption\Util;
- use OCP\AppFramework\Controller;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\Attribute\NoAdminRequired;
- use OCP\AppFramework\Http\Attribute\UseSession;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\IL10N;
- use OCP\IRequest;
- use OCP\ISession;
- use OCP\IUserManager;
- use OCP\IUserSession;
- class SettingsController extends Controller {
-
- public function __construct(
- $AppName,
- IRequest $request,
- private IL10N $l,
- private IUserManager $userManager,
- private IUserSession $userSession,
- private KeyManager $keyManager,
- private Crypt $crypt,
- private Session $session,
- private ISession $ocSession,
- private Util $util,
- ) {
- parent::__construct($AppName, $request);
- }
-
-
-
- public function updatePrivateKeyPassword($oldPassword, $newPassword) {
- $result = false;
- $uid = $this->userSession->getUser()->getUID();
- $errorMessage = $this->l->t('Could not update the private key password.');
-
- $passwordCorrect = $this->userManager->checkPassword($uid, $newPassword);
- if ($passwordCorrect === false) {
-
-
-
- $loginName = $this->ocSession->get('loginname');
- $passwordCorrect = $this->userManager->checkPassword($loginName, $newPassword);
- }
- if ($passwordCorrect !== false) {
- $encryptedKey = $this->keyManager->getPrivateKey($uid);
- $decryptedKey = $this->crypt->decryptPrivateKey($encryptedKey, $oldPassword, $uid);
- if ($decryptedKey) {
- $encryptedKey = $this->crypt->encryptPrivateKey($decryptedKey, $newPassword, $uid);
- $header = $this->crypt->generateHeader();
- if ($encryptedKey) {
- $this->keyManager->setPrivateKey($uid, $header . $encryptedKey);
- $this->session->setPrivateKey($decryptedKey);
- $result = true;
- }
- } else {
- $errorMessage = $this->l->t('The old password was not correct, please try again.');
- }
- } else {
- $errorMessage = $this->l->t('The current log-in password was not correct, please try again.');
- }
- if ($result === true) {
- $this->session->setStatus(Session::INIT_SUCCESSFUL);
- return new DataResponse(
- ['message' => $this->l->t('Private key password successfully updated.')]
- );
- } else {
- return new DataResponse(
- ['message' => $errorMessage],
- Http::STATUS_BAD_REQUEST
- );
- }
- }
-
-
- public function setEncryptHomeStorage($encryptHomeStorage) {
- $this->util->setEncryptHomeStorage($encryptHomeStorage);
- return new DataResponse();
- }
- }
|