@@ -58,326 +58,326 @@ |
||
| 58 | 58 | use OCP\Util; |
| 59 | 59 | |
| 60 | 60 | class LoginController extends Controller { |
| 61 | - public const LOGIN_MSG_INVALIDPASSWORD = 'invalidpassword'; |
|
| 62 | - public const LOGIN_MSG_USERDISABLED = 'userdisabled'; |
|
| 63 | - |
|
| 64 | - /** @var IUserManager */ |
|
| 65 | - private $userManager; |
|
| 66 | - /** @var IConfig */ |
|
| 67 | - private $config; |
|
| 68 | - /** @var ISession */ |
|
| 69 | - private $session; |
|
| 70 | - /** @var IUserSession|Session */ |
|
| 71 | - private $userSession; |
|
| 72 | - /** @var IURLGenerator */ |
|
| 73 | - private $urlGenerator; |
|
| 74 | - /** @var ILogger */ |
|
| 75 | - private $logger; |
|
| 76 | - /** @var Defaults */ |
|
| 77 | - private $defaults; |
|
| 78 | - /** @var Throttler */ |
|
| 79 | - private $throttler; |
|
| 80 | - /** @var Chain */ |
|
| 81 | - private $loginChain; |
|
| 82 | - /** @var IInitialStateService */ |
|
| 83 | - private $initialStateService; |
|
| 84 | - /** @var WebAuthnManager */ |
|
| 85 | - private $webAuthnManager; |
|
| 86 | - |
|
| 87 | - public function __construct(?string $appName, |
|
| 88 | - IRequest $request, |
|
| 89 | - IUserManager $userManager, |
|
| 90 | - IConfig $config, |
|
| 91 | - ISession $session, |
|
| 92 | - IUserSession $userSession, |
|
| 93 | - IURLGenerator $urlGenerator, |
|
| 94 | - ILogger $logger, |
|
| 95 | - Defaults $defaults, |
|
| 96 | - Throttler $throttler, |
|
| 97 | - Chain $loginChain, |
|
| 98 | - IInitialStateService $initialStateService, |
|
| 99 | - WebAuthnManager $webAuthnManager) { |
|
| 100 | - parent::__construct($appName, $request); |
|
| 101 | - $this->userManager = $userManager; |
|
| 102 | - $this->config = $config; |
|
| 103 | - $this->session = $session; |
|
| 104 | - $this->userSession = $userSession; |
|
| 105 | - $this->urlGenerator = $urlGenerator; |
|
| 106 | - $this->logger = $logger; |
|
| 107 | - $this->defaults = $defaults; |
|
| 108 | - $this->throttler = $throttler; |
|
| 109 | - $this->loginChain = $loginChain; |
|
| 110 | - $this->initialStateService = $initialStateService; |
|
| 111 | - $this->webAuthnManager = $webAuthnManager; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @NoAdminRequired |
|
| 116 | - * @UseSession |
|
| 117 | - * |
|
| 118 | - * @return RedirectResponse |
|
| 119 | - */ |
|
| 120 | - public function logout() { |
|
| 121 | - $loginToken = $this->request->getCookie('nc_token'); |
|
| 122 | - if (!is_null($loginToken)) { |
|
| 123 | - $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
| 124 | - } |
|
| 125 | - $this->userSession->logout(); |
|
| 126 | - |
|
| 127 | - $response = new RedirectResponse($this->urlGenerator->linkToRouteAbsolute( |
|
| 128 | - 'core.login.showLoginForm', |
|
| 129 | - ['clear' => true] // this param the the code in login.js may be removed when the "Clear-Site-Data" is working in the browsers |
|
| 130 | - )); |
|
| 131 | - |
|
| 132 | - $this->session->set('clearingExecutionContexts', '1'); |
|
| 133 | - $this->session->close(); |
|
| 134 | - |
|
| 135 | - if (!$this->request->isUserAgent([Request::USER_AGENT_CHROME, Request::USER_AGENT_ANDROID_MOBILE_CHROME])) { |
|
| 136 | - $response->addHeader('Clear-Site-Data', '"cache", "storage"'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $response; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @PublicPage |
|
| 144 | - * @NoCSRFRequired |
|
| 145 | - * @UseSession |
|
| 146 | - * |
|
| 147 | - * @param string $user |
|
| 148 | - * @param string $redirect_url |
|
| 149 | - * |
|
| 150 | - * @return TemplateResponse|RedirectResponse |
|
| 151 | - */ |
|
| 152 | - public function showLoginForm(string $user = null, string $redirect_url = null): Http\Response { |
|
| 153 | - if ($this->userSession->isLoggedIn()) { |
|
| 154 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $loginMessages = $this->session->get('loginMessages'); |
|
| 158 | - if (is_array($loginMessages)) { |
|
| 159 | - [$errors, $messages] = $loginMessages; |
|
| 160 | - $this->initialStateService->provideInitialState('core', 'loginMessages', $messages); |
|
| 161 | - $this->initialStateService->provideInitialState('core', 'loginErrors', $errors); |
|
| 162 | - } |
|
| 163 | - $this->session->remove('loginMessages'); |
|
| 164 | - |
|
| 165 | - if ($user !== null && $user !== '') { |
|
| 166 | - $this->initialStateService->provideInitialState('core', 'loginUsername', $user); |
|
| 167 | - } else { |
|
| 168 | - $this->initialStateService->provideInitialState('core', 'loginUsername', ''); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - $this->initialStateService->provideInitialState( |
|
| 172 | - 'core', |
|
| 173 | - 'loginAutocomplete', |
|
| 174 | - $this->config->getSystemValue('login_form_autocomplete', true) === true |
|
| 175 | - ); |
|
| 176 | - |
|
| 177 | - if (!empty($redirect_url)) { |
|
| 178 | - [$url, ] = explode('?', $redirect_url); |
|
| 179 | - if ($url !== $this->urlGenerator->linkToRoute('core.login.logout')) { |
|
| 180 | - $this->initialStateService->provideInitialState('core', 'loginRedirectUrl', $redirect_url); |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $this->initialStateService->provideInitialState( |
|
| 185 | - 'core', |
|
| 186 | - 'loginThrottleDelay', |
|
| 187 | - $this->throttler->getDelay($this->request->getRemoteAddress()) |
|
| 188 | - ); |
|
| 189 | - |
|
| 190 | - $this->setPasswordResetInitialState($user); |
|
| 191 | - |
|
| 192 | - $this->initialStateService->provideInitialState('core', 'webauthn-available', $this->webAuthnManager->isWebAuthnAvailable()); |
|
| 193 | - |
|
| 194 | - $this->initialStateService->provideInitialState('core', 'hideLoginForm', $this->config->getSystemValueBool('hide_login_form', false)); |
|
| 195 | - |
|
| 196 | - // OpenGraph Support: http://ogp.me/ |
|
| 197 | - Util::addHeader('meta', ['property' => 'og:title', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
| 198 | - Util::addHeader('meta', ['property' => 'og:description', 'content' => Util::sanitizeHTML($this->defaults->getSlogan())]); |
|
| 199 | - Util::addHeader('meta', ['property' => 'og:site_name', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
| 200 | - Util::addHeader('meta', ['property' => 'og:url', 'content' => $this->urlGenerator->getAbsoluteURL('/')]); |
|
| 201 | - Util::addHeader('meta', ['property' => 'og:type', 'content' => 'website']); |
|
| 202 | - Util::addHeader('meta', ['property' => 'og:image', 'content' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png'))]); |
|
| 203 | - |
|
| 204 | - $parameters = [ |
|
| 205 | - 'alt_login' => OC_App::getAlternativeLogIns(), |
|
| 206 | - ]; |
|
| 207 | - |
|
| 208 | - $this->initialStateService->provideInitialState('core', 'countAlternativeLogins', count($parameters['alt_login'])); |
|
| 209 | - |
|
| 210 | - return new TemplateResponse( |
|
| 211 | - $this->appName, 'login', $parameters, 'guest' |
|
| 212 | - ); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Sets the password reset state |
|
| 217 | - * |
|
| 218 | - * @param string $username |
|
| 219 | - */ |
|
| 220 | - private function setPasswordResetInitialState(?string $username): void { |
|
| 221 | - if ($username !== null && $username !== '') { |
|
| 222 | - $user = $this->userManager->get($username); |
|
| 223 | - } else { |
|
| 224 | - $user = null; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - $passwordLink = $this->config->getSystemValue('lost_password_link', ''); |
|
| 228 | - |
|
| 229 | - $this->initialStateService->provideInitialState( |
|
| 230 | - 'core', |
|
| 231 | - 'loginResetPasswordLink', |
|
| 232 | - $passwordLink |
|
| 233 | - ); |
|
| 234 | - |
|
| 235 | - $this->initialStateService->provideInitialState( |
|
| 236 | - 'core', |
|
| 237 | - 'loginCanResetPassword', |
|
| 238 | - $this->canResetPassword($passwordLink, $user) |
|
| 239 | - ); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * @param string|null $passwordLink |
|
| 244 | - * @param IUser|null $user |
|
| 245 | - * |
|
| 246 | - * Users may not change their passwords if: |
|
| 247 | - * - The account is disabled |
|
| 248 | - * - The backend doesn't support password resets |
|
| 249 | - * - The password reset function is disabled |
|
| 250 | - * |
|
| 251 | - * @return bool |
|
| 252 | - */ |
|
| 253 | - private function canResetPassword(?string $passwordLink, ?IUser $user): bool { |
|
| 254 | - if ($passwordLink === 'disabled') { |
|
| 255 | - return false; |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - if (!$passwordLink && $user !== null) { |
|
| 259 | - return $user->canChangePassword(); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - if ($user !== null && $user->isEnabled() === false) { |
|
| 263 | - return false; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - return true; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - private function generateRedirect(?string $redirectUrl): RedirectResponse { |
|
| 270 | - if ($redirectUrl !== null && $this->userSession->isLoggedIn()) { |
|
| 271 | - $location = $this->urlGenerator->getAbsoluteURL($redirectUrl); |
|
| 272 | - // Deny the redirect if the URL contains a @ |
|
| 273 | - // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
| 274 | - if (strpos($location, '@') === false) { |
|
| 275 | - return new RedirectResponse($location); |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * @PublicPage |
|
| 283 | - * @UseSession |
|
| 284 | - * @NoCSRFRequired |
|
| 285 | - * @BruteForceProtection(action=login) |
|
| 286 | - * |
|
| 287 | - * @param string $user |
|
| 288 | - * @param string $password |
|
| 289 | - * @param string $redirect_url |
|
| 290 | - * @param string $timezone |
|
| 291 | - * @param string $timezone_offset |
|
| 292 | - * |
|
| 293 | - * @return RedirectResponse |
|
| 294 | - */ |
|
| 295 | - public function tryLogin(string $user, |
|
| 296 | - string $password, |
|
| 297 | - string $redirect_url = null, |
|
| 298 | - string $timezone = '', |
|
| 299 | - string $timezone_offset = ''): RedirectResponse { |
|
| 300 | - // If the user is already logged in and the CSRF check does not pass then |
|
| 301 | - // simply redirect the user to the correct page as required. This is the |
|
| 302 | - // case when an user has already logged-in, in another tab. |
|
| 303 | - if (!$this->request->passesCSRFCheck()) { |
|
| 304 | - return $this->generateRedirect($redirect_url); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - $data = new LoginData( |
|
| 308 | - $this->request, |
|
| 309 | - trim($user), |
|
| 310 | - $password, |
|
| 311 | - $redirect_url, |
|
| 312 | - $timezone, |
|
| 313 | - $timezone_offset |
|
| 314 | - ); |
|
| 315 | - $result = $this->loginChain->process($data); |
|
| 316 | - if (!$result->isSuccess()) { |
|
| 317 | - return $this->createLoginFailedResponse( |
|
| 318 | - $data->getUsername(), |
|
| 319 | - $user, |
|
| 320 | - $redirect_url, |
|
| 321 | - $result->getErrorMessage() |
|
| 322 | - ); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - if ($result->getRedirectUrl() !== null) { |
|
| 326 | - return new RedirectResponse($result->getRedirectUrl()); |
|
| 327 | - } |
|
| 328 | - return $this->generateRedirect($redirect_url); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Creates a login failed response. |
|
| 333 | - * |
|
| 334 | - * @param string $user |
|
| 335 | - * @param string $originalUser |
|
| 336 | - * @param string $redirect_url |
|
| 337 | - * @param string $loginMessage |
|
| 338 | - * |
|
| 339 | - * @return RedirectResponse |
|
| 340 | - */ |
|
| 341 | - private function createLoginFailedResponse( |
|
| 342 | - $user, $originalUser, $redirect_url, string $loginMessage) { |
|
| 343 | - // Read current user and append if possible we need to |
|
| 344 | - // return the unmodified user otherwise we will leak the login name |
|
| 345 | - $args = $user !== null ? ['user' => $originalUser, 'direct' => 1] : []; |
|
| 346 | - if ($redirect_url !== null) { |
|
| 347 | - $args['redirect_url'] = $redirect_url; |
|
| 348 | - } |
|
| 349 | - $response = new RedirectResponse( |
|
| 350 | - $this->urlGenerator->linkToRoute('core.login.showLoginForm', $args) |
|
| 351 | - ); |
|
| 352 | - $response->throttle(['user' => substr($user, 0, 64)]); |
|
| 353 | - $this->session->set('loginMessages', [ |
|
| 354 | - [$loginMessage], [] |
|
| 355 | - ]); |
|
| 356 | - return $response; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * @NoAdminRequired |
|
| 361 | - * @UseSession |
|
| 362 | - * @BruteForceProtection(action=sudo) |
|
| 363 | - * |
|
| 364 | - * @param string $password |
|
| 365 | - * |
|
| 366 | - * @return DataResponse |
|
| 367 | - * @license GNU AGPL version 3 or any later version |
|
| 368 | - * |
|
| 369 | - */ |
|
| 370 | - public function confirmPassword($password) { |
|
| 371 | - $loginName = $this->userSession->getLoginName(); |
|
| 372 | - $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
| 373 | - if ($loginResult === false) { |
|
| 374 | - $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 375 | - $response->throttle(); |
|
| 376 | - return $response; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - $confirmTimestamp = time(); |
|
| 380 | - $this->session->set('last-password-confirm', $confirmTimestamp); |
|
| 381 | - return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
| 382 | - } |
|
| 61 | + public const LOGIN_MSG_INVALIDPASSWORD = 'invalidpassword'; |
|
| 62 | + public const LOGIN_MSG_USERDISABLED = 'userdisabled'; |
|
| 63 | + |
|
| 64 | + /** @var IUserManager */ |
|
| 65 | + private $userManager; |
|
| 66 | + /** @var IConfig */ |
|
| 67 | + private $config; |
|
| 68 | + /** @var ISession */ |
|
| 69 | + private $session; |
|
| 70 | + /** @var IUserSession|Session */ |
|
| 71 | + private $userSession; |
|
| 72 | + /** @var IURLGenerator */ |
|
| 73 | + private $urlGenerator; |
|
| 74 | + /** @var ILogger */ |
|
| 75 | + private $logger; |
|
| 76 | + /** @var Defaults */ |
|
| 77 | + private $defaults; |
|
| 78 | + /** @var Throttler */ |
|
| 79 | + private $throttler; |
|
| 80 | + /** @var Chain */ |
|
| 81 | + private $loginChain; |
|
| 82 | + /** @var IInitialStateService */ |
|
| 83 | + private $initialStateService; |
|
| 84 | + /** @var WebAuthnManager */ |
|
| 85 | + private $webAuthnManager; |
|
| 86 | + |
|
| 87 | + public function __construct(?string $appName, |
|
| 88 | + IRequest $request, |
|
| 89 | + IUserManager $userManager, |
|
| 90 | + IConfig $config, |
|
| 91 | + ISession $session, |
|
| 92 | + IUserSession $userSession, |
|
| 93 | + IURLGenerator $urlGenerator, |
|
| 94 | + ILogger $logger, |
|
| 95 | + Defaults $defaults, |
|
| 96 | + Throttler $throttler, |
|
| 97 | + Chain $loginChain, |
|
| 98 | + IInitialStateService $initialStateService, |
|
| 99 | + WebAuthnManager $webAuthnManager) { |
|
| 100 | + parent::__construct($appName, $request); |
|
| 101 | + $this->userManager = $userManager; |
|
| 102 | + $this->config = $config; |
|
| 103 | + $this->session = $session; |
|
| 104 | + $this->userSession = $userSession; |
|
| 105 | + $this->urlGenerator = $urlGenerator; |
|
| 106 | + $this->logger = $logger; |
|
| 107 | + $this->defaults = $defaults; |
|
| 108 | + $this->throttler = $throttler; |
|
| 109 | + $this->loginChain = $loginChain; |
|
| 110 | + $this->initialStateService = $initialStateService; |
|
| 111 | + $this->webAuthnManager = $webAuthnManager; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @NoAdminRequired |
|
| 116 | + * @UseSession |
|
| 117 | + * |
|
| 118 | + * @return RedirectResponse |
|
| 119 | + */ |
|
| 120 | + public function logout() { |
|
| 121 | + $loginToken = $this->request->getCookie('nc_token'); |
|
| 122 | + if (!is_null($loginToken)) { |
|
| 123 | + $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
| 124 | + } |
|
| 125 | + $this->userSession->logout(); |
|
| 126 | + |
|
| 127 | + $response = new RedirectResponse($this->urlGenerator->linkToRouteAbsolute( |
|
| 128 | + 'core.login.showLoginForm', |
|
| 129 | + ['clear' => true] // this param the the code in login.js may be removed when the "Clear-Site-Data" is working in the browsers |
|
| 130 | + )); |
|
| 131 | + |
|
| 132 | + $this->session->set('clearingExecutionContexts', '1'); |
|
| 133 | + $this->session->close(); |
|
| 134 | + |
|
| 135 | + if (!$this->request->isUserAgent([Request::USER_AGENT_CHROME, Request::USER_AGENT_ANDROID_MOBILE_CHROME])) { |
|
| 136 | + $response->addHeader('Clear-Site-Data', '"cache", "storage"'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $response; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @PublicPage |
|
| 144 | + * @NoCSRFRequired |
|
| 145 | + * @UseSession |
|
| 146 | + * |
|
| 147 | + * @param string $user |
|
| 148 | + * @param string $redirect_url |
|
| 149 | + * |
|
| 150 | + * @return TemplateResponse|RedirectResponse |
|
| 151 | + */ |
|
| 152 | + public function showLoginForm(string $user = null, string $redirect_url = null): Http\Response { |
|
| 153 | + if ($this->userSession->isLoggedIn()) { |
|
| 154 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $loginMessages = $this->session->get('loginMessages'); |
|
| 158 | + if (is_array($loginMessages)) { |
|
| 159 | + [$errors, $messages] = $loginMessages; |
|
| 160 | + $this->initialStateService->provideInitialState('core', 'loginMessages', $messages); |
|
| 161 | + $this->initialStateService->provideInitialState('core', 'loginErrors', $errors); |
|
| 162 | + } |
|
| 163 | + $this->session->remove('loginMessages'); |
|
| 164 | + |
|
| 165 | + if ($user !== null && $user !== '') { |
|
| 166 | + $this->initialStateService->provideInitialState('core', 'loginUsername', $user); |
|
| 167 | + } else { |
|
| 168 | + $this->initialStateService->provideInitialState('core', 'loginUsername', ''); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + $this->initialStateService->provideInitialState( |
|
| 172 | + 'core', |
|
| 173 | + 'loginAutocomplete', |
|
| 174 | + $this->config->getSystemValue('login_form_autocomplete', true) === true |
|
| 175 | + ); |
|
| 176 | + |
|
| 177 | + if (!empty($redirect_url)) { |
|
| 178 | + [$url, ] = explode('?', $redirect_url); |
|
| 179 | + if ($url !== $this->urlGenerator->linkToRoute('core.login.logout')) { |
|
| 180 | + $this->initialStateService->provideInitialState('core', 'loginRedirectUrl', $redirect_url); |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $this->initialStateService->provideInitialState( |
|
| 185 | + 'core', |
|
| 186 | + 'loginThrottleDelay', |
|
| 187 | + $this->throttler->getDelay($this->request->getRemoteAddress()) |
|
| 188 | + ); |
|
| 189 | + |
|
| 190 | + $this->setPasswordResetInitialState($user); |
|
| 191 | + |
|
| 192 | + $this->initialStateService->provideInitialState('core', 'webauthn-available', $this->webAuthnManager->isWebAuthnAvailable()); |
|
| 193 | + |
|
| 194 | + $this->initialStateService->provideInitialState('core', 'hideLoginForm', $this->config->getSystemValueBool('hide_login_form', false)); |
|
| 195 | + |
|
| 196 | + // OpenGraph Support: http://ogp.me/ |
|
| 197 | + Util::addHeader('meta', ['property' => 'og:title', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
| 198 | + Util::addHeader('meta', ['property' => 'og:description', 'content' => Util::sanitizeHTML($this->defaults->getSlogan())]); |
|
| 199 | + Util::addHeader('meta', ['property' => 'og:site_name', 'content' => Util::sanitizeHTML($this->defaults->getName())]); |
|
| 200 | + Util::addHeader('meta', ['property' => 'og:url', 'content' => $this->urlGenerator->getAbsoluteURL('/')]); |
|
| 201 | + Util::addHeader('meta', ['property' => 'og:type', 'content' => 'website']); |
|
| 202 | + Util::addHeader('meta', ['property' => 'og:image', 'content' => $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-touch.png'))]); |
|
| 203 | + |
|
| 204 | + $parameters = [ |
|
| 205 | + 'alt_login' => OC_App::getAlternativeLogIns(), |
|
| 206 | + ]; |
|
| 207 | + |
|
| 208 | + $this->initialStateService->provideInitialState('core', 'countAlternativeLogins', count($parameters['alt_login'])); |
|
| 209 | + |
|
| 210 | + return new TemplateResponse( |
|
| 211 | + $this->appName, 'login', $parameters, 'guest' |
|
| 212 | + ); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Sets the password reset state |
|
| 217 | + * |
|
| 218 | + * @param string $username |
|
| 219 | + */ |
|
| 220 | + private function setPasswordResetInitialState(?string $username): void { |
|
| 221 | + if ($username !== null && $username !== '') { |
|
| 222 | + $user = $this->userManager->get($username); |
|
| 223 | + } else { |
|
| 224 | + $user = null; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + $passwordLink = $this->config->getSystemValue('lost_password_link', ''); |
|
| 228 | + |
|
| 229 | + $this->initialStateService->provideInitialState( |
|
| 230 | + 'core', |
|
| 231 | + 'loginResetPasswordLink', |
|
| 232 | + $passwordLink |
|
| 233 | + ); |
|
| 234 | + |
|
| 235 | + $this->initialStateService->provideInitialState( |
|
| 236 | + 'core', |
|
| 237 | + 'loginCanResetPassword', |
|
| 238 | + $this->canResetPassword($passwordLink, $user) |
|
| 239 | + ); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * @param string|null $passwordLink |
|
| 244 | + * @param IUser|null $user |
|
| 245 | + * |
|
| 246 | + * Users may not change their passwords if: |
|
| 247 | + * - The account is disabled |
|
| 248 | + * - The backend doesn't support password resets |
|
| 249 | + * - The password reset function is disabled |
|
| 250 | + * |
|
| 251 | + * @return bool |
|
| 252 | + */ |
|
| 253 | + private function canResetPassword(?string $passwordLink, ?IUser $user): bool { |
|
| 254 | + if ($passwordLink === 'disabled') { |
|
| 255 | + return false; |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + if (!$passwordLink && $user !== null) { |
|
| 259 | + return $user->canChangePassword(); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + if ($user !== null && $user->isEnabled() === false) { |
|
| 263 | + return false; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + return true; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + private function generateRedirect(?string $redirectUrl): RedirectResponse { |
|
| 270 | + if ($redirectUrl !== null && $this->userSession->isLoggedIn()) { |
|
| 271 | + $location = $this->urlGenerator->getAbsoluteURL($redirectUrl); |
|
| 272 | + // Deny the redirect if the URL contains a @ |
|
| 273 | + // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
| 274 | + if (strpos($location, '@') === false) { |
|
| 275 | + return new RedirectResponse($location); |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * @PublicPage |
|
| 283 | + * @UseSession |
|
| 284 | + * @NoCSRFRequired |
|
| 285 | + * @BruteForceProtection(action=login) |
|
| 286 | + * |
|
| 287 | + * @param string $user |
|
| 288 | + * @param string $password |
|
| 289 | + * @param string $redirect_url |
|
| 290 | + * @param string $timezone |
|
| 291 | + * @param string $timezone_offset |
|
| 292 | + * |
|
| 293 | + * @return RedirectResponse |
|
| 294 | + */ |
|
| 295 | + public function tryLogin(string $user, |
|
| 296 | + string $password, |
|
| 297 | + string $redirect_url = null, |
|
| 298 | + string $timezone = '', |
|
| 299 | + string $timezone_offset = ''): RedirectResponse { |
|
| 300 | + // If the user is already logged in and the CSRF check does not pass then |
|
| 301 | + // simply redirect the user to the correct page as required. This is the |
|
| 302 | + // case when an user has already logged-in, in another tab. |
|
| 303 | + if (!$this->request->passesCSRFCheck()) { |
|
| 304 | + return $this->generateRedirect($redirect_url); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + $data = new LoginData( |
|
| 308 | + $this->request, |
|
| 309 | + trim($user), |
|
| 310 | + $password, |
|
| 311 | + $redirect_url, |
|
| 312 | + $timezone, |
|
| 313 | + $timezone_offset |
|
| 314 | + ); |
|
| 315 | + $result = $this->loginChain->process($data); |
|
| 316 | + if (!$result->isSuccess()) { |
|
| 317 | + return $this->createLoginFailedResponse( |
|
| 318 | + $data->getUsername(), |
|
| 319 | + $user, |
|
| 320 | + $redirect_url, |
|
| 321 | + $result->getErrorMessage() |
|
| 322 | + ); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + if ($result->getRedirectUrl() !== null) { |
|
| 326 | + return new RedirectResponse($result->getRedirectUrl()); |
|
| 327 | + } |
|
| 328 | + return $this->generateRedirect($redirect_url); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Creates a login failed response. |
|
| 333 | + * |
|
| 334 | + * @param string $user |
|
| 335 | + * @param string $originalUser |
|
| 336 | + * @param string $redirect_url |
|
| 337 | + * @param string $loginMessage |
|
| 338 | + * |
|
| 339 | + * @return RedirectResponse |
|
| 340 | + */ |
|
| 341 | + private function createLoginFailedResponse( |
|
| 342 | + $user, $originalUser, $redirect_url, string $loginMessage) { |
|
| 343 | + // Read current user and append if possible we need to |
|
| 344 | + // return the unmodified user otherwise we will leak the login name |
|
| 345 | + $args = $user !== null ? ['user' => $originalUser, 'direct' => 1] : []; |
|
| 346 | + if ($redirect_url !== null) { |
|
| 347 | + $args['redirect_url'] = $redirect_url; |
|
| 348 | + } |
|
| 349 | + $response = new RedirectResponse( |
|
| 350 | + $this->urlGenerator->linkToRoute('core.login.showLoginForm', $args) |
|
| 351 | + ); |
|
| 352 | + $response->throttle(['user' => substr($user, 0, 64)]); |
|
| 353 | + $this->session->set('loginMessages', [ |
|
| 354 | + [$loginMessage], [] |
|
| 355 | + ]); |
|
| 356 | + return $response; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * @NoAdminRequired |
|
| 361 | + * @UseSession |
|
| 362 | + * @BruteForceProtection(action=sudo) |
|
| 363 | + * |
|
| 364 | + * @param string $password |
|
| 365 | + * |
|
| 366 | + * @return DataResponse |
|
| 367 | + * @license GNU AGPL version 3 or any later version |
|
| 368 | + * |
|
| 369 | + */ |
|
| 370 | + public function confirmPassword($password) { |
|
| 371 | + $loginName = $this->userSession->getLoginName(); |
|
| 372 | + $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
| 373 | + if ($loginResult === false) { |
|
| 374 | + $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 375 | + $response->throttle(); |
|
| 376 | + return $response; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + $confirmTimestamp = time(); |
|
| 380 | + $this->session->set('last-password-confirm', $confirmTimestamp); |
|
| 381 | + return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
| 382 | + } |
|
| 383 | 383 | } |