1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
<?php
/**
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
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 {
/** @var IL10N */
private $l;
/** @var IUserManager */
private $userManager;
/** @var IUserSession */
private $userSession;
/** @var KeyManager */
private $keyManager;
/** @var Crypt */
private $crypt;
/** @var Session */
private $session;
/** @var ISession */
private $ocSession;
/** @var Util */
private $util;
/**
* @param string $AppName
* @param IRequest $request
* @param IL10N $l10n
* @param IUserManager $userManager
* @param IUserSession $userSession
* @param KeyManager $keyManager
* @param Crypt $crypt
* @param Session $session
* @param ISession $ocSession
* @param Util $util
*/
public function __construct($AppName,
IRequest $request,
IL10N $l10n,
IUserManager $userManager,
IUserSession $userSession,
KeyManager $keyManager,
Crypt $crypt,
Session $session,
ISession $ocSession,
Util $util
) {
parent::__construct($AppName, $request);
$this->l = $l10n;
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->keyManager = $keyManager;
$this->crypt = $crypt;
$this->session = $session;
$this->ocSession = $ocSession;
$this->util = $util;
}
/**
* @param string $oldPassword
* @param string $newPassword
* @return DataResponse
*/
#[NoAdminRequired]
#[UseSession]
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);
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);
}
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
);
}
}
/**
* @param bool $encryptHomeStorage
* @return DataResponse
*/
#[UseSession]
public function setEncryptHomeStorage($encryptHomeStorage) {
$this->util->setEncryptHomeStorage($encryptHomeStorage);
return new DataResponse();
}
}
|