lostcontroller.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Core\LostPassword\Controller;
  9. use \OCP\AppFramework\Controller;
  10. use \OCP\AppFramework\Http\JSONResponse;
  11. use \OCP\AppFramework\Http\TemplateResponse;
  12. use \OCP\IURLGenerator;
  13. use \OCP\IRequest;
  14. use \OCP\IL10N;
  15. use \OCP\IConfig;
  16. use \OCP\IUserSession;
  17. use \OC\Core\LostPassword\EncryptedDataException;
  18. class LostController extends Controller {
  19. /**
  20. * @var \OCP\IURLGenerator
  21. */
  22. protected $urlGenerator;
  23. /**
  24. * @var \OCP\IUserManager
  25. */
  26. protected $userManager;
  27. /**
  28. * @var \OC_Defaults
  29. */
  30. protected $defaults;
  31. /**
  32. * @var IL10N
  33. */
  34. protected $l10n;
  35. protected $from;
  36. protected $isDataEncrypted;
  37. /**
  38. * @var IConfig
  39. */
  40. protected $config;
  41. /**
  42. * @var IUserSession
  43. */
  44. protected $userSession;
  45. public function __construct($appName,
  46. IRequest $request,
  47. IURLGenerator $urlGenerator,
  48. $userManager,
  49. $defaults,
  50. IL10N $l10n,
  51. IConfig $config,
  52. IUserSession $userSession,
  53. $from,
  54. $isDataEncrypted) {
  55. parent::__construct($appName, $request);
  56. $this->urlGenerator = $urlGenerator;
  57. $this->userManager = $userManager;
  58. $this->defaults = $defaults;
  59. $this->l10n = $l10n;
  60. $this->from = $from;
  61. $this->isDataEncrypted = $isDataEncrypted;
  62. $this->config = $config;
  63. $this->userSession = $userSession;
  64. }
  65. /**
  66. * Someone wants to reset their password:
  67. *
  68. * @PublicPage
  69. * @NoCSRFRequired
  70. *
  71. * @param string $token
  72. * @param string $userId
  73. */
  74. public function resetform($token, $userId) {
  75. return new TemplateResponse(
  76. 'core/lostpassword',
  77. 'resetpassword',
  78. array(
  79. 'isEncrypted' => $this->isDataEncrypted,
  80. 'link' => $this->getLink('core.lost.setPassword', $userId, $token),
  81. ),
  82. 'guest'
  83. );
  84. }
  85. private function error($message, array $additional=array()) {
  86. return array_merge(array('status' => 'error', 'msg' => $message), $additional);
  87. }
  88. private function success() {
  89. return array('status'=>'success');
  90. }
  91. /**
  92. * @PublicPage
  93. *
  94. * @param string $user
  95. * @param bool $proceed
  96. */
  97. public function email($user, $proceed){
  98. // FIXME: use HTTP error codes
  99. try {
  100. $this->sendEmail($user, $proceed);
  101. } catch (EncryptedDataException $e){
  102. return $this->error('', array('encryption' => '1'));
  103. } catch (\Exception $e){
  104. return $this->error($e->getMessage());
  105. }
  106. return $this->success();
  107. }
  108. /**
  109. * @PublicPage
  110. */
  111. public function setPassword($token, $userId, $password) {
  112. try {
  113. $user = $this->userManager->get($userId);
  114. if (!$this->checkToken($userId, $token)) {
  115. throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid'));
  116. }
  117. if (!$user->setPassword($password)) {
  118. throw new \Exception();
  119. }
  120. // FIXME: should be added to the all config at some point
  121. \OC_Preferences::deleteKey($userId, 'owncloud', 'lostpassword');
  122. $this->userSession->unsetMagicInCookie();
  123. } catch (\Exception $e){
  124. return $this->error($e->getMessage());
  125. }
  126. return $this->success();
  127. }
  128. protected function sendEmail($user, $proceed) {
  129. if ($this->isDataEncrypted && !$proceed){
  130. throw new EncryptedDataException();
  131. }
  132. if (!$this->userManager->userExists($user)) {
  133. throw new \Exception(
  134. $this->l10n->t('Couldn\'t send reset email. Please make sure '.
  135. 'your username is correct.'));
  136. }
  137. $token = hash('sha256', \OC_Util::generateRandomBytes(30));
  138. // Hash the token again to prevent timing attacks
  139. $this->config->setUserValue(
  140. $user, 'owncloud', 'lostpassword', hash('sha256', $token)
  141. );
  142. $email = $this->config->getUserValue($user, 'settings', 'email');
  143. if (empty($email)) {
  144. throw new \Exception(
  145. $this->l10n->t('Couldn\'t send reset email because there is no '.
  146. 'email address for this username. Please ' .
  147. 'contact your administrator.')
  148. );
  149. }
  150. $link = $this->getLink('core.lost.resetform', $user, $token);
  151. $tmpl = new \OC_Template('core/lostpassword', 'email');
  152. $tmpl->assign('link', $link, false);
  153. $msg = $tmpl->fetchPage();
  154. try {
  155. // FIXME: should be added to the container and injected in here
  156. \OC_Mail::send(
  157. $email,
  158. $user,
  159. $this->l10n->t('%s password reset', array($this->defaults->getName())),
  160. $msg,
  161. $this->from,
  162. $this->defaults->getName()
  163. );
  164. } catch (\Exception $e) {
  165. throw new \Exception($this->l10n->t(
  166. 'Couldn\'t send reset email. Please contact your administrator.'
  167. ));
  168. }
  169. }
  170. protected function getLink($route, $user, $token){
  171. $parameters = array(
  172. 'token' => $token,
  173. 'userId' => $user
  174. );
  175. $link = $this->urlGenerator->linkToRoute($route, $parameters);
  176. return $this->urlGenerator->getAbsoluteUrl($link);
  177. }
  178. protected function checkToken($user, $token) {
  179. return $this->config->getUserValue(
  180. $user, 'owncloud', 'lostpassword'
  181. ) === hash('sha256', $token);
  182. }
  183. }