@@ -49,276 +49,276 @@ |
||
| 49 | 49 | use OC\Hooks\PublicEmitter; |
| 50 | 50 | |
| 51 | 51 | class LoginController extends Controller { |
| 52 | - /** @var IUserManager */ |
|
| 53 | - private $userManager; |
|
| 54 | - /** @var IConfig */ |
|
| 55 | - private $config; |
|
| 56 | - /** @var ISession */ |
|
| 57 | - private $session; |
|
| 58 | - /** @var IUserSession|Session */ |
|
| 59 | - private $userSession; |
|
| 60 | - /** @var IURLGenerator */ |
|
| 61 | - private $urlGenerator; |
|
| 62 | - /** @var ILogger */ |
|
| 63 | - private $logger; |
|
| 64 | - /** @var Manager */ |
|
| 65 | - private $twoFactorManager; |
|
| 52 | + /** @var IUserManager */ |
|
| 53 | + private $userManager; |
|
| 54 | + /** @var IConfig */ |
|
| 55 | + private $config; |
|
| 56 | + /** @var ISession */ |
|
| 57 | + private $session; |
|
| 58 | + /** @var IUserSession|Session */ |
|
| 59 | + private $userSession; |
|
| 60 | + /** @var IURLGenerator */ |
|
| 61 | + private $urlGenerator; |
|
| 62 | + /** @var ILogger */ |
|
| 63 | + private $logger; |
|
| 64 | + /** @var Manager */ |
|
| 65 | + private $twoFactorManager; |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * @param string $appName |
|
| 69 | - * @param IRequest $request |
|
| 70 | - * @param IUserManager $userManager |
|
| 71 | - * @param IConfig $config |
|
| 72 | - * @param ISession $session |
|
| 73 | - * @param IUserSession $userSession |
|
| 74 | - * @param IURLGenerator $urlGenerator |
|
| 75 | - * @param ILogger $logger |
|
| 76 | - * @param Manager $twoFactorManager |
|
| 77 | - */ |
|
| 78 | - public function __construct($appName, |
|
| 79 | - IRequest $request, |
|
| 80 | - IUserManager $userManager, |
|
| 81 | - IConfig $config, |
|
| 82 | - ISession $session, |
|
| 83 | - IUserSession $userSession, |
|
| 84 | - IURLGenerator $urlGenerator, |
|
| 85 | - ILogger $logger, |
|
| 86 | - Manager $twoFactorManager) { |
|
| 87 | - parent::__construct($appName, $request); |
|
| 88 | - $this->userManager = $userManager; |
|
| 89 | - $this->config = $config; |
|
| 90 | - $this->session = $session; |
|
| 91 | - $this->userSession = $userSession; |
|
| 92 | - $this->urlGenerator = $urlGenerator; |
|
| 93 | - $this->logger = $logger; |
|
| 94 | - $this->twoFactorManager = $twoFactorManager; |
|
| 95 | - } |
|
| 67 | + /** |
|
| 68 | + * @param string $appName |
|
| 69 | + * @param IRequest $request |
|
| 70 | + * @param IUserManager $userManager |
|
| 71 | + * @param IConfig $config |
|
| 72 | + * @param ISession $session |
|
| 73 | + * @param IUserSession $userSession |
|
| 74 | + * @param IURLGenerator $urlGenerator |
|
| 75 | + * @param ILogger $logger |
|
| 76 | + * @param Manager $twoFactorManager |
|
| 77 | + */ |
|
| 78 | + public function __construct($appName, |
|
| 79 | + IRequest $request, |
|
| 80 | + IUserManager $userManager, |
|
| 81 | + IConfig $config, |
|
| 82 | + ISession $session, |
|
| 83 | + IUserSession $userSession, |
|
| 84 | + IURLGenerator $urlGenerator, |
|
| 85 | + ILogger $logger, |
|
| 86 | + Manager $twoFactorManager) { |
|
| 87 | + parent::__construct($appName, $request); |
|
| 88 | + $this->userManager = $userManager; |
|
| 89 | + $this->config = $config; |
|
| 90 | + $this->session = $session; |
|
| 91 | + $this->userSession = $userSession; |
|
| 92 | + $this->urlGenerator = $urlGenerator; |
|
| 93 | + $this->logger = $logger; |
|
| 94 | + $this->twoFactorManager = $twoFactorManager; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * @NoAdminRequired |
|
| 99 | - * @UseSession |
|
| 100 | - * |
|
| 101 | - * @return RedirectResponse |
|
| 102 | - */ |
|
| 103 | - public function logout() { |
|
| 104 | - $loginToken = $this->request->getCookie('nc_token'); |
|
| 105 | - if (!is_null($loginToken)) { |
|
| 106 | - $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
| 107 | - } |
|
| 108 | - $this->userSession->logout(); |
|
| 97 | + /** |
|
| 98 | + * @NoAdminRequired |
|
| 99 | + * @UseSession |
|
| 100 | + * |
|
| 101 | + * @return RedirectResponse |
|
| 102 | + */ |
|
| 103 | + public function logout() { |
|
| 104 | + $loginToken = $this->request->getCookie('nc_token'); |
|
| 105 | + if (!is_null($loginToken)) { |
|
| 106 | + $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
| 107 | + } |
|
| 108 | + $this->userSession->logout(); |
|
| 109 | 109 | |
| 110 | - return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
| 111 | - } |
|
| 110 | + return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - /** |
|
| 114 | - * @PublicPage |
|
| 115 | - * @NoCSRFRequired |
|
| 116 | - * @UseSession |
|
| 117 | - * |
|
| 118 | - * @param string $user |
|
| 119 | - * @param string $redirect_url |
|
| 120 | - * @param string $remember_login |
|
| 121 | - * |
|
| 122 | - * @return TemplateResponse|RedirectResponse |
|
| 123 | - */ |
|
| 124 | - public function showLoginForm($user, $redirect_url, $remember_login) { |
|
| 125 | - if ($this->userSession->isLoggedIn()) { |
|
| 126 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 127 | - } |
|
| 113 | + /** |
|
| 114 | + * @PublicPage |
|
| 115 | + * @NoCSRFRequired |
|
| 116 | + * @UseSession |
|
| 117 | + * |
|
| 118 | + * @param string $user |
|
| 119 | + * @param string $redirect_url |
|
| 120 | + * @param string $remember_login |
|
| 121 | + * |
|
| 122 | + * @return TemplateResponse|RedirectResponse |
|
| 123 | + */ |
|
| 124 | + public function showLoginForm($user, $redirect_url, $remember_login) { |
|
| 125 | + if ($this->userSession->isLoggedIn()) { |
|
| 126 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | - $parameters = array(); |
|
| 130 | - $loginMessages = $this->session->get('loginMessages'); |
|
| 131 | - $errors = []; |
|
| 132 | - $messages = []; |
|
| 133 | - if (is_array($loginMessages)) { |
|
| 134 | - list($errors, $messages) = $loginMessages; |
|
| 135 | - } |
|
| 136 | - $this->session->remove('loginMessages'); |
|
| 137 | - foreach ($errors as $value) { |
|
| 138 | - $parameters[$value] = true; |
|
| 139 | - } |
|
| 129 | + $parameters = array(); |
|
| 130 | + $loginMessages = $this->session->get('loginMessages'); |
|
| 131 | + $errors = []; |
|
| 132 | + $messages = []; |
|
| 133 | + if (is_array($loginMessages)) { |
|
| 134 | + list($errors, $messages) = $loginMessages; |
|
| 135 | + } |
|
| 136 | + $this->session->remove('loginMessages'); |
|
| 137 | + foreach ($errors as $value) { |
|
| 138 | + $parameters[$value] = true; |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - $parameters['messages'] = $messages; |
|
| 142 | - if (!is_null($user) && $user !== '') { |
|
| 143 | - $parameters['loginName'] = $user; |
|
| 144 | - $parameters['user_autofocus'] = false; |
|
| 145 | - } else { |
|
| 146 | - $parameters['loginName'] = ''; |
|
| 147 | - $parameters['user_autofocus'] = true; |
|
| 148 | - } |
|
| 149 | - if (!empty($redirect_url)) { |
|
| 150 | - $parameters['redirect_url'] = $redirect_url; |
|
| 151 | - } |
|
| 141 | + $parameters['messages'] = $messages; |
|
| 142 | + if (!is_null($user) && $user !== '') { |
|
| 143 | + $parameters['loginName'] = $user; |
|
| 144 | + $parameters['user_autofocus'] = false; |
|
| 145 | + } else { |
|
| 146 | + $parameters['loginName'] = ''; |
|
| 147 | + $parameters['user_autofocus'] = true; |
|
| 148 | + } |
|
| 149 | + if (!empty($redirect_url)) { |
|
| 150 | + $parameters['redirect_url'] = $redirect_url; |
|
| 151 | + } |
|
| 152 | 152 | |
| 153 | - $parameters['canResetPassword'] = true; |
|
| 154 | - $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', ''); |
|
| 155 | - if (!$parameters['resetPasswordLink']) { |
|
| 156 | - if (!is_null($user) && $user !== '') { |
|
| 157 | - $userObj = $this->userManager->get($user); |
|
| 158 | - if ($userObj instanceof IUser) { |
|
| 159 | - $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - } elseif ($parameters['resetPasswordLink'] === 'disabled') { |
|
| 163 | - $parameters['canResetPassword'] = false; |
|
| 164 | - } |
|
| 153 | + $parameters['canResetPassword'] = true; |
|
| 154 | + $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', ''); |
|
| 155 | + if (!$parameters['resetPasswordLink']) { |
|
| 156 | + if (!is_null($user) && $user !== '') { |
|
| 157 | + $userObj = $this->userManager->get($user); |
|
| 158 | + if ($userObj instanceof IUser) { |
|
| 159 | + $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + } elseif ($parameters['resetPasswordLink'] === 'disabled') { |
|
| 163 | + $parameters['canResetPassword'] = false; |
|
| 164 | + } |
|
| 165 | 165 | |
| 166 | - $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
| 167 | - $parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0; |
|
| 166 | + $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
| 167 | + $parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0; |
|
| 168 | 168 | |
| 169 | - if (!is_null($user) && $user !== '') { |
|
| 170 | - $parameters['loginName'] = $user; |
|
| 171 | - $parameters['user_autofocus'] = false; |
|
| 172 | - } else { |
|
| 173 | - $parameters['loginName'] = ''; |
|
| 174 | - $parameters['user_autofocus'] = true; |
|
| 175 | - } |
|
| 169 | + if (!is_null($user) && $user !== '') { |
|
| 170 | + $parameters['loginName'] = $user; |
|
| 171 | + $parameters['user_autofocus'] = false; |
|
| 172 | + } else { |
|
| 173 | + $parameters['loginName'] = ''; |
|
| 174 | + $parameters['user_autofocus'] = true; |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - return new TemplateResponse( |
|
| 178 | - $this->appName, 'login', $parameters, 'guest' |
|
| 179 | - ); |
|
| 180 | - } |
|
| 177 | + return new TemplateResponse( |
|
| 178 | + $this->appName, 'login', $parameters, 'guest' |
|
| 179 | + ); |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * @param string $redirectUrl |
|
| 184 | - * @return RedirectResponse |
|
| 185 | - */ |
|
| 186 | - private function generateRedirect($redirectUrl) { |
|
| 187 | - if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
| 188 | - $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
| 189 | - // Deny the redirect if the URL contains a @ |
|
| 190 | - // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
| 191 | - if (strpos($location, '@') === false) { |
|
| 192 | - return new RedirectResponse($location); |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 196 | - } |
|
| 182 | + /** |
|
| 183 | + * @param string $redirectUrl |
|
| 184 | + * @return RedirectResponse |
|
| 185 | + */ |
|
| 186 | + private function generateRedirect($redirectUrl) { |
|
| 187 | + if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
| 188 | + $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
| 189 | + // Deny the redirect if the URL contains a @ |
|
| 190 | + // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
| 191 | + if (strpos($location, '@') === false) { |
|
| 192 | + return new RedirectResponse($location); |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 196 | + } |
|
| 197 | 197 | |
| 198 | - /** |
|
| 199 | - * @PublicPage |
|
| 200 | - * @UseSession |
|
| 201 | - * @NoCSRFRequired |
|
| 202 | - * @BruteForceProtection(action=login) |
|
| 203 | - * |
|
| 204 | - * @param string $user |
|
| 205 | - * @param string $password |
|
| 206 | - * @param string $redirect_url |
|
| 207 | - * @param boolean $remember_login |
|
| 208 | - * @param string $timezone |
|
| 209 | - * @param string $timezone_offset |
|
| 210 | - * @return RedirectResponse |
|
| 211 | - */ |
|
| 212 | - public function tryLogin($user, $password, $redirect_url, $remember_login = false, $timezone = '', $timezone_offset = '') { |
|
| 213 | - if(!is_string($user)) { |
|
| 214 | - throw new \InvalidArgumentException('Username must be string'); |
|
| 215 | - } |
|
| 198 | + /** |
|
| 199 | + * @PublicPage |
|
| 200 | + * @UseSession |
|
| 201 | + * @NoCSRFRequired |
|
| 202 | + * @BruteForceProtection(action=login) |
|
| 203 | + * |
|
| 204 | + * @param string $user |
|
| 205 | + * @param string $password |
|
| 206 | + * @param string $redirect_url |
|
| 207 | + * @param boolean $remember_login |
|
| 208 | + * @param string $timezone |
|
| 209 | + * @param string $timezone_offset |
|
| 210 | + * @return RedirectResponse |
|
| 211 | + */ |
|
| 212 | + public function tryLogin($user, $password, $redirect_url, $remember_login = false, $timezone = '', $timezone_offset = '') { |
|
| 213 | + if(!is_string($user)) { |
|
| 214 | + throw new \InvalidArgumentException('Username must be string'); |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - // If the user is already logged in and the CSRF check does not pass then |
|
| 218 | - // simply redirect the user to the correct page as required. This is the |
|
| 219 | - // case when an user has already logged-in, in another tab. |
|
| 220 | - if(!$this->request->passesCSRFCheck()) { |
|
| 221 | - return $this->generateRedirect($redirect_url); |
|
| 222 | - } |
|
| 217 | + // If the user is already logged in and the CSRF check does not pass then |
|
| 218 | + // simply redirect the user to the correct page as required. This is the |
|
| 219 | + // case when an user has already logged-in, in another tab. |
|
| 220 | + if(!$this->request->passesCSRFCheck()) { |
|
| 221 | + return $this->generateRedirect($redirect_url); |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | - if ($this->userManager instanceof PublicEmitter) { |
|
| 225 | - $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
| 226 | - } |
|
| 224 | + if ($this->userManager instanceof PublicEmitter) { |
|
| 225 | + $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - $originalUser = $user; |
|
| 229 | - // TODO: Add all the insane error handling |
|
| 230 | - /* @var $loginResult IUser */ |
|
| 231 | - $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
| 232 | - if ($loginResult === false) { |
|
| 233 | - $users = $this->userManager->getByEmail($user); |
|
| 234 | - // we only allow login by email if unique |
|
| 235 | - if (count($users) === 1) { |
|
| 236 | - $user = $users[0]->getUID(); |
|
| 237 | - $loginResult = $this->userManager->checkPassword($user, $password); |
|
| 238 | - } else { |
|
| 239 | - $this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - if ($loginResult === false) { |
|
| 243 | - // Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name |
|
| 244 | - $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
| 245 | - if (!is_null($redirect_url)) { |
|
| 246 | - $args['redirect_url'] = $redirect_url; |
|
| 247 | - } |
|
| 248 | - $response = new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args)); |
|
| 249 | - $response->throttle(); |
|
| 250 | - $this->session->set('loginMessages', [ |
|
| 251 | - ['invalidpassword'], [] |
|
| 252 | - ]); |
|
| 253 | - return $response; |
|
| 254 | - } |
|
| 255 | - // TODO: remove password checks from above and let the user session handle failures |
|
| 256 | - // requires https://github.com/owncloud/core/pull/24616 |
|
| 257 | - $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
| 258 | - $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, (int)$remember_login); |
|
| 228 | + $originalUser = $user; |
|
| 229 | + // TODO: Add all the insane error handling |
|
| 230 | + /* @var $loginResult IUser */ |
|
| 231 | + $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
| 232 | + if ($loginResult === false) { |
|
| 233 | + $users = $this->userManager->getByEmail($user); |
|
| 234 | + // we only allow login by email if unique |
|
| 235 | + if (count($users) === 1) { |
|
| 236 | + $user = $users[0]->getUID(); |
|
| 237 | + $loginResult = $this->userManager->checkPassword($user, $password); |
|
| 238 | + } else { |
|
| 239 | + $this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + if ($loginResult === false) { |
|
| 243 | + // Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name |
|
| 244 | + $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
| 245 | + if (!is_null($redirect_url)) { |
|
| 246 | + $args['redirect_url'] = $redirect_url; |
|
| 247 | + } |
|
| 248 | + $response = new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args)); |
|
| 249 | + $response->throttle(); |
|
| 250 | + $this->session->set('loginMessages', [ |
|
| 251 | + ['invalidpassword'], [] |
|
| 252 | + ]); |
|
| 253 | + return $response; |
|
| 254 | + } |
|
| 255 | + // TODO: remove password checks from above and let the user session handle failures |
|
| 256 | + // requires https://github.com/owncloud/core/pull/24616 |
|
| 257 | + $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
| 258 | + $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, (int)$remember_login); |
|
| 259 | 259 | |
| 260 | - // User has successfully logged in, now remove the password reset link, when it is available |
|
| 261 | - $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
| 260 | + // User has successfully logged in, now remove the password reset link, when it is available |
|
| 261 | + $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
| 262 | 262 | |
| 263 | - $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
| 263 | + $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
| 264 | 264 | |
| 265 | - if ($timezone_offset !== '') { |
|
| 266 | - $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
| 267 | - $this->session->set('timezone', $timezone_offset); |
|
| 268 | - } |
|
| 265 | + if ($timezone_offset !== '') { |
|
| 266 | + $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
| 267 | + $this->session->set('timezone', $timezone_offset); |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
| 271 | - $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
| 270 | + if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
| 271 | + $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
| 272 | 272 | |
| 273 | - $providers = $this->twoFactorManager->getProviders($loginResult); |
|
| 274 | - if (count($providers) === 1) { |
|
| 275 | - // Single provider, hence we can redirect to that provider's challenge page directly |
|
| 276 | - /* @var $provider IProvider */ |
|
| 277 | - $provider = array_pop($providers); |
|
| 278 | - $url = 'core.TwoFactorChallenge.showChallenge'; |
|
| 279 | - $urlParams = [ |
|
| 280 | - 'challengeProviderId' => $provider->getId(), |
|
| 281 | - ]; |
|
| 282 | - } else { |
|
| 283 | - $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
| 284 | - $urlParams = []; |
|
| 285 | - } |
|
| 273 | + $providers = $this->twoFactorManager->getProviders($loginResult); |
|
| 274 | + if (count($providers) === 1) { |
|
| 275 | + // Single provider, hence we can redirect to that provider's challenge page directly |
|
| 276 | + /* @var $provider IProvider */ |
|
| 277 | + $provider = array_pop($providers); |
|
| 278 | + $url = 'core.TwoFactorChallenge.showChallenge'; |
|
| 279 | + $urlParams = [ |
|
| 280 | + 'challengeProviderId' => $provider->getId(), |
|
| 281 | + ]; |
|
| 282 | + } else { |
|
| 283 | + $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
| 284 | + $urlParams = []; |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - if (!is_null($redirect_url)) { |
|
| 288 | - $urlParams['redirect_url'] = $redirect_url; |
|
| 289 | - } |
|
| 287 | + if (!is_null($redirect_url)) { |
|
| 288 | + $urlParams['redirect_url'] = $redirect_url; |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
| 292 | - } |
|
| 291 | + return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
| 292 | + } |
|
| 293 | 293 | |
| 294 | - if ($remember_login) { |
|
| 295 | - $this->userSession->createRememberMeToken($loginResult); |
|
| 296 | - } |
|
| 294 | + if ($remember_login) { |
|
| 295 | + $this->userSession->createRememberMeToken($loginResult); |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - return $this->generateRedirect($redirect_url); |
|
| 299 | - } |
|
| 298 | + return $this->generateRedirect($redirect_url); |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | - /** |
|
| 302 | - * @NoAdminRequired |
|
| 303 | - * @UseSession |
|
| 304 | - * @BruteForceProtection(action=sudo) |
|
| 305 | - * |
|
| 306 | - * @license GNU AGPL version 3 or any later version |
|
| 307 | - * |
|
| 308 | - * @param string $password |
|
| 309 | - * @return DataResponse |
|
| 310 | - */ |
|
| 311 | - public function confirmPassword($password) { |
|
| 312 | - $loginName = $this->userSession->getLoginName(); |
|
| 313 | - $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
| 314 | - if ($loginResult === false) { |
|
| 315 | - $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 316 | - $response->throttle(); |
|
| 317 | - return $response; |
|
| 318 | - } |
|
| 301 | + /** |
|
| 302 | + * @NoAdminRequired |
|
| 303 | + * @UseSession |
|
| 304 | + * @BruteForceProtection(action=sudo) |
|
| 305 | + * |
|
| 306 | + * @license GNU AGPL version 3 or any later version |
|
| 307 | + * |
|
| 308 | + * @param string $password |
|
| 309 | + * @return DataResponse |
|
| 310 | + */ |
|
| 311 | + public function confirmPassword($password) { |
|
| 312 | + $loginName = $this->userSession->getLoginName(); |
|
| 313 | + $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
| 314 | + if ($loginResult === false) { |
|
| 315 | + $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 316 | + $response->throttle(); |
|
| 317 | + return $response; |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - $confirmTimestamp = time(); |
|
| 321 | - $this->session->set('last-password-confirm', $confirmTimestamp); |
|
| 322 | - return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
| 323 | - } |
|
| 320 | + $confirmTimestamp = time(); |
|
| 321 | + $this->session->set('last-password-confirm', $confirmTimestamp); |
|
| 322 | + return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
| 323 | + } |
|
| 324 | 324 | } |
@@ -55,303 +55,303 @@ |
||
| 55 | 55 | */ |
| 56 | 56 | class LostController extends Controller { |
| 57 | 57 | |
| 58 | - /** @var IURLGenerator */ |
|
| 59 | - protected $urlGenerator; |
|
| 60 | - /** @var IUserManager */ |
|
| 61 | - protected $userManager; |
|
| 62 | - /** @var Defaults */ |
|
| 63 | - protected $defaults; |
|
| 64 | - /** @var IL10N */ |
|
| 65 | - protected $l10n; |
|
| 66 | - /** @var string */ |
|
| 67 | - protected $from; |
|
| 68 | - /** @var IManager */ |
|
| 69 | - protected $encryptionManager; |
|
| 70 | - /** @var IConfig */ |
|
| 71 | - protected $config; |
|
| 72 | - /** @var ISecureRandom */ |
|
| 73 | - protected $secureRandom; |
|
| 74 | - /** @var IMailer */ |
|
| 75 | - protected $mailer; |
|
| 76 | - /** @var ITimeFactory */ |
|
| 77 | - protected $timeFactory; |
|
| 78 | - /** @var ICrypto */ |
|
| 79 | - protected $crypto; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param string $appName |
|
| 83 | - * @param IRequest $request |
|
| 84 | - * @param IURLGenerator $urlGenerator |
|
| 85 | - * @param IUserManager $userManager |
|
| 86 | - * @param Defaults $defaults |
|
| 87 | - * @param IL10N $l10n |
|
| 88 | - * @param IConfig $config |
|
| 89 | - * @param ISecureRandom $secureRandom |
|
| 90 | - * @param string $defaultMailAddress |
|
| 91 | - * @param IManager $encryptionManager |
|
| 92 | - * @param IMailer $mailer |
|
| 93 | - * @param ITimeFactory $timeFactory |
|
| 94 | - * @param ICrypto $crypto |
|
| 95 | - */ |
|
| 96 | - public function __construct($appName, |
|
| 97 | - IRequest $request, |
|
| 98 | - IURLGenerator $urlGenerator, |
|
| 99 | - IUserManager $userManager, |
|
| 100 | - Defaults $defaults, |
|
| 101 | - IL10N $l10n, |
|
| 102 | - IConfig $config, |
|
| 103 | - ISecureRandom $secureRandom, |
|
| 104 | - $defaultMailAddress, |
|
| 105 | - IManager $encryptionManager, |
|
| 106 | - IMailer $mailer, |
|
| 107 | - ITimeFactory $timeFactory, |
|
| 108 | - ICrypto $crypto) { |
|
| 109 | - parent::__construct($appName, $request); |
|
| 110 | - $this->urlGenerator = $urlGenerator; |
|
| 111 | - $this->userManager = $userManager; |
|
| 112 | - $this->defaults = $defaults; |
|
| 113 | - $this->l10n = $l10n; |
|
| 114 | - $this->secureRandom = $secureRandom; |
|
| 115 | - $this->from = $defaultMailAddress; |
|
| 116 | - $this->encryptionManager = $encryptionManager; |
|
| 117 | - $this->config = $config; |
|
| 118 | - $this->mailer = $mailer; |
|
| 119 | - $this->timeFactory = $timeFactory; |
|
| 120 | - $this->crypto = $crypto; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Someone wants to reset their password: |
|
| 125 | - * |
|
| 126 | - * @PublicPage |
|
| 127 | - * @NoCSRFRequired |
|
| 128 | - * |
|
| 129 | - * @param string $token |
|
| 130 | - * @param string $userId |
|
| 131 | - * @return TemplateResponse |
|
| 132 | - */ |
|
| 133 | - public function resetform($token, $userId) { |
|
| 134 | - if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 135 | - return new TemplateResponse('core', 'error', [ |
|
| 136 | - 'errors' => [['error' => $this->l10n->t('Password reset is disabled')]] |
|
| 137 | - ], |
|
| 138 | - 'guest' |
|
| 139 | - ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - try { |
|
| 143 | - $this->checkPasswordResetToken($token, $userId); |
|
| 144 | - } catch (\Exception $e) { |
|
| 145 | - return new TemplateResponse( |
|
| 146 | - 'core', 'error', [ |
|
| 147 | - "errors" => array(array("error" => $e->getMessage())) |
|
| 148 | - ], |
|
| 149 | - 'guest' |
|
| 150 | - ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return new TemplateResponse( |
|
| 154 | - 'core', |
|
| 155 | - 'lostpassword/resetpassword', |
|
| 156 | - array( |
|
| 157 | - 'link' => $this->urlGenerator->linkToRouteAbsolute('core.lost.setPassword', array('userId' => $userId, 'token' => $token)), |
|
| 158 | - ), |
|
| 159 | - 'guest' |
|
| 160 | - ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @param string $token |
|
| 165 | - * @param string $userId |
|
| 166 | - * @throws \Exception |
|
| 167 | - */ |
|
| 168 | - protected function checkPasswordResetToken($token, $userId) { |
|
| 169 | - $user = $this->userManager->get($userId); |
|
| 170 | - if($user === null) { |
|
| 171 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - try { |
|
| 175 | - $encryptedToken = $this->config->getUserValue($userId, 'core', 'lostpassword', null); |
|
| 176 | - $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
| 177 | - $decryptedToken = $this->crypto->decrypt($encryptedToken, $mailAddress.$this->config->getSystemValue('secret')); |
|
| 178 | - } catch (\Exception $e) { |
|
| 179 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $splittedToken = explode(':', $decryptedToken); |
|
| 183 | - if(count($splittedToken) !== 2) { |
|
| 184 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*12) || |
|
| 188 | - $user->getLastLogin() > $splittedToken[0]) { |
|
| 189 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired')); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - if (!hash_equals($splittedToken[1], $token)) { |
|
| 193 | - throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @param $message |
|
| 199 | - * @param array $additional |
|
| 200 | - * @return array |
|
| 201 | - */ |
|
| 202 | - private function error($message, array $additional=array()) { |
|
| 203 | - return array_merge(array('status' => 'error', 'msg' => $message), $additional); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @return array |
|
| 208 | - */ |
|
| 209 | - private function success() { |
|
| 210 | - return array('status'=>'success'); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * @PublicPage |
|
| 215 | - * @BruteForceProtection(action=passwordResetEmail) |
|
| 216 | - * @AnonRateThrottle(limit=10, period=300) |
|
| 217 | - * |
|
| 218 | - * @param string $user |
|
| 219 | - * @return JSONResponse |
|
| 220 | - */ |
|
| 221 | - public function email($user){ |
|
| 222 | - if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 223 | - return new JSONResponse($this->error($this->l10n->t('Password reset is disabled'))); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // FIXME: use HTTP error codes |
|
| 227 | - try { |
|
| 228 | - $this->sendEmail($user); |
|
| 229 | - } catch (\Exception $e){ |
|
| 230 | - $response = new JSONResponse($this->error($e->getMessage())); |
|
| 231 | - $response->throttle(); |
|
| 232 | - return $response; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $response = new JSONResponse($this->success()); |
|
| 236 | - $response->throttle(); |
|
| 237 | - return $response; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * @PublicPage |
|
| 242 | - * @param string $token |
|
| 243 | - * @param string $userId |
|
| 244 | - * @param string $password |
|
| 245 | - * @param boolean $proceed |
|
| 246 | - * @return array |
|
| 247 | - */ |
|
| 248 | - public function setPassword($token, $userId, $password, $proceed) { |
|
| 249 | - if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 250 | - return $this->error($this->l10n->t('Password reset is disabled')); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - if ($this->encryptionManager->isEnabled() && !$proceed) { |
|
| 254 | - return $this->error('', array('encryption' => true)); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - try { |
|
| 258 | - $this->checkPasswordResetToken($token, $userId); |
|
| 259 | - $user = $this->userManager->get($userId); |
|
| 260 | - |
|
| 261 | - \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'pre_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 262 | - |
|
| 263 | - if (!$user->setPassword($password)) { |
|
| 264 | - throw new \Exception(); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'post_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 268 | - |
|
| 269 | - $this->config->deleteUserValue($userId, 'core', 'lostpassword'); |
|
| 270 | - @\OC_User::unsetMagicInCookie(); |
|
| 271 | - } catch (\Exception $e){ |
|
| 272 | - return $this->error($e->getMessage()); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - return $this->success(); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * @param string $input |
|
| 280 | - * @throws \Exception |
|
| 281 | - */ |
|
| 282 | - protected function sendEmail($input) { |
|
| 283 | - $user = $this->findUserByIdOrMail($input); |
|
| 284 | - $email = $user->getEMailAddress(); |
|
| 285 | - |
|
| 286 | - if (empty($email)) { |
|
| 287 | - throw new \Exception( |
|
| 288 | - $this->l10n->t('Could not send reset email because there is no email address for this username. Please contact your administrator.') |
|
| 289 | - ); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - // Generate the token. It is stored encrypted in the database with the |
|
| 293 | - // secret being the users' email address appended with the system secret. |
|
| 294 | - // This makes the token automatically invalidate once the user changes |
|
| 295 | - // their email address. |
|
| 296 | - $token = $this->secureRandom->generate( |
|
| 297 | - 21, |
|
| 298 | - ISecureRandom::CHAR_DIGITS. |
|
| 299 | - ISecureRandom::CHAR_LOWER. |
|
| 300 | - ISecureRandom::CHAR_UPPER |
|
| 301 | - ); |
|
| 302 | - $tokenValue = $this->timeFactory->getTime() .':'. $token; |
|
| 303 | - $encryptedValue = $this->crypto->encrypt($tokenValue, $email . $this->config->getSystemValue('secret')); |
|
| 304 | - $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
| 305 | - |
|
| 306 | - $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user->getUID(), 'token' => $token)); |
|
| 307 | - |
|
| 308 | - $emailTemplate = $this->mailer->createEMailTemplate(); |
|
| 309 | - |
|
| 310 | - $emailTemplate->addHeader(); |
|
| 311 | - $emailTemplate->addHeading($this->l10n->t('Password reset')); |
|
| 312 | - |
|
| 313 | - $emailTemplate->addBodyText( |
|
| 314 | - $this->l10n->t('Click the following button to reset your password. If you have not requested the password reset, then ignore this email.'), |
|
| 315 | - $this->l10n->t('Click the following link to reset your password. If you have not requested the password reset, then ignore this email.') |
|
| 316 | - ); |
|
| 317 | - |
|
| 318 | - $emailTemplate->addBodyButton( |
|
| 319 | - $this->l10n->t('Reset your password'), |
|
| 320 | - $link, |
|
| 321 | - false |
|
| 322 | - ); |
|
| 323 | - $emailTemplate->addFooter(); |
|
| 324 | - |
|
| 325 | - try { |
|
| 326 | - $message = $this->mailer->createMessage(); |
|
| 327 | - $message->setTo([$email => $user->getUID()]); |
|
| 328 | - $message->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()])); |
|
| 329 | - $message->setPlainBody($emailTemplate->renderText()); |
|
| 330 | - $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 331 | - $message->setFrom([$this->from => $this->defaults->getName()]); |
|
| 332 | - $this->mailer->send($message); |
|
| 333 | - } catch (\Exception $e) { |
|
| 334 | - throw new \Exception($this->l10n->t( |
|
| 335 | - 'Couldn\'t send reset email. Please contact your administrator.' |
|
| 336 | - )); |
|
| 337 | - } |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @param string $input |
|
| 342 | - * @return IUser |
|
| 343 | - * @throws \Exception |
|
| 344 | - */ |
|
| 345 | - protected function findUserByIdOrMail($input) { |
|
| 346 | - $user = $this->userManager->get($input); |
|
| 347 | - if ($user instanceof IUser) { |
|
| 348 | - return $user; |
|
| 349 | - } |
|
| 350 | - $users = $this->userManager->getByEmail($input); |
|
| 351 | - if (count($users) === 1) { |
|
| 352 | - return $users[0]; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - throw new \InvalidArgumentException($this->l10n->t('Couldn\'t send reset email. Please make sure your username is correct.')); |
|
| 356 | - } |
|
| 58 | + /** @var IURLGenerator */ |
|
| 59 | + protected $urlGenerator; |
|
| 60 | + /** @var IUserManager */ |
|
| 61 | + protected $userManager; |
|
| 62 | + /** @var Defaults */ |
|
| 63 | + protected $defaults; |
|
| 64 | + /** @var IL10N */ |
|
| 65 | + protected $l10n; |
|
| 66 | + /** @var string */ |
|
| 67 | + protected $from; |
|
| 68 | + /** @var IManager */ |
|
| 69 | + protected $encryptionManager; |
|
| 70 | + /** @var IConfig */ |
|
| 71 | + protected $config; |
|
| 72 | + /** @var ISecureRandom */ |
|
| 73 | + protected $secureRandom; |
|
| 74 | + /** @var IMailer */ |
|
| 75 | + protected $mailer; |
|
| 76 | + /** @var ITimeFactory */ |
|
| 77 | + protected $timeFactory; |
|
| 78 | + /** @var ICrypto */ |
|
| 79 | + protected $crypto; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param string $appName |
|
| 83 | + * @param IRequest $request |
|
| 84 | + * @param IURLGenerator $urlGenerator |
|
| 85 | + * @param IUserManager $userManager |
|
| 86 | + * @param Defaults $defaults |
|
| 87 | + * @param IL10N $l10n |
|
| 88 | + * @param IConfig $config |
|
| 89 | + * @param ISecureRandom $secureRandom |
|
| 90 | + * @param string $defaultMailAddress |
|
| 91 | + * @param IManager $encryptionManager |
|
| 92 | + * @param IMailer $mailer |
|
| 93 | + * @param ITimeFactory $timeFactory |
|
| 94 | + * @param ICrypto $crypto |
|
| 95 | + */ |
|
| 96 | + public function __construct($appName, |
|
| 97 | + IRequest $request, |
|
| 98 | + IURLGenerator $urlGenerator, |
|
| 99 | + IUserManager $userManager, |
|
| 100 | + Defaults $defaults, |
|
| 101 | + IL10N $l10n, |
|
| 102 | + IConfig $config, |
|
| 103 | + ISecureRandom $secureRandom, |
|
| 104 | + $defaultMailAddress, |
|
| 105 | + IManager $encryptionManager, |
|
| 106 | + IMailer $mailer, |
|
| 107 | + ITimeFactory $timeFactory, |
|
| 108 | + ICrypto $crypto) { |
|
| 109 | + parent::__construct($appName, $request); |
|
| 110 | + $this->urlGenerator = $urlGenerator; |
|
| 111 | + $this->userManager = $userManager; |
|
| 112 | + $this->defaults = $defaults; |
|
| 113 | + $this->l10n = $l10n; |
|
| 114 | + $this->secureRandom = $secureRandom; |
|
| 115 | + $this->from = $defaultMailAddress; |
|
| 116 | + $this->encryptionManager = $encryptionManager; |
|
| 117 | + $this->config = $config; |
|
| 118 | + $this->mailer = $mailer; |
|
| 119 | + $this->timeFactory = $timeFactory; |
|
| 120 | + $this->crypto = $crypto; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Someone wants to reset their password: |
|
| 125 | + * |
|
| 126 | + * @PublicPage |
|
| 127 | + * @NoCSRFRequired |
|
| 128 | + * |
|
| 129 | + * @param string $token |
|
| 130 | + * @param string $userId |
|
| 131 | + * @return TemplateResponse |
|
| 132 | + */ |
|
| 133 | + public function resetform($token, $userId) { |
|
| 134 | + if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 135 | + return new TemplateResponse('core', 'error', [ |
|
| 136 | + 'errors' => [['error' => $this->l10n->t('Password reset is disabled')]] |
|
| 137 | + ], |
|
| 138 | + 'guest' |
|
| 139 | + ); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + try { |
|
| 143 | + $this->checkPasswordResetToken($token, $userId); |
|
| 144 | + } catch (\Exception $e) { |
|
| 145 | + return new TemplateResponse( |
|
| 146 | + 'core', 'error', [ |
|
| 147 | + "errors" => array(array("error" => $e->getMessage())) |
|
| 148 | + ], |
|
| 149 | + 'guest' |
|
| 150 | + ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return new TemplateResponse( |
|
| 154 | + 'core', |
|
| 155 | + 'lostpassword/resetpassword', |
|
| 156 | + array( |
|
| 157 | + 'link' => $this->urlGenerator->linkToRouteAbsolute('core.lost.setPassword', array('userId' => $userId, 'token' => $token)), |
|
| 158 | + ), |
|
| 159 | + 'guest' |
|
| 160 | + ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @param string $token |
|
| 165 | + * @param string $userId |
|
| 166 | + * @throws \Exception |
|
| 167 | + */ |
|
| 168 | + protected function checkPasswordResetToken($token, $userId) { |
|
| 169 | + $user = $this->userManager->get($userId); |
|
| 170 | + if($user === null) { |
|
| 171 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + try { |
|
| 175 | + $encryptedToken = $this->config->getUserValue($userId, 'core', 'lostpassword', null); |
|
| 176 | + $mailAddress = !is_null($user->getEMailAddress()) ? $user->getEMailAddress() : ''; |
|
| 177 | + $decryptedToken = $this->crypto->decrypt($encryptedToken, $mailAddress.$this->config->getSystemValue('secret')); |
|
| 178 | + } catch (\Exception $e) { |
|
| 179 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $splittedToken = explode(':', $decryptedToken); |
|
| 183 | + if(count($splittedToken) !== 2) { |
|
| 184 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*12) || |
|
| 188 | + $user->getLastLogin() > $splittedToken[0]) { |
|
| 189 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired')); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + if (!hash_equals($splittedToken[1], $token)) { |
|
| 193 | + throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @param $message |
|
| 199 | + * @param array $additional |
|
| 200 | + * @return array |
|
| 201 | + */ |
|
| 202 | + private function error($message, array $additional=array()) { |
|
| 203 | + return array_merge(array('status' => 'error', 'msg' => $message), $additional); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @return array |
|
| 208 | + */ |
|
| 209 | + private function success() { |
|
| 210 | + return array('status'=>'success'); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * @PublicPage |
|
| 215 | + * @BruteForceProtection(action=passwordResetEmail) |
|
| 216 | + * @AnonRateThrottle(limit=10, period=300) |
|
| 217 | + * |
|
| 218 | + * @param string $user |
|
| 219 | + * @return JSONResponse |
|
| 220 | + */ |
|
| 221 | + public function email($user){ |
|
| 222 | + if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 223 | + return new JSONResponse($this->error($this->l10n->t('Password reset is disabled'))); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // FIXME: use HTTP error codes |
|
| 227 | + try { |
|
| 228 | + $this->sendEmail($user); |
|
| 229 | + } catch (\Exception $e){ |
|
| 230 | + $response = new JSONResponse($this->error($e->getMessage())); |
|
| 231 | + $response->throttle(); |
|
| 232 | + return $response; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $response = new JSONResponse($this->success()); |
|
| 236 | + $response->throttle(); |
|
| 237 | + return $response; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * @PublicPage |
|
| 242 | + * @param string $token |
|
| 243 | + * @param string $userId |
|
| 244 | + * @param string $password |
|
| 245 | + * @param boolean $proceed |
|
| 246 | + * @return array |
|
| 247 | + */ |
|
| 248 | + public function setPassword($token, $userId, $password, $proceed) { |
|
| 249 | + if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
|
| 250 | + return $this->error($this->l10n->t('Password reset is disabled')); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + if ($this->encryptionManager->isEnabled() && !$proceed) { |
|
| 254 | + return $this->error('', array('encryption' => true)); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + try { |
|
| 258 | + $this->checkPasswordResetToken($token, $userId); |
|
| 259 | + $user = $this->userManager->get($userId); |
|
| 260 | + |
|
| 261 | + \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'pre_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 262 | + |
|
| 263 | + if (!$user->setPassword($password)) { |
|
| 264 | + throw new \Exception(); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + \OC_Hook::emit('\OC\Core\LostPassword\Controller\LostController', 'post_passwordReset', array('uid' => $userId, 'password' => $password)); |
|
| 268 | + |
|
| 269 | + $this->config->deleteUserValue($userId, 'core', 'lostpassword'); |
|
| 270 | + @\OC_User::unsetMagicInCookie(); |
|
| 271 | + } catch (\Exception $e){ |
|
| 272 | + return $this->error($e->getMessage()); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + return $this->success(); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * @param string $input |
|
| 280 | + * @throws \Exception |
|
| 281 | + */ |
|
| 282 | + protected function sendEmail($input) { |
|
| 283 | + $user = $this->findUserByIdOrMail($input); |
|
| 284 | + $email = $user->getEMailAddress(); |
|
| 285 | + |
|
| 286 | + if (empty($email)) { |
|
| 287 | + throw new \Exception( |
|
| 288 | + $this->l10n->t('Could not send reset email because there is no email address for this username. Please contact your administrator.') |
|
| 289 | + ); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + // Generate the token. It is stored encrypted in the database with the |
|
| 293 | + // secret being the users' email address appended with the system secret. |
|
| 294 | + // This makes the token automatically invalidate once the user changes |
|
| 295 | + // their email address. |
|
| 296 | + $token = $this->secureRandom->generate( |
|
| 297 | + 21, |
|
| 298 | + ISecureRandom::CHAR_DIGITS. |
|
| 299 | + ISecureRandom::CHAR_LOWER. |
|
| 300 | + ISecureRandom::CHAR_UPPER |
|
| 301 | + ); |
|
| 302 | + $tokenValue = $this->timeFactory->getTime() .':'. $token; |
|
| 303 | + $encryptedValue = $this->crypto->encrypt($tokenValue, $email . $this->config->getSystemValue('secret')); |
|
| 304 | + $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
|
| 305 | + |
|
| 306 | + $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user->getUID(), 'token' => $token)); |
|
| 307 | + |
|
| 308 | + $emailTemplate = $this->mailer->createEMailTemplate(); |
|
| 309 | + |
|
| 310 | + $emailTemplate->addHeader(); |
|
| 311 | + $emailTemplate->addHeading($this->l10n->t('Password reset')); |
|
| 312 | + |
|
| 313 | + $emailTemplate->addBodyText( |
|
| 314 | + $this->l10n->t('Click the following button to reset your password. If you have not requested the password reset, then ignore this email.'), |
|
| 315 | + $this->l10n->t('Click the following link to reset your password. If you have not requested the password reset, then ignore this email.') |
|
| 316 | + ); |
|
| 317 | + |
|
| 318 | + $emailTemplate->addBodyButton( |
|
| 319 | + $this->l10n->t('Reset your password'), |
|
| 320 | + $link, |
|
| 321 | + false |
|
| 322 | + ); |
|
| 323 | + $emailTemplate->addFooter(); |
|
| 324 | + |
|
| 325 | + try { |
|
| 326 | + $message = $this->mailer->createMessage(); |
|
| 327 | + $message->setTo([$email => $user->getUID()]); |
|
| 328 | + $message->setSubject($this->l10n->t('%s password reset', [$this->defaults->getName()])); |
|
| 329 | + $message->setPlainBody($emailTemplate->renderText()); |
|
| 330 | + $message->setHtmlBody($emailTemplate->renderHtml()); |
|
| 331 | + $message->setFrom([$this->from => $this->defaults->getName()]); |
|
| 332 | + $this->mailer->send($message); |
|
| 333 | + } catch (\Exception $e) { |
|
| 334 | + throw new \Exception($this->l10n->t( |
|
| 335 | + 'Couldn\'t send reset email. Please contact your administrator.' |
|
| 336 | + )); |
|
| 337 | + } |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @param string $input |
|
| 342 | + * @return IUser |
|
| 343 | + * @throws \Exception |
|
| 344 | + */ |
|
| 345 | + protected function findUserByIdOrMail($input) { |
|
| 346 | + $user = $this->userManager->get($input); |
|
| 347 | + if ($user instanceof IUser) { |
|
| 348 | + return $user; |
|
| 349 | + } |
|
| 350 | + $users = $this->userManager->getByEmail($input); |
|
| 351 | + if (count($users) === 1) { |
|
| 352 | + return $users[0]; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + throw new \InvalidArgumentException($this->l10n->t('Couldn\'t send reset email. Please make sure your username is correct.')); |
|
| 356 | + } |
|
| 357 | 357 | } |
@@ -167,7 +167,7 @@ discard block |
||
| 167 | 167 | */ |
| 168 | 168 | protected function checkPasswordResetToken($token, $userId) { |
| 169 | 169 | $user = $this->userManager->get($userId); |
| 170 | - if($user === null) { |
|
| 170 | + if ($user === null) { |
|
| 171 | 171 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
| 172 | 172 | } |
| 173 | 173 | |
@@ -180,11 +180,11 @@ discard block |
||
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | $splittedToken = explode(':', $decryptedToken); |
| 183 | - if(count($splittedToken) !== 2) { |
|
| 183 | + if (count($splittedToken) !== 2) { |
|
| 184 | 184 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is invalid')); |
| 185 | 185 | } |
| 186 | 186 | |
| 187 | - if ($splittedToken[0] < ($this->timeFactory->getTime() - 60*60*12) || |
|
| 187 | + if ($splittedToken[0] < ($this->timeFactory->getTime() - 60 * 60 * 12) || |
|
| 188 | 188 | $user->getLastLogin() > $splittedToken[0]) { |
| 189 | 189 | throw new \Exception($this->l10n->t('Couldn\'t reset password because the token is expired')); |
| 190 | 190 | } |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | * @param array $additional |
| 200 | 200 | * @return array |
| 201 | 201 | */ |
| 202 | - private function error($message, array $additional=array()) { |
|
| 202 | + private function error($message, array $additional = array()) { |
|
| 203 | 203 | return array_merge(array('status' => 'error', 'msg' => $message), $additional); |
| 204 | 204 | } |
| 205 | 205 | |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | * @param string $user |
| 219 | 219 | * @return JSONResponse |
| 220 | 220 | */ |
| 221 | - public function email($user){ |
|
| 221 | + public function email($user) { |
|
| 222 | 222 | if ($this->config->getSystemValue('lost_password_link', '') !== '') { |
| 223 | 223 | return new JSONResponse($this->error($this->l10n->t('Password reset is disabled'))); |
| 224 | 224 | } |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | // FIXME: use HTTP error codes |
| 227 | 227 | try { |
| 228 | 228 | $this->sendEmail($user); |
| 229 | - } catch (\Exception $e){ |
|
| 229 | + } catch (\Exception $e) { |
|
| 230 | 230 | $response = new JSONResponse($this->error($e->getMessage())); |
| 231 | 231 | $response->throttle(); |
| 232 | 232 | return $response; |
@@ -268,7 +268,7 @@ discard block |
||
| 268 | 268 | |
| 269 | 269 | $this->config->deleteUserValue($userId, 'core', 'lostpassword'); |
| 270 | 270 | @\OC_User::unsetMagicInCookie(); |
| 271 | - } catch (\Exception $e){ |
|
| 271 | + } catch (\Exception $e) { |
|
| 272 | 272 | return $this->error($e->getMessage()); |
| 273 | 273 | } |
| 274 | 274 | |
@@ -299,8 +299,8 @@ discard block |
||
| 299 | 299 | ISecureRandom::CHAR_LOWER. |
| 300 | 300 | ISecureRandom::CHAR_UPPER |
| 301 | 301 | ); |
| 302 | - $tokenValue = $this->timeFactory->getTime() .':'. $token; |
|
| 303 | - $encryptedValue = $this->crypto->encrypt($tokenValue, $email . $this->config->getSystemValue('secret')); |
|
| 302 | + $tokenValue = $this->timeFactory->getTime().':'.$token; |
|
| 303 | + $encryptedValue = $this->crypto->encrypt($tokenValue, $email.$this->config->getSystemValue('secret')); |
|
| 304 | 304 | $this->config->setUserValue($user->getUID(), 'core', 'lostpassword', $encryptedValue); |
| 305 | 305 | |
| 306 | 306 | $link = $this->urlGenerator->linkToRouteAbsolute('core.lost.resetform', array('userId' => $user->getUID(), 'token' => $token)); |