91 if ( self::$instance === null ) {
92 self::$instance =
new self();
94 return self::$instance;
114 !self::$globalSession
115 || self::$globalSessionRequest !==
$request
116 || $id !==
'' && self::$globalSession->getId() !== $id
118 self::$globalSessionRequest =
$request;
128 self::$globalSession =
$request->getSession();
133 self::$globalSession = self::singleton()->getSessionById( $id,
true,
$request )
137 return self::$globalSession;
147 if ( isset(
$options[
'config'] ) ) {
149 if ( !$this->config instanceof
Config ) {
150 throw new \InvalidArgumentException(
151 '$options[\'config\'] must be an instance of Config'
158 if ( isset(
$options[
'logger'] ) ) {
159 if ( !
$options[
'logger'] instanceof LoggerInterface ) {
160 throw new \InvalidArgumentException(
161 '$options[\'logger\'] must be an instance of LoggerInterface'
171 throw new \InvalidArgumentException(
172 '$options[\'store\'] must be an instance of BagOStuff'
181 register_shutdown_function( [ $this,
'shutdown' ] );
200 if ( !self::validateSessionId( $id ) ) {
201 throw new \InvalidArgumentException(
'Invalid session ID' );
211 if ( isset( $this->allSessionBackends[$id] ) ) {
217 if ( is_array( $this->
store->get( $key ) ) ) {
224 if ( $create && $session === null ) {
229 $this->logger->error(
'Failed to create empty session: {exception}',
231 'method' => __METHOD__,
252 if ( $id !== null ) {
253 if ( !self::validateSessionId( $id ) ) {
254 throw new \InvalidArgumentException(
'Invalid session ID' );
258 if ( is_array( $this->
store->get( $key ) ) ) {
259 throw new \InvalidArgumentException(
'Session ID already exists' );
268 $info = $provider->newSessionInfo( $id );
272 if ( $info->getProvider() !== $provider ) {
273 throw new \UnexpectedValueException(
274 "$provider returned an empty session info for a different provider: $info"
277 if ( $id !== null && $info->getId() !== $id ) {
278 throw new \UnexpectedValueException(
279 "$provider returned empty session info with a wrong id: " .
280 $info->getId() .
' != ' . $id
283 if ( !$info->isIdSafe() ) {
284 throw new \UnexpectedValueException(
285 "$provider returned empty session info with id flagged unsafe"
289 if ( $compare > 0 ) {
292 if ( $compare === 0 ) {
300 if ( count( $infos ) > 1 ) {
301 throw new \UnexpectedValueException(
302 'Multiple empty sessions tied for top priority: ' . implode(
', ', $infos )
304 } elseif ( count( $infos ) < 1 ) {
305 throw new \UnexpectedValueException(
'No provider could provide an empty session!' );
317 $authUser->resetAuthToken();
321 $provider->invalidateSessionsForUser( $user );
327 if ( defined(
'MW_NO_SESSION' ) &&
MW_NO_SESSION !==
'warn' ) {
331 if ( $this->varyHeaders === null ) {
335 if ( !isset( $headers[$header] ) ) {
339 $headers[
$header] = array_unique( array_merge( $headers[$header],
$options ) );
343 $this->varyHeaders = $headers;
350 if ( defined(
'MW_NO_SESSION' ) &&
MW_NO_SESSION !==
'warn' ) {
354 if ( $this->varyCookies === null ) {
357 $cookies = array_merge( $cookies, $provider->getVaryCookies() );
359 $this->varyCookies = array_values( array_unique( $cookies ) );
370 return is_string( $id ) && preg_match(
'/^[a-zA-Z0-9_-]{32,}$/', $id );
388 return \MediaWiki\Auth\AuthManager::singleton()->autoCreateUser(
390 \
MediaWiki\Auth\AuthManager::AUTOCREATE_SOURCE_SESSION,
409 $provider->preventSessionsForUser(
$username );
420 return !empty( $this->preventUsers[
$username] );
428 if ( $this->sessionProviders === null ) {
429 $this->sessionProviders = [];
430 foreach ( $this->config->get(
'SessionProviders' )
as $spec ) {
432 $provider->setLogger( $this->logger );
433 $provider->setConfig( $this->config );
434 $provider->setManager( $this );
435 if ( isset( $this->sessionProviders[(
string)$provider] ) ) {
436 throw new \UnexpectedValueException(
"Duplicate provider name \"$provider\"" );
438 $this->sessionProviders[(
string)$provider] = $provider;
456 return isset( $providers[
$name] ) ? $providers[
$name] : null;
464 if ( $this->allSessionBackends ) {
465 $this->logger->debug(
'Saving all sessions on shutdown' );
466 if ( session_id() !==
'' ) {
468 session_write_close();
471 foreach ( $this->allSessionBackends
as $backend ) {
472 $backend->shutdown();
486 $info = $provider->provideSessionInfo( $request );
490 if ( $info->getProvider() !== $provider ) {
491 throw new \UnexpectedValueException(
492 "$provider returned session info for a different provider: $info"
501 usort( $infos,
'MediaWiki\\Session\\SessionInfo::compare' );
504 $info = array_pop( $infos );
508 $info = array_pop( $infos );
519 $info->getProvider()->unpersistSession( $request );
524 $info->getProvider()->unpersistSession( $request );
528 if ( count( $retInfos ) > 1 ) {
529 $ex = new \OverflowException(
530 'Multiple sessions for this request tied for top priority: ' . implode(
', ', $retInfos )
532 $ex->sessionInfos = $retInfos;
536 return $retInfos ? $retInfos[0] : null;
554 $failHandler =
function ()
use ( $key, &$info, $request ) {
555 $this->
store->delete( $key );
559 $failHandler =
function () {
566 if ( $blob !==
false ) {
568 if ( !is_array( $blob ) ) {
569 $this->logger->warning(
'Session "{session}": Bad data', [
572 $this->
store->delete( $key );
573 return $failHandler();
577 if ( !isset( $blob[
'data'] ) || !is_array( $blob[
'data'] ) ||
578 !isset( $blob[
'metadata'] ) || !is_array( $blob[
'metadata'] )
580 $this->logger->warning(
'Session "{session}": Bad data structure', [
583 $this->
store->delete( $key );
584 return $failHandler();
587 $data = $blob[
'data'];
588 $metadata = $blob[
'metadata'];
592 if ( !array_key_exists(
'userId', $metadata ) ||
593 !array_key_exists(
'userName', $metadata ) ||
594 !array_key_exists(
'userToken', $metadata ) ||
595 !array_key_exists(
'provider', $metadata )
597 $this->logger->warning(
'Session "{session}": Bad metadata', [
600 $this->
store->delete( $key );
601 return $failHandler();
606 if ( $provider === null ) {
607 $newParams[
'provider'] = $provider = $this->
getProvider( $metadata[
'provider'] );
609 $this->logger->warning(
610 'Session "{session}": Unknown provider ' . $metadata[
'provider'],
615 $this->
store->delete( $key );
616 return $failHandler();
618 } elseif ( $metadata[
'provider'] !== (
string)$provider ) {
619 $this->logger->warning(
'Session "{session}": Wrong provider ' .
620 $metadata[
'provider'] .
' !== ' . $provider,
624 return $failHandler();
629 if ( isset( $metadata[
'providerMetadata'] ) ) {
630 if ( $providerMetadata === null ) {
631 $newParams[
'metadata'] = $metadata[
'providerMetadata'];
634 $newProviderMetadata = $provider->mergeMetadata(
635 $metadata[
'providerMetadata'], $providerMetadata
637 if ( $newProviderMetadata !== $providerMetadata ) {
638 $newParams[
'metadata'] = $newProviderMetadata;
641 $this->logger->warning(
642 'Session "{session}": Metadata merge failed: {exception}',
648 return $failHandler();
658 if ( $metadata[
'userId'] ) {
660 } elseif ( $metadata[
'userName'] !== null ) {
666 $this->logger->error(
'Session "{session}": {exception}', [
670 return $failHandler();
672 $newParams[
'userInfo'] = $userInfo;
676 if ( $metadata[
'userId'] ) {
677 if ( $metadata[
'userId'] !== $userInfo->getId() ) {
678 $this->logger->warning(
679 'Session "{session}": User ID mismatch, {uid_a} !== {uid_b}',
682 'uid_a' => $metadata[
'userId'],
683 'uid_b' => $userInfo->
getId(),
685 return $failHandler();
689 if ( $metadata[
'userName'] !== null &&
690 $userInfo->getName() !== $metadata[
'userName']
692 $this->logger->warning(
693 'Session "{session}": User ID matched but name didn\'t (rename?), {uname_a} !== {uname_b}',
696 'uname_a' => $metadata[
'userName'],
697 'uname_b' => $userInfo->getName(),
699 return $failHandler();
702 } elseif ( $metadata[
'userName'] !== null ) {
703 if ( $metadata[
'userName'] !== $userInfo->getName() ) {
704 $this->logger->warning(
705 'Session "{session}": User name mismatch, {uname_a} !== {uname_b}',
708 'uname_a' => $metadata[
'userName'],
709 'uname_b' => $userInfo->getName(),
711 return $failHandler();
713 } elseif ( !$userInfo->isAnon() ) {
716 $this->logger->warning(
717 'Session "{session}": Metadata has an anonymous user, but a non-anon user was provided',
721 return $failHandler();
726 if ( $metadata[
'userToken'] !== null &&
727 $userInfo->getToken() !== $metadata[
'userToken']
729 $this->logger->warning(
'Session "{session}": User token mismatch', [
732 return $failHandler();
734 if ( !$userInfo->isVerified() ) {
735 $newParams[
'userInfo'] = $userInfo->verified();
738 if ( !empty( $metadata[
'remember'] ) && !$info->
wasRemembered() ) {
739 $newParams[
'remembered'] =
true;
741 if ( !empty( $metadata[
'forceHTTPS'] ) && !$info->
forceHTTPS() ) {
742 $newParams[
'forceHTTPS'] =
true;
744 if ( !empty( $metadata[
'persisted'] ) && !$info->
wasPersisted() ) {
745 $newParams[
'persisted'] =
true;
749 $newParams[
'idIsSafe'] =
true;
754 $this->logger->warning(
755 'Session "{session}": Null provider and no metadata',
759 return $failHandler();
768 'Session "{session}": No user provided and provider cannot set user',
772 return $failHandler();
775 $this->logger->warning(
776 'Session "{session}": Unverified user provided and no metadata to auth it',
780 return $failHandler();
789 $newParams[
'idIsSafe'] =
true;
795 $newParams[
'copyFrom'] = $info;
801 if ( !$info->
getProvider()->refreshSessionInfo( $info, $request, $providerMetadata ) ) {
802 return $failHandler();
806 'metadata' => $providerMetadata,
814 $reason =
'Hook aborted';
817 [ &$reason, $info, $request, $metadata, $data ]
819 $this->logger->warning(
'Session "{session}": ' . $reason, [
822 return $failHandler();
838 if ( defined(
'MW_NO_SESSION' ) ) {
841 $this->logger->error(
'Sessions are supposed to be disabled for this entry point', [
842 'exception' =>
new \BadMethodCallException(
'Sessions are disabled for this entry point' ),
845 throw new \BadMethodCallException(
'Sessions are disabled for this entry point' );
850 $id = $info->
getId();
852 if ( !isset( $this->allSessionBackends[$id] ) ) {
853 if ( !isset( $this->allSessionIds[$id] ) ) {
854 $this->allSessionIds[$id] =
new SessionId( $id );
857 $this->allSessionIds[$id],
861 $this->config->get(
'ObjectCacheSessionExpiry' )
863 $this->allSessionBackends[$id] = $backend;
866 $backend = $this->allSessionBackends[$id];
867 $delay = $backend->delaySave();
872 $backend->setRememberUser(
true );
877 $session = $backend->getSession( $request );
883 \Wikimedia\ScopedCallback::consume( $delay );
893 $id = $backend->
getId();
894 if ( !isset( $this->allSessionBackends[$id] ) || !isset( $this->allSessionIds[$id] ) ||
895 $this->allSessionBackends[$id] !== $backend ||
898 throw new \InvalidArgumentException(
'Backend was not registered with this SessionManager' );
901 unset( $this->allSessionBackends[$id] );
912 $oldId = (
string)$sessionId;
913 if ( !isset( $this->allSessionBackends[$oldId] ) || !isset( $this->allSessionIds[$oldId] ) ||
914 $this->allSessionBackends[$oldId] !== $backend ||
915 $this->allSessionIds[$oldId] !== $sessionId
917 throw new \InvalidArgumentException(
'Backend was not registered with this SessionManager' );
922 unset( $this->allSessionBackends[$oldId], $this->allSessionIds[$oldId] );
923 $sessionId->setId( $newId );
924 $this->allSessionBackends[$newId] = $backend;
925 $this->allSessionIds[$newId] = $sessionId;
936 }
while ( isset( $this->allSessionIds[$id] ) || is_array( $this->
store->get( $key ) ) );
954 if ( !defined(
'MW_PHPUNIT_TEST' ) ) {
956 throw new MWException( __METHOD__ .
' may only be called from unit tests!' );
960 self::$globalSession = null;
961 self::$globalSessionRequest = null;
static getObjectFromSpec($spec)
Instantiate an object based on a specification array.
saveSettings()
Save this user's settings into the database.
processing should stop and the error should be shown to the user * false
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static getInstance($id)
Get a cached instance of the specified type of cache object.
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
setToken($token=false)
Set the random token (used for persistent authentication) Called from loadDefaults() among other plac...
MediaWiki s SiteStore can be cached and stored in a flat in a json format If the SiteStore is frequently the file cache may provide a performance benefit over a database store
static getMain()
Static methods.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
wfDeprecated($function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
setSessionId(SessionId $sessionId)
Set the session for this request.
static getDefaultInstance()
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
this hook is for auditing only or null if authentication failed before getting that far $username
error also a ContextSource you ll probably need to make sure the header is varied on $request
static generateHex($chars, $forceStrong=false)
Generate a run of (ideally) cryptographically random data and return it in hexadecimal string format...
wfMemcKey()
Make a cache key for the local wiki.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Allows to change the fields on the form that will be generated $name