@@ -62,344 +62,344 @@ |
||
| 62 | 62 | * @package OC\Core\Controller |
| 63 | 63 | */ |
| 64 | 64 | class LostController extends Controller { |
| 65 | - /** @var IURLGenerator */ |
|
| 66 | - protected $urlGenerator; |
|
| 67 | - /** @var IUserManager */ |
|
| 68 | - protected $userManager; |
|
| 69 | - /** @var Defaults */ |
|
| 70 | - protected $defaults; |
|
| 71 | - /** @var IL10N */ |
|
| 72 | - protected $l10n; |
|
| 73 | - /** @var string */ |
|
| 74 | - protected $from; |
|
| 75 | - /** @var IManager */ |
|
| 76 | - protected $encryptionManager; |
|
| 77 | - /** @var IConfig */ |
|
| 78 | - protected $config; |
|
| 79 | - /** @var ISecureRandom */ |
|
| 80 | - protected $secureRandom; |
|
| 81 | - /** @var IMailer */ |
|
| 82 | - protected $mailer; |
|
| 83 | - /** @var ITimeFactory */ |
|
| 84 | - protected $timeFactory; |
|
| 85 | - /** @var ICrypto */ |
|
| 86 | - protected $crypto; |
|
| 87 | - /** @var ILogger */ |
|
| 88 | - private $logger; |
|
| 89 | - /** @var Manager */ |
|
| 90 | - private $twoFactorManager; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param string $appName |
|
| 94 | - * @param IRequest $request |
|
| 95 | - * @param IURLGenerator $urlGenerator |
|
| 96 | - * @param IUserManager $userManager |
|
| 97 | - * @param Defaults $defaults |
|
| 98 | - * @param IL10N $l10n |
|
| 99 | - * @param IConfig $config |
|
| 100 | - * @param ISecureRandom $secureRandom |
|
| 101 | - * @param string $defaultMailAddress |
|
| 102 | - * @param IManager $encryptionManager |
|
| 103 | - * @param IMailer $mailer |
|
| 104 | - * @param ITimeFactory $timeFactory |
|
| 105 | - * @param ICrypto $crypto |
|
| 106 | - */ |
|
| 107 | - public function __construct($appName, |
|
| 108 | - IRequest $request, |
|
| 109 | - IURLGenerator $urlGenerator, |
|
| 110 | - IUserManager $userManager, |
|
| 111 | - Defaults $defaults, |
|
| 112 | - IL10N $l10n, |
|
| 113 | - IConfig $config, |
|
| 114 | - ISecureRandom $secureRandom, |
|
| 115 | - $defaultMailAddress, |
|
| 116 | - IManager $encryptionManager, |
|
| 117 | - IMailer $mailer, |
|
| 118 | - ITimeFactory $timeFactory, |
|
| 119 | - ICrypto $crypto, |
|
| 120 | - ILogger $logger, |
|
| 121 | - Manager $twoFactorManager) { |
|
| 122 | - parent::__construct($appName, $request); |
|
| 123 | - $this->urlGenerator = $urlGenerator; |
|
| 124 | - $this->userManager = $userManager; |
|
| 125 | - $this->defaults = $defaults; |
|
| 126 | - $this->l10n = $l10n; |
|
| 127 | - $this->secureRandom = $secureRandom; |
|
| 128 | - $this->from = $defaultMailAddress; |
|
| 129 | - $this->encryptionManager = $encryptionManager; |
|
| 130 | - $this->config = $config; |
|
| 131 | - $this->mailer = $mailer; |
|
| 132 | - $this->timeFactory = $timeFactory; |
|
| 133 | - $this->crypto = $crypto; |
|
| 134 | - $this->logger = $logger; |
|
| 135 | - $this->twoFactorManager = $twoFactorManager; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Someone wants to reset their password: |
|
| 140 | - * |
|
| 141 | - * @PublicPage |
|
| 142 | - * @NoCSRFRequired |
|
| 143 | - * |
|
| 144 | - * @param string $token |
|
| 145 | - * @param string $userId |
|
| 146 | - * @return TemplateResponse |
|
| 147 | - */ |
|
| 148 | - public function resetform($token, $userId) { |
|
| 149 | - if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 150 | - return new TemplateResponse('core', 'error', [ |
|
| 151 | - 'errors' => [['error' => $this->l10n->t('Password reset is disabled')]] |
|
| 152 | - ], |
|
| 153 | - 'guest' |
|
| 154 | - ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - try { |
|
| 158 | - $this->checkPasswordResetToken($token, $userId); |
|
| 159 | - } catch (\Exception $e) { |
|
| 160 | - return new TemplateResponse( |
|
| 161 | - 'core', 'error', [ |
|
| 162 | - "errors" => array(array("error" => $e->getMessage())) |
|
| 163 | - ], |
|
| 164 | - 'guest' |
|
| 165 | - ); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return new TemplateResponse( |
|
| 169 | - 'core', |
|
| 170 | - 'lostpassword/resetpassword', |
|
| 171 | - array( |
|
| 172 | - 'link' => $this->urlGenerator->linkToRouteAbsolute('core.lost.setPassword', array('userId' => $userId, 'token' => $token)), |
|
| 173 | - ), |
|
| 174 | - 'guest' |
|
| 175 | - ); |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param string $token |
|
| 180 | - * @param string $userId |
|
| 181 | - * @throws \Exception |
|
| 182 | - */ |
|
| 183 | - protected function checkPasswordResetToken($token, $userId) { |
|
| 184 | - $user = $this->userManager->get($userId); |
|
| 185 | - if($user === null || !$user->isEnabled()) { |
|
| 186 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - try { |
|
| 190 | - $encryptedToken = $this->config->getUserValue($userId, 'core', 'lostpassword', null); |
|
| 191 | - $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
| 192 | - $decryptedToken = $this->crypto->decrypt($encryptedToken, $mailAddress.$this->config->getSystemValue('secret')); |
|
| 193 | - } catch (\Exception $e) { |
|
| 194 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $splittedToken = explode(':', $decryptedToken); |
|
| 198 | - if(count($splittedToken) !== 2) { |
|
| 199 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*24*7) || |
|
| 203 | - $user->getLastLogin() > $splittedToken[0]) { |
|
| 204 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired')); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - if (!hash_equals($splittedToken[1], $token)) { |
|
| 208 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 209 | - } |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @param $message |
|
| 214 | - * @param array $additional |
|
| 215 | - * @return array |
|
| 216 | - */ |
|
| 217 | - private function error($message, array $additional=array()) { |
|
| 218 | - return array_merge(array('status' => 'error', 'msg' => $message), $additional); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param array $data |
|
| 223 | - * @return array |
|
| 224 | - */ |
|
| 225 | - private function success($data = []) { |
|
| 226 | - return array_merge($data, ['status'=>'success']); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * @PublicPage |
|
| 231 | - * @BruteForceProtection(action=passwordResetEmail) |
|
| 232 | - * @AnonRateThrottle(limit=10, period=300) |
|
| 233 | - * |
|
| 234 | - * @param string $user |
|
| 235 | - * @return JSONResponse |
|
| 236 | - */ |
|
| 237 | - public function email($user){ |
|
| 238 | - if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 239 | - return new JSONResponse($this->error($this->l10n->t('Password reset is disabled'))); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - \OCP\Util::emitHook( |
|
| 243 | - '\OCA\Files_Sharing\API\Server2Server', |
|
| 244 | - 'preLoginNameUsedAsUserName', |
|
| 245 | - ['uid' => &$user] |
|
| 246 | - ); |
|
| 247 | - |
|
| 248 | - // FIXME: use HTTP error codes |
|
| 249 | - try { |
|
| 250 | - $this->sendEmail($user); |
|
| 251 | - } catch (\Exception $e) { |
|
| 252 | - // Ignore the error since we do not want to leak this info |
|
| 253 | - $this->logger->logException($e, [ |
|
| 254 | - 'level' => ILogger::WARN |
|
| 255 | - ]); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - $response = new JSONResponse($this->success()); |
|
| 259 | - $response->throttle(); |
|
| 260 | - return $response; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * @PublicPage |
|
| 265 | - * @param string $token |
|
| 266 | - * @param string $userId |
|
| 267 | - * @param string $password |
|
| 268 | - * @param boolean $proceed |
|
| 269 | - * @return array |
|
| 270 | - */ |
|
| 271 | - public function setPassword($token, $userId, $password, $proceed) { |
|
| 272 | - if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 273 | - return $this->error($this->l10n->t('Password reset is disabled')); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - if ($this->encryptionManager->isEnabled() && !$proceed) { |
|
| 277 | - $encryptionModules = $this->encryptionManager->getEncryptionModules(); |
|
| 278 | - foreach ($encryptionModules as $module) { |
|
| 279 | - /** @var IEncryptionModule $instance */ |
|
| 280 | - $instance = call_user_func($module['callback']); |
|
| 281 | - // this way we can find out whether per-user keys are used or a system wide encryption key |
|
| 282 | - if ($instance->needDetailedAccessList()) { |
|
| 283 | - return $this->error('', array('encryption' => true)); |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - try { |
|
| 289 | - $this->checkPasswordResetToken($token, $userId); |
|
| 290 | - $user = $this->userManager->get($userId); |
|
| 291 | - |
|
| 292 | - \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'pre_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 293 | - |
|
| 294 | - if (!$user->setPassword($password)) { |
|
| 295 | - throw new \Exception(); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'post_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 299 | - |
|
| 300 | - $this->twoFactorManager->clearTwoFactorPending($userId); |
|
| 301 | - |
|
| 302 | - $this->config->deleteUserValue($userId, 'core', 'lostpassword'); |
|
| 303 | - @\OC::$server->getUserSession()->unsetMagicInCookie(); |
|
| 304 | - } catch (HintException $e){ |
|
| 305 | - return $this->error($e->getHint()); |
|
| 306 | - } catch (\Exception $e){ |
|
| 307 | - return $this->error($e->getMessage()); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return $this->success(['user' => $userId]); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * @param string $input |
|
| 315 | - * @throws \Exception |
|
| 316 | - */ |
|
| 317 | - protected function sendEmail($input) { |
|
| 318 | - $user = $this->findUserByIdOrMail($input); |
|
| 319 | - $email = $user->getEMailAddress(); |
|
| 320 | - |
|
| 321 | - if (empty($email)) { |
|
| 322 | - throw new \Exception( |
|
| 323 | - $this->l10n->t('Could not send reset email because there is no email address for this username. Please contact your administrator.') |
|
| 324 | - ); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - // Generate the token. It is stored encrypted in the database with the |
|
| 328 | - // secret being the users' email address appended with the system secret. |
|
| 329 | - // This makes the token automatically invalidate once the user changes |
|
| 330 | - // their email address. |
|
| 331 | - $token = $this->secureRandom->generate( |
|
| 332 | - 21, |
|
| 333 | - ISecureRandom::CHAR_DIGITS. |
|
| 334 | - ISecureRandom::CHAR_LOWER. |
|
| 335 | - ISecureRandom::CHAR_UPPER |
|
| 336 | - ); |
|
| 337 | - $tokenValue = $this->timeFactory->getTime() .':'. $token; |
|
| 338 | - $encryptedValue = $this->crypto->encrypt($tokenValue, $email . $this->config->getSystemValue('secret')); |
|
| 339 | - $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
| 340 | - |
|
| 341 | - $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user->getUID(), 'token' => $token)); |
|
| 342 | - |
|
| 343 | - $emailTemplate = $this->mailer->createEMailTemplate('core.ResetPassword', [ |
|
| 344 | - 'link' => $link, |
|
| 345 | - ]); |
|
| 346 | - |
|
| 347 | - $emailTemplate->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()])); |
|
| 348 | - $emailTemplate->addHeader(); |
|
| 349 | - $emailTemplate->addHeading($this->l10n->t('Password reset')); |
|
| 350 | - |
|
| 351 | - $emailTemplate->addBodyText( |
|
| 352 | - htmlspecialchars($this->l10n->t('Click the following button to reset your password. If you have not requested the password reset, then ignore this email.')), |
|
| 353 | - $this->l10n->t('Click the following link to reset your password. If you have not requested the password reset, then ignore this email.') |
|
| 354 | - ); |
|
| 355 | - |
|
| 356 | - $emailTemplate->addBodyButton( |
|
| 357 | - htmlspecialchars($this->l10n->t('Reset your password')), |
|
| 358 | - $link, |
|
| 359 | - false |
|
| 360 | - ); |
|
| 361 | - $emailTemplate->addFooter(); |
|
| 362 | - |
|
| 363 | - try { |
|
| 364 | - $message = $this->mailer->createMessage(); |
|
| 365 | - $message->setTo([$email => $user->getUID()]); |
|
| 366 | - $message->setFrom([$this->from => $this->defaults->getName()]); |
|
| 367 | - $message->useTemplate($emailTemplate); |
|
| 368 | - $this->mailer->send($message); |
|
| 369 | - } catch (\Exception $e) { |
|
| 370 | - throw new \Exception($this->l10n->t( |
|
| 371 | - 'Couldn\'t send reset email. Please contact your administrator.' |
|
| 372 | - )); |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * @param string $input |
|
| 378 | - * @return IUser |
|
| 379 | - * @throws \InvalidArgumentException |
|
| 380 | - */ |
|
| 381 | - protected function findUserByIdOrMail($input) { |
|
| 382 | - $userNotFound = new \InvalidArgumentException( |
|
| 383 | - $this->l10n->t('Couldn\'t send reset email. Please make sure your username is correct.') |
|
| 384 | - ); |
|
| 385 | - |
|
| 386 | - $user = $this->userManager->get($input); |
|
| 387 | - if ($user instanceof IUser) { |
|
| 388 | - if (!$user->isEnabled()) { |
|
| 389 | - throw $userNotFound; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - return $user; |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - $users = array_filter($this->userManager->getByEmail($input), function (IUser $user) { |
|
| 396 | - return $user->isEnabled(); |
|
| 397 | - }); |
|
| 398 | - |
|
| 399 | - if (count($users) === 1) { |
|
| 400 | - return reset($users); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - throw $userNotFound; |
|
| 404 | - } |
|
| 65 | + /** @var IURLGenerator */ |
|
| 66 | + protected $urlGenerator; |
|
| 67 | + /** @var IUserManager */ |
|
| 68 | + protected $userManager; |
|
| 69 | + /** @var Defaults */ |
|
| 70 | + protected $defaults; |
|
| 71 | + /** @var IL10N */ |
|
| 72 | + protected $l10n; |
|
| 73 | + /** @var string */ |
|
| 74 | + protected $from; |
|
| 75 | + /** @var IManager */ |
|
| 76 | + protected $encryptionManager; |
|
| 77 | + /** @var IConfig */ |
|
| 78 | + protected $config; |
|
| 79 | + /** @var ISecureRandom */ |
|
| 80 | + protected $secureRandom; |
|
| 81 | + /** @var IMailer */ |
|
| 82 | + protected $mailer; |
|
| 83 | + /** @var ITimeFactory */ |
|
| 84 | + protected $timeFactory; |
|
| 85 | + /** @var ICrypto */ |
|
| 86 | + protected $crypto; |
|
| 87 | + /** @var ILogger */ |
|
| 88 | + private $logger; |
|
| 89 | + /** @var Manager */ |
|
| 90 | + private $twoFactorManager; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param string $appName |
|
| 94 | + * @param IRequest $request |
|
| 95 | + * @param IURLGenerator $urlGenerator |
|
| 96 | + * @param IUserManager $userManager |
|
| 97 | + * @param Defaults $defaults |
|
| 98 | + * @param IL10N $l10n |
|
| 99 | + * @param IConfig $config |
|
| 100 | + * @param ISecureRandom $secureRandom |
|
| 101 | + * @param string $defaultMailAddress |
|
| 102 | + * @param IManager $encryptionManager |
|
| 103 | + * @param IMailer $mailer |
|
| 104 | + * @param ITimeFactory $timeFactory |
|
| 105 | + * @param ICrypto $crypto |
|
| 106 | + */ |
|
| 107 | + public function __construct($appName, |
|
| 108 | + IRequest $request, |
|
| 109 | + IURLGenerator $urlGenerator, |
|
| 110 | + IUserManager $userManager, |
|
| 111 | + Defaults $defaults, |
|
| 112 | + IL10N $l10n, |
|
| 113 | + IConfig $config, |
|
| 114 | + ISecureRandom $secureRandom, |
|
| 115 | + $defaultMailAddress, |
|
| 116 | + IManager $encryptionManager, |
|
| 117 | + IMailer $mailer, |
|
| 118 | + ITimeFactory $timeFactory, |
|
| 119 | + ICrypto $crypto, |
|
| 120 | + ILogger $logger, |
|
| 121 | + Manager $twoFactorManager) { |
|
| 122 | + parent::__construct($appName, $request); |
|
| 123 | + $this->urlGenerator = $urlGenerator; |
|
| 124 | + $this->userManager = $userManager; |
|
| 125 | + $this->defaults = $defaults; |
|
| 126 | + $this->l10n = $l10n; |
|
| 127 | + $this->secureRandom = $secureRandom; |
|
| 128 | + $this->from = $defaultMailAddress; |
|
| 129 | + $this->encryptionManager = $encryptionManager; |
|
| 130 | + $this->config = $config; |
|
| 131 | + $this->mailer = $mailer; |
|
| 132 | + $this->timeFactory = $timeFactory; |
|
| 133 | + $this->crypto = $crypto; |
|
| 134 | + $this->logger = $logger; |
|
| 135 | + $this->twoFactorManager = $twoFactorManager; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Someone wants to reset their password: |
|
| 140 | + * |
|
| 141 | + * @PublicPage |
|
| 142 | + * @NoCSRFRequired |
|
| 143 | + * |
|
| 144 | + * @param string $token |
|
| 145 | + * @param string $userId |
|
| 146 | + * @return TemplateResponse |
|
| 147 | + */ |
|
| 148 | + public function resetform($token, $userId) { |
|
| 149 | + if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 150 | + return new TemplateResponse('core', 'error', [ |
|
| 151 | + 'errors' => [['error' => $this->l10n->t('Password reset is disabled')]] |
|
| 152 | + ], |
|
| 153 | + 'guest' |
|
| 154 | + ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + try { |
|
| 158 | + $this->checkPasswordResetToken($token, $userId); |
|
| 159 | + } catch (\Exception $e) { |
|
| 160 | + return new TemplateResponse( |
|
| 161 | + 'core', 'error', [ |
|
| 162 | + "errors" => array(array("error" => $e->getMessage())) |
|
| 163 | + ], |
|
| 164 | + 'guest' |
|
| 165 | + ); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return new TemplateResponse( |
|
| 169 | + 'core', |
|
| 170 | + 'lostpassword/resetpassword', |
|
| 171 | + array( |
|
| 172 | + 'link' => $this->urlGenerator->linkToRouteAbsolute('core.lost.setPassword', array('userId' => $userId, 'token' => $token)), |
|
| 173 | + ), |
|
| 174 | + 'guest' |
|
| 175 | + ); |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param string $token |
|
| 180 | + * @param string $userId |
|
| 181 | + * @throws \Exception |
|
| 182 | + */ |
|
| 183 | + protected function checkPasswordResetToken($token, $userId) { |
|
| 184 | + $user = $this->userManager->get($userId); |
|
| 185 | + if($user === null || !$user->isEnabled()) { |
|
| 186 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + try { |
|
| 190 | + $encryptedToken = $this->config->getUserValue($userId, 'core', 'lostpassword', null); |
|
| 191 | + $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
| 192 | + $decryptedToken = $this->crypto->decrypt($encryptedToken, $mailAddress.$this->config->getSystemValue('secret')); |
|
| 193 | + } catch (\Exception $e) { |
|
| 194 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $splittedToken = explode(':', $decryptedToken); |
|
| 198 | + if(count($splittedToken) !== 2) { |
|
| 199 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*24*7) || |
|
| 203 | + $user->getLastLogin() > $splittedToken[0]) { |
|
| 204 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired')); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + if (!hash_equals($splittedToken[1], $token)) { |
|
| 208 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @param $message |
|
| 214 | + * @param array $additional |
|
| 215 | + * @return array |
|
| 216 | + */ |
|
| 217 | + private function error($message, array $additional=array()) { |
|
| 218 | + return array_merge(array('status' => 'error', 'msg' => $message), $additional); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param array $data |
|
| 223 | + * @return array |
|
| 224 | + */ |
|
| 225 | + private function success($data = []) { |
|
| 226 | + return array_merge($data, ['status'=>'success']); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * @PublicPage |
|
| 231 | + * @BruteForceProtection(action=passwordResetEmail) |
|
| 232 | + * @AnonRateThrottle(limit=10, period=300) |
|
| 233 | + * |
|
| 234 | + * @param string $user |
|
| 235 | + * @return JSONResponse |
|
| 236 | + */ |
|
| 237 | + public function email($user){ |
|
| 238 | + if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 239 | + return new JSONResponse($this->error($this->l10n->t('Password reset is disabled'))); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + \OCP\Util::emitHook( |
|
| 243 | + '\OCA\Files_Sharing\API\Server2Server', |
|
| 244 | + 'preLoginNameUsedAsUserName', |
|
| 245 | + ['uid' => &$user] |
|
| 246 | + ); |
|
| 247 | + |
|
| 248 | + // FIXME: use HTTP error codes |
|
| 249 | + try { |
|
| 250 | + $this->sendEmail($user); |
|
| 251 | + } catch (\Exception $e) { |
|
| 252 | + // Ignore the error since we do not want to leak this info |
|
| 253 | + $this->logger->logException($e, [ |
|
| 254 | + 'level' => ILogger::WARN |
|
| 255 | + ]); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + $response = new JSONResponse($this->success()); |
|
| 259 | + $response->throttle(); |
|
| 260 | + return $response; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * @PublicPage |
|
| 265 | + * @param string $token |
|
| 266 | + * @param string $userId |
|
| 267 | + * @param string $password |
|
| 268 | + * @param boolean $proceed |
|
| 269 | + * @return array |
|
| 270 | + */ |
|
| 271 | + public function setPassword($token, $userId, $password, $proceed) { |
|
| 272 | + if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 273 | + return $this->error($this->l10n->t('Password reset is disabled')); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + if ($this->encryptionManager->isEnabled() && !$proceed) { |
|
| 277 | + $encryptionModules = $this->encryptionManager->getEncryptionModules(); |
|
| 278 | + foreach ($encryptionModules as $module) { |
|
| 279 | + /** @var IEncryptionModule $instance */ |
|
| 280 | + $instance = call_user_func($module['callback']); |
|
| 281 | + // this way we can find out whether per-user keys are used or a system wide encryption key |
|
| 282 | + if ($instance->needDetailedAccessList()) { |
|
| 283 | + return $this->error('', array('encryption' => true)); |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + try { |
|
| 289 | + $this->checkPasswordResetToken($token, $userId); |
|
| 290 | + $user = $this->userManager->get($userId); |
|
| 291 | + |
|
| 292 | + \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'pre_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 293 | + |
|
| 294 | + if (!$user->setPassword($password)) { |
|
| 295 | + throw new \Exception(); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'post_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 299 | + |
|
| 300 | + $this->twoFactorManager->clearTwoFactorPending($userId); |
|
| 301 | + |
|
| 302 | + $this->config->deleteUserValue($userId, 'core', 'lostpassword'); |
|
| 303 | + @\OC::$server->getUserSession()->unsetMagicInCookie(); |
|
| 304 | + } catch (HintException $e){ |
|
| 305 | + return $this->error($e->getHint()); |
|
| 306 | + } catch (\Exception $e){ |
|
| 307 | + return $this->error($e->getMessage()); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return $this->success(['user' => $userId]); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * @param string $input |
|
| 315 | + * @throws \Exception |
|
| 316 | + */ |
|
| 317 | + protected function sendEmail($input) { |
|
| 318 | + $user = $this->findUserByIdOrMail($input); |
|
| 319 | + $email = $user->getEMailAddress(); |
|
| 320 | + |
|
| 321 | + if (empty($email)) { |
|
| 322 | + throw new \Exception( |
|
| 323 | + $this->l10n->t('Could not send reset email because there is no email address for this username. Please contact your administrator.') |
|
| 324 | + ); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + // Generate the token. It is stored encrypted in the database with the |
|
| 328 | + // secret being the users' email address appended with the system secret. |
|
| 329 | + // This makes the token automatically invalidate once the user changes |
|
| 330 | + // their email address. |
|
| 331 | + $token = $this->secureRandom->generate( |
|
| 332 | + 21, |
|
| 333 | + ISecureRandom::CHAR_DIGITS. |
|
| 334 | + ISecureRandom::CHAR_LOWER. |
|
| 335 | + ISecureRandom::CHAR_UPPER |
|
| 336 | + ); |
|
| 337 | + $tokenValue = $this->timeFactory->getTime() .':'. $token; |
|
| 338 | + $encryptedValue = $this->crypto->encrypt($tokenValue, $email . $this->config->getSystemValue('secret')); |
|
| 339 | + $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
| 340 | + |
|
| 341 | + $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user->getUID(), 'token' => $token)); |
|
| 342 | + |
|
| 343 | + $emailTemplate = $this->mailer->createEMailTemplate('core.ResetPassword', [ |
|
| 344 | + 'link' => $link, |
|
| 345 | + ]); |
|
| 346 | + |
|
| 347 | + $emailTemplate->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()])); |
|
| 348 | + $emailTemplate->addHeader(); |
|
| 349 | + $emailTemplate->addHeading($this->l10n->t('Password reset')); |
|
| 350 | + |
|
| 351 | + $emailTemplate->addBodyText( |
|
| 352 | + htmlspecialchars($this->l10n->t('Click the following button to reset your password. If you have not requested the password reset, then ignore this email.')), |
|
| 353 | + $this->l10n->t('Click the following link to reset your password. If you have not requested the password reset, then ignore this email.') |
|
| 354 | + ); |
|
| 355 | + |
|
| 356 | + $emailTemplate->addBodyButton( |
|
| 357 | + htmlspecialchars($this->l10n->t('Reset your password')), |
|
| 358 | + $link, |
|
| 359 | + false |
|
| 360 | + ); |
|
| 361 | + $emailTemplate->addFooter(); |
|
| 362 | + |
|
| 363 | + try { |
|
| 364 | + $message = $this->mailer->createMessage(); |
|
| 365 | + $message->setTo([$email => $user->getUID()]); |
|
| 366 | + $message->setFrom([$this->from => $this->defaults->getName()]); |
|
| 367 | + $message->useTemplate($emailTemplate); |
|
| 368 | + $this->mailer->send($message); |
|
| 369 | + } catch (\Exception $e) { |
|
| 370 | + throw new \Exception($this->l10n->t( |
|
| 371 | + 'Couldn\'t send reset email. Please contact your administrator.' |
|
| 372 | + )); |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * @param string $input |
|
| 378 | + * @return IUser |
|
| 379 | + * @throws \InvalidArgumentException |
|
| 380 | + */ |
|
| 381 | + protected function findUserByIdOrMail($input) { |
|
| 382 | + $userNotFound = new \InvalidArgumentException( |
|
| 383 | + $this->l10n->t('Couldn\'t send reset email. Please make sure your username is correct.') |
|
| 384 | + ); |
|
| 385 | + |
|
| 386 | + $user = $this->userManager->get($input); |
|
| 387 | + if ($user instanceof IUser) { |
|
| 388 | + if (!$user->isEnabled()) { |
|
| 389 | + throw $userNotFound; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + return $user; |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + $users = array_filter($this->userManager->getByEmail($input), function (IUser $user) { |
|
| 396 | + return $user->isEnabled(); |
|
| 397 | + }); |
|
| 398 | + |
|
| 399 | + if (count($users) === 1) { |
|
| 400 | + return reset($users); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + throw $userNotFound; |
|
| 404 | + } |
|
| 405 | 405 | } |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | */ |
| 183 | 183 | protected function checkPasswordResetToken($token, $userId) { |
| 184 | 184 | $user = $this->userManager->get($userId); |
| 185 | - if($user === null || !$user->isEnabled()) { |
|
| 185 | + if ($user === null || !$user->isEnabled()) { |
|
| 186 | 186 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
| 187 | 187 | } |
| 188 | 188 | |
@@ -195,11 +195,11 @@ discard block |
||
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | $splittedToken = explode(':', $decryptedToken); |
| 198 | - if(count($splittedToken) !== 2) { |
|
| 198 | + if (count($splittedToken) !== 2) { |
|
| 199 | 199 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*24*7) || |
|
| 202 | + if ($splittedToken[0] < ($this->timeFactory->getTime() - 60 * 60 * 24 * 7) || |
|
| 203 | 203 | $user->getLastLogin() > $splittedToken[0]) { |
| 204 | 204 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired')); |
| 205 | 205 | } |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | * @param array $additional |
| 215 | 215 | * @return array |
| 216 | 216 | */ |
| 217 | - private function error($message, array $additional=array()) { |
|
| 217 | + private function error($message, array $additional = array()) { |
|
| 218 | 218 | return array_merge(array('status' => 'error', 'msg' => $message), $additional); |
| 219 | 219 | } |
| 220 | 220 | |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | * @param string $user |
| 235 | 235 | * @return JSONResponse |
| 236 | 236 | */ |
| 237 | - public function email($user){ |
|
| 237 | + public function email($user) { |
|
| 238 | 238 | if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
| 239 | 239 | return new JSONResponse($this->error($this->l10n->t('Password reset is disabled'))); |
| 240 | 240 | } |
@@ -301,9 +301,9 @@ discard block |
||
| 301 | 301 | |
| 302 | 302 | $this->config->deleteUserValue($userId, 'core', 'lostpassword'); |
| 303 | 303 | @\OC::$server->getUserSession()->unsetMagicInCookie(); |
| 304 | - } catch (HintException $e){ |
|
| 304 | + } catch (HintException $e) { |
|
| 305 | 305 | return $this->error($e->getHint()); |
| 306 | - } catch (\Exception $e){ |
|
| 306 | + } catch (\Exception $e) { |
|
| 307 | 307 | return $this->error($e->getMessage()); |
| 308 | 308 | } |
| 309 | 309 | |
@@ -334,8 +334,8 @@ discard block |
||
| 334 | 334 | ISecureRandom::CHAR_LOWER. |
| 335 | 335 | ISecureRandom::CHAR_UPPER |
| 336 | 336 | ); |
| 337 | - $tokenValue = $this->timeFactory->getTime() .':'. $token; |
|
| 338 | - $encryptedValue = $this->crypto->encrypt($tokenValue, $email . $this->config->getSystemValue('secret')); |
|
| 337 | + $tokenValue = $this->timeFactory->getTime().':'.$token; |
|
| 338 | + $encryptedValue = $this->crypto->encrypt($tokenValue, $email.$this->config->getSystemValue('secret')); |
|
| 339 | 339 | $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
| 340 | 340 | |
| 341 | 341 | $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user->getUID(), 'token' => $token)); |
@@ -392,7 +392,7 @@ discard block |
||
| 392 | 392 | return $user; |
| 393 | 393 | } |
| 394 | 394 | |
| 395 | - $users = array_filter($this->userManager->getByEmail($input), function (IUser $user) { |
|
| 395 | + $users = array_filter($this->userManager->getByEmail($input), function(IUser $user) { |
|
| 396 | 396 | return $user->isEnabled(); |
| 397 | 397 | }); |
| 398 | 398 | |