123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bernhard Posselt <dev@bernhard-posselt.com>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Julius Haertl <jus@bitgrid.net>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Victor Dubiniuk <dubiniuk@owncloud.com>
- *
- * @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,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OC\Core\Controller;
- use OCA\Encryption\Exceptions\PrivateKeyMissingException;
- use \OCP\AppFramework\Controller;
- use \OCP\AppFramework\Http\TemplateResponse;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\Encryption\IManager;
- use \OCP\IURLGenerator;
- use \OCP\IRequest;
- use \OCP\IL10N;
- use \OCP\IConfig;
- use OCP\IUserManager;
- use OCP\Mail\IMailer;
- use OCP\Security\ICrypto;
- use OCP\Security\ISecureRandom;
- /**
- * Class LostController
- *
- * Successfully changing a password will emit the post_passwordReset hook.
- *
- * @package OC\Core\Controller
- */
- class LostController extends Controller {
- /** @var IURLGenerator */
- protected $urlGenerator;
- /** @var IUserManager */
- protected $userManager;
- /** @var \OC_Defaults */
- protected $defaults;
- /** @var IL10N */
- protected $l10n;
- /** @var string */
- protected $from;
- /** @var IManager */
- protected $encryptionManager;
- /** @var IConfig */
- protected $config;
- /** @var ISecureRandom */
- protected $secureRandom;
- /** @var IMailer */
- protected $mailer;
- /** @var ITimeFactory */
- protected $timeFactory;
- /** @var ICrypto */
- protected $crypto;
- /**
- * @param string $appName
- * @param IRequest $request
- * @param IURLGenerator $urlGenerator
- * @param IUserManager $userManager
- * @param \OC_Defaults $defaults
- * @param IL10N $l10n
- * @param IConfig $config
- * @param ISecureRandom $secureRandom
- * @param string $defaultMailAddress
- * @param IManager $encryptionManager
- * @param IMailer $mailer
- * @param ITimeFactory $timeFactory
- * @param ICrypto $crypto
- */
- public function __construct($appName,
- IRequest $request,
- IURLGenerator $urlGenerator,
- IUserManager $userManager,
- \OC_Defaults $defaults,
- IL10N $l10n,
- IConfig $config,
- ISecureRandom $secureRandom,
- $defaultMailAddress,
- IManager $encryptionManager,
- IMailer $mailer,
- ITimeFactory $timeFactory,
- ICrypto $crypto) {
- parent::__construct($appName, $request);
- $this->urlGenerator = $urlGenerator;
- $this->userManager = $userManager;
- $this->defaults = $defaults;
- $this->l10n = $l10n;
- $this->secureRandom = $secureRandom;
- $this->from = $defaultMailAddress;
- $this->encryptionManager = $encryptionManager;
- $this->config = $config;
- $this->mailer = $mailer;
- $this->timeFactory = $timeFactory;
- $this->crypto = $crypto;
- }
- /**
- * Someone wants to reset their password:
- *
- * @PublicPage
- * @NoCSRFRequired
- *
- * @param string $token
- * @param string $userId
- * @return TemplateResponse
- */
- public function resetform($token, $userId) {
- try {
- $this->checkPasswordResetToken($token, $userId);
- } catch (\Exception $e) {
- return new TemplateResponse(
- 'core', 'error', [
- "errors" => array(array("error" => $e->getMessage()))
- ],
- 'guest'
- );
- }
- return new TemplateResponse(
- 'core',
- 'lostpassword/resetpassword',
- array(
- 'link' => $this->urlGenerator->linkToRouteAbsolute('core.lost.setPassword', array('userId' => $userId, 'token' => $token)),
- ),
- 'guest'
- );
- }
- /**
- * @param string $token
- * @param string $userId
- * @throws \Exception
- */
- protected function checkPasswordResetToken($token, $userId) {
- $user = $this->userManager->get($userId);
- if($user === null) {
- throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid'));
- }
- try {
- $encryptedToken = $this->config->getUserValue($userId, 'core', 'lostpassword', null);
- $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : '';
- $decryptedToken = $this->crypto->decrypt($encryptedToken, $mailAddress.$this->config->getSystemValue('secret'));
- } catch (\Exception $e) {
- throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid'));
- }
- $splittedToken = explode(':', $decryptedToken);
- if(count($splittedToken) !== 2) {
- throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid'));
- }
- if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*12) ||
- $user->getLastLogin() > $splittedToken[0]) {
- throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired'));
- }
- if (!hash_equals($splittedToken[1], $token)) {
- throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid'));
- }
- }
- /**
- * @param $message
- * @param array $additional
- * @return array
- */
- private function error($message, array $additional=array()) {
- return array_merge(array('status' => 'error', 'msg' => $message), $additional);
- }
- /**
- * @return array
- */
- private function success() {
- return array('status'=>'success');
- }
- /**
- * @PublicPage
- *
- * @param string $user
- * @return array
- */
- public function email($user){
- // FIXME: use HTTP error codes
- try {
- $this->sendEmail($user);
- } catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success();
- }
- /**
- * @PublicPage
- * @param string $token
- * @param string $userId
- * @param string $password
- * @param boolean $proceed
- * @return array
- */
- public function setPassword($token, $userId, $password, $proceed) {
- if ($this->encryptionManager->isEnabled() && !$proceed) {
- return $this->error('', array('encryption' => true));
- }
- try {
- $this->checkPasswordResetToken($token, $userId);
- $user = $this->userManager->get($userId);
- if (!$user->setPassword($password)) {
- throw new \Exception();
- }
- \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'post_passwordReset', array('uid' => $userId, 'password' => $password));
- $this->config->deleteUserValue($userId, 'core', 'lostpassword');
- @\OC_User::unsetMagicInCookie();
- } catch (PrivateKeyMissingException $e) {
- // in this case it is OK if we couldn't reset the users private key
- // They chose explicitely to continue at the password reset dialog
- // (see $proceed flag)
- return $this->success();
- } catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success();
- }
- /**
- * @param string $user
- * @throws \Exception
- */
- protected function sendEmail($user) {
- if (!$this->userManager->userExists($user)) {
- throw new \Exception($this->l10n->t('Couldn\'t send reset email. Please make sure your username is correct.'));
- }
- $userObject = $this->userManager->get($user);
- $email = $userObject->getEMailAddress();
- if (empty($email)) {
- throw new \Exception(
- $this->l10n->t('Could not send reset email because there is no email address for this username. Please contact your administrator.')
- );
- }
- // Generate the token. It is stored encrypted in the database with the
- // secret being the users' email address appended with the system secret.
- // This makes the token automatically invalidate once the user changes
- // their email address.
- $token = $this->secureRandom->generate(
- 21,
- ISecureRandom::CHAR_DIGITS.
- ISecureRandom::CHAR_LOWER.
- ISecureRandom::CHAR_UPPER
- );
- $tokenValue = $this->timeFactory->getTime() .':'. $token;
- $mailAddress = !is_null($userObject->getEMailAddress()) ? $userObject->getEMailAddress() : '';
- $encryptedValue = $this->crypto->encrypt($tokenValue, $mailAddress.$this->config->getSystemValue('secret'));
- $this->config->setUserValue($user, 'core', 'lostpassword', $encryptedValue);
- $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user, 'token' => $token));
- $tmpl = new \OC_Template('core', 'lostpassword/email');
- $tmpl->assign('link', $link);
- $msg = $tmpl->fetchPage();
- try {
- $message = $this->mailer->createMessage();
- $message->setTo([$email => $user]);
- $message->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()]));
- $message->setPlainBody($msg);
- $message->setFrom([$this->from => $this->defaults->getName()]);
- $this->mailer->send($message);
- } catch (\Exception $e) {
- throw new \Exception($this->l10n->t(
- 'Couldn\'t send reset email. Please contact your administrator.'
- ));
- }
- }
- }
|