@@ -49,278 +49,278 @@ |
||
| 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 | - $response = new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
| 111 | - $response->addHeader('Clear-Site-Data', '"cache", "cookies", "storage", "executionContexts"'); |
|
| 112 | - return $response; |
|
| 113 | - } |
|
| 110 | + $response = new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
| 111 | + $response->addHeader('Clear-Site-Data', '"cache", "cookies", "storage", "executionContexts"'); |
|
| 112 | + return $response; |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * @PublicPage |
|
| 117 | - * @NoCSRFRequired |
|
| 118 | - * @UseSession |
|
| 119 | - * |
|
| 120 | - * @param string $user |
|
| 121 | - * @param string $redirect_url |
|
| 122 | - * @param string $remember_login |
|
| 123 | - * |
|
| 124 | - * @return TemplateResponse|RedirectResponse |
|
| 125 | - */ |
|
| 126 | - public function showLoginForm($user, $redirect_url, $remember_login) { |
|
| 127 | - if ($this->userSession->isLoggedIn()) { |
|
| 128 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 129 | - } |
|
| 115 | + /** |
|
| 116 | + * @PublicPage |
|
| 117 | + * @NoCSRFRequired |
|
| 118 | + * @UseSession |
|
| 119 | + * |
|
| 120 | + * @param string $user |
|
| 121 | + * @param string $redirect_url |
|
| 122 | + * @param string $remember_login |
|
| 123 | + * |
|
| 124 | + * @return TemplateResponse|RedirectResponse |
|
| 125 | + */ |
|
| 126 | + public function showLoginForm($user, $redirect_url, $remember_login) { |
|
| 127 | + if ($this->userSession->isLoggedIn()) { |
|
| 128 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - $parameters = array(); |
|
| 132 | - $loginMessages = $this->session->get('loginMessages'); |
|
| 133 | - $errors = []; |
|
| 134 | - $messages = []; |
|
| 135 | - if (is_array($loginMessages)) { |
|
| 136 | - list($errors, $messages) = $loginMessages; |
|
| 137 | - } |
|
| 138 | - $this->session->remove('loginMessages'); |
|
| 139 | - foreach ($errors as $value) { |
|
| 140 | - $parameters[$value] = true; |
|
| 141 | - } |
|
| 131 | + $parameters = array(); |
|
| 132 | + $loginMessages = $this->session->get('loginMessages'); |
|
| 133 | + $errors = []; |
|
| 134 | + $messages = []; |
|
| 135 | + if (is_array($loginMessages)) { |
|
| 136 | + list($errors, $messages) = $loginMessages; |
|
| 137 | + } |
|
| 138 | + $this->session->remove('loginMessages'); |
|
| 139 | + foreach ($errors as $value) { |
|
| 140 | + $parameters[$value] = true; |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - $parameters['messages'] = $messages; |
|
| 144 | - if (!is_null($user) && $user !== '') { |
|
| 145 | - $parameters['loginName'] = $user; |
|
| 146 | - $parameters['user_autofocus'] = false; |
|
| 147 | - } else { |
|
| 148 | - $parameters['loginName'] = ''; |
|
| 149 | - $parameters['user_autofocus'] = true; |
|
| 150 | - } |
|
| 151 | - if (!empty($redirect_url)) { |
|
| 152 | - $parameters['redirect_url'] = $redirect_url; |
|
| 153 | - } |
|
| 143 | + $parameters['messages'] = $messages; |
|
| 144 | + if (!is_null($user) && $user !== '') { |
|
| 145 | + $parameters['loginName'] = $user; |
|
| 146 | + $parameters['user_autofocus'] = false; |
|
| 147 | + } else { |
|
| 148 | + $parameters['loginName'] = ''; |
|
| 149 | + $parameters['user_autofocus'] = true; |
|
| 150 | + } |
|
| 151 | + if (!empty($redirect_url)) { |
|
| 152 | + $parameters['redirect_url'] = $redirect_url; |
|
| 153 | + } |
|
| 154 | 154 | |
| 155 | - $parameters['canResetPassword'] = true; |
|
| 156 | - $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', ''); |
|
| 157 | - if (!$parameters['resetPasswordLink']) { |
|
| 158 | - if (!is_null($user) && $user !== '') { |
|
| 159 | - $userObj = $this->userManager->get($user); |
|
| 160 | - if ($userObj instanceof IUser) { |
|
| 161 | - $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - } elseif ($parameters['resetPasswordLink'] === 'disabled') { |
|
| 165 | - $parameters['canResetPassword'] = false; |
|
| 166 | - } |
|
| 155 | + $parameters['canResetPassword'] = true; |
|
| 156 | + $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', ''); |
|
| 157 | + if (!$parameters['resetPasswordLink']) { |
|
| 158 | + if (!is_null($user) && $user !== '') { |
|
| 159 | + $userObj = $this->userManager->get($user); |
|
| 160 | + if ($userObj instanceof IUser) { |
|
| 161 | + $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + } elseif ($parameters['resetPasswordLink'] === 'disabled') { |
|
| 165 | + $parameters['canResetPassword'] = false; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
| 169 | - $parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0; |
|
| 168 | + $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
| 169 | + $parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0; |
|
| 170 | 170 | |
| 171 | - if (!is_null($user) && $user !== '') { |
|
| 172 | - $parameters['loginName'] = $user; |
|
| 173 | - $parameters['user_autofocus'] = false; |
|
| 174 | - } else { |
|
| 175 | - $parameters['loginName'] = ''; |
|
| 176 | - $parameters['user_autofocus'] = true; |
|
| 177 | - } |
|
| 171 | + if (!is_null($user) && $user !== '') { |
|
| 172 | + $parameters['loginName'] = $user; |
|
| 173 | + $parameters['user_autofocus'] = false; |
|
| 174 | + } else { |
|
| 175 | + $parameters['loginName'] = ''; |
|
| 176 | + $parameters['user_autofocus'] = true; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - return new TemplateResponse( |
|
| 180 | - $this->appName, 'login', $parameters, 'guest' |
|
| 181 | - ); |
|
| 182 | - } |
|
| 179 | + return new TemplateResponse( |
|
| 180 | + $this->appName, 'login', $parameters, 'guest' |
|
| 181 | + ); |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * @param string $redirectUrl |
|
| 186 | - * @return RedirectResponse |
|
| 187 | - */ |
|
| 188 | - private function generateRedirect($redirectUrl) { |
|
| 189 | - if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
| 190 | - $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
| 191 | - // Deny the redirect if the URL contains a @ |
|
| 192 | - // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
| 193 | - if (strpos($location, '@') === false) { |
|
| 194 | - return new RedirectResponse($location); |
|
| 195 | - } |
|
| 196 | - } |
|
| 197 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 198 | - } |
|
| 184 | + /** |
|
| 185 | + * @param string $redirectUrl |
|
| 186 | + * @return RedirectResponse |
|
| 187 | + */ |
|
| 188 | + private function generateRedirect($redirectUrl) { |
|
| 189 | + if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
| 190 | + $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
| 191 | + // Deny the redirect if the URL contains a @ |
|
| 192 | + // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
| 193 | + if (strpos($location, '@') === false) { |
|
| 194 | + return new RedirectResponse($location); |
|
| 195 | + } |
|
| 196 | + } |
|
| 197 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - /** |
|
| 201 | - * @PublicPage |
|
| 202 | - * @UseSession |
|
| 203 | - * @NoCSRFRequired |
|
| 204 | - * @BruteForceProtection(action=login) |
|
| 205 | - * |
|
| 206 | - * @param string $user |
|
| 207 | - * @param string $password |
|
| 208 | - * @param string $redirect_url |
|
| 209 | - * @param boolean $remember_login |
|
| 210 | - * @param string $timezone |
|
| 211 | - * @param string $timezone_offset |
|
| 212 | - * @return RedirectResponse |
|
| 213 | - */ |
|
| 214 | - public function tryLogin($user, $password, $redirect_url, $remember_login = false, $timezone = '', $timezone_offset = '') { |
|
| 215 | - if(!is_string($user)) { |
|
| 216 | - throw new \InvalidArgumentException('Username must be string'); |
|
| 217 | - } |
|
| 200 | + /** |
|
| 201 | + * @PublicPage |
|
| 202 | + * @UseSession |
|
| 203 | + * @NoCSRFRequired |
|
| 204 | + * @BruteForceProtection(action=login) |
|
| 205 | + * |
|
| 206 | + * @param string $user |
|
| 207 | + * @param string $password |
|
| 208 | + * @param string $redirect_url |
|
| 209 | + * @param boolean $remember_login |
|
| 210 | + * @param string $timezone |
|
| 211 | + * @param string $timezone_offset |
|
| 212 | + * @return RedirectResponse |
|
| 213 | + */ |
|
| 214 | + public function tryLogin($user, $password, $redirect_url, $remember_login = false, $timezone = '', $timezone_offset = '') { |
|
| 215 | + if(!is_string($user)) { |
|
| 216 | + throw new \InvalidArgumentException('Username must be string'); |
|
| 217 | + } |
|
| 218 | 218 | |
| 219 | - // If the user is already logged in and the CSRF check does not pass then |
|
| 220 | - // simply redirect the user to the correct page as required. This is the |
|
| 221 | - // case when an user has already logged-in, in another tab. |
|
| 222 | - if(!$this->request->passesCSRFCheck()) { |
|
| 223 | - return $this->generateRedirect($redirect_url); |
|
| 224 | - } |
|
| 219 | + // If the user is already logged in and the CSRF check does not pass then |
|
| 220 | + // simply redirect the user to the correct page as required. This is the |
|
| 221 | + // case when an user has already logged-in, in another tab. |
|
| 222 | + if(!$this->request->passesCSRFCheck()) { |
|
| 223 | + return $this->generateRedirect($redirect_url); |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - if ($this->userManager instanceof PublicEmitter) { |
|
| 227 | - $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
| 228 | - } |
|
| 226 | + if ($this->userManager instanceof PublicEmitter) { |
|
| 227 | + $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - $originalUser = $user; |
|
| 231 | - // TODO: Add all the insane error handling |
|
| 232 | - /* @var $loginResult IUser */ |
|
| 233 | - $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
| 234 | - if ($loginResult === false) { |
|
| 235 | - $users = $this->userManager->getByEmail($user); |
|
| 236 | - // we only allow login by email if unique |
|
| 237 | - if (count($users) === 1) { |
|
| 238 | - $user = $users[0]->getUID(); |
|
| 239 | - $loginResult = $this->userManager->checkPassword($user, $password); |
|
| 240 | - } else { |
|
| 241 | - $this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - if ($loginResult === false) { |
|
| 245 | - // Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name |
|
| 246 | - $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
| 247 | - if (!is_null($redirect_url)) { |
|
| 248 | - $args['redirect_url'] = $redirect_url; |
|
| 249 | - } |
|
| 250 | - $response = new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args)); |
|
| 251 | - $response->throttle(['user' => $user]); |
|
| 252 | - $this->session->set('loginMessages', [ |
|
| 253 | - ['invalidpassword'], [] |
|
| 254 | - ]); |
|
| 255 | - return $response; |
|
| 256 | - } |
|
| 257 | - // TODO: remove password checks from above and let the user session handle failures |
|
| 258 | - // requires https://github.com/owncloud/core/pull/24616 |
|
| 259 | - $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
| 260 | - $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, (int)$remember_login); |
|
| 230 | + $originalUser = $user; |
|
| 231 | + // TODO: Add all the insane error handling |
|
| 232 | + /* @var $loginResult IUser */ |
|
| 233 | + $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
| 234 | + if ($loginResult === false) { |
|
| 235 | + $users = $this->userManager->getByEmail($user); |
|
| 236 | + // we only allow login by email if unique |
|
| 237 | + if (count($users) === 1) { |
|
| 238 | + $user = $users[0]->getUID(); |
|
| 239 | + $loginResult = $this->userManager->checkPassword($user, $password); |
|
| 240 | + } else { |
|
| 241 | + $this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + if ($loginResult === false) { |
|
| 245 | + // Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name |
|
| 246 | + $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
| 247 | + if (!is_null($redirect_url)) { |
|
| 248 | + $args['redirect_url'] = $redirect_url; |
|
| 249 | + } |
|
| 250 | + $response = new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args)); |
|
| 251 | + $response->throttle(['user' => $user]); |
|
| 252 | + $this->session->set('loginMessages', [ |
|
| 253 | + ['invalidpassword'], [] |
|
| 254 | + ]); |
|
| 255 | + return $response; |
|
| 256 | + } |
|
| 257 | + // TODO: remove password checks from above and let the user session handle failures |
|
| 258 | + // requires https://github.com/owncloud/core/pull/24616 |
|
| 259 | + $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
| 260 | + $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, (int)$remember_login); |
|
| 261 | 261 | |
| 262 | - // User has successfully logged in, now remove the password reset link, when it is available |
|
| 263 | - $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
| 262 | + // User has successfully logged in, now remove the password reset link, when it is available |
|
| 263 | + $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
| 264 | 264 | |
| 265 | - $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
| 265 | + $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
| 266 | 266 | |
| 267 | - if ($timezone_offset !== '') { |
|
| 268 | - $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
| 269 | - $this->session->set('timezone', $timezone_offset); |
|
| 270 | - } |
|
| 267 | + if ($timezone_offset !== '') { |
|
| 268 | + $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
| 269 | + $this->session->set('timezone', $timezone_offset); |
|
| 270 | + } |
|
| 271 | 271 | |
| 272 | - if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
| 273 | - $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
| 272 | + if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
| 273 | + $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
| 274 | 274 | |
| 275 | - $providers = $this->twoFactorManager->getProviders($loginResult); |
|
| 276 | - if (count($providers) === 1) { |
|
| 277 | - // Single provider, hence we can redirect to that provider's challenge page directly |
|
| 278 | - /* @var $provider IProvider */ |
|
| 279 | - $provider = array_pop($providers); |
|
| 280 | - $url = 'core.TwoFactorChallenge.showChallenge'; |
|
| 281 | - $urlParams = [ |
|
| 282 | - 'challengeProviderId' => $provider->getId(), |
|
| 283 | - ]; |
|
| 284 | - } else { |
|
| 285 | - $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
| 286 | - $urlParams = []; |
|
| 287 | - } |
|
| 275 | + $providers = $this->twoFactorManager->getProviders($loginResult); |
|
| 276 | + if (count($providers) === 1) { |
|
| 277 | + // Single provider, hence we can redirect to that provider's challenge page directly |
|
| 278 | + /* @var $provider IProvider */ |
|
| 279 | + $provider = array_pop($providers); |
|
| 280 | + $url = 'core.TwoFactorChallenge.showChallenge'; |
|
| 281 | + $urlParams = [ |
|
| 282 | + 'challengeProviderId' => $provider->getId(), |
|
| 283 | + ]; |
|
| 284 | + } else { |
|
| 285 | + $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
| 286 | + $urlParams = []; |
|
| 287 | + } |
|
| 288 | 288 | |
| 289 | - if (!is_null($redirect_url)) { |
|
| 290 | - $urlParams['redirect_url'] = $redirect_url; |
|
| 291 | - } |
|
| 289 | + if (!is_null($redirect_url)) { |
|
| 290 | + $urlParams['redirect_url'] = $redirect_url; |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
| 294 | - } |
|
| 293 | + return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | - if ($remember_login) { |
|
| 297 | - $this->userSession->createRememberMeToken($loginResult); |
|
| 298 | - } |
|
| 296 | + if ($remember_login) { |
|
| 297 | + $this->userSession->createRememberMeToken($loginResult); |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | - return $this->generateRedirect($redirect_url); |
|
| 301 | - } |
|
| 300 | + return $this->generateRedirect($redirect_url); |
|
| 301 | + } |
|
| 302 | 302 | |
| 303 | - /** |
|
| 304 | - * @NoAdminRequired |
|
| 305 | - * @UseSession |
|
| 306 | - * @BruteForceProtection(action=sudo) |
|
| 307 | - * |
|
| 308 | - * @license GNU AGPL version 3 or any later version |
|
| 309 | - * |
|
| 310 | - * @param string $password |
|
| 311 | - * @return DataResponse |
|
| 312 | - */ |
|
| 313 | - public function confirmPassword($password) { |
|
| 314 | - $loginName = $this->userSession->getLoginName(); |
|
| 315 | - $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
| 316 | - if ($loginResult === false) { |
|
| 317 | - $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 318 | - $response->throttle(); |
|
| 319 | - return $response; |
|
| 320 | - } |
|
| 303 | + /** |
|
| 304 | + * @NoAdminRequired |
|
| 305 | + * @UseSession |
|
| 306 | + * @BruteForceProtection(action=sudo) |
|
| 307 | + * |
|
| 308 | + * @license GNU AGPL version 3 or any later version |
|
| 309 | + * |
|
| 310 | + * @param string $password |
|
| 311 | + * @return DataResponse |
|
| 312 | + */ |
|
| 313 | + public function confirmPassword($password) { |
|
| 314 | + $loginName = $this->userSession->getLoginName(); |
|
| 315 | + $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
| 316 | + if ($loginResult === false) { |
|
| 317 | + $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
| 318 | + $response->throttle(); |
|
| 319 | + return $response; |
|
| 320 | + } |
|
| 321 | 321 | |
| 322 | - $confirmTimestamp = time(); |
|
| 323 | - $this->session->set('last-password-confirm', $confirmTimestamp); |
|
| 324 | - return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
| 325 | - } |
|
| 322 | + $confirmTimestamp = time(); |
|
| 323 | + $this->session->set('last-password-confirm', $confirmTimestamp); |
|
| 324 | + return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
| 325 | + } |
|
| 326 | 326 | } |
@@ -35,49 +35,49 @@ |
||
| 35 | 35 | * @package OC\AppFramework\Middleware\Security |
| 36 | 36 | */ |
| 37 | 37 | class BruteForceMiddleware extends Middleware { |
| 38 | - /** @var ControllerMethodReflector */ |
|
| 39 | - private $reflector; |
|
| 40 | - /** @var Throttler */ |
|
| 41 | - private $throttler; |
|
| 42 | - /** @var IRequest */ |
|
| 43 | - private $request; |
|
| 38 | + /** @var ControllerMethodReflector */ |
|
| 39 | + private $reflector; |
|
| 40 | + /** @var Throttler */ |
|
| 41 | + private $throttler; |
|
| 42 | + /** @var IRequest */ |
|
| 43 | + private $request; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @param ControllerMethodReflector $controllerMethodReflector |
|
| 47 | - * @param Throttler $throttler |
|
| 48 | - * @param IRequest $request |
|
| 49 | - */ |
|
| 50 | - public function __construct(ControllerMethodReflector $controllerMethodReflector, |
|
| 51 | - Throttler $throttler, |
|
| 52 | - IRequest $request) { |
|
| 53 | - $this->reflector = $controllerMethodReflector; |
|
| 54 | - $this->throttler = $throttler; |
|
| 55 | - $this->request = $request; |
|
| 56 | - } |
|
| 45 | + /** |
|
| 46 | + * @param ControllerMethodReflector $controllerMethodReflector |
|
| 47 | + * @param Throttler $throttler |
|
| 48 | + * @param IRequest $request |
|
| 49 | + */ |
|
| 50 | + public function __construct(ControllerMethodReflector $controllerMethodReflector, |
|
| 51 | + Throttler $throttler, |
|
| 52 | + IRequest $request) { |
|
| 53 | + $this->reflector = $controllerMethodReflector; |
|
| 54 | + $this->throttler = $throttler; |
|
| 55 | + $this->request = $request; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * {@inheritDoc} |
|
| 60 | - */ |
|
| 61 | - public function beforeController($controller, $methodName) { |
|
| 62 | - parent::beforeController($controller, $methodName); |
|
| 58 | + /** |
|
| 59 | + * {@inheritDoc} |
|
| 60 | + */ |
|
| 61 | + public function beforeController($controller, $methodName) { |
|
| 62 | + parent::beforeController($controller, $methodName); |
|
| 63 | 63 | |
| 64 | - if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
| 65 | - $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
| 66 | - $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
|
| 67 | - } |
|
| 68 | - } |
|
| 64 | + if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
| 65 | + $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
| 66 | + $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * {@inheritDoc} |
|
| 72 | - */ |
|
| 73 | - public function afterController($controller, $methodName, Response $response) { |
|
| 74 | - if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
| 75 | - $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
| 76 | - $ip = $this->request->getRemoteAddress(); |
|
| 77 | - $this->throttler->sleepDelay($ip, $action); |
|
| 78 | - $this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata()); |
|
| 79 | - } |
|
| 70 | + /** |
|
| 71 | + * {@inheritDoc} |
|
| 72 | + */ |
|
| 73 | + public function afterController($controller, $methodName, Response $response) { |
|
| 74 | + if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
| 75 | + $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
| 76 | + $ip = $this->request->getRemoteAddress(); |
|
| 77 | + $this->throttler->sleepDelay($ip, $action); |
|
| 78 | + $this->throttler->registerAttempt($action, $ip, $response->getThrottleMetadata()); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - return parent::afterController($controller, $methodName, $response); |
|
| 82 | - } |
|
| 81 | + return parent::afterController($controller, $methodName, $response); |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -42,318 +42,318 @@ |
||
| 42 | 42 | */ |
| 43 | 43 | class Response { |
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate'] |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - private $headers = array( |
|
| 50 | - 'Cache-Control' => 'no-cache, no-store, must-revalidate' |
|
| 51 | - ); |
|
| 52 | - |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Cookies that will be need to be constructed as header |
|
| 56 | - * @var array |
|
| 57 | - */ |
|
| 58 | - private $cookies = array(); |
|
| 59 | - |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * HTTP status code - defaults to STATUS OK |
|
| 63 | - * @var int |
|
| 64 | - */ |
|
| 65 | - private $status = Http::STATUS_OK; |
|
| 66 | - |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Last modified date |
|
| 70 | - * @var \DateTime |
|
| 71 | - */ |
|
| 72 | - private $lastModified; |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * ETag |
|
| 77 | - * @var string |
|
| 78 | - */ |
|
| 79 | - private $ETag; |
|
| 80 | - |
|
| 81 | - /** @var ContentSecurityPolicy|null Used Content-Security-Policy */ |
|
| 82 | - private $contentSecurityPolicy = null; |
|
| 83 | - |
|
| 84 | - /** @var bool */ |
|
| 85 | - private $throttled = false; |
|
| 86 | - /** @var array */ |
|
| 87 | - private $throttleMetadata = []; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Caches the response |
|
| 91 | - * @param int $cacheSeconds the amount of seconds that should be cached |
|
| 92 | - * if 0 then caching will be disabled |
|
| 93 | - * @return $this |
|
| 94 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 95 | - */ |
|
| 96 | - public function cacheFor($cacheSeconds) { |
|
| 97 | - |
|
| 98 | - if($cacheSeconds > 0) { |
|
| 99 | - $this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds . ', must-revalidate'); |
|
| 100 | - } else { |
|
| 101 | - $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - return $this; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Adds a new cookie to the response |
|
| 109 | - * @param string $name The name of the cookie |
|
| 110 | - * @param string $value The value of the cookie |
|
| 111 | - * @param \DateTime|null $expireDate Date on that the cookie should expire, if set |
|
| 112 | - * to null cookie will be considered as session |
|
| 113 | - * cookie. |
|
| 114 | - * @return $this |
|
| 115 | - * @since 8.0.0 |
|
| 116 | - */ |
|
| 117 | - public function addCookie($name, $value, \DateTime $expireDate = null) { |
|
| 118 | - $this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate); |
|
| 119 | - return $this; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Set the specified cookies |
|
| 125 | - * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) |
|
| 126 | - * @return $this |
|
| 127 | - * @since 8.0.0 |
|
| 128 | - */ |
|
| 129 | - public function setCookies(array $cookies) { |
|
| 130 | - $this->cookies = $cookies; |
|
| 131 | - return $this; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Invalidates the specified cookie |
|
| 137 | - * @param string $name |
|
| 138 | - * @return $this |
|
| 139 | - * @since 8.0.0 |
|
| 140 | - */ |
|
| 141 | - public function invalidateCookie($name) { |
|
| 142 | - $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00')); |
|
| 143 | - return $this; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Invalidates the specified cookies |
|
| 148 | - * @param array $cookieNames array('foo', 'bar') |
|
| 149 | - * @return $this |
|
| 150 | - * @since 8.0.0 |
|
| 151 | - */ |
|
| 152 | - public function invalidateCookies(array $cookieNames) { |
|
| 153 | - foreach($cookieNames as $cookieName) { |
|
| 154 | - $this->invalidateCookie($cookieName); |
|
| 155 | - } |
|
| 156 | - return $this; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Returns the cookies |
|
| 161 | - * @return array |
|
| 162 | - * @since 8.0.0 |
|
| 163 | - */ |
|
| 164 | - public function getCookies() { |
|
| 165 | - return $this->cookies; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Adds a new header to the response that will be called before the render |
|
| 170 | - * function |
|
| 171 | - * @param string $name The name of the HTTP header |
|
| 172 | - * @param string $value The value, null will delete it |
|
| 173 | - * @return $this |
|
| 174 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 175 | - */ |
|
| 176 | - public function addHeader($name, $value) { |
|
| 177 | - $name = trim($name); // always remove leading and trailing whitespace |
|
| 178 | - // to be able to reliably check for security |
|
| 179 | - // headers |
|
| 180 | - |
|
| 181 | - if(is_null($value)) { |
|
| 182 | - unset($this->headers[$name]); |
|
| 183 | - } else { |
|
| 184 | - $this->headers[$name] = $value; |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - return $this; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Set the headers |
|
| 193 | - * @param array $headers value header pairs |
|
| 194 | - * @return $this |
|
| 195 | - * @since 8.0.0 |
|
| 196 | - */ |
|
| 197 | - public function setHeaders(array $headers) { |
|
| 198 | - $this->headers = $headers; |
|
| 199 | - |
|
| 200 | - return $this; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Returns the set headers |
|
| 206 | - * @return array the headers |
|
| 207 | - * @since 6.0.0 |
|
| 208 | - */ |
|
| 209 | - public function getHeaders() { |
|
| 210 | - $mergeWith = []; |
|
| 211 | - |
|
| 212 | - if($this->lastModified) { |
|
| 213 | - $mergeWith['Last-Modified'] = |
|
| 214 | - $this->lastModified->format(\DateTime::RFC2822); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Build Content-Security-Policy and use default if none has been specified |
|
| 218 | - if(is_null($this->contentSecurityPolicy)) { |
|
| 219 | - $this->setContentSecurityPolicy(new ContentSecurityPolicy()); |
|
| 220 | - } |
|
| 221 | - $this->headers['Content-Security-Policy'] = $this->contentSecurityPolicy->buildPolicy(); |
|
| 222 | - |
|
| 223 | - if($this->ETag) { |
|
| 224 | - $mergeWith['ETag'] = '"' . $this->ETag . '"'; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - return array_merge($mergeWith, $this->headers); |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * By default renders no output |
|
| 233 | - * @return null |
|
| 234 | - * @since 6.0.0 |
|
| 235 | - */ |
|
| 236 | - public function render() { |
|
| 237 | - return null; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Set response status |
|
| 243 | - * @param int $status a HTTP status code, see also the STATUS constants |
|
| 244 | - * @return Response Reference to this object |
|
| 245 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 246 | - */ |
|
| 247 | - public function setStatus($status) { |
|
| 248 | - $this->status = $status; |
|
| 249 | - |
|
| 250 | - return $this; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Set a Content-Security-Policy |
|
| 255 | - * @param EmptyContentSecurityPolicy $csp Policy to set for the response object |
|
| 256 | - * @return $this |
|
| 257 | - * @since 8.1.0 |
|
| 258 | - */ |
|
| 259 | - public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) { |
|
| 260 | - $this->contentSecurityPolicy = $csp; |
|
| 261 | - return $this; |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * Get the currently used Content-Security-Policy |
|
| 266 | - * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if |
|
| 267 | - * none specified. |
|
| 268 | - * @since 8.1.0 |
|
| 269 | - */ |
|
| 270 | - public function getContentSecurityPolicy() { |
|
| 271 | - return $this->contentSecurityPolicy; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Get response status |
|
| 277 | - * @since 6.0.0 |
|
| 278 | - */ |
|
| 279 | - public function getStatus() { |
|
| 280 | - return $this->status; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Get the ETag |
|
| 286 | - * @return string the etag |
|
| 287 | - * @since 6.0.0 |
|
| 288 | - */ |
|
| 289 | - public function getETag() { |
|
| 290 | - return $this->ETag; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Get "last modified" date |
|
| 296 | - * @return \DateTime RFC2822 formatted last modified date |
|
| 297 | - * @since 6.0.0 |
|
| 298 | - */ |
|
| 299 | - public function getLastModified() { |
|
| 300 | - return $this->lastModified; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * Set the ETag |
|
| 306 | - * @param string $ETag |
|
| 307 | - * @return Response Reference to this object |
|
| 308 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 309 | - */ |
|
| 310 | - public function setETag($ETag) { |
|
| 311 | - $this->ETag = $ETag; |
|
| 312 | - |
|
| 313 | - return $this; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Set "last modified" date |
|
| 319 | - * @param \DateTime $lastModified |
|
| 320 | - * @return Response Reference to this object |
|
| 321 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
| 322 | - */ |
|
| 323 | - public function setLastModified($lastModified) { |
|
| 324 | - $this->lastModified = $lastModified; |
|
| 325 | - |
|
| 326 | - return $this; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - /** |
|
| 330 | - * Marks the response as to throttle. Will be throttled when the |
|
| 331 | - * @BruteForceProtection annotation is added. |
|
| 332 | - * |
|
| 333 | - * @param array $metadata |
|
| 334 | - * @since 12.0.0 |
|
| 335 | - */ |
|
| 336 | - public function throttle(array $metadata = []) { |
|
| 337 | - $this->throttled = true; |
|
| 338 | - $this->throttleMetadata = $metadata; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Returns the throttle metadata, defaults to empty array |
|
| 343 | - * |
|
| 344 | - * @return array |
|
| 345 | - * @since 13.0.0 |
|
| 346 | - */ |
|
| 347 | - public function getThrottleMetadata() { |
|
| 348 | - return $this->throttleMetadata; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Whether the current response is throttled. |
|
| 353 | - * |
|
| 354 | - * @since 12.0.0 |
|
| 355 | - */ |
|
| 356 | - public function isThrottled() { |
|
| 357 | - return $this->throttled; |
|
| 358 | - } |
|
| 45 | + /** |
|
| 46 | + * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate'] |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + private $headers = array( |
|
| 50 | + 'Cache-Control' => 'no-cache, no-store, must-revalidate' |
|
| 51 | + ); |
|
| 52 | + |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Cookies that will be need to be constructed as header |
|
| 56 | + * @var array |
|
| 57 | + */ |
|
| 58 | + private $cookies = array(); |
|
| 59 | + |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * HTTP status code - defaults to STATUS OK |
|
| 63 | + * @var int |
|
| 64 | + */ |
|
| 65 | + private $status = Http::STATUS_OK; |
|
| 66 | + |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Last modified date |
|
| 70 | + * @var \DateTime |
|
| 71 | + */ |
|
| 72 | + private $lastModified; |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * ETag |
|
| 77 | + * @var string |
|
| 78 | + */ |
|
| 79 | + private $ETag; |
|
| 80 | + |
|
| 81 | + /** @var ContentSecurityPolicy|null Used Content-Security-Policy */ |
|
| 82 | + private $contentSecurityPolicy = null; |
|
| 83 | + |
|
| 84 | + /** @var bool */ |
|
| 85 | + private $throttled = false; |
|
| 86 | + /** @var array */ |
|
| 87 | + private $throttleMetadata = []; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Caches the response |
|
| 91 | + * @param int $cacheSeconds the amount of seconds that should be cached |
|
| 92 | + * if 0 then caching will be disabled |
|
| 93 | + * @return $this |
|
| 94 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 95 | + */ |
|
| 96 | + public function cacheFor($cacheSeconds) { |
|
| 97 | + |
|
| 98 | + if($cacheSeconds > 0) { |
|
| 99 | + $this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds . ', must-revalidate'); |
|
| 100 | + } else { |
|
| 101 | + $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + return $this; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Adds a new cookie to the response |
|
| 109 | + * @param string $name The name of the cookie |
|
| 110 | + * @param string $value The value of the cookie |
|
| 111 | + * @param \DateTime|null $expireDate Date on that the cookie should expire, if set |
|
| 112 | + * to null cookie will be considered as session |
|
| 113 | + * cookie. |
|
| 114 | + * @return $this |
|
| 115 | + * @since 8.0.0 |
|
| 116 | + */ |
|
| 117 | + public function addCookie($name, $value, \DateTime $expireDate = null) { |
|
| 118 | + $this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate); |
|
| 119 | + return $this; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Set the specified cookies |
|
| 125 | + * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) |
|
| 126 | + * @return $this |
|
| 127 | + * @since 8.0.0 |
|
| 128 | + */ |
|
| 129 | + public function setCookies(array $cookies) { |
|
| 130 | + $this->cookies = $cookies; |
|
| 131 | + return $this; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Invalidates the specified cookie |
|
| 137 | + * @param string $name |
|
| 138 | + * @return $this |
|
| 139 | + * @since 8.0.0 |
|
| 140 | + */ |
|
| 141 | + public function invalidateCookie($name) { |
|
| 142 | + $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00')); |
|
| 143 | + return $this; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Invalidates the specified cookies |
|
| 148 | + * @param array $cookieNames array('foo', 'bar') |
|
| 149 | + * @return $this |
|
| 150 | + * @since 8.0.0 |
|
| 151 | + */ |
|
| 152 | + public function invalidateCookies(array $cookieNames) { |
|
| 153 | + foreach($cookieNames as $cookieName) { |
|
| 154 | + $this->invalidateCookie($cookieName); |
|
| 155 | + } |
|
| 156 | + return $this; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Returns the cookies |
|
| 161 | + * @return array |
|
| 162 | + * @since 8.0.0 |
|
| 163 | + */ |
|
| 164 | + public function getCookies() { |
|
| 165 | + return $this->cookies; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Adds a new header to the response that will be called before the render |
|
| 170 | + * function |
|
| 171 | + * @param string $name The name of the HTTP header |
|
| 172 | + * @param string $value The value, null will delete it |
|
| 173 | + * @return $this |
|
| 174 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 175 | + */ |
|
| 176 | + public function addHeader($name, $value) { |
|
| 177 | + $name = trim($name); // always remove leading and trailing whitespace |
|
| 178 | + // to be able to reliably check for security |
|
| 179 | + // headers |
|
| 180 | + |
|
| 181 | + if(is_null($value)) { |
|
| 182 | + unset($this->headers[$name]); |
|
| 183 | + } else { |
|
| 184 | + $this->headers[$name] = $value; |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + return $this; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Set the headers |
|
| 193 | + * @param array $headers value header pairs |
|
| 194 | + * @return $this |
|
| 195 | + * @since 8.0.0 |
|
| 196 | + */ |
|
| 197 | + public function setHeaders(array $headers) { |
|
| 198 | + $this->headers = $headers; |
|
| 199 | + |
|
| 200 | + return $this; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Returns the set headers |
|
| 206 | + * @return array the headers |
|
| 207 | + * @since 6.0.0 |
|
| 208 | + */ |
|
| 209 | + public function getHeaders() { |
|
| 210 | + $mergeWith = []; |
|
| 211 | + |
|
| 212 | + if($this->lastModified) { |
|
| 213 | + $mergeWith['Last-Modified'] = |
|
| 214 | + $this->lastModified->format(\DateTime::RFC2822); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Build Content-Security-Policy and use default if none has been specified |
|
| 218 | + if(is_null($this->contentSecurityPolicy)) { |
|
| 219 | + $this->setContentSecurityPolicy(new ContentSecurityPolicy()); |
|
| 220 | + } |
|
| 221 | + $this->headers['Content-Security-Policy'] = $this->contentSecurityPolicy->buildPolicy(); |
|
| 222 | + |
|
| 223 | + if($this->ETag) { |
|
| 224 | + $mergeWith['ETag'] = '"' . $this->ETag . '"'; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + return array_merge($mergeWith, $this->headers); |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + |
|
| 231 | + /** |
|
| 232 | + * By default renders no output |
|
| 233 | + * @return null |
|
| 234 | + * @since 6.0.0 |
|
| 235 | + */ |
|
| 236 | + public function render() { |
|
| 237 | + return null; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Set response status |
|
| 243 | + * @param int $status a HTTP status code, see also the STATUS constants |
|
| 244 | + * @return Response Reference to this object |
|
| 245 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 246 | + */ |
|
| 247 | + public function setStatus($status) { |
|
| 248 | + $this->status = $status; |
|
| 249 | + |
|
| 250 | + return $this; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Set a Content-Security-Policy |
|
| 255 | + * @param EmptyContentSecurityPolicy $csp Policy to set for the response object |
|
| 256 | + * @return $this |
|
| 257 | + * @since 8.1.0 |
|
| 258 | + */ |
|
| 259 | + public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) { |
|
| 260 | + $this->contentSecurityPolicy = $csp; |
|
| 261 | + return $this; |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * Get the currently used Content-Security-Policy |
|
| 266 | + * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if |
|
| 267 | + * none specified. |
|
| 268 | + * @since 8.1.0 |
|
| 269 | + */ |
|
| 270 | + public function getContentSecurityPolicy() { |
|
| 271 | + return $this->contentSecurityPolicy; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Get response status |
|
| 277 | + * @since 6.0.0 |
|
| 278 | + */ |
|
| 279 | + public function getStatus() { |
|
| 280 | + return $this->status; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Get the ETag |
|
| 286 | + * @return string the etag |
|
| 287 | + * @since 6.0.0 |
|
| 288 | + */ |
|
| 289 | + public function getETag() { |
|
| 290 | + return $this->ETag; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Get "last modified" date |
|
| 296 | + * @return \DateTime RFC2822 formatted last modified date |
|
| 297 | + * @since 6.0.0 |
|
| 298 | + */ |
|
| 299 | + public function getLastModified() { |
|
| 300 | + return $this->lastModified; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * Set the ETag |
|
| 306 | + * @param string $ETag |
|
| 307 | + * @return Response Reference to this object |
|
| 308 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 309 | + */ |
|
| 310 | + public function setETag($ETag) { |
|
| 311 | + $this->ETag = $ETag; |
|
| 312 | + |
|
| 313 | + return $this; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Set "last modified" date |
|
| 319 | + * @param \DateTime $lastModified |
|
| 320 | + * @return Response Reference to this object |
|
| 321 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
| 322 | + */ |
|
| 323 | + public function setLastModified($lastModified) { |
|
| 324 | + $this->lastModified = $lastModified; |
|
| 325 | + |
|
| 326 | + return $this; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + /** |
|
| 330 | + * Marks the response as to throttle. Will be throttled when the |
|
| 331 | + * @BruteForceProtection annotation is added. |
|
| 332 | + * |
|
| 333 | + * @param array $metadata |
|
| 334 | + * @since 12.0.0 |
|
| 335 | + */ |
|
| 336 | + public function throttle(array $metadata = []) { |
|
| 337 | + $this->throttled = true; |
|
| 338 | + $this->throttleMetadata = $metadata; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Returns the throttle metadata, defaults to empty array |
|
| 343 | + * |
|
| 344 | + * @return array |
|
| 345 | + * @since 13.0.0 |
|
| 346 | + */ |
|
| 347 | + public function getThrottleMetadata() { |
|
| 348 | + return $this->throttleMetadata; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Whether the current response is throttled. |
|
| 353 | + * |
|
| 354 | + * @since 12.0.0 |
|
| 355 | + */ |
|
| 356 | + public function isThrottled() { |
|
| 357 | + return $this->throttled; |
|
| 358 | + } |
|
| 359 | 359 | } |