62 parent::__construct();
65 'cookieOptions' => [],
70 if ( !isset(
$params[
'priority'] ) ) {
71 throw new \InvalidArgumentException( __METHOD__ .
': priority must be specified' );
76 throw new \InvalidArgumentException( __METHOD__ .
': Invalid priority' );
79 if ( !is_array(
$params[
'cookieOptions'] ) ) {
80 throw new \InvalidArgumentException( __METHOD__ .
': cookieOptions must be an array' );
83 $this->priority =
$params[
'priority'];
84 $this->cookieOptions =
$params[
'cookieOptions'];
86 unset( $this->params[
'priority'] );
87 unset( $this->params[
'cookieOptions'] );
96 'callUserSetCookiesHook' =>
false,
101 $this->useCrossSiteCookies = strcasecmp(
$config->
get(
'CookieSameSite' ),
'none' ) === 0;
104 $this->cookieOptions += [
109 'secure' =>
$config->
get(
'CookieSecure' ) || $this->config->get(
'ForceHTTPS' ),
110 'httpOnly' =>
$config->
get(
'CookieHttpOnly' ),
111 'sameSite' =>
$config->
get(
'CookieSameSite' ),
116 $sessionId = $this->
getCookie( $request, $this->params[
'sessionName'],
'' );
119 'forceHTTPS' => $this->
getCookie( $request,
'forceHTTPS',
'',
false )
122 $info[
'id'] = $sessionId;
123 $info[
'persisted'] =
true;
127 if ( $userId !==
null ) {
130 }
catch ( \InvalidArgumentException $ex ) {
135 if ( $userName !==
null && $userInfo->getName() !== $userName ) {
136 $this->logger->warning(
137 'Session "{session}" requested with mismatched UserID and UserName cookies.',
139 'session' => $sessionId,
142 'cookie_username' => $userName,
143 'username' => $userInfo->getName(),
149 if ( $token !==
null ) {
150 if ( !hash_equals( $userInfo->getToken(), $token ) ) {
151 $this->logger->warning(
152 'Session "{session}" requested with invalid Token cookie.',
154 'session' => $sessionId,
156 'username' => $userInfo->getName(),
160 $info[
'userInfo'] = $userInfo->verified();
161 $info[
'persisted'] =
true;
162 } elseif ( isset( $info[
'id'] ) ) {
163 $info[
'userInfo'] = $userInfo;
169 } elseif ( isset( $info[
'id'] ) ) {
175 $this->logger->debug(
176 'Session "{session}" requested without UserID cookie',
178 'session' => $info[
'id'],
202 $this->logger->debug( __METHOD__ .
': Headers already sent' );
212 if ( $this->params[
'callUserSetCookiesHook'] && !$user->isAnon() ) {
213 \Hooks::run(
'UserSetCookies', [ $user, &$sessionData, &$cookies ] );
220 $options[
'secure'] = $this->config->get(
'CookieSecure' )
221 || $this->config->get(
'ForceHTTPS' );
224 $response->setCookie( $this->params[
'sessionName'], $session->
getId(),
null,
225 [
'prefix' =>
'' ] + $options
228 foreach ( $cookies as $key => $value ) {
229 if ( $value ===
false ) {
230 $response->clearCookie( $key, $options );
233 $expiration = $expirationDuration ? $expirationDuration + time() :
null;
234 $response->setCookie( $key, (
string)$value, $expiration, $options );
241 if ( $sessionData ) {
242 $session->
addData( $sessionData );
250 $this->logger->debug( __METHOD__ .
': Headers already sent' );
260 $this->params[
'sessionName'], [
'prefix' =>
'' ] + $this->cookieOptions
263 foreach ( $cookies as $key => $value ) {
264 $response->clearCookie( $key, $this->cookieOptions );
280 if ( $this->config->get(
'ForceHTTPS' ) ) {
286 if ( $backend->shouldRememberUser() ) {
291 $expiration = $expirationDuration ? $expirationDuration + time() :
null;
295 $response->setCookie(
'forceHTTPS',
'true', $expiration,
296 [
'prefix' =>
'',
'secure' =>
false ] + $this->cookieOptions );
299 [
'prefix' =>
'',
'secure' =>
false ] + $this->cookieOptions );
309 if ( $loggedOut + 86400 > time() &&
310 $loggedOut !== (
int)$this->
getCookie( $request,
'LoggedOut', $this->cookieOptions[
'prefix'] )
312 $request->
response()->setCookie(
'LoggedOut', $loggedOut, $loggedOut + 86400,
313 $this->cookieOptions );
321 $this->cookieOptions[
'prefix'] .
'Token',
322 $this->cookieOptions[
'prefix'] .
'LoggedOut',
323 $this->params[
'sessionName'],
329 $name = $this->
getCookie( $request,
'UserName', $this->cookieOptions[
'prefix'] );
330 if ( $name !==
null ) {
333 return $name ===
false ? null : $name;
342 $prefix = $this->cookieOptions[
'prefix'];
344 $this->
getCookie( $request,
'UserID', $prefix ),
345 $this->
getCookie( $request,
'UserName', $prefix ),
346 $this->
getCookie( $request,
'Token', $prefix ),
358 protected function getCookie( $request, $key, $prefix, $default =
null ) {
359 if ( $this->useCrossSiteCookies ) {
360 $value = $request->getCrossSiteCookie( $key, $prefix, $default );
362 $value = $request->getCookie( $key, $prefix, $default );
364 if ( $value ===
'deleted' ) {
382 if ( $user->isAnon() ) {
389 'UserID' => $user->getId(),
390 'UserName' => $user->getName(),
391 'Token' => $remember ? (string)$user->getToken() :
false,
404 if ( !$user->isAnon() && $this->params[
'callUserSetCookiesHook'] ) {
406 'wsUserID' => $user->getId(),
407 'wsToken' => $user->getToken(),
408 'wsUserName' => $user->getName(),
416 return wfMessage(
'sessionprovider-nocookies' );
431 return [
'UserID',
'UserName',
'Token' ];
446 $normalExpiration = $this->config->get(
'CookieExpiration' );
448 if ( $shouldRememberUser && in_array( $cookieName, $extendedCookies,
true ) ) {
449 $extendedExpiration = $this->config->get(
'ExtendedLoginCookieExpiration' );
451 return ( $extendedExpiration !==
null ) ? (int)$extendedExpiration : (int)$normalExpiration;
453 return (
int)$normalExpiration;
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
static getCanonicalName( $name, $validate='valid')
Given unvalidated user input, return a canonical username, or false if the username is invalid.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
response()
Return a handle to WebResponse style object, for setting cookies, headers and other stuff,...
Interface for configuration instances.
get( $name)
Get a configuration variable such as "Sitename" or "UploadMaintenance.".