MediaWiki master
CookieSessionProvider.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Session;
25
30
38
40 protected $params = [];
41
43 protected $cookieOptions = [];
44
58 public function __construct( $params = [] ) {
59 parent::__construct();
60
61 $params += [
62 'cookieOptions' => [],
63 // @codeCoverageIgnoreStart
64 ];
65 // @codeCoverageIgnoreEnd
66
67 if ( !isset( $params['priority'] ) ) {
68 throw new \InvalidArgumentException( __METHOD__ . ': priority must be specified' );
69 }
70 if ( $params['priority'] < SessionInfo::MIN_PRIORITY ||
72 ) {
73 throw new \InvalidArgumentException( __METHOD__ . ': Invalid priority' );
74 }
75
76 if ( !is_array( $params['cookieOptions'] ) ) {
77 throw new \InvalidArgumentException( __METHOD__ . ': cookieOptions must be an array' );
78 }
79
80 $this->priority = $params['priority'];
81 $this->cookieOptions = $params['cookieOptions'];
82 $this->params = $params;
83 unset( $this->params['priority'] );
84 unset( $this->params['cookieOptions'] );
85 }
86
87 protected function postInitSetup() {
88 $this->params += [
89 'sessionName' =>
91 ?: $this->getConfig()->get( MainConfigNames::CookiePrefix ) . '_session',
92 ];
93
94 $sameSite = $this->getConfig()->get( MainConfigNames::CookieSameSite );
95
96 // @codeCoverageIgnoreStart
97 $this->cookieOptions += [
98 // @codeCoverageIgnoreEnd
99 'prefix' => $this->getConfig()->get( MainConfigNames::CookiePrefix ),
100 'path' => $this->getConfig()->get( MainConfigNames::CookiePath ),
101 'domain' => $this->getConfig()->get( MainConfigNames::CookieDomain ),
102 'secure' => $this->getConfig()->get( MainConfigNames::CookieSecure )
103 || $this->getConfig()->get( MainConfigNames::ForceHTTPS ),
104 'httpOnly' => $this->getConfig()->get( MainConfigNames::CookieHttpOnly ),
105 'sameSite' => $sameSite,
106 ];
107 }
108
109 public function provideSessionInfo( WebRequest $request ) {
110 $sessionId = $this->getCookie( $request, $this->params['sessionName'], '' );
111 $info = [
112 'provider' => $this,
113 'forceHTTPS' => $this->getCookie( $request, 'forceHTTPS', '', false )
114 ];
115 if ( SessionManager::validateSessionId( $sessionId ) ) {
116 $info['id'] = $sessionId;
117 $info['persisted'] = true;
118 }
119
120 [ $userId, $userName, $token ] = $this->getUserInfoFromCookies( $request );
121 if ( $userId !== null ) {
122 try {
123 $userInfo = UserInfo::newFromId( $userId );
124 } catch ( \InvalidArgumentException $ex ) {
125 return null;
126 }
127
128 if ( $userName !== null && $userInfo->getName() !== $userName ) {
129 $this->logger->warning(
130 'Session "{session}" requested with mismatched UserID and UserName cookies.',
131 [
132 'session' => $sessionId,
133 'mismatch' => [
134 'userid' => $userId,
135 'cookie_username' => $userName,
136 'username' => $userInfo->getName(),
137 ],
138 ] );
139 return null;
140 }
141
142 if ( $token !== null ) {
143 if ( !hash_equals( $userInfo->getToken(), $token ) ) {
144 $this->logger->warning(
145 'Session "{session}" requested with invalid Token cookie.',
146 [
147 'session' => $sessionId,
148 'userid' => $userId,
149 'username' => $userInfo->getName(),
150 ] );
151 return null;
152 }
153 $info['userInfo'] = $userInfo->verified();
154 $info['persisted'] = true; // If we have user+token, it should be
155 } elseif ( isset( $info['id'] ) ) {
156 $info['userInfo'] = $userInfo;
157 } else {
158 // No point in returning, loadSessionInfoFromStore() will
159 // reject it anyway.
160 return null;
161 }
162 } elseif ( isset( $info['id'] ) ) {
163 // No UserID cookie, so insist that the session is anonymous.
164 // Note: this event occurs for several normal activities:
165 // * anon visits Special:UserLogin
166 // * anon browsing after seeing Special:UserLogin
167 // * anon browsing after edit or preview
168 $this->logger->debug(
169 'Session "{session}" requested without UserID cookie',
170 [
171 'session' => $info['id'],
172 ] );
173 $info['userInfo'] = UserInfo::newAnonymous();
174 } else {
175 // No session ID and no user is the same as an empty session, so
176 // there's no point.
177 return null;
178 }
179
180 return new SessionInfo( $this->priority, $info );
181 }
182
183 public function persistsSessionId() {
184 return true;
185 }
186
187 public function canChangeUser() {
188 return true;
189 }
190
191 public function persistSession( SessionBackend $session, WebRequest $request ) {
192 $response = $request->response();
193 if ( $response->headersSent() ) {
194 // Can't do anything now
195 $this->logger->debug( __METHOD__ . ': Headers already sent' );
196 return;
197 }
198
199 $user = $session->getUser();
200
201 $cookies = $this->cookieDataToExport( $user, $session->shouldRememberUser() );
202 $sessionData = $this->sessionDataToExport( $user );
203
204 $options = $this->cookieOptions;
205
206 $forceHTTPS = $session->shouldForceHTTPS() || $user->requiresHTTPS();
207 if ( $forceHTTPS ) {
208 $options['secure'] = $this->getConfig()->get( MainConfigNames::CookieSecure )
209 || $this->getConfig()->get( MainConfigNames::ForceHTTPS );
210 }
211
212 $response->setCookie( $this->params['sessionName'], $session->getId(), null,
213 [ 'prefix' => '' ] + $options
214 );
215
216 foreach ( $cookies as $key => $value ) {
217 if ( $value === false ) {
218 $response->clearCookie( $key, $options );
219 } else {
220 $expirationDuration = $this->getLoginCookieExpiration( $key, $session->shouldRememberUser() );
221 $expiration = $expirationDuration ? $expirationDuration + time() : null;
222 $response->setCookie( $key, (string)$value, $expiration, $options );
223 }
224 }
225
226 $this->setForceHTTPSCookie( $forceHTTPS, $session, $request );
227 $this->setLoggedOutCookie( $session->getLoggedOutTimestamp(), $request );
228
229 if ( $sessionData ) {
230 $session->addData( $sessionData );
231 }
232 }
233
234 public function unpersistSession( WebRequest $request ) {
235 $response = $request->response();
236 if ( $response->headersSent() ) {
237 // Can't do anything now
238 $this->logger->debug( __METHOD__ . ': Headers already sent' );
239 return;
240 }
241
242 $cookies = [
243 'UserID' => false,
244 'Token' => false,
245 ];
246
247 $response->clearCookie(
248 $this->params['sessionName'], [ 'prefix' => '' ] + $this->cookieOptions
249 );
250
251 foreach ( $cookies as $key => $value ) {
252 $response->clearCookie( $key, $this->cookieOptions );
253 }
254
255 $this->setForceHTTPSCookie( false, null, $request );
256 }
257
265 protected function setForceHTTPSCookie( $set, ?SessionBackend $backend, WebRequest $request ) {
266 if ( $this->getConfig()->get( MainConfigNames::ForceHTTPS ) ) {
267 // No need to send a cookie if the wiki is always HTTPS (T256095)
268 return;
269 }
270 $response = $request->response();
271 if ( $set ) {
272 if ( $backend->shouldRememberUser() ) {
273 $expirationDuration = $this->getLoginCookieExpiration(
274 'forceHTTPS',
275 true
276 );
277 $expiration = $expirationDuration ? $expirationDuration + time() : null;
278 } else {
279 $expiration = null;
280 }
281 $response->setCookie( 'forceHTTPS', 'true', $expiration,
282 [ 'prefix' => '', 'secure' => false ] + $this->cookieOptions );
283 } else {
284 $response->clearCookie( 'forceHTTPS',
285 [ 'prefix' => '', 'secure' => false ] + $this->cookieOptions );
286 }
287 }
288
293 protected function setLoggedOutCookie( $loggedOut, WebRequest $request ) {
294 if ( $loggedOut + 86400 > time() &&
295 $loggedOut !== (int)$this->getCookie( $request, 'LoggedOut', $this->cookieOptions['prefix'] )
296 ) {
297 $request->response()->setCookie( 'LoggedOut', (string)$loggedOut, $loggedOut + 86400,
298 $this->cookieOptions );
299 }
300 }
301
302 public function getVaryCookies() {
303 return [
304 // Vary on token and session because those are the real authn
305 // determiners. UserID and UserName don't matter without those.
306 $this->cookieOptions['prefix'] . 'Token',
307 $this->cookieOptions['prefix'] . 'LoggedOut',
308 $this->params['sessionName'],
309 'forceHTTPS',
310 ];
311 }
312
313 public function suggestLoginUsername( WebRequest $request ) {
314 $name = $this->getCookie( $request, 'UserName', $this->cookieOptions['prefix'] );
315 if ( $name !== null ) {
316 if ( $this->userNameUtils->isTemp( $name ) ) {
317 $name = false;
318 } else {
319 $name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE );
320 }
321 }
322 return $name === false ? null : $name;
323 }
324
330 protected function getUserInfoFromCookies( $request ) {
331 $prefix = $this->cookieOptions['prefix'];
332 return [
333 $this->getCookie( $request, 'UserID', $prefix ),
334 $this->getCookie( $request, 'UserName', $prefix ),
335 $this->getCookie( $request, 'Token', $prefix ),
336 ];
337 }
338
347 protected function getCookie( $request, $key, $prefix, $default = null ) {
348 $value = $request->getCookie( $key, $prefix, $default );
349 if ( $value === 'deleted' ) {
350 // PHP uses this value when deleting cookies. A legitimate cookie will never have
351 // this value (usernames start with uppercase, token is longer, other auth cookies
352 // are booleans or integers). Seeing this means that in a previous request we told the
353 // client to delete the cookie, but it has poor cookie handling. Pretend the cookie is
354 // not there to avoid invalidating the session.
355 return null;
356 }
357 return $value;
358 }
359
366 protected function cookieDataToExport( $user, $remember ) {
367 if ( $user->isAnon() ) {
368 return [
369 'UserID' => false,
370 'Token' => false,
371 ];
372 } else {
373 return [
374 'UserID' => $user->getId(),
375 'UserName' => $user->getName(),
376 'Token' => $remember ? (string)$user->getToken() : false,
377 ];
378 }
379 }
380
386 protected function sessionDataToExport( $user ) {
387 return [];
388 }
389
390 public function whyNoSession() {
391 return wfMessage( 'sessionprovider-nocookies' );
392 }
393
394 public function getRememberUserDuration() {
395 return min( $this->getLoginCookieExpiration( 'UserID', true ),
396 $this->getLoginCookieExpiration( 'Token', true ) ) ?: null;
397 }
398
405 protected function getExtendedLoginCookies() {
406 return [ 'UserID', 'UserName', 'Token' ];
407 }
408
419 protected function getLoginCookieExpiration( $cookieName, $shouldRememberUser ) {
420 $extendedCookies = $this->getExtendedLoginCookies();
421 $normalExpiration = $this->getConfig()->get( MainConfigNames::CookieExpiration );
422
423 if ( $shouldRememberUser && in_array( $cookieName, $extendedCookies, true ) ) {
424 $extendedExpiration = $this->getConfig()->get( MainConfigNames::ExtendedLoginCookieExpiration );
425
426 return ( $extendedExpiration !== null ) ? (int)$extendedExpiration : (int)$normalExpiration;
427 } else {
428 return (int)$normalExpiration;
429 }
430 }
431}
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,...
response()
Return a handle to WebResponse style object, for setting cookies, headers and other stuff,...
A CookieSessionProvider persists sessions using cookies.
suggestLoginUsername(WebRequest $request)
Get a suggested username for the login form.
canChangeUser()
Indicate whether the user associated with the request can be changed.
sessionDataToExport( $user)
Return extra data to store in the session.
persistSession(SessionBackend $session, WebRequest $request)
Persist a session into a request/response.
getUserInfoFromCookies( $request)
Fetch the user identity from cookies.
whyNoSession()
Return a Message for why sessions might not be being persisted.For example, "check whether you're blo...
setLoggedOutCookie( $loggedOut, WebRequest $request)
getExtendedLoginCookies()
Gets the list of cookies that must be set to the 'remember me' duration, if $wgExtendedLoginCookieExp...
setForceHTTPSCookie( $set, ?SessionBackend $backend, WebRequest $request)
Set the "forceHTTPS" cookie, unless $wgForceHTTPS prevents it.
getVaryCookies()
Return the list of cookies that need varying on.
getCookie( $request, $key, $prefix, $default=null)
Get a cookie.
persistsSessionId()
Indicate whether self::persistSession() can save arbitrary session IDs.
provideSessionInfo(WebRequest $request)
Provide session info for a request.
cookieDataToExport( $user, $remember)
Return the data to store in cookies.
getRememberUserDuration()
Returns the duration (in seconds) for which users will be remembered when Session::setRememberUser() ...
unpersistSession(WebRequest $request)
Remove any persisted session from a request/response.
getLoginCookieExpiration( $cookieName, $shouldRememberUser)
Returns the lifespan of the login cookies, in seconds.
postInitSetup()
A provider can override this to do any necessary setup after init() is called.
This is the actual workhorse for Session.
addData(array $newData)
Add data to the session.
shouldForceHTTPS()
Whether HTTPS should be forced.
getId()
Returns the session ID.
getLoggedOutTimestamp()
Fetch the "logged out" timestamp.
getUser()
Returns the authenticated user for this session.
shouldRememberUser()
Indicate whether the user should be remembered independently of the session ID.
Value object returned by SessionProvider.
const MIN_PRIORITY
Minimum allowed priority.
const MAX_PRIORITY
Maximum allowed priority.
static validateSessionId( $id)
Validate a session ID.
A SessionProvider provides SessionInfo and support for Session.
static newAnonymous()
Create an instance for an anonymous (i.e.
Definition UserInfo.php:78
static newFromId( $id, $verified=false)
Create an instance for a logged-in user by ID.
Definition UserInfo.php:88
internal since 1.36
Definition User.php:93
Shared interface for rigor levels when dealing with User methods.