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'] );
98 $this->useCrossSiteCookies = $sameSite !==
null && strcasecmp( $sameSite,
'none' ) === 0;
101 $this->cookieOptions += [
109 'sameSite' => $sameSite,
114 $sessionId = $this->
getCookie( $request, $this->params[
'sessionName'],
'' );
117 'forceHTTPS' => $this->
getCookie( $request,
'forceHTTPS',
'',
false )
120 $info[
'id'] = $sessionId;
121 $info[
'persisted'] =
true;
125 if ( $userId !==
null ) {
128 }
catch ( \InvalidArgumentException $ex ) {
132 if ( $userName !==
null && $userInfo->getName() !== $userName ) {
133 $this->logger->warning(
134 'Session "{session}" requested with mismatched UserID and UserName cookies.',
136 'session' => $sessionId,
139 'cookie_username' => $userName,
140 'username' => $userInfo->getName(),
146 if ( $token !==
null ) {
147 if ( !hash_equals( $userInfo->getToken(), $token ) ) {
148 $this->logger->warning(
149 'Session "{session}" requested with invalid Token cookie.',
151 'session' => $sessionId,
153 'username' => $userInfo->getName(),
157 $info[
'userInfo'] = $userInfo->verified();
158 $info[
'persisted'] =
true;
159 } elseif ( isset( $info[
'id'] ) ) {
160 $info[
'userInfo'] = $userInfo;
166 } elseif ( isset( $info[
'id'] ) ) {
172 $this->logger->debug(
173 'Session "{session}" requested without UserID cookie',
175 'session' => $info[
'id'],
197 if ( $response->headersSent() ) {
199 $this->logger->debug( __METHOD__ .
': Headers already sent' );
216 $response->setCookie( $this->params[
'sessionName'], $session->
getId(),
null,
217 [
'prefix' =>
'' ] + $options
220 foreach ( $cookies as $key => $value ) {
221 if ( $value ===
false ) {
222 $response->clearCookie( $key, $options );
225 $expiration = $expirationDuration ? $expirationDuration + time() :
null;
226 $response->setCookie( $key, (
string)$value, $expiration, $options );
233 if ( $sessionData ) {
234 $session->
addData( $sessionData );
240 if ( $response->headersSent() ) {
242 $this->logger->debug( __METHOD__ .
': Headers already sent' );
251 $response->clearCookie(
252 $this->params[
'sessionName'], [
'prefix' =>
'' ] + $this->cookieOptions
255 foreach ( $cookies as $key => $value ) {
256 $response->clearCookie( $key, $this->cookieOptions );
281 $expiration = $expirationDuration ? $expirationDuration + time() :
null;
285 $response->setCookie(
'forceHTTPS',
'true', $expiration,
286 [
'prefix' =>
'',
'secure' =>
false ] + $this->cookieOptions );
288 $response->clearCookie(
'forceHTTPS',
289 [
'prefix' =>
'',
'secure' =>
false ] + $this->cookieOptions );
298 if ( $loggedOut + 86400 > time() &&
299 $loggedOut !== (
int)$this->
getCookie( $request,
'LoggedOut', $this->cookieOptions[
'prefix'] )
301 $request->
response()->setCookie(
'LoggedOut', (
string)$loggedOut, $loggedOut + 86400,
302 $this->cookieOptions );
310 $this->cookieOptions[
'prefix'] .
'Token',
311 $this->cookieOptions[
'prefix'] .
'LoggedOut',
312 $this->params[
'sessionName'],
318 $name = $this->
getCookie( $request,
'UserName', $this->cookieOptions[
'prefix'] );
319 if ( $name !==
null ) {
320 if ( $this->userNameUtils->isTemp( $name ) ) {
323 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE );
326 return $name ===
false ? null : $name;
335 $prefix = $this->cookieOptions[
'prefix'];
337 $this->
getCookie( $request,
'UserID', $prefix ),
338 $this->
getCookie( $request,
'UserName', $prefix ),
339 $this->
getCookie( $request,
'Token', $prefix ),
351 protected function getCookie( $request, $key, $prefix, $default =
null ) {
352 if ( $this->useCrossSiteCookies ) {
353 $value = $request->getCrossSiteCookie( $key, $prefix, $default );
355 $value = $request->getCookie( $key, $prefix, $default );
357 if ( $value ===
'deleted' ) {
375 if ( $user->isAnon() ) {
382 'UserID' => $user->getId(),
383 'UserName' => $user->getName(),
384 'Token' => $remember ? (string)$user->getToken() :
false,
399 return wfMessage(
'sessionprovider-nocookies' );
414 return [
'UserID',
'UserName',
'Token' ];
431 if ( $shouldRememberUser && in_array( $cookieName, $extendedCookies,
true ) ) {
434 return ( $extendedExpiration !==
null ) ? (int)$extendedExpiration : (int)$normalExpiration;
436 return (
int)$normalExpiration;
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
A class containing constants representing the names of configuration variables.
const ForceHTTPS
Name constant for the ForceHTTPS setting, for use with Config::get()
const CookieExpiration
Name constant for the CookieExpiration setting, for use with Config::get()
const CookieDomain
Name constant for the CookieDomain setting, for use with Config::get()
const CookiePath
Name constant for the CookiePath setting, for use with Config::get()
const CookieSameSite
Name constant for the CookieSameSite setting, for use with Config::get()
const CookieSecure
Name constant for the CookieSecure setting, for use with Config::get()
const SessionName
Name constant for the SessionName setting, for use with Config::get()
const ExtendedLoginCookieExpiration
Name constant for the ExtendedLoginCookieExpiration setting, for use with Config::get()
const CookiePrefix
Name constant for the CookiePrefix setting, for use with Config::get()
const CookieHttpOnly
Name constant for the CookieHttpOnly setting, for use with Config::get()
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,...