2016-02-01 20:44:03 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* MediaWiki session provider base class
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Session
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Session;
|
|
|
|
|
|
|
|
|
|
use Language;
|
2023-09-20 07:54:42 +00:00
|
|
|
use MediaWiki\Config\Config;
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
|
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2022-04-13 15:28:26 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-09-07 11:46:15 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2021-04-29 20:44:45 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2023-11-01 17:23:46 +00:00
|
|
|
use MWRestrictions;
|
2020-01-10 00:00:51 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A SessionProvider provides SessionInfo and support for Session
|
|
|
|
|
*
|
2022-10-27 13:14:16 +00:00
|
|
|
* A SessionProvider is responsible for taking a WebRequest and determining
|
2016-02-01 20:44:03 +00:00
|
|
|
* the authenticated session that it's a part of. It does this by returning an
|
|
|
|
|
* SessionInfo object with basic information about the session it thinks is
|
|
|
|
|
* associated with the request, namely the session ID and possibly the
|
|
|
|
|
* authenticated user the session belongs to.
|
|
|
|
|
*
|
|
|
|
|
* The SessionProvider also provides for updating the WebResponse with
|
|
|
|
|
* information necessary to provide the client with data that the client will
|
|
|
|
|
* send with later requests, and for populating the Vary and Key headers with
|
|
|
|
|
* the data necessary to correctly vary the cache on these client requests.
|
|
|
|
|
*
|
|
|
|
|
* An important part of the latter is indicating whether it even *can* tell the
|
|
|
|
|
* client to include such data in future requests, via the persistsSessionId()
|
|
|
|
|
* and canChangeUser() methods. The cases are (in order of decreasing
|
|
|
|
|
* commonness):
|
|
|
|
|
* - Cannot persist ID, no changing User: The request identifies and
|
|
|
|
|
* authenticates a particular local user, and the client cannot be
|
|
|
|
|
* instructed to include an arbitrary session ID with future requests. For
|
|
|
|
|
* example, OAuth or SSL certificate auth.
|
|
|
|
|
* - Can persist ID and can change User: The client can be instructed to
|
|
|
|
|
* return at least one piece of arbitrary data, that being the session ID.
|
|
|
|
|
* The user identity might also be given to the client, otherwise it's saved
|
|
|
|
|
* in the session data. For example, cookie-based sessions.
|
|
|
|
|
* - Can persist ID but no changing User: The request uniquely identifies and
|
|
|
|
|
* authenticates a local user, and the client can be instructed to return an
|
|
|
|
|
* arbitrary session ID with future requests. For example, HTTP Digest
|
|
|
|
|
* authentication might somehow use the 'opaque' field as a session ID
|
|
|
|
|
* (although getting MediaWiki to return 401 responses without breaking
|
|
|
|
|
* other stuff might be a challenge).
|
|
|
|
|
* - Cannot persist ID but can change User: I can't think of a way this
|
|
|
|
|
* would make sense.
|
|
|
|
|
*
|
|
|
|
|
* Note that many methods that are technically "cannot persist ID" could be
|
2016-08-27 05:40:37 +00:00
|
|
|
* turned into "can persist ID but not change User" using a session cookie,
|
2016-02-01 20:44:03 +00:00
|
|
|
* as implemented by ImmutableSessionProviderWithCookie. If doing so, different
|
|
|
|
|
* session cookie names should be used for different providers to avoid
|
|
|
|
|
* collisions.
|
|
|
|
|
*
|
2020-07-13 09:00:30 +00:00
|
|
|
* @stable to extend
|
2016-02-01 20:44:03 +00:00
|
|
|
* @ingroup Session
|
|
|
|
|
* @since 1.27
|
2016-08-27 05:40:37 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:SessionManager_and_AuthManager
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
2021-04-29 20:44:45 +00:00
|
|
|
abstract class SessionProvider implements SessionProviderInterface {
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
|
|
/** @var Config */
|
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
|
|
/** @var SessionManager */
|
|
|
|
|
protected $manager;
|
|
|
|
|
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
/** @var HookContainer */
|
|
|
|
|
private $hookContainer;
|
|
|
|
|
|
|
|
|
|
/** @var HookRunner */
|
|
|
|
|
private $hookRunner;
|
|
|
|
|
|
2021-04-29 20:44:45 +00:00
|
|
|
/** @var UserNameUtils */
|
|
|
|
|
protected $userNameUtils;
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/** @var int Session priority. Used for the default newSessionInfo(), but
|
|
|
|
|
* could be used by subclasses too.
|
|
|
|
|
*/
|
|
|
|
|
protected $priority;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-13 08:53:06 +00:00
|
|
|
* @stable to call
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct() {
|
|
|
|
|
$this->priority = SessionInfo::MIN_PRIORITY + 10;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 20:44:45 +00:00
|
|
|
/**
|
|
|
|
|
* Initialise with dependencies of a SessionProvider
|
|
|
|
|
*
|
|
|
|
|
* @since 1.37
|
2021-05-28 12:56:45 +00:00
|
|
|
* @internal In production code SessionManager will initialize the
|
|
|
|
|
* SessionProvider, in tests SessionProviderTestTrait must be used.
|
2021-04-29 20:44:45 +00:00
|
|
|
*
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
* @param Config $config
|
|
|
|
|
* @param SessionManager $manager
|
|
|
|
|
* @param HookContainer $hookContainer
|
|
|
|
|
* @param UserNameUtils $userNameUtils
|
|
|
|
|
*/
|
|
|
|
|
public function init(
|
|
|
|
|
LoggerInterface $logger,
|
|
|
|
|
Config $config,
|
|
|
|
|
SessionManager $manager,
|
|
|
|
|
HookContainer $hookContainer,
|
|
|
|
|
UserNameUtils $userNameUtils
|
|
|
|
|
) {
|
|
|
|
|
$this->logger = $logger;
|
2021-05-07 19:56:38 +00:00
|
|
|
$this->config = $config;
|
2021-04-29 20:44:45 +00:00
|
|
|
$this->manager = $manager;
|
|
|
|
|
$this->hookContainer = $hookContainer;
|
|
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
|
|
|
|
$this->userNameUtils = $userNameUtils;
|
|
|
|
|
$this->postInitSetup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A provider can override this to do any necessary setup after init()
|
|
|
|
|
* is called.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @stable to override
|
|
|
|
|
*/
|
|
|
|
|
protected function postInitSetup() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets a logger instance on the object.
|
|
|
|
|
*
|
|
|
|
|
* @deprecated since 1.37. For extension-defined session providers
|
|
|
|
|
* that were using this method to trigger other work, please override
|
|
|
|
|
* SessionProvider::postInitSetup instead. If your extension
|
|
|
|
|
* was using this to explicitly change the logger of an existing
|
|
|
|
|
* SessionProvider object, please file a report on phabricator
|
|
|
|
|
* - there is no non-deprecated way to do this anymore.
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
*/
|
2016-02-01 20:44:03 +00:00
|
|
|
public function setLogger( LoggerInterface $logger ) {
|
2021-05-07 19:56:38 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.37' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->logger = $logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set configuration
|
2021-04-29 20:44:45 +00:00
|
|
|
*
|
|
|
|
|
* @deprecated since 1.37. For extension-defined session providers
|
|
|
|
|
* that were using this method to trigger other work, please override
|
|
|
|
|
* SessionProvider::postInitSetup instead. If your extension
|
|
|
|
|
* was using this to explicitly change the Config of an existing
|
|
|
|
|
* SessionProvider object, please file a report on phabricator
|
|
|
|
|
* - there is no non-deprecated way to do this anymore.
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param Config $config
|
|
|
|
|
*/
|
|
|
|
|
public function setConfig( Config $config ) {
|
2021-05-07 19:56:38 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.37' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->config = $config;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 20:44:45 +00:00
|
|
|
/**
|
|
|
|
|
* Get the config
|
|
|
|
|
*
|
|
|
|
|
* @since 1.37
|
|
|
|
|
* @return Config
|
|
|
|
|
*/
|
|
|
|
|
protected function getConfig() {
|
|
|
|
|
return $this->config;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* Set the session manager
|
2021-04-29 20:44:45 +00:00
|
|
|
*
|
|
|
|
|
* @deprecated since 1.37. For extension-defined session providers
|
|
|
|
|
* that were using this method to trigger other work, please override
|
|
|
|
|
* SessionProvider::postInitSetup instead. If your extension
|
|
|
|
|
* was using this to explicitly change the SessionManager of an existing
|
|
|
|
|
* SessionProvider object, please file a report on phabricator
|
|
|
|
|
* - there is no non-deprecated way to do this anymore.
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param SessionManager $manager
|
|
|
|
|
*/
|
|
|
|
|
public function setManager( SessionManager $manager ) {
|
2021-05-07 19:56:38 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.37' );
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->manager = $manager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the session manager
|
|
|
|
|
* @return SessionManager
|
|
|
|
|
*/
|
|
|
|
|
public function getManager() {
|
|
|
|
|
return $this->manager;
|
|
|
|
|
}
|
|
|
|
|
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
/**
|
|
|
|
|
* @internal
|
2021-04-29 20:44:45 +00:00
|
|
|
* @deprecated since 1.37. For extension-defined session providers
|
|
|
|
|
* that were using this method to trigger other work, please override
|
|
|
|
|
* SessionProvider::postInitSetup instead. If your extension
|
|
|
|
|
* was using this to explicitly change the HookContainer of an existing
|
|
|
|
|
* SessionProvider object, please file a report on phabricator
|
|
|
|
|
* - there is no non-deprecated way to do this anymore.
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
* @param HookContainer $hookContainer
|
|
|
|
|
*/
|
|
|
|
|
public function setHookContainer( $hookContainer ) {
|
2021-05-07 19:56:38 +00:00
|
|
|
wfDeprecated( __METHOD__, '1.37' );
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->hookContainer = $hookContainer;
|
|
|
|
|
$this->hookRunner = new HookRunner( $hookContainer );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the HookContainer
|
|
|
|
|
*
|
|
|
|
|
* @return HookContainer
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function getHookContainer(): HookContainer {
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
return $this->hookContainer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the HookRunner
|
|
|
|
|
*
|
|
|
|
|
* @internal This is for use by core only. Hook interfaces may be removed
|
|
|
|
|
* without notice.
|
|
|
|
|
* @since 1.35
|
|
|
|
|
* @return HookRunner
|
|
|
|
|
*/
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function getHookRunner(): HookRunner {
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
return $this->hookRunner;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* Provide session info for a request
|
|
|
|
|
*
|
|
|
|
|
* If no session exists for the request, return null. Otherwise return an
|
|
|
|
|
* SessionInfo object identifying the session.
|
|
|
|
|
*
|
|
|
|
|
* If multiple SessionProviders provide sessions, the one with highest
|
|
|
|
|
* priority wins. In case of a tie, an exception is thrown.
|
|
|
|
|
* SessionProviders are encouraged to make priorities user-configurable
|
|
|
|
|
* unless only max-priority makes sense.
|
|
|
|
|
*
|
|
|
|
|
* @warning This will be called early in the MediaWiki setup process,
|
2019-04-11 13:36:15 +00:00
|
|
|
* before $wgUser, $wgLang, $wgOut, $wgTitle, the global parser, and
|
|
|
|
|
* corresponding pieces of the main RequestContext are set up! If you try
|
|
|
|
|
* to use these, things *will* break.
|
2016-02-01 20:44:03 +00:00
|
|
|
* @note The SessionProvider must not attempt to auto-create users.
|
|
|
|
|
* MediaWiki will do this later (when it's safe) if the chosen session has
|
|
|
|
|
* a user with a valid name but no ID.
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionManager only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param WebRequest $request
|
|
|
|
|
* @return SessionInfo|null
|
|
|
|
|
*/
|
|
|
|
|
abstract public function provideSessionInfo( WebRequest $request );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide session info for a new, empty session
|
|
|
|
|
*
|
|
|
|
|
* Return null if such a session cannot be created. This base
|
|
|
|
|
* implementation assumes that it only makes sense if a session ID can be
|
|
|
|
|
* persisted and changing users is allowed.
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2016-02-01 20:44:03 +00:00
|
|
|
*
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionManager only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param string|null $id ID to force for the new session
|
|
|
|
|
* @return SessionInfo|null
|
|
|
|
|
* If non-null, must return true for $info->isIdSafe(); pass true for
|
|
|
|
|
* $data['idIsSafe'] to ensure this.
|
|
|
|
|
*/
|
|
|
|
|
public function newSessionInfo( $id = null ) {
|
|
|
|
|
if ( $this->canChangeUser() && $this->persistsSessionId() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return new SessionInfo( $this->priority, [
|
2016-02-01 20:44:03 +00:00
|
|
|
'id' => $id,
|
|
|
|
|
'provider' => $this,
|
|
|
|
|
'persisted' => false,
|
|
|
|
|
'idIsSafe' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Merge saved session provider metadata
|
|
|
|
|
*
|
2016-08-27 05:40:37 +00:00
|
|
|
* This method will be used to compare the metadata returned by
|
|
|
|
|
* provideSessionInfo() with the saved metadata (which has been returned by
|
|
|
|
|
* provideSessionInfo() the last time the session was saved), and merge the two
|
|
|
|
|
* into the new saved metadata, or abort if the current request is not a valid
|
|
|
|
|
* continuation of the session.
|
|
|
|
|
*
|
2016-02-01 20:44:03 +00:00
|
|
|
* The default implementation checks that anything in both arrays is
|
|
|
|
|
* identical, then returns $providedMetadata.
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2016-02-01 20:44:03 +00:00
|
|
|
*
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionManager only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param array $savedMetadata Saved provider metadata
|
2016-08-27 05:40:37 +00:00
|
|
|
* @param array $providedMetadata Provided provider metadata (from the SessionInfo)
|
2016-02-01 20:44:03 +00:00
|
|
|
* @return array Resulting metadata
|
2016-08-27 05:40:37 +00:00
|
|
|
* @throws MetadataMergeException If the metadata cannot be merged.
|
|
|
|
|
* Such exceptions will be handled by SessionManager and are a safe way of rejecting
|
|
|
|
|
* a suspicious or incompatible session. The provider is expected to write an
|
|
|
|
|
* appropriate message to its logger.
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
public function mergeMetadata( array $savedMetadata, array $providedMetadata ) {
|
|
|
|
|
foreach ( $providedMetadata as $k => $v ) {
|
|
|
|
|
if ( array_key_exists( $k, $savedMetadata ) && $savedMetadata[$k] !== $v ) {
|
2016-02-11 16:55:37 +00:00
|
|
|
$e = new MetadataMergeException( "Key \"$k\" changed" );
|
|
|
|
|
$e->setContext( [
|
|
|
|
|
'old_value' => $savedMetadata[$k],
|
|
|
|
|
'new_value' => $v,
|
|
|
|
|
] );
|
|
|
|
|
throw $e;
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $providedMetadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate a loaded SessionInfo and refresh provider metadata
|
|
|
|
|
*
|
|
|
|
|
* This is similar in purpose to the 'SessionCheckInfo' hook, and also
|
|
|
|
|
* allows for updating the provider metadata. On failure, the provider is
|
|
|
|
|
* expected to write an appropriate message to its logger.
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2016-02-01 20:44:03 +00:00
|
|
|
*
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionManager only
|
2016-08-27 05:40:37 +00:00
|
|
|
* @param SessionInfo $info Any changes by mergeMetadata() will already be reflected here.
|
2022-10-27 13:14:16 +00:00
|
|
|
* @param WebRequest $request
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param array|null &$metadata Provider metadata, may be altered.
|
|
|
|
|
* @return bool Return false to reject the SessionInfo after all.
|
|
|
|
|
*/
|
|
|
|
|
public function refreshSessionInfo( SessionInfo $info, WebRequest $request, &$metadata ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicate whether self::persistSession() can save arbitrary session IDs
|
|
|
|
|
*
|
|
|
|
|
* If false, any session passed to self::persistSession() will have an ID
|
|
|
|
|
* that was originally provided by self::provideSessionInfo().
|
|
|
|
|
*
|
|
|
|
|
* If true, the provider may be passed sessions with arbitrary session IDs,
|
|
|
|
|
* and will be expected to manipulate the request in such a way that future
|
|
|
|
|
* requests will cause self::provideSessionInfo() to provide a SessionInfo
|
|
|
|
|
* with that ID.
|
|
|
|
|
*
|
|
|
|
|
* For example, a session provider for OAuth would function by matching the
|
|
|
|
|
* OAuth headers to a particular user, and then would use self::hashToSessionId()
|
|
|
|
|
* to turn the user and OAuth client ID (and maybe also the user token and
|
|
|
|
|
* client secret) into a session ID, and therefore can't easily assign that
|
|
|
|
|
* user+client a different ID. Similarly, a session provider for SSL client
|
|
|
|
|
* certificates would function by matching the certificate to a particular
|
|
|
|
|
* user, and then would use self::hashToSessionId() to turn the user and
|
|
|
|
|
* certificate fingerprint into a session ID, and therefore can't easily
|
|
|
|
|
* assign a different ID either. On the other hand, a provider that saves
|
|
|
|
|
* the session ID into a cookie can easily just set the cookie to a
|
|
|
|
|
* different value.
|
|
|
|
|
*
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionBackend only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
abstract public function persistsSessionId();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicate whether the user associated with the request can be changed
|
|
|
|
|
*
|
|
|
|
|
* If false, any session passed to self::persistSession() will have a user
|
|
|
|
|
* that was originally provided by self::provideSessionInfo(). Further,
|
|
|
|
|
* self::provideSessionInfo() may only provide sessions that have a user
|
|
|
|
|
* already set.
|
|
|
|
|
*
|
|
|
|
|
* If true, the provider may be passed sessions with arbitrary users, and
|
|
|
|
|
* will be expected to manipulate the request in such a way that future
|
|
|
|
|
* requests will cause self::provideSessionInfo() to provide a SessionInfo
|
|
|
|
|
* with that ID. This can be as simple as not passing any 'userInfo' into
|
|
|
|
|
* SessionInfo's constructor, in which case SessionInfo will load the user
|
|
|
|
|
* from the saved session's metadata.
|
|
|
|
|
*
|
|
|
|
|
* For example, a session provider for OAuth or SSL client certificates
|
|
|
|
|
* would function by matching the OAuth headers or certificate to a
|
|
|
|
|
* particular user, and thus would return false here since it can't
|
|
|
|
|
* arbitrarily assign those OAuth credentials or that certificate to a
|
|
|
|
|
* different user. A session provider that shoves information into cookies,
|
|
|
|
|
* on the other hand, could easily do so.
|
|
|
|
|
*
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionBackend only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
abstract public function canChangeUser();
|
|
|
|
|
|
2016-05-13 00:03:20 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the duration (in seconds) for which users will be remembered when
|
|
|
|
|
* Session::setRememberUser() is set. Null means setting the remember flag will
|
|
|
|
|
* have no effect (and endpoints should not offer that option).
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2016-05-13 00:03:20 +00:00
|
|
|
* @return int|null
|
|
|
|
|
*/
|
|
|
|
|
public function getRememberUserDuration() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* Notification that the session ID was reset
|
|
|
|
|
*
|
|
|
|
|
* No need to persist here, persistSession() will be called if appropriate.
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2016-02-01 20:44:03 +00:00
|
|
|
*
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionBackend only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param SessionBackend $session Session to persist
|
|
|
|
|
* @param string $oldId Old session ID
|
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
|
*/
|
|
|
|
|
public function sessionIdWasReset( SessionBackend $session, $oldId ) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Persist a session into a request/response
|
|
|
|
|
*
|
|
|
|
|
* For example, you might set cookies for the session's ID, user ID, user
|
|
|
|
|
* name, and user token on the passed request.
|
|
|
|
|
*
|
|
|
|
|
* To correctly persist a user independently of the session ID, the
|
|
|
|
|
* provider should persist both the user ID (or name, but preferably the
|
|
|
|
|
* ID) and the user token. When reading the data from the request, it
|
|
|
|
|
* should construct a User object from the ID/name and then verify that the
|
|
|
|
|
* User object's token matches the token included in the request. Should
|
|
|
|
|
* the tokens not match, an anonymous user *must* be passed to
|
|
|
|
|
* SessionInfo::__construct().
|
|
|
|
|
*
|
|
|
|
|
* When persisting a user independently of the session ID,
|
|
|
|
|
* $session->shouldRememberUser() should be checked first. If this returns
|
|
|
|
|
* false, the user token *must not* be saved to cookies. The user name
|
|
|
|
|
* and/or ID may be persisted, and should be used to construct an
|
|
|
|
|
* unverified UserInfo to pass to SessionInfo::__construct().
|
|
|
|
|
*
|
2022-01-09 17:58:53 +00:00
|
|
|
* A backend that cannot persist session ID or user info should implement
|
2016-02-01 20:44:03 +00:00
|
|
|
* this as a no-op.
|
|
|
|
|
*
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionBackend only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param SessionBackend $session Session to persist
|
2022-10-27 13:14:16 +00:00
|
|
|
* @param WebRequest $request Request into which to persist the session
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function persistSession( SessionBackend $session, WebRequest $request );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove any persisted session from a request/response
|
|
|
|
|
*
|
|
|
|
|
* For example, blank and expire any cookies set by self::persistSession().
|
|
|
|
|
*
|
2022-01-09 17:58:53 +00:00
|
|
|
* A backend that cannot persist session ID or user info should implement
|
2016-02-01 20:44:03 +00:00
|
|
|
* this as a no-op.
|
|
|
|
|
*
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionManager only
|
2022-10-27 13:14:16 +00:00
|
|
|
* @param WebRequest $request Request from which to remove any session data
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
abstract public function unpersistSession( WebRequest $request );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prevent future sessions for the user
|
|
|
|
|
*
|
|
|
|
|
* If the provider is capable of returning a SessionInfo with a verified
|
|
|
|
|
* UserInfo for the named user in some manner other than by validating
|
|
|
|
|
* against $user->getToken(), steps must be taken to prevent that from
|
2021-04-18 15:49:35 +00:00
|
|
|
* occurring in the future. This might add the username to a list, or
|
2016-02-01 20:44:03 +00:00
|
|
|
* it might just delete whatever authentication credentials would allow
|
|
|
|
|
* such a session in the first place (e.g. remove all OAuth grants or
|
|
|
|
|
* delete record of the SSL client certificate).
|
|
|
|
|
*
|
|
|
|
|
* The intention is that the named account will never again be usable for
|
|
|
|
|
* normal login (i.e. there is no way to undo the prevention of access).
|
|
|
|
|
*
|
|
|
|
|
* Note that the passed user name might not exist locally (i.e.
|
2022-12-22 17:38:02 +00:00
|
|
|
* UserIdentity::getId() === 0); the name should still be
|
2016-02-01 20:44:03 +00:00
|
|
|
* prevented, if applicable.
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionManager only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param string $username
|
|
|
|
|
*/
|
|
|
|
|
public function preventSessionsForUser( $username ) {
|
|
|
|
|
if ( !$this->canChangeUser() ) {
|
|
|
|
|
throw new \BadMethodCallException(
|
2019-03-21 15:10:02 +00:00
|
|
|
__METHOD__ . ' must be implemented when canChangeUser() is false'
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-10 20:40:46 +00:00
|
|
|
/**
|
|
|
|
|
* Invalidate existing sessions for a user
|
|
|
|
|
*
|
|
|
|
|
* If the provider has its own equivalent of CookieSessionProvider's Token
|
|
|
|
|
* cookie (and doesn't use User::getToken() to implement it), it should
|
|
|
|
|
* reset whatever token it does use here.
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionManager only
|
2017-08-11 16:09:41 +00:00
|
|
|
* @param User $user
|
2016-05-10 20:40:46 +00:00
|
|
|
*/
|
|
|
|
|
public function invalidateSessionsForUser( User $user ) {
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* Return the HTTP headers that need varying on.
|
|
|
|
|
*
|
|
|
|
|
* The return value is such that someone could theoretically do this:
|
|
|
|
|
* @code
|
2023-08-17 07:00:05 +00:00
|
|
|
* foreach ( $provider->getVaryHeaders() as $header ) {
|
2023-08-16 08:15:04 +00:00
|
|
|
* $outputPage->addVaryHeader( $header );
|
2017-02-25 21:53:36 +00:00
|
|
|
* }
|
2016-02-01 20:44:03 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionManager only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getVaryHeaders() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the list of cookies that need varying on.
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionManager only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
public function getVaryCookies() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a suggested username for the login form
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by \MediaWiki\Session\SessionBackend only
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param WebRequest $request
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function suggestLoginUsername( WebRequest $request ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the rights allowed the user when the specified session is active.
|
2016-08-27 05:40:37 +00:00
|
|
|
*
|
|
|
|
|
* This is mainly meant for allowing the user to restrict access to the account
|
2022-07-07 14:41:24 +00:00
|
|
|
* by certain methods; you probably want to use this with GrantsInfo. The returned
|
2016-08-27 05:40:37 +00:00
|
|
|
* rights will be intersected with the user's actual rights.
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param SessionBackend $backend
|
|
|
|
|
* @return null|string[] Allowed user rights, or null to allow all.
|
|
|
|
|
*/
|
|
|
|
|
public function getAllowedUserRights( SessionBackend $backend ) {
|
|
|
|
|
if ( $backend->getProvider() !== $this ) {
|
|
|
|
|
// Not that this should ever happen...
|
|
|
|
|
throw new \InvalidArgumentException( 'Backend\'s provider isn\'t $this' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 17:23:46 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch any restrictions imposed on logins or actions when this
|
|
|
|
|
* session is active.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.42
|
|
|
|
|
* @stable to override
|
|
|
|
|
* @return MWRestrictions|null
|
|
|
|
|
*/
|
|
|
|
|
public function getRestrictions( ?array $providerMetadata ): ?MWRestrictions {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* @note Only override this if it makes sense to instantiate multiple
|
|
|
|
|
* instances of the provider. Value returned must be unique across
|
|
|
|
|
* configured providers. If you override this, you'll likely need to
|
|
|
|
|
* override self::describeMessage() as well.
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function __toString() {
|
2017-03-07 02:14:14 +00:00
|
|
|
return static::class;
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a Message identifying this session type
|
|
|
|
|
*
|
|
|
|
|
* This default implementation takes the class name, lowercases it,
|
|
|
|
|
* replaces backslashes with dashes, and prefixes 'sessionprovider-' to
|
2016-04-06 22:22:33 +00:00
|
|
|
* determine the message key. For example, MediaWiki\Session\CookieSessionProvider
|
2016-02-01 20:44:03 +00:00
|
|
|
* produces 'sessionprovider-mediawiki-session-cookiesessionprovider'.
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2016-02-01 20:44:03 +00:00
|
|
|
* @note If self::__toString() is overridden, this will likely need to be
|
|
|
|
|
* overridden as well.
|
|
|
|
|
* @warning This will be called early during MediaWiki startup. Do not
|
2019-04-11 13:36:15 +00:00
|
|
|
* use $wgUser, $wgLang, $wgOut, the global Parser, or their equivalents via
|
2016-02-01 20:44:03 +00:00
|
|
|
* RequestContext from this method!
|
2016-06-30 22:08:33 +00:00
|
|
|
* @return \Message
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
protected function describeMessage() {
|
|
|
|
|
return wfMessage(
|
2017-03-07 02:14:14 +00:00
|
|
|
'sessionprovider-' . str_replace( '\\', '-', strtolower( static::class ) )
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 11:13:22 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-03-12 11:13:22 +00:00
|
|
|
*/
|
2016-02-01 20:44:03 +00:00
|
|
|
public function describe( Language $lang ) {
|
|
|
|
|
$msg = $this->describeMessage();
|
|
|
|
|
$msg->inLanguage( $lang );
|
|
|
|
|
if ( $msg->isDisabled() ) {
|
|
|
|
|
$msg = wfMessage( 'sessionprovider-generic', (string)$this )->inLanguage( $lang );
|
|
|
|
|
}
|
|
|
|
|
return $msg->plain();
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 11:13:22 +00:00
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-03-12 11:13:22 +00:00
|
|
|
*/
|
2016-02-01 20:44:03 +00:00
|
|
|
public function whyNoSession() {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 02:10:07 +00:00
|
|
|
/**
|
|
|
|
|
* Most session providers require protection against CSRF attacks (usually via CSRF tokens)
|
|
|
|
|
*
|
2020-07-13 08:57:12 +00:00
|
|
|
* @stable to override
|
2020-03-11 02:10:07 +00:00
|
|
|
* @return bool false
|
|
|
|
|
*/
|
|
|
|
|
public function safeAgainstCsrf() {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-12 19:08:54 +00:00
|
|
|
/**
|
|
|
|
|
* Returns true if this provider is exempt from autocreate user permissions check
|
|
|
|
|
*
|
|
|
|
|
* By default returns false, meaning this provider respects the normal rights
|
|
|
|
|
* of anonymous user creation. When true the permission checks will be bypassed
|
|
|
|
|
* and the user will always be created (subject to other limitations, like read
|
|
|
|
|
* only db and such).
|
|
|
|
|
*
|
|
|
|
|
* @stable to override
|
|
|
|
|
* @since 1.42
|
|
|
|
|
*/
|
|
|
|
|
public function canAlwaysAutocreate(): bool {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* Hash data as a session ID
|
|
|
|
|
*
|
|
|
|
|
* Generally this will only be used when self::persistsSessionId() is false and
|
|
|
|
|
* the provider has to base the session ID on the verified user's identity
|
2016-05-10 19:25:39 +00:00
|
|
|
* or other static data. The SessionInfo should then typically have the
|
|
|
|
|
* 'forceUse' flag set to avoid persistent session failure if validation of
|
|
|
|
|
* the stored data fails.
|
2016-02-01 20:44:03 +00:00
|
|
|
*
|
|
|
|
|
* @param string $data
|
2022-04-01 15:58:32 +00:00
|
|
|
* @param string|null $key Defaults to $this->getConfig()->get( MainConfigNames::SecretKey )
|
2016-02-01 20:44:03 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
final protected function hashToSessionId( $data, $key = null ) {
|
|
|
|
|
if ( !is_string( $data ) ) {
|
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
|
'$data must be a string, ' . gettype( $data ) . ' was passed'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
if ( $key !== null && !is_string( $key ) ) {
|
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
|
'$key must be a string or null, ' . gettype( $key ) . ' was passed'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 15:28:26 +00:00
|
|
|
$hash = \MWCryptHash::hmac( "$this\n$data",
|
|
|
|
|
$key ?: $this->getConfig()->get( MainConfigNames::SecretKey ), false );
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( strlen( $hash ) < 32 ) {
|
|
|
|
|
// Should never happen, even md5 is 128 bits
|
|
|
|
|
// @codeCoverageIgnoreStart
|
2018-08-14 07:56:35 +00:00
|
|
|
throw new \UnexpectedValueException( 'Hash function returned less than 128 bits' );
|
2016-02-01 20:44:03 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
if ( strlen( $hash ) >= 40 ) {
|
2016-05-07 11:59:31 +00:00
|
|
|
$hash = \Wikimedia\base_convert( $hash, 16, 32, 32 );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
return substr( $hash, -32 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|