isAuthorizedBackendSession()) { $result = ['success' => true]; if ($this->hasLoginBeenProcessed()) { /** @var \TYPO3\CMS\Core\FormProtection\BackendFormProtection $formProtection */ $formProtection = FormProtectionFactory::get(); $formProtection->setSessionTokenFromRegistry(); $formProtection->persistSessionToken(); } } else { $result = ['success' => false]; } return new JsonResponse(['login' => $result]); } /** * Logs out the current BE user * * @param ServerRequestInterface $request * @return ResponseInterface */ public function logoutAction(ServerRequestInterface $request): ResponseInterface { $backendUser = $this->getBackendUser(); $backendUser->logoff(); return new JsonResponse([ 'logout' => [ 'success' => !isset($backendUser->user['uid']) ] ]); } public function preflightAction(ServerRequestInterface $request): ResponseInterface { $headers = $request->getHeaders(); return new JsonResponse([ 'capabilities' => [ 'cookie' => !empty($request->getCookieParams()), // using legacy `Referer` (sic!) header name 'referrer' => array_filter($headers['referer'] ?? []) !== [], ], ]); } /** * Refreshes the login without needing login information. We just refresh the session. * * @param ServerRequestInterface $request * @return ResponseInterface */ public function refreshAction(ServerRequestInterface $request): ResponseInterface { $this->getBackendUser()->checkAuthentication(); return new JsonResponse([ 'refresh' => [ 'success' => true ] ]); } /** * Checks if the user session is expired yet * * @param ServerRequestInterface $request * @return ResponseInterface */ public function isTimedOutAction(ServerRequestInterface $request): ResponseInterface { $session = [ 'timed_out' => false, 'will_time_out' => false, 'locked' => false ]; $backendUser = $this->getBackendUser(); if (@is_file(Environment::getLegacyConfigPath() . '/LOCK_BACKEND')) { $session['locked'] = true; } elseif (!isset($backendUser->user['uid'])) { $session['timed_out'] = true; } else { $backendUser->fetchUserSession(true); $ses_tstamp = $backendUser->user['ses_tstamp']; $timeout = $backendUser->sessionTimeout; // If 120 seconds from now is later than the session timeout, we need to show the refresh dialog. // 120 is somewhat arbitrary to allow for a little room during the countdown and load times, etc. $session['will_time_out'] = $GLOBALS['EXEC_TIME'] >= $ses_tstamp + $timeout - 120; } return new JsonResponse(['login' => $session]); } /** * Checks if a user is logged in and the session is active. * * @return bool */ protected function isAuthorizedBackendSession() { $backendUser = $this->getBackendUser(); return $backendUser !== null && $backendUser instanceof BackendUserAuthentication && isset($backendUser->user['uid']); } /** * Check whether the user was already authorized or not * * @return bool */ protected function hasLoginBeenProcessed() { $loginFormData = $this->getBackendUser()->getLoginFormData(); return $loginFormData['status'] === 'login' && !empty($loginFormData['uname']) && !empty($loginFormData['uident']); } /** * @return BackendUserAuthentication|null */ protected function getBackendUser() { return $GLOBALS['BE_USER'] ?? null; } }