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
377 * @codeCoverageIgnore
379 public static function autoCreateUser( User
$user ) {
380 wfDeprecated( __METHOD__
, '1.27' );
381 return \MediaWiki\Auth\AuthManager
::singleton()->autoCreateUser(
383 \MediaWiki\Auth\AuthManager
::AUTOCREATE_SOURCE_SESSION
,
389 * Prevent future sessions for the user
391 * The intention is that the named account will never again be usable for
392 * normal login (i.e. there is no way to undo the prevention of access).
394 * @private For use from \User::newSystemUser only
395 * @param string $username
397 public function preventSessionsForUser( $username ) {
398 $this->preventUsers
[$username] = true;
400 // Instruct the session providers to kill any other sessions too.
401 foreach ( $this->getProviders() as $provider ) {
402 $provider->preventSessionsForUser( $username );
407 * Test if a user is prevented
408 * @private For use from SessionBackend only
409 * @param string $username
412 public function isUserSessionPrevented( $username ) {
413 return !empty( $this->preventUsers
[$username] );
417 * Get the available SessionProviders
418 * @return SessionProvider[]
420 protected function getProviders() {
421 if ( $this->sessionProviders
=== null ) {
422 $this->sessionProviders
= [];
423 foreach ( $this->config
->get( 'SessionProviders' ) as $spec ) {
424 $provider = \ObjectFactory
::getObjectFromSpec( $spec );
425 $provider->setLogger( $this->logger
);
426 $provider->setConfig( $this->config
);
427 $provider->setManager( $this );
428 if ( isset( $this->sessionProviders
[(string)$provider] ) ) {
429 throw new \
UnexpectedValueException( "Duplicate provider name \"$provider\"" );
431 $this->sessionProviders
[(string)$provider] = $provider;
434 return $this->sessionProviders
;
438 * Get a session provider by name
440 * Generally, this will only be used by internal implementation of some
441 * special session-providing mechanism. General purpose code, if it needs
442 * to access a SessionProvider at all, will use Session::getProvider().
444 * @param string $name
445 * @return SessionProvider|null
447 public function getProvider( $name ) {
448 $providers = $this->getProviders();
449 return isset( $providers[$name] ) ?
$providers[$name] : null;
453 * Save all active sessions on shutdown
454 * @private For internal use with register_shutdown_function()
456 public function shutdown() {
457 if ( $this->allSessionBackends
) {
458 $this->logger
->debug( 'Saving all sessions on shutdown' );
459 if ( session_id() !== '' ) {
460 // @codeCoverageIgnoreStart
461 session_write_close();
463 // @codeCoverageIgnoreEnd
464 foreach ( $this->allSessionBackends
as $backend ) {
465 $backend->shutdown();
471 * Fetch the SessionInfo(s) for a request
472 * @param WebRequest $request
473 * @return SessionInfo|null
475 private function getSessionInfoForRequest( WebRequest
$request ) {
476 // Call all providers to fetch "the" session
478 foreach ( $this->getProviders() as $provider ) {
479 $info = $provider->provideSessionInfo( $request );
483 if ( $info->getProvider() !== $provider ) {
484 throw new \
UnexpectedValueException(
485 "$provider returned session info for a different provider: $info"
491 // Sort the SessionInfos. Then find the first one that can be
492 // successfully loaded, and then all the ones after it with the same
494 usort( $infos, 'MediaWiki\\Session\\SessionInfo::compare' );
497 $info = array_pop( $infos );
498 if ( $this->loadSessionInfoFromStore( $info, $request ) ) {
501 $info = array_pop( $infos );
502 if ( SessionInfo
::compare( $retInfos[0], $info ) ) {
503 // We hit a lower priority, stop checking.
506 if ( $this->loadSessionInfoFromStore( $info, $request ) ) {
507 // This is going to error out below, but we want to
508 // provide a complete list.
511 // Session load failed, so unpersist it from this request
512 $info->getProvider()->unpersistSession( $request );
516 // Session load failed, so unpersist it from this request
517 $info->getProvider()->unpersistSession( $request );
521 if ( count( $retInfos ) > 1 ) {
522 $ex = new \
OverflowException(
523 'Multiple sessions for this request tied for top priority: ' . implode( ', ', $retInfos )
525 $ex->sessionInfos
= $retInfos;
529 return $retInfos ?
$retInfos[0] : null;
533 * Load and verify the session info against the store
535 * @param SessionInfo &$info Will likely be replaced with an updated SessionInfo instance
536 * @param WebRequest $request
537 * @return bool Whether the session info matches the stored data (if any)
539 private function loadSessionInfoFromStore( SessionInfo
&$info, WebRequest
$request ) {
540 $key = wfMemcKey( 'MWSession', $info->getId() );
541 $blob = $this->store
->get( $key );
543 // If we got data from the store and the SessionInfo says to force use,
544 // "fail" means to delete the data from the store and retry. Otherwise,
545 // "fail" is just return false.
546 if ( $info->forceUse() && $blob !== false ) {
547 $failHandler = function () use ( $key, &$info, $request ) {
548 $this->store
->delete( $key );
549 return $this->loadSessionInfoFromStore( $info, $request );
552 $failHandler = function () {
559 if ( $blob !== false ) {
560 // Sanity check: blob must be an array, if it's saved at all
561 if ( !is_array( $blob ) ) {
562 $this->logger
->warning( 'Session "{session}": Bad data', [
565 $this->store
->delete( $key );
566 return $failHandler();
569 // Sanity check: blob has data and metadata arrays
570 if ( !isset( $blob['data'] ) ||
!is_array( $blob['data'] ) ||
571 !isset( $blob['metadata'] ) ||
!is_array( $blob['metadata'] )
573 $this->logger
->warning( 'Session "{session}": Bad data structure', [
576 $this->store
->delete( $key );
577 return $failHandler();
580 $data = $blob['data'];
581 $metadata = $blob['metadata'];
583 // Sanity check: metadata must be an array and must contain certain
584 // keys, if it's saved at all
585 if ( !array_key_exists( 'userId', $metadata ) ||
586 !array_key_exists( 'userName', $metadata ) ||
587 !array_key_exists( 'userToken', $metadata ) ||
588 !array_key_exists( 'provider', $metadata )
590 $this->logger
->warning( 'Session "{session}": Bad metadata', [
593 $this->store
->delete( $key );
594 return $failHandler();
597 // First, load the provider from metadata, or validate it against the metadata.
598 $provider = $info->getProvider();
599 if ( $provider === null ) {
600 $newParams['provider'] = $provider = $this->getProvider( $metadata['provider'] );
602 $this->logger
->warning(
603 'Session "{session}": Unknown provider ' . $metadata['provider'],
608 $this->store
->delete( $key );
609 return $failHandler();
611 } elseif ( $metadata['provider'] !== (string)$provider ) {
612 $this->logger
->warning( 'Session "{session}": Wrong provider ' .
613 $metadata['provider'] . ' !== ' . $provider,
617 return $failHandler();
620 // Load provider metadata from metadata, or validate it against the metadata
621 $providerMetadata = $info->getProviderMetadata();
622 if ( isset( $metadata['providerMetadata'] ) ) {
623 if ( $providerMetadata === null ) {
624 $newParams['metadata'] = $metadata['providerMetadata'];
627 $newProviderMetadata = $provider->mergeMetadata(
628 $metadata['providerMetadata'], $providerMetadata
630 if ( $newProviderMetadata !== $providerMetadata ) {
631 $newParams['metadata'] = $newProviderMetadata;
633 } catch ( MetadataMergeException
$ex ) {
634 $this->logger
->warning(
635 'Session "{session}": Metadata merge failed: {exception}',
639 ] +
$ex->getContext()
641 return $failHandler();
646 // Next, load the user from metadata, or validate it against the metadata.
647 $userInfo = $info->getUserInfo();
649 // For loading, id is preferred to name.
651 if ( $metadata['userId'] ) {
652 $userInfo = UserInfo
::newFromId( $metadata['userId'] );
653 } elseif ( $metadata['userName'] !== null ) { // Shouldn't happen, but just in case
654 $userInfo = UserInfo
::newFromName( $metadata['userName'] );
656 $userInfo = UserInfo
::newAnonymous();
658 } catch ( \InvalidArgumentException
$ex ) {
659 $this->logger
->error( 'Session "{session}": {exception}', [
663 return $failHandler();
665 $newParams['userInfo'] = $userInfo;
667 // User validation passes if user ID matches, or if there
668 // is no saved ID and the names match.
669 if ( $metadata['userId'] ) {
670 if ( $metadata['userId'] !== $userInfo->getId() ) {
671 $this->logger
->warning(
672 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}',
675 'uid_a' => $metadata['userId'],
676 'uid_b' => $userInfo->getId(),
678 return $failHandler();
681 // If the user was renamed, probably best to fail here.
682 if ( $metadata['userName'] !== null &&
683 $userInfo->getName() !== $metadata['userName']
685 $this->logger
->warning(
686 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}',
689 'uname_a' => $metadata['userName'],
690 'uname_b' => $userInfo->getName(),
692 return $failHandler();
695 } elseif ( $metadata['userName'] !== null ) { // Shouldn't happen, but just in case
696 if ( $metadata['userName'] !== $userInfo->getName() ) {
697 $this->logger
->warning(
698 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}',
701 'uname_a' => $metadata['userName'],
702 'uname_b' => $userInfo->getName(),
704 return $failHandler();
706 } elseif ( !$userInfo->isAnon() ) {
707 // Metadata specifies an anonymous user, but the passed-in
708 // user isn't anonymous.
709 $this->logger
->warning(
710 'Session "{session}": Metadata has an anonymous user, but a non-anon user was provided',
714 return $failHandler();
718 // And if we have a token in the metadata, it must match the loaded/provided user.
719 if ( $metadata['userToken'] !== null &&
720 $userInfo->getToken() !== $metadata['userToken']
722 $this->logger
->warning( 'Session "{session}": User token mismatch', [
725 return $failHandler();
727 if ( !$userInfo->isVerified() ) {
728 $newParams['userInfo'] = $userInfo->verified();
731 if ( !empty( $metadata['remember'] ) && !$info->wasRemembered() ) {
732 $newParams['remembered'] = true;
734 if ( !empty( $metadata['forceHTTPS'] ) && !$info->forceHTTPS() ) {
735 $newParams['forceHTTPS'] = true;
737 if ( !empty( $metadata['persisted'] ) && !$info->wasPersisted() ) {
738 $newParams['persisted'] = true;
741 if ( !$info->isIdSafe() ) {
742 $newParams['idIsSafe'] = true;
745 // No metadata, so we can't load the provider if one wasn't given.
746 if ( $info->getProvider() === null ) {
747 $this->logger
->warning(
748 'Session "{session}": Null provider and no metadata',
752 return $failHandler();
755 // If no user was provided and no metadata, it must be anon.
756 if ( !$info->getUserInfo() ) {
757 if ( $info->getProvider()->canChangeUser() ) {
758 $newParams['userInfo'] = UserInfo
::newAnonymous();
761 'Session "{session}": No user provided and provider cannot set user',
765 return $failHandler();
767 } elseif ( !$info->getUserInfo()->isVerified() ) {
768 $this->logger
->warning(
769 'Session "{session}": Unverified user provided and no metadata to auth it',
773 return $failHandler();
779 if ( !$info->getProvider()->persistsSessionId() && !$info->isIdSafe() ) {
780 // The ID doesn't come from the user, so it should be safe
781 // (and if not, nothing we can do about it anyway)
782 $newParams['idIsSafe'] = true;
786 // Construct the replacement SessionInfo, if necessary
788 $newParams['copyFrom'] = $info;
789 $info = new SessionInfo( $info->getPriority(), $newParams );
792 // Allow the provider to check the loaded SessionInfo
793 $providerMetadata = $info->getProviderMetadata();
794 if ( !$info->getProvider()->refreshSessionInfo( $info, $request, $providerMetadata ) ) {
795 return $failHandler();
797 if ( $providerMetadata !== $info->getProviderMetadata() ) {
798 $info = new SessionInfo( $info->getPriority(), [
799 'metadata' => $providerMetadata,
804 // Give hooks a chance to abort. Combined with the SessionMetadata
805 // hook, this can allow for tying a session to an IP address or the
807 $reason = 'Hook aborted';
810 [ &$reason, $info, $request, $metadata, $data ]
812 $this->logger
->warning( 'Session "{session}": ' . $reason, [
815 return $failHandler();
822 * Create a session corresponding to the passed SessionInfo
823 * @private For use by a SessionProvider that needs to specially create its
825 * @param SessionInfo $info
826 * @param WebRequest $request
829 public function getSessionFromInfo( SessionInfo
$info, WebRequest
$request ) {
830 // @codeCoverageIgnoreStart
831 if ( defined( 'MW_NO_SESSION' ) ) {
832 if ( MW_NO_SESSION
=== 'warn' ) {
833 // Undocumented safety case for converting existing entry points
834 $this->logger
->error( 'Sessions are supposed to be disabled for this entry point', [
835 'exception' => new \
BadMethodCallException( 'Sessions are disabled for this entry point' ),
838 throw new \
BadMethodCallException( 'Sessions are disabled for this entry point' );
841 // @codeCoverageIgnoreEnd
843 $id = $info->getId();
845 if ( !isset( $this->allSessionBackends
[$id] ) ) {
846 if ( !isset( $this->allSessionIds
[$id] ) ) {
847 $this->allSessionIds
[$id] = new SessionId( $id );
849 $backend = new SessionBackend(
850 $this->allSessionIds
[$id],
854 $this->config
->get( 'ObjectCacheSessionExpiry' )
856 $this->allSessionBackends
[$id] = $backend;
857 $delay = $backend->delaySave();
859 $backend = $this->allSessionBackends
[$id];
860 $delay = $backend->delaySave();
861 if ( $info->wasPersisted() ) {
864 if ( $info->wasRemembered() ) {
865 $backend->setRememberUser( true );
869 $request->setSessionId( $backend->getSessionId() );
870 $session = $backend->getSession( $request );
872 if ( !$info->isIdSafe() ) {
876 \ScopedCallback
::consume( $delay );
881 * Deregister a SessionBackend
882 * @private For use from \MediaWiki\Session\SessionBackend only
883 * @param SessionBackend $backend
885 public function deregisterSessionBackend( SessionBackend
$backend ) {
886 $id = $backend->getId();
887 if ( !isset( $this->allSessionBackends
[$id] ) ||
!isset( $this->allSessionIds
[$id] ) ||
888 $this->allSessionBackends
[$id] !== $backend ||
889 $this->allSessionIds
[$id] !== $backend->getSessionId()
891 throw new \
InvalidArgumentException( 'Backend was not registered with this SessionManager' );
894 unset( $this->allSessionBackends
[$id] );
895 // Explicitly do not unset $this->allSessionIds[$id]
899 * Change a SessionBackend's ID
900 * @private For use from \MediaWiki\Session\SessionBackend only
901 * @param SessionBackend $backend
903 public function changeBackendId( SessionBackend
$backend ) {
904 $sessionId = $backend->getSessionId();
905 $oldId = (string)$sessionId;
906 if ( !isset( $this->allSessionBackends
[$oldId] ) ||
!isset( $this->allSessionIds
[$oldId] ) ||
907 $this->allSessionBackends
[$oldId] !== $backend ||
908 $this->allSessionIds
[$oldId] !== $sessionId
910 throw new \
InvalidArgumentException( 'Backend was not registered with this SessionManager' );
913 $newId = $this->generateSessionId();
915 unset( $this->allSessionBackends
[$oldId], $this->allSessionIds
[$oldId] );
916 $sessionId->setId( $newId );
917 $this->allSessionBackends
[$newId] = $backend;
918 $this->allSessionIds
[$newId] = $sessionId;
922 * Generate a new random session ID
925 public function generateSessionId() {
927 $id = \Wikimedia\base_convert
( \MWCryptRand
::generateHex( 40 ), 16, 32, 32 );
928 $key = wfMemcKey( 'MWSession', $id );
929 } while ( isset( $this->allSessionIds
[$id] ) ||
is_array( $this->store
->get( $key ) ) );
934 * Call setters on a PHPSessionHandler
935 * @private Use PhpSessionHandler::install()
936 * @param PHPSessionHandler $handler
938 public function setupPHPSessionHandler( PHPSessionHandler
$handler ) {
939 $handler->setManager( $this, $this->store
, $this->logger
);
943 * Reset the internal caching for unit testing
945 public static function resetCache() {
946 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
947 // @codeCoverageIgnoreStart
948 throw new MWException( __METHOD__
. ' may only be called from unit tests!' );
949 // @codeCoverageIgnoreEnd
952 self
::$globalSession = null;
953 self
::$globalSessionRequest = null;