3 * MediaWiki\Session entry point
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 namespace MediaWiki\Session
;
27 use Psr\Log\LoggerInterface
;
36 * This serves as the entry point to the MediaWiki session handling system.
41 final class SessionManager
implements SessionManagerInterface
{
42 /** @var SessionManager|null */
43 private static $instance = null;
45 /** @var Session|null */
46 private static $globalSession = null;
48 /** @var WebRequest|null */
49 private static $globalSessionRequest = null;
51 /** @var LoggerInterface */
57 /** @var CachedBagOStuff|null */
60 /** @var SessionProvider[] */
61 private $sessionProviders = null;
64 private $varyCookies = null;
67 private $varyHeaders = null;
69 /** @var SessionBackend[] */
70 private $allSessionBackends = [];
72 /** @var SessionId[] */
73 private $allSessionIds = [];
76 private $preventUsers = [];
79 * Get the global SessionManager
80 * @return SessionManagerInterface
81 * (really a SessionManager, but this is to make IDEs less confused)
83 public static function singleton() {
84 if ( self
::$instance === null ) {
85 self
::$instance = new self();
87 return self
::$instance;
91 * Get the "global" session
93 * If PHP's session_id() has been set, returns that session. Otherwise
94 * returns the session for RequestContext::getMain()->getRequest().
98 public static function getGlobalSession() {
99 if ( !PHPSessionHandler
::isEnabled() ) {
105 $request = \RequestContext
::getMain()->getRequest();
107 !self
::$globalSession // No global session is set up yet
108 || self
::$globalSessionRequest !== $request // The global WebRequest changed
109 ||
$id !== '' && self
::$globalSession->getId() !== $id // Someone messed with session_id()
111 self
::$globalSessionRequest = $request;
113 // session_id() wasn't used, so fetch the Session from the WebRequest.
114 // We use $request->getSession() instead of $singleton->getSessionForRequest()
115 // because doing the latter would require a public
116 // "$request->getSessionId()" method that would confuse end
117 // users by returning SessionId|null where they'd expect it to
118 // be short for $request->getSession()->getId(), and would
119 // wind up being a duplicate of the code in
120 // $request->getSession() anyway.
121 self
::$globalSession = $request->getSession();
123 // Someone used session_id(), so we need to follow suit.
124 // Note this overwrites whatever session might already be
125 // associated with $request with the one for $id.
126 self
::$globalSession = self
::singleton()->getSessionById( $id, true, $request )
127 ?
: $request->getSession();
130 return self
::$globalSession;
134 * @param array $options
135 * - config: Config to fetch configuration from. Defaults to the default 'main' config.
136 * - logger: LoggerInterface to use for logging. Defaults to the 'session' channel.
137 * - store: BagOStuff to store session data in.
139 public function __construct( $options = [] ) {
140 if ( isset( $options['config'] ) ) {
141 $this->config
= $options['config'];
142 if ( !$this->config
instanceof Config
) {
143 throw new \
InvalidArgumentException(
144 '$options[\'config\'] must be an instance of Config'
148 $this->config
= \ConfigFactory
::getDefaultInstance()->makeConfig( 'main' );
151 if ( isset( $options['logger'] ) ) {
152 if ( !$options['logger'] instanceof LoggerInterface
) {
153 throw new \
InvalidArgumentException(
154 '$options[\'logger\'] must be an instance of LoggerInterface'
157 $this->setLogger( $options['logger'] );
159 $this->setLogger( \MediaWiki\Logger\LoggerFactory
::getInstance( 'session' ) );
162 if ( isset( $options['store'] ) ) {
163 if ( !$options['store'] instanceof BagOStuff
) {
164 throw new \
InvalidArgumentException(
165 '$options[\'store\'] must be an instance of BagOStuff'
168 $store = $options['store'];
170 $store = \ObjectCache
::getInstance( $this->config
->get( 'SessionCacheType' ) );
172 $this->store
= $store instanceof CachedBagOStuff ?
$store : new CachedBagOStuff( $store );
174 register_shutdown_function( [ $this, 'shutdown' ] );
177 public function setLogger( LoggerInterface
$logger ) {
178 $this->logger
= $logger;
181 public function getSessionForRequest( WebRequest
$request ) {
182 $info = $this->getSessionInfoForRequest( $request );
185 $session = $this->getEmptySession( $request );
187 $session = $this->getSessionFromInfo( $info, $request );
192 public function getSessionById( $id, $create = false, WebRequest
$request = null ) {
193 if ( !self
::validateSessionId( $id ) ) {
194 throw new \
InvalidArgumentException( 'Invalid session ID' );
197 $request = new FauxRequest
;
201 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, [ 'id' => $id, 'idIsSafe' => true ] );
203 // If we already have the backend loaded, use it directly
204 if ( isset( $this->allSessionBackends
[$id] ) ) {
205 return $this->getSessionFromInfo( $info, $request );
208 // Test if the session is in storage, and if so try to load it.
209 $key = wfMemcKey( 'MWSession', $id );
210 if ( is_array( $this->store
->get( $key ) ) ) {
211 $create = false; // If loading fails, don't bother creating because it probably will fail too.
212 if ( $this->loadSessionInfoFromStore( $info, $request ) ) {
213 $session = $this->getSessionFromInfo( $info, $request );
217 if ( $create && $session === null ) {
220 $session = $this->getEmptySessionInternal( $request, $id );
221 } catch ( \Exception
$ex ) {
222 $this->logger
->error( 'Failed to create empty session: {exception}',
224 'method' => __METHOD__
,
234 public function getEmptySession( WebRequest
$request = null ) {
235 return $this->getEmptySessionInternal( $request );
239 * @see SessionManagerInterface::getEmptySession
240 * @param WebRequest|null $request
241 * @param string|null $id ID to force on the new session
244 private function getEmptySessionInternal( WebRequest
$request = null, $id = null ) {
245 if ( $id !== null ) {
246 if ( !self
::validateSessionId( $id ) ) {
247 throw new \
InvalidArgumentException( 'Invalid session ID' );
250 $key = wfMemcKey( 'MWSession', $id );
251 if ( is_array( $this->store
->get( $key ) ) ) {
252 throw new \
InvalidArgumentException( 'Session ID already exists' );
256 $request = new FauxRequest
;
260 foreach ( $this->getProviders() as $provider ) {
261 $info = $provider->newSessionInfo( $id );
265 if ( $info->getProvider() !== $provider ) {
266 throw new \
UnexpectedValueException(
267 "$provider returned an empty session info for a different provider: $info"
270 if ( $id !== null && $info->getId() !== $id ) {
271 throw new \
UnexpectedValueException(
272 "$provider returned empty session info with a wrong id: " .
273 $info->getId() . ' != ' . $id
276 if ( !$info->isIdSafe() ) {
277 throw new \
UnexpectedValueException(
278 "$provider returned empty session info with id flagged unsafe"
281 $compare = $infos ? SessionInfo
::compare( $infos[0], $info ) : -1;
282 if ( $compare > 0 ) {
285 if ( $compare === 0 ) {
292 // Make sure there's exactly one
293 if ( count( $infos ) > 1 ) {
294 throw new \
UnexpectedValueException(
295 'Multiple empty sessions tied for top priority: ' . implode( ', ', $infos )
297 } elseif ( count( $infos ) < 1 ) {
298 throw new \
UnexpectedValueException( 'No provider could provide an empty session!' );
301 return $this->getSessionFromInfo( $infos[0], $request );
304 public function invalidateSessionsForUser( User
$user ) {
306 $user->saveSettings();
308 $authUser = \MediaWiki\Auth\AuthManager
::callLegacyAuthPlugin( 'getUserInstance', [ &$user ] );
310 $authUser->resetAuthToken();
313 foreach ( $this->getProviders() as $provider ) {
314 $provider->invalidateSessionsForUser( $user );
318 public function getVaryHeaders() {
319 // @codeCoverageIgnoreStart
320 if ( defined( 'MW_NO_SESSION' ) && MW_NO_SESSION
!== 'warn' ) {
323 // @codeCoverageIgnoreEnd
324 if ( $this->varyHeaders
=== null ) {
326 foreach ( $this->getProviders() as $provider ) {
327 foreach ( $provider->getVaryHeaders() as $header => $options ) {
328 if ( !isset( $headers[$header] ) ) {
329 $headers[$header] = [];
331 if ( is_array( $options ) ) {
332 $headers[$header] = array_unique( array_merge( $headers[$header], $options ) );
336 $this->varyHeaders
= $headers;
338 return $this->varyHeaders
;
341 public function getVaryCookies() {
342 // @codeCoverageIgnoreStart
343 if ( defined( 'MW_NO_SESSION' ) && MW_NO_SESSION
!== 'warn' ) {
346 // @codeCoverageIgnoreEnd
347 if ( $this->varyCookies
=== null ) {
349 foreach ( $this->getProviders() as $provider ) {
350 $cookies = array_merge( $cookies, $provider->getVaryCookies() );
352 $this->varyCookies
= array_values( array_unique( $cookies ) );
354 return $this->varyCookies
;
358 * Validate a session ID
362 public static function validateSessionId( $id ) {
363 return is_string( $id ) && preg_match( '/^[a-zA-Z0-9_-]{32,}$/', $id );
367 * @name Internal methods
372 * Auto-create the given user, if necessary
373 * @private Don't call this yourself. Let Setup.php do it for you at the right time.
374 * @deprecated since 1.27, use MediaWiki\Auth\AuthManager::autoCreateUser instead
375 * @param User $user User to auto-create
376 * @return bool Success
378 public static function autoCreateUser( User
$user ) {
379 global $wgAuth, $wgDisableAuthManager;
381 // @codeCoverageIgnoreStart
382 if ( !$wgDisableAuthManager ) {
383 wfDeprecated( __METHOD__
, '1.27' );
384 return \MediaWiki\Auth\AuthManager
::singleton()->autoCreateUser(
386 \MediaWiki\Auth\AuthManager
::AUTOCREATE_SOURCE_SESSION
,
390 // @codeCoverageIgnoreEnd
392 $logger = self
::singleton()->logger
;
394 // Much of this code is based on that in CentralAuth
396 // Try the local user from the slave DB
397 $localId = User
::idFromName( $user->getName() );
400 // Fetch the user ID from the master, so that we don't try to create the user
401 // when they already exist, due to replication lag
402 // @codeCoverageIgnoreStart
403 if ( !$localId && wfGetLB()->getReaderIndex() != 0 ) {
404 $localId = User
::idFromName( $user->getName(), User
::READ_LATEST
);
405 $flags = User
::READ_LATEST
;
407 // @codeCoverageIgnoreEnd
410 // User exists after all.
411 $user->setId( $localId );
412 $user->loadFromId( $flags );
416 // Denied by AuthPlugin? But ignore AuthPlugin itself.
417 if ( get_class( $wgAuth ) !== 'AuthPlugin' && !$wgAuth->autoCreate() ) {
418 $logger->debug( __METHOD__
. ': denied by AuthPlugin' );
424 // Wiki is read-only?
425 if ( wfReadOnly() ) {
426 $logger->debug( __METHOD__
. ': denied by wfReadOnly()' );
432 $userName = $user->getName();
434 // Check the session, if we tried to create this user already there's
435 // no point in retrying.
436 $session = self
::getGlobalSession();
437 $reason = $session->get( 'MWSession::AutoCreateBlacklist' );
439 $logger->debug( __METHOD__
. ": blacklisted in session ($reason)" );
445 // Is the IP user able to create accounts?
447 if ( !$anon->isAllowedAny( 'createaccount', 'autocreateaccount' )
448 ||
$anon->isBlockedFromCreateAccount()
450 // Blacklist the user to avoid repeated DB queries subsequently
451 $logger->debug( __METHOD__
. ': user is blocked from this wiki, blacklisting' );
452 $session->set( 'MWSession::AutoCreateBlacklist', 'blocked', 600 );
459 // Check for validity of username
460 if ( !User
::isCreatableName( $userName ) ) {
461 $logger->debug( __METHOD__
. ': Invalid username, blacklisting' );
462 $session->set( 'MWSession::AutoCreateBlacklist', 'invalid username', 600 );
469 // Give other extensions a chance to stop auto creation.
470 $user->loadDefaults( $userName );
472 if ( !\Hooks
::run( 'AbortAutoAccount', [ $user, &$abortMessage ] ) ) {
473 // In this case we have no way to return the message to the user,
474 // but we can log it.
475 $logger->debug( __METHOD__
. ": denied by hook: $abortMessage" );
476 $session->set( 'MWSession::AutoCreateBlacklist', "hook aborted: $abortMessage", 600 );
483 // Make sure the name has not been changed
484 if ( $user->getName() !== $userName ) {
487 throw new \
UnexpectedValueException(
488 'AbortAutoAccount hook tried to change the user name'
492 // Ignore warnings about master connections/writes...hard to avoid here
493 \Profiler
::instance()->getTransactionProfiler()->resetExpectations();
495 $cache = \ObjectCache
::getLocalClusterInstance();
496 $backoffKey = wfMemcKey( 'MWSession', 'autocreate-failed', md5( $userName ) );
497 if ( $cache->get( $backoffKey ) ) {
498 $logger->debug( __METHOD__
. ': denied by prior creation attempt failures' );
504 // Checks passed, create the user...
505 $from = isset( $_SERVER['REQUEST_URI'] ) ?
$_SERVER['REQUEST_URI'] : 'CLI';
506 $logger->info( __METHOD__
. ': creating new user ({username}) - from: {url}',
508 'username' => $userName,
513 // Insert the user into the local DB master
514 $status = $user->addToDatabase();
515 if ( !$status->isOK() ) {
516 // @codeCoverageIgnoreStart
517 // double-check for a race condition (T70012)
518 $id = User
::idFromName( $user->getName(), User
::READ_LATEST
);
520 $logger->info( __METHOD__
. ': tried to autocreate existing user',
522 'username' => $userName,
526 __METHOD__
. ': failed with message ' . $status->getWikiText( false, false, 'en' ),
528 'username' => $userName,
533 $user->loadFromId( User
::READ_LATEST
);
535 // @codeCoverageIgnoreEnd
537 } catch ( \Exception
$ex ) {
538 // @codeCoverageIgnoreStart
539 $logger->error( __METHOD__
. ': failed with exception {exception}', [
541 'username' => $userName,
543 // Do not keep throwing errors for a while
544 $cache->set( $backoffKey, 1, 600 );
545 // Bubble up error; which should normally trigger DB rollbacks
547 // @codeCoverageIgnoreEnd
551 // @codeCoverageIgnoreStart
553 $wgAuth->initUser( $tmpUser, true );
554 if ( $tmpUser !== $user ) {
555 $logger->warning( __METHOD__
. ': ' .
556 get_class( $wgAuth ) . '::initUser() replaced the user object' );
558 // @codeCoverageIgnoreEnd
560 # Notify hooks (e.g. Newuserlog)
561 \Hooks
::run( 'AuthPluginAutoCreate', [ $user ] );
562 \Hooks
::run( 'LocalUserCreated', [ $user, true ] );
564 $user->saveSettings();
567 \DeferredUpdates
::addUpdate( new \
SiteStatsUpdate( 0, 0, 0, 0, 1 ) );
569 # Watch user's userpage and talk page
570 $user->addWatch( $user->getUserPage(), User
::IGNORE_USER_RIGHTS
);
576 * Prevent future sessions for the user
578 * The intention is that the named account will never again be usable for
579 * normal login (i.e. there is no way to undo the prevention of access).
581 * @private For use from \User::newSystemUser only
582 * @param string $username
584 public function preventSessionsForUser( $username ) {
585 $this->preventUsers
[$username] = true;
587 // Instruct the session providers to kill any other sessions too.
588 foreach ( $this->getProviders() as $provider ) {
589 $provider->preventSessionsForUser( $username );
594 * Test if a user is prevented
595 * @private For use from SessionBackend only
596 * @param string $username
599 public function isUserSessionPrevented( $username ) {
600 return !empty( $this->preventUsers
[$username] );
604 * Get the available SessionProviders
605 * @return SessionProvider[]
607 protected function getProviders() {
608 if ( $this->sessionProviders
=== null ) {
609 $this->sessionProviders
= [];
610 foreach ( $this->config
->get( 'SessionProviders' ) as $spec ) {
611 $provider = \ObjectFactory
::getObjectFromSpec( $spec );
612 $provider->setLogger( $this->logger
);
613 $provider->setConfig( $this->config
);
614 $provider->setManager( $this );
615 if ( isset( $this->sessionProviders
[(string)$provider] ) ) {
616 throw new \
UnexpectedValueException( "Duplicate provider name \"$provider\"" );
618 $this->sessionProviders
[(string)$provider] = $provider;
621 return $this->sessionProviders
;
625 * Get a session provider by name
627 * Generally, this will only be used by internal implementation of some
628 * special session-providing mechanism. General purpose code, if it needs
629 * to access a SessionProvider at all, will use Session::getProvider().
631 * @param string $name
632 * @return SessionProvider|null
634 public function getProvider( $name ) {
635 $providers = $this->getProviders();
636 return isset( $providers[$name] ) ?
$providers[$name] : null;
640 * Save all active sessions on shutdown
641 * @private For internal use with register_shutdown_function()
643 public function shutdown() {
644 if ( $this->allSessionBackends
) {
645 $this->logger
->debug( 'Saving all sessions on shutdown' );
646 if ( session_id() !== '' ) {
647 // @codeCoverageIgnoreStart
648 session_write_close();
650 // @codeCoverageIgnoreEnd
651 foreach ( $this->allSessionBackends
as $backend ) {
652 $backend->shutdown();
658 * Fetch the SessionInfo(s) for a request
659 * @param WebRequest $request
660 * @return SessionInfo|null
662 private function getSessionInfoForRequest( WebRequest
$request ) {
663 // Call all providers to fetch "the" session
665 foreach ( $this->getProviders() as $provider ) {
666 $info = $provider->provideSessionInfo( $request );
670 if ( $info->getProvider() !== $provider ) {
671 throw new \
UnexpectedValueException(
672 "$provider returned session info for a different provider: $info"
678 // Sort the SessionInfos. Then find the first one that can be
679 // successfully loaded, and then all the ones after it with the same
681 usort( $infos, 'MediaWiki\\Session\\SessionInfo::compare' );
684 $info = array_pop( $infos );
685 if ( $this->loadSessionInfoFromStore( $info, $request ) ) {
688 $info = array_pop( $infos );
689 if ( SessionInfo
::compare( $retInfos[0], $info ) ) {
690 // We hit a lower priority, stop checking.
693 if ( $this->loadSessionInfoFromStore( $info, $request ) ) {
694 // This is going to error out below, but we want to
695 // provide a complete list.
698 // Session load failed, so unpersist it from this request
699 $info->getProvider()->unpersistSession( $request );
703 // Session load failed, so unpersist it from this request
704 $info->getProvider()->unpersistSession( $request );
708 if ( count( $retInfos ) > 1 ) {
709 $ex = new \
OverflowException(
710 'Multiple sessions for this request tied for top priority: ' . implode( ', ', $retInfos )
712 $ex->sessionInfos
= $retInfos;
716 return $retInfos ?
$retInfos[0] : null;
720 * Load and verify the session info against the store
722 * @param SessionInfo &$info Will likely be replaced with an updated SessionInfo instance
723 * @param WebRequest $request
724 * @return bool Whether the session info matches the stored data (if any)
726 private function loadSessionInfoFromStore( SessionInfo
&$info, WebRequest
$request ) {
727 $key = wfMemcKey( 'MWSession', $info->getId() );
728 $blob = $this->store
->get( $key );
730 // If we got data from the store and the SessionInfo says to force use,
731 // "fail" means to delete the data from the store and retry. Otherwise,
732 // "fail" is just return false.
733 if ( $info->forceUse() && $blob !== false ) {
734 $failHandler = function () use ( $key, &$info, $request ) {
735 $this->store
->delete( $key );
736 return $this->loadSessionInfoFromStore( $info, $request );
739 $failHandler = function () {
746 if ( $blob !== false ) {
747 // Sanity check: blob must be an array, if it's saved at all
748 if ( !is_array( $blob ) ) {
749 $this->logger
->warning( 'Session "{session}": Bad data', [
752 $this->store
->delete( $key );
753 return $failHandler();
756 // Sanity check: blob has data and metadata arrays
757 if ( !isset( $blob['data'] ) ||
!is_array( $blob['data'] ) ||
758 !isset( $blob['metadata'] ) ||
!is_array( $blob['metadata'] )
760 $this->logger
->warning( 'Session "{session}": Bad data structure', [
763 $this->store
->delete( $key );
764 return $failHandler();
767 $data = $blob['data'];
768 $metadata = $blob['metadata'];
770 // Sanity check: metadata must be an array and must contain certain
771 // keys, if it's saved at all
772 if ( !array_key_exists( 'userId', $metadata ) ||
773 !array_key_exists( 'userName', $metadata ) ||
774 !array_key_exists( 'userToken', $metadata ) ||
775 !array_key_exists( 'provider', $metadata )
777 $this->logger
->warning( 'Session "{session}": Bad metadata', [
780 $this->store
->delete( $key );
781 return $failHandler();
784 // First, load the provider from metadata, or validate it against the metadata.
785 $provider = $info->getProvider();
786 if ( $provider === null ) {
787 $newParams['provider'] = $provider = $this->getProvider( $metadata['provider'] );
789 $this->logger
->warning(
790 'Session "{session}": Unknown provider ' . $metadata['provider'],
795 $this->store
->delete( $key );
796 return $failHandler();
798 } elseif ( $metadata['provider'] !== (string)$provider ) {
799 $this->logger
->warning( 'Session "{session}": Wrong provider ' .
800 $metadata['provider'] . ' !== ' . $provider,
804 return $failHandler();
807 // Load provider metadata from metadata, or validate it against the metadata
808 $providerMetadata = $info->getProviderMetadata();
809 if ( isset( $metadata['providerMetadata'] ) ) {
810 if ( $providerMetadata === null ) {
811 $newParams['metadata'] = $metadata['providerMetadata'];
814 $newProviderMetadata = $provider->mergeMetadata(
815 $metadata['providerMetadata'], $providerMetadata
817 if ( $newProviderMetadata !== $providerMetadata ) {
818 $newParams['metadata'] = $newProviderMetadata;
820 } catch ( MetadataMergeException
$ex ) {
821 $this->logger
->warning(
822 'Session "{session}": Metadata merge failed: {exception}',
826 ] +
$ex->getContext()
828 return $failHandler();
833 // Next, load the user from metadata, or validate it against the metadata.
834 $userInfo = $info->getUserInfo();
836 // For loading, id is preferred to name.
838 if ( $metadata['userId'] ) {
839 $userInfo = UserInfo
::newFromId( $metadata['userId'] );
840 } elseif ( $metadata['userName'] !== null ) { // Shouldn't happen, but just in case
841 $userInfo = UserInfo
::newFromName( $metadata['userName'] );
843 $userInfo = UserInfo
::newAnonymous();
845 } catch ( \InvalidArgumentException
$ex ) {
846 $this->logger
->error( 'Session "{session}": {exception}', [
850 return $failHandler();
852 $newParams['userInfo'] = $userInfo;
854 // User validation passes if user ID matches, or if there
855 // is no saved ID and the names match.
856 if ( $metadata['userId'] ) {
857 if ( $metadata['userId'] !== $userInfo->getId() ) {
858 $this->logger
->warning(
859 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}',
862 'uid_a' => $metadata['userId'],
863 'uid_b' => $userInfo->getId(),
865 return $failHandler();
868 // If the user was renamed, probably best to fail here.
869 if ( $metadata['userName'] !== null &&
870 $userInfo->getName() !== $metadata['userName']
872 $this->logger
->warning(
873 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}',
876 'uname_a' => $metadata['userName'],
877 'uname_b' => $userInfo->getName(),
879 return $failHandler();
882 } elseif ( $metadata['userName'] !== null ) { // Shouldn't happen, but just in case
883 if ( $metadata['userName'] !== $userInfo->getName() ) {
884 $this->logger
->warning(
885 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}',
888 'uname_a' => $metadata['userName'],
889 'uname_b' => $userInfo->getName(),
891 return $failHandler();
893 } elseif ( !$userInfo->isAnon() ) {
894 // Metadata specifies an anonymous user, but the passed-in
895 // user isn't anonymous.
896 $this->logger
->warning(
897 'Session "{session}": Metadata has an anonymous user, but a non-anon user was provided',
901 return $failHandler();
905 // And if we have a token in the metadata, it must match the loaded/provided user.
906 if ( $metadata['userToken'] !== null &&
907 $userInfo->getToken() !== $metadata['userToken']
909 $this->logger
->warning( 'Session "{session}": User token mismatch', [
912 return $failHandler();
914 if ( !$userInfo->isVerified() ) {
915 $newParams['userInfo'] = $userInfo->verified();
918 if ( !empty( $metadata['remember'] ) && !$info->wasRemembered() ) {
919 $newParams['remembered'] = true;
921 if ( !empty( $metadata['forceHTTPS'] ) && !$info->forceHTTPS() ) {
922 $newParams['forceHTTPS'] = true;
924 if ( !empty( $metadata['persisted'] ) && !$info->wasPersisted() ) {
925 $newParams['persisted'] = true;
928 if ( !$info->isIdSafe() ) {
929 $newParams['idIsSafe'] = true;
932 // No metadata, so we can't load the provider if one wasn't given.
933 if ( $info->getProvider() === null ) {
934 $this->logger
->warning(
935 'Session "{session}": Null provider and no metadata',
939 return $failHandler();
942 // If no user was provided and no metadata, it must be anon.
943 if ( !$info->getUserInfo() ) {
944 if ( $info->getProvider()->canChangeUser() ) {
945 $newParams['userInfo'] = UserInfo
::newAnonymous();
948 'Session "{session}": No user provided and provider cannot set user',
952 return $failHandler();
954 } elseif ( !$info->getUserInfo()->isVerified() ) {
955 $this->logger
->warning(
956 'Session "{session}": Unverified user provided and no metadata to auth it',
960 return $failHandler();
966 if ( !$info->getProvider()->persistsSessionId() && !$info->isIdSafe() ) {
967 // The ID doesn't come from the user, so it should be safe
968 // (and if not, nothing we can do about it anyway)
969 $newParams['idIsSafe'] = true;
973 // Construct the replacement SessionInfo, if necessary
975 $newParams['copyFrom'] = $info;
976 $info = new SessionInfo( $info->getPriority(), $newParams );
979 // Allow the provider to check the loaded SessionInfo
980 $providerMetadata = $info->getProviderMetadata();
981 if ( !$info->getProvider()->refreshSessionInfo( $info, $request, $providerMetadata ) ) {
982 return $failHandler();
984 if ( $providerMetadata !== $info->getProviderMetadata() ) {
985 $info = new SessionInfo( $info->getPriority(), [
986 'metadata' => $providerMetadata,
991 // Give hooks a chance to abort. Combined with the SessionMetadata
992 // hook, this can allow for tying a session to an IP address or the
994 $reason = 'Hook aborted';
997 [ &$reason, $info, $request, $metadata, $data ]
999 $this->logger
->warning( 'Session "{session}": ' . $reason, [
1002 return $failHandler();
1009 * Create a session corresponding to the passed SessionInfo
1010 * @private For use by a SessionProvider that needs to specially create its
1012 * @param SessionInfo $info
1013 * @param WebRequest $request
1016 public function getSessionFromInfo( SessionInfo
$info, WebRequest
$request ) {
1017 // @codeCoverageIgnoreStart
1018 if ( defined( 'MW_NO_SESSION' ) ) {
1019 if ( MW_NO_SESSION
=== 'warn' ) {
1020 // Undocumented safety case for converting existing entry points
1021 $this->logger
->error( 'Sessions are supposed to be disabled for this entry point', [
1022 'exception' => new \
BadMethodCallException( 'Sessions are disabled for this entry point' ),
1025 throw new \
BadMethodCallException( 'Sessions are disabled for this entry point' );
1028 // @codeCoverageIgnoreEnd
1030 $id = $info->getId();
1032 if ( !isset( $this->allSessionBackends
[$id] ) ) {
1033 if ( !isset( $this->allSessionIds
[$id] ) ) {
1034 $this->allSessionIds
[$id] = new SessionId( $id );
1036 $backend = new SessionBackend(
1037 $this->allSessionIds
[$id],
1041 $this->config
->get( 'ObjectCacheSessionExpiry' )
1043 $this->allSessionBackends
[$id] = $backend;
1044 $delay = $backend->delaySave();
1046 $backend = $this->allSessionBackends
[$id];
1047 $delay = $backend->delaySave();
1048 if ( $info->wasPersisted() ) {
1049 $backend->persist();
1051 if ( $info->wasRemembered() ) {
1052 $backend->setRememberUser( true );
1056 $request->setSessionId( $backend->getSessionId() );
1057 $session = $backend->getSession( $request );
1059 if ( !$info->isIdSafe() ) {
1060 $session->resetId();
1063 \ScopedCallback
::consume( $delay );
1068 * Deregister a SessionBackend
1069 * @private For use from \MediaWiki\Session\SessionBackend only
1070 * @param SessionBackend $backend
1072 public function deregisterSessionBackend( SessionBackend
$backend ) {
1073 $id = $backend->getId();
1074 if ( !isset( $this->allSessionBackends
[$id] ) ||
!isset( $this->allSessionIds
[$id] ) ||
1075 $this->allSessionBackends
[$id] !== $backend ||
1076 $this->allSessionIds
[$id] !== $backend->getSessionId()
1078 throw new \
InvalidArgumentException( 'Backend was not registered with this SessionManager' );
1081 unset( $this->allSessionBackends
[$id] );
1082 // Explicitly do not unset $this->allSessionIds[$id]
1086 * Change a SessionBackend's ID
1087 * @private For use from \MediaWiki\Session\SessionBackend only
1088 * @param SessionBackend $backend
1090 public function changeBackendId( SessionBackend
$backend ) {
1091 $sessionId = $backend->getSessionId();
1092 $oldId = (string)$sessionId;
1093 if ( !isset( $this->allSessionBackends
[$oldId] ) ||
!isset( $this->allSessionIds
[$oldId] ) ||
1094 $this->allSessionBackends
[$oldId] !== $backend ||
1095 $this->allSessionIds
[$oldId] !== $sessionId
1097 throw new \
InvalidArgumentException( 'Backend was not registered with this SessionManager' );
1100 $newId = $this->generateSessionId();
1102 unset( $this->allSessionBackends
[$oldId], $this->allSessionIds
[$oldId] );
1103 $sessionId->setId( $newId );
1104 $this->allSessionBackends
[$newId] = $backend;
1105 $this->allSessionIds
[$newId] = $sessionId;
1109 * Generate a new random session ID
1112 public function generateSessionId() {
1114 $id = \Wikimedia\base_convert
( \MWCryptRand
::generateHex( 40 ), 16, 32, 32 );
1115 $key = wfMemcKey( 'MWSession', $id );
1116 } while ( isset( $this->allSessionIds
[$id] ) ||
is_array( $this->store
->get( $key ) ) );
1121 * Call setters on a PHPSessionHandler
1122 * @private Use PhpSessionHandler::install()
1123 * @param PHPSessionHandler $handler
1125 public function setupPHPSessionHandler( PHPSessionHandler
$handler ) {
1126 $handler->setManager( $this, $this->store
, $this->logger
);
1130 * Reset the internal caching for unit testing
1132 public static function resetCache() {
1133 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
1134 // @codeCoverageIgnoreStart
1135 throw new MWException( __METHOD__
. ' may only be called from unit tests!' );
1136 // @codeCoverageIgnoreEnd
1139 self
::$globalSession = null;
1140 self
::$globalSessionRequest = null;