2015-11-22 20:17:00 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Authentication (and possibly Authorization in the future) system entry point
|
|
|
|
|
*
|
|
|
|
|
* 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 Auth
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Auth;
|
|
|
|
|
|
|
|
|
|
use Config;
|
2021-07-22 11:38:45 +00:00
|
|
|
use Language;
|
2020-10-30 10:55:50 +00:00
|
|
|
use MediaWiki\Block\BlockManager;
|
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;
|
2021-07-22 11:38:45 +00:00
|
|
|
use MediaWiki\Languages\LanguageConverterFactory;
|
2022-04-25 15:19:41 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2021-05-24 13:40:37 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
2021-04-21 16:25:17 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
2021-02-22 20:38:43 +00:00
|
|
|
use MediaWiki\Permissions\PermissionStatus;
|
2021-08-05 21:09:06 +00:00
|
|
|
use MediaWiki\User\BotPasswordStore;
|
2022-02-28 03:05:58 +00:00
|
|
|
use MediaWiki\User\TempUser\TempUserCreator;
|
2021-08-05 21:09:06 +00:00
|
|
|
use MediaWiki\User\UserFactory;
|
2021-04-21 16:25:17 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-08-05 21:09:06 +00:00
|
|
|
use MediaWiki\User\UserIdentityLookup;
|
2020-04-08 18:19:49 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2021-08-05 21:09:06 +00:00
|
|
|
use MediaWiki\User\UserOptionsManager;
|
2022-03-16 04:08:00 +00:00
|
|
|
use MediaWiki\User\UserRigorOptions;
|
2021-04-13 00:43:46 +00:00
|
|
|
use MediaWiki\Watchlist\WatchlistManager;
|
2015-11-22 20:17:00 +00:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
2019-11-08 21:24:00 +00:00
|
|
|
use Psr\Log\NullLogger;
|
2020-10-10 21:03:11 +00:00
|
|
|
use ReadOnlyMode;
|
2021-02-22 20:38:43 +00:00
|
|
|
use SpecialPage;
|
2015-11-22 20:17:00 +00:00
|
|
|
use Status;
|
|
|
|
|
use StatusValue;
|
|
|
|
|
use User;
|
|
|
|
|
use WebRequest;
|
2022-03-09 22:16:22 +00:00
|
|
|
use Wikimedia\ObjectFactory\ObjectFactory;
|
2021-07-22 11:38:45 +00:00
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
2021-05-15 08:09:28 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This serves as the entry point to the authentication system.
|
|
|
|
|
*
|
|
|
|
|
* In the future, it may also serve as the entry point to the authorization
|
|
|
|
|
* system.
|
|
|
|
|
*
|
2016-08-27 05:40:37 +00:00
|
|
|
* If you are looking at this because you are working on an extension that creates its own
|
|
|
|
|
* login or signup page, then 1) you really shouldn't do that, 2) if you feel you absolutely
|
|
|
|
|
* have to, subclass AuthManagerSpecialPage or build it on the client side using the clientlogin
|
|
|
|
|
* or the createaccount API. Trying to call this class directly will very likely end up in
|
|
|
|
|
* security vulnerabilities or broken UX in edge cases.
|
|
|
|
|
*
|
|
|
|
|
* If you are working on an extension that needs to integrate with the authentication system
|
|
|
|
|
* (e.g. by providing a new login method, or doing extra permission checks), you'll probably
|
|
|
|
|
* need to write an AuthenticationProvider.
|
|
|
|
|
*
|
|
|
|
|
* If you want to create a "reserved" user programmatically, User::newSystemUser() might be what
|
|
|
|
|
* you are looking for. If you want to change user data, use User::changeAuthenticationData().
|
|
|
|
|
* Code that is related to some SessionProvider or PrimaryAuthenticationProvider can
|
|
|
|
|
* create a (non-reserved) user by calling AuthManager::autoCreateUser(); it is then the provider's
|
|
|
|
|
* responsibility to ensure that the user can authenticate somehow (see especially
|
2018-12-30 02:41:27 +00:00
|
|
|
* PrimaryAuthenticationProvider::autoCreatedAccount()). The same functionality can also be used
|
|
|
|
|
* from Maintenance scripts such as createAndPromote.php.
|
2016-08-27 05:40:37 +00:00
|
|
|
* If you are writing code that is not associated with such a provider and needs to create accounts
|
|
|
|
|
* programmatically for real users, you should rethink your architecture. There is no good way to
|
|
|
|
|
* do that as such code has no knowledge of what authentication methods are enabled on the wiki and
|
|
|
|
|
* cannot provide any means for users to access the accounts it would create.
|
|
|
|
|
*
|
|
|
|
|
* The two main control flows when using this class are as follows:
|
|
|
|
|
* * Login, user creation or account linking code will call getAuthenticationRequests(), populate
|
|
|
|
|
* the requests with data (by using them to build a HTMLForm and have the user fill it, or by
|
|
|
|
|
* exposing a form specification via the API, so that the client can build it), and pass them to
|
|
|
|
|
* the appropriate begin* method. That will return either a success/failure response, or more
|
|
|
|
|
* requests to fill (either by building a form or by redirecting the user to some external
|
|
|
|
|
* provider which will send the data back), in which case they need to be submitted to the
|
|
|
|
|
* appropriate continue* method and that step has to be repeated until the response is a success
|
|
|
|
|
* or failure response. AuthManager will use the session to maintain internal state during the
|
|
|
|
|
* process.
|
|
|
|
|
* * Code doing an authentication data change will call getAuthenticationRequests(), select
|
|
|
|
|
* a single request, populate it, and pass it to allowsAuthenticationDataChange() and then
|
|
|
|
|
* changeAuthenticationData(). If the data change is user-initiated, the whole process needs
|
|
|
|
|
* to be preceded by a call to securitySensitiveOperationStatus() and aborted if that returns
|
|
|
|
|
* a non-OK status.
|
|
|
|
|
*
|
2015-11-22 20:17:00 +00:00
|
|
|
* @ingroup Auth
|
|
|
|
|
* @since 1.27
|
2016-08-27 05:40:37 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:SessionManager_and_AuthManager
|
2015-11-22 20:17:00 +00:00
|
|
|
*/
|
|
|
|
|
class AuthManager implements LoggerAwareInterface {
|
|
|
|
|
/** Log in with an existing (not necessarily local) user */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const ACTION_LOGIN = 'login';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Continue a login process that was interrupted by the need for user input or communication
|
2019-08-05 17:00:00 +00:00
|
|
|
* with an external provider
|
|
|
|
|
*/
|
2019-10-31 05:00:32 +00:00
|
|
|
public const ACTION_LOGIN_CONTINUE = 'login-continue';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Create a new user */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const ACTION_CREATE = 'create';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Continue a user creation process that was interrupted by the need for user input or
|
2019-08-05 17:00:00 +00:00
|
|
|
* communication with an external provider
|
|
|
|
|
*/
|
2019-10-31 05:00:32 +00:00
|
|
|
public const ACTION_CREATE_CONTINUE = 'create-continue';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Link an existing user to a third-party account */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const ACTION_LINK = 'link';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Continue a user linking process that was interrupted by the need for user input or
|
2019-08-05 17:00:00 +00:00
|
|
|
* communication with an external provider
|
|
|
|
|
*/
|
2019-10-31 05:00:32 +00:00
|
|
|
public const ACTION_LINK_CONTINUE = 'link-continue';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Change a user's credentials */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const ACTION_CHANGE = 'change';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Remove a user's credentials */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const ACTION_REMOVE = 'remove';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Like ACTION_REMOVE but for linking providers only */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const ACTION_UNLINK = 'unlink';
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
/** Security-sensitive operations are ok. */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const SEC_OK = 'ok';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Security-sensitive operations should re-authenticate. */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const SEC_REAUTH = 'reauth';
|
2015-11-22 20:17:00 +00:00
|
|
|
/** Security-sensitive should not be performed. */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const SEC_FAIL = 'fail';
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
/** Auto-creation is due to SessionManager */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const AUTOCREATE_SOURCE_SESSION = \MediaWiki\Session\SessionManager::class;
|
2015-11-22 20:17:00 +00:00
|
|
|
|
2018-12-30 02:41:27 +00:00
|
|
|
/** Auto-creation is due to a Maintenance script */
|
2019-10-31 05:00:32 +00:00
|
|
|
public const AUTOCREATE_SOURCE_MAINT = '::Maintenance::';
|
2018-12-30 02:41:27 +00:00
|
|
|
|
2022-02-28 03:05:58 +00:00
|
|
|
/** Auto-creation is due to temporary account creation on page save */
|
|
|
|
|
public const AUTOCREATE_SOURCE_TEMP = TempUserCreator::class;
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
/** @var WebRequest */
|
|
|
|
|
private $request;
|
|
|
|
|
|
|
|
|
|
/** @var Config */
|
|
|
|
|
private $config;
|
|
|
|
|
|
2019-11-08 21:24:00 +00:00
|
|
|
/** @var ObjectFactory */
|
|
|
|
|
private $objectFactory;
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
private $logger;
|
|
|
|
|
|
2020-04-08 18:19:49 +00:00
|
|
|
/** @var UserNameUtils */
|
|
|
|
|
private $userNameUtils;
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
/** @var AuthenticationProvider[] */
|
|
|
|
|
private $allAuthenticationProviders = [];
|
|
|
|
|
|
|
|
|
|
/** @var PreAuthenticationProvider[] */
|
|
|
|
|
private $preAuthenticationProviders = null;
|
|
|
|
|
|
|
|
|
|
/** @var PrimaryAuthenticationProvider[] */
|
|
|
|
|
private $primaryAuthenticationProviders = null;
|
|
|
|
|
|
|
|
|
|
/** @var SecondaryAuthenticationProvider[] */
|
|
|
|
|
private $secondaryAuthenticationProviders = null;
|
|
|
|
|
|
|
|
|
|
/** @var CreatedAccountAuthenticationRequest[] */
|
|
|
|
|
private $createdAccountAuthenticationRequests = [];
|
|
|
|
|
|
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;
|
|
|
|
|
|
2020-10-10 21:03:11 +00:00
|
|
|
/** @var ReadOnlyMode */
|
|
|
|
|
private $readOnlyMode;
|
|
|
|
|
|
2020-10-30 10:55:50 +00:00
|
|
|
/** @var BlockManager */
|
|
|
|
|
private $blockManager;
|
|
|
|
|
|
2021-04-13 00:43:46 +00:00
|
|
|
/** @var WatchlistManager */
|
|
|
|
|
private $watchlistManager;
|
|
|
|
|
|
2021-07-22 11:38:45 +00:00
|
|
|
/** @var ILoadBalancer */
|
|
|
|
|
private $loadBalancer;
|
|
|
|
|
|
|
|
|
|
/** @var Language */
|
|
|
|
|
private $contentLanguage;
|
|
|
|
|
|
|
|
|
|
/** @var LanguageConverterFactory */
|
|
|
|
|
private $languageConverterFactory;
|
|
|
|
|
|
2021-08-05 21:09:06 +00:00
|
|
|
/** @var BotPasswordStore */
|
|
|
|
|
private $botPasswordStore;
|
|
|
|
|
|
|
|
|
|
/** @var UserFactory */
|
|
|
|
|
private $userFactory;
|
|
|
|
|
|
|
|
|
|
/** @var UserIdentityLookup */
|
|
|
|
|
private $userIdentityLookup;
|
|
|
|
|
|
|
|
|
|
/** @var UserOptionsManager */
|
|
|
|
|
private $userOptionsManager;
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
/**
|
|
|
|
|
* @param WebRequest $request
|
|
|
|
|
* @param Config $config
|
2019-11-08 21:24:00 +00:00
|
|
|
* @param ObjectFactory $objectFactory
|
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
|
2020-10-10 21:03:11 +00:00
|
|
|
* @param ReadOnlyMode $readOnlyMode
|
2020-04-08 18:19:49 +00:00
|
|
|
* @param UserNameUtils $userNameUtils
|
2020-10-30 10:55:50 +00:00
|
|
|
* @param BlockManager $blockManager
|
2021-04-13 00:43:46 +00:00
|
|
|
* @param WatchlistManager $watchlistManager
|
2021-07-22 11:38:45 +00:00
|
|
|
* @param ILoadBalancer $loadBalancer
|
|
|
|
|
* @param Language $contentLanguage
|
|
|
|
|
* @param LanguageConverterFactory $languageConverterFactory
|
2021-08-05 21:09:06 +00:00
|
|
|
* @param BotPasswordStore $botPasswordStore
|
|
|
|
|
* @param UserFactory $userFactory
|
|
|
|
|
* @param UserIdentityLookup $userIdentityLookup
|
|
|
|
|
* @param UserOptionsManager $userOptionsManager
|
2015-11-22 20:17:00 +00:00
|
|
|
*/
|
2020-02-20 09:45:13 +00:00
|
|
|
public function __construct(
|
|
|
|
|
WebRequest $request,
|
|
|
|
|
Config $config,
|
|
|
|
|
ObjectFactory $objectFactory,
|
2020-10-10 21:03:11 +00:00
|
|
|
HookContainer $hookContainer,
|
2020-04-08 18:19:49 +00:00
|
|
|
ReadOnlyMode $readOnlyMode,
|
2020-10-30 10:55:50 +00:00
|
|
|
UserNameUtils $userNameUtils,
|
|
|
|
|
BlockManager $blockManager,
|
2021-07-22 11:38:45 +00:00
|
|
|
WatchlistManager $watchlistManager,
|
|
|
|
|
ILoadBalancer $loadBalancer,
|
|
|
|
|
Language $contentLanguage,
|
2021-08-05 21:09:06 +00:00
|
|
|
LanguageConverterFactory $languageConverterFactory,
|
|
|
|
|
BotPasswordStore $botPasswordStore,
|
|
|
|
|
UserFactory $userFactory,
|
|
|
|
|
UserIdentityLookup $userIdentityLookup,
|
|
|
|
|
UserOptionsManager $userOptionsManager
|
2020-02-20 09:45:13 +00:00
|
|
|
) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->request = $request;
|
|
|
|
|
$this->config = $config;
|
2019-11-08 21:24:00 +00:00
|
|
|
$this->objectFactory = $objectFactory;
|
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 );
|
2019-11-08 21:24:00 +00:00
|
|
|
$this->setLogger( new NullLogger() );
|
2020-10-10 21:03:11 +00:00
|
|
|
$this->readOnlyMode = $readOnlyMode;
|
2020-04-08 18:19:49 +00:00
|
|
|
$this->userNameUtils = $userNameUtils;
|
2020-10-30 10:55:50 +00:00
|
|
|
$this->blockManager = $blockManager;
|
2021-04-13 00:43:46 +00:00
|
|
|
$this->watchlistManager = $watchlistManager;
|
2021-07-22 11:38:45 +00:00
|
|
|
$this->loadBalancer = $loadBalancer;
|
|
|
|
|
$this->contentLanguage = $contentLanguage;
|
|
|
|
|
$this->languageConverterFactory = $languageConverterFactory;
|
2021-08-05 21:09:06 +00:00
|
|
|
$this->botPasswordStore = $botPasswordStore;
|
|
|
|
|
$this->userFactory = $userFactory;
|
|
|
|
|
$this->userIdentityLookup = $userIdentityLookup;
|
|
|
|
|
$this->userOptionsManager = $userOptionsManager;
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param LoggerInterface $logger
|
|
|
|
|
*/
|
|
|
|
|
public function setLogger( LoggerInterface $logger ) {
|
|
|
|
|
$this->logger = $logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return WebRequest
|
|
|
|
|
*/
|
|
|
|
|
public function getRequest() {
|
|
|
|
|
return $this->request;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Force certain PrimaryAuthenticationProviders
|
|
|
|
|
* @deprecated For backwards compatibility only
|
|
|
|
|
* @param PrimaryAuthenticationProvider[] $providers
|
|
|
|
|
* @param string $why
|
|
|
|
|
*/
|
|
|
|
|
public function forcePrimaryAuthenticationProviders( array $providers, $why ) {
|
|
|
|
|
$this->logger->warning( "Overriding AuthManager primary authn because $why" );
|
|
|
|
|
|
|
|
|
|
if ( $this->primaryAuthenticationProviders !== null ) {
|
|
|
|
|
$this->logger->warning(
|
|
|
|
|
'PrimaryAuthenticationProviders have already been accessed! I hope nothing breaks.'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->allAuthenticationProviders = array_diff_key(
|
|
|
|
|
$this->allAuthenticationProviders,
|
|
|
|
|
$this->primaryAuthenticationProviders
|
|
|
|
|
);
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
$session->remove( 'AuthManager::accountLinkState' );
|
|
|
|
|
$this->createdAccountAuthenticationRequests = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->primaryAuthenticationProviders = [];
|
|
|
|
|
foreach ( $providers as $provider ) {
|
2021-04-16 13:17:10 +00:00
|
|
|
if ( !$provider instanceof AbstractPrimaryAuthenticationProvider ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
throw new \RuntimeException(
|
2021-04-16 13:17:10 +00:00
|
|
|
'Expected instance of MediaWiki\\Auth\\AbstractPrimaryAuthenticationProvider, got ' .
|
2015-11-22 20:17:00 +00:00
|
|
|
get_class( $provider )
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-04-16 13:17:10 +00:00
|
|
|
$provider->init( $this->logger, $this, $this->hookContainer, $this->config, $this->userNameUtils );
|
2015-11-22 20:17:00 +00:00
|
|
|
$id = $provider->getUniqueId();
|
|
|
|
|
if ( isset( $this->allAuthenticationProviders[$id] ) ) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
"Duplicate specifications for id $id (classes " .
|
|
|
|
|
get_class( $provider ) . ' and ' .
|
|
|
|
|
get_class( $this->allAuthenticationProviders[$id] ) . ')'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$this->allAuthenticationProviders[$id] = $provider;
|
|
|
|
|
$this->primaryAuthenticationProviders[$id] = $provider;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
/***************************************************************************/
|
|
|
|
|
// region Authentication
|
|
|
|
|
/** @name Authentication */
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicate whether user authentication is possible
|
|
|
|
|
*
|
|
|
|
|
* It may not be if the session is provided by something like OAuth
|
|
|
|
|
* for which each individual request includes authentication data.
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function canAuthenticateNow() {
|
|
|
|
|
return $this->request->getSession()->canSetUser();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start an authentication flow
|
2016-05-26 17:09:14 +00:00
|
|
|
*
|
|
|
|
|
* In addition to the AuthenticationRequests returned by
|
|
|
|
|
* $this->getAuthenticationRequests(), a client might include a
|
|
|
|
|
* CreateFromLoginAuthenticationRequest from a previous login attempt to
|
|
|
|
|
* preserve state.
|
|
|
|
|
*
|
|
|
|
|
* Instead of the AuthenticationRequests returned by
|
|
|
|
|
* $this->getAuthenticationRequests(), a client might pass a
|
|
|
|
|
* CreatedAccountAuthenticationRequest from an account creation that just
|
|
|
|
|
* succeeded to log in to the just-created account.
|
|
|
|
|
*
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param AuthenticationRequest[] $reqs
|
|
|
|
|
* @param string $returnToUrl Url that REDIRECT responses should eventually
|
|
|
|
|
* return to.
|
|
|
|
|
* @return AuthenticationResponse See self::continueAuthentication()
|
|
|
|
|
*/
|
|
|
|
|
public function beginAuthentication( array $reqs, $returnToUrl ) {
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
if ( !$session->canSetUser() ) {
|
|
|
|
|
// Caller should have called canAuthenticateNow()
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
|
|
|
|
throw new \LogicException( 'Authentication is not possible now' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$guessUserName = null;
|
|
|
|
|
foreach ( $reqs as $req ) {
|
|
|
|
|
$req->returnToUrl = $returnToUrl;
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
if ( $req->username !== null && $req->username !== '' ) {
|
|
|
|
|
if ( $guessUserName === null ) {
|
|
|
|
|
$guessUserName = $req->username;
|
|
|
|
|
} elseif ( $guessUserName !== $req->username ) {
|
|
|
|
|
$guessUserName = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for special-case login of a just-created account
|
|
|
|
|
$req = AuthenticationRequest::getRequestByClass(
|
|
|
|
|
$reqs, CreatedAccountAuthenticationRequest::class
|
|
|
|
|
);
|
|
|
|
|
if ( $req ) {
|
|
|
|
|
if ( !in_array( $req, $this->createdAccountAuthenticationRequests, true ) ) {
|
|
|
|
|
throw new \LogicException(
|
|
|
|
|
'CreatedAccountAuthenticationRequests are only valid on ' .
|
|
|
|
|
'the same AuthManager that created the account'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-05 21:09:06 +00:00
|
|
|
$user = $this->userFactory->newFromName( (string)$req->username );
|
2015-11-22 20:17:00 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
if ( !$user ) {
|
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
|
"CreatedAccountAuthenticationRequest had invalid username \"{$req->username}\""
|
|
|
|
|
);
|
|
|
|
|
} elseif ( $user->getId() != $req->id ) {
|
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
|
"ID for \"{$req->username}\" was {$user->getId()}, expected {$req->id}"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
|
|
|
|
|
$this->logger->info( 'Logging in {user} after account creation', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$ret = AuthenticationResponse::newPass( $user->getName() );
|
|
|
|
|
$this->setSessionDataForUser( $user );
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAuthentication', [ $user, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
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->getHookRunner()->onAuthManagerLoginAuthenticateAudit(
|
|
|
|
|
$ret, $user, $user->getName(), [] );
|
2015-11-22 20:17:00 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->removeAuthenticationSessionData( null );
|
|
|
|
|
|
|
|
|
|
foreach ( $this->getPreAuthenticationProviders() as $provider ) {
|
|
|
|
|
$status = $provider->testForAuthentication( $reqs );
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$this->logger->debug( 'Login failed in pre-authentication by ' . $provider->getUniqueId() );
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
Status::wrap( $status )->getMessage()
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAuthentication',
|
2021-08-05 21:09:06 +00:00
|
|
|
[ $this->userFactory->newFromName( (string)$guessUserName ), $ret ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
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->getHookRunner()->onAuthManagerLoginAuthenticateAudit( $ret, null, $guessUserName, [] );
|
2015-11-22 20:17:00 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$state = [
|
|
|
|
|
'reqs' => $reqs,
|
|
|
|
|
'returnToUrl' => $returnToUrl,
|
|
|
|
|
'guessUserName' => $guessUserName,
|
|
|
|
|
'primary' => null,
|
|
|
|
|
'primaryResponse' => null,
|
|
|
|
|
'secondary' => [],
|
|
|
|
|
'maybeLink' => [],
|
|
|
|
|
'continueRequests' => [],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Preserve state from a previous failed login
|
|
|
|
|
$req = AuthenticationRequest::getRequestByClass(
|
|
|
|
|
$reqs, CreateFromLoginAuthenticationRequest::class
|
|
|
|
|
);
|
|
|
|
|
if ( $req ) {
|
|
|
|
|
$state['maybeLink'] = $req->maybeLink;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
$session->setSecret( 'AuthManager::authnState', $state );
|
|
|
|
|
$session->persist();
|
|
|
|
|
|
|
|
|
|
return $this->continueAuthentication( $reqs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Continue an authentication flow
|
|
|
|
|
*
|
|
|
|
|
* Return values are interpreted as follows:
|
|
|
|
|
* - status FAIL: Authentication failed. If $response->createRequest is
|
|
|
|
|
* set, that may be passed to self::beginAuthentication() or to
|
2016-05-26 17:09:14 +00:00
|
|
|
* self::beginAccountCreation() to preserve state.
|
2015-11-22 20:17:00 +00:00
|
|
|
* - status REDIRECT: The client should be redirected to the contained URL,
|
|
|
|
|
* new AuthenticationRequests should be made (if any), then
|
|
|
|
|
* AuthManager::continueAuthentication() should be called.
|
|
|
|
|
* - status UI: The client should be presented with a user interface for
|
|
|
|
|
* the fields in the specified AuthenticationRequests, then new
|
|
|
|
|
* AuthenticationRequests should be made, then
|
|
|
|
|
* AuthManager::continueAuthentication() should be called.
|
|
|
|
|
* - status RESTART: The user logged in successfully with a third-party
|
|
|
|
|
* service, but the third-party credentials aren't attached to any local
|
|
|
|
|
* account. This could be treated as a UI or a FAIL.
|
|
|
|
|
* - status PASS: Authentication was successful.
|
|
|
|
|
*
|
|
|
|
|
* @param AuthenticationRequest[] $reqs
|
|
|
|
|
* @return AuthenticationResponse
|
|
|
|
|
*/
|
|
|
|
|
public function continueAuthentication( array $reqs ) {
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
try {
|
|
|
|
|
if ( !$session->canSetUser() ) {
|
|
|
|
|
// Caller should have called canAuthenticateNow()
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
throw new \LogicException( 'Authentication is not possible now' );
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$state = $session->getSecret( 'AuthManager::authnState' );
|
|
|
|
|
if ( !is_array( $state ) ) {
|
|
|
|
|
return AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-authn-not-in-progress' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$state['continueRequests'] = [];
|
|
|
|
|
|
|
|
|
|
$guessUserName = $state['guessUserName'];
|
|
|
|
|
|
|
|
|
|
foreach ( $reqs as $req ) {
|
|
|
|
|
$req->returnToUrl = $state['returnToUrl'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 1: Choose an primary authentication provider, and call it until it succeeds.
|
|
|
|
|
|
|
|
|
|
if ( $state['primary'] === null ) {
|
|
|
|
|
// We haven't picked a PrimaryAuthenticationProvider yet
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
$guessUserName = null;
|
|
|
|
|
foreach ( $reqs as $req ) {
|
|
|
|
|
if ( $req->username !== null && $req->username !== '' ) {
|
|
|
|
|
if ( $guessUserName === null ) {
|
|
|
|
|
$guessUserName = $req->username;
|
|
|
|
|
} elseif ( $guessUserName !== $req->username ) {
|
|
|
|
|
$guessUserName = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$state['guessUserName'] = $guessUserName;
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
$state['reqs'] = $reqs;
|
|
|
|
|
|
|
|
|
|
foreach ( $this->getPrimaryAuthenticationProviders() as $id => $provider ) {
|
|
|
|
|
$res = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
switch ( $res->status ) {
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::PASS:
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['primary'] = $id;
|
|
|
|
|
$state['primaryResponse'] = $res;
|
|
|
|
|
$this->logger->debug( "Primary login with $id succeeded" );
|
|
|
|
|
break 2;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::FAIL:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( "Login failed in primary authentication by $id" );
|
|
|
|
|
if ( $res->createRequest || $state['maybeLink'] ) {
|
|
|
|
|
$res->createRequest = new CreateFromLoginAuthenticationRequest(
|
|
|
|
|
$res->createRequest, $state['maybeLink']
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-08-05 21:09:06 +00:00
|
|
|
$this->callMethodOnProviders(
|
|
|
|
|
7,
|
|
|
|
|
'postAuthentication',
|
|
|
|
|
[
|
|
|
|
|
$this->userFactory->newFromName( (string)$guessUserName ),
|
|
|
|
|
$res
|
|
|
|
|
]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
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->getHookRunner()->onAuthManagerLoginAuthenticateAudit(
|
|
|
|
|
$res, null, $guessUserName, [] );
|
2015-11-22 20:17:00 +00:00
|
|
|
return $res;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::ABSTAIN:
|
2015-11-22 20:17:00 +00:00
|
|
|
// Continue loop
|
|
|
|
|
break;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::REDIRECT:
|
|
|
|
|
case AuthenticationResponse::UI:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( "Primary login with $id returned $res->status" );
|
2016-06-01 15:58:44 +00:00
|
|
|
$this->fillRequests( $res->neededRequests, self::ACTION_LOGIN, $guessUserName );
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['primary'] = $id;
|
|
|
|
|
$state['continueRequests'] = $res->neededRequests;
|
|
|
|
|
$session->setSecret( 'AuthManager::authnState', $state );
|
|
|
|
|
return $res;
|
|
|
|
|
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
default:
|
|
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . "::beginPrimaryAuthentication() returned $res->status"
|
|
|
|
|
);
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-09 09:09:00 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in loop before, if passed
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( $state['primary'] === null ) {
|
|
|
|
|
$this->logger->debug( 'Login failed in primary authentication because no provider accepted' );
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-authn-no-primary' )
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAuthentication',
|
2021-08-05 21:09:06 +00:00
|
|
|
[ $this->userFactory->newFromName( (string)$guessUserName ), $ret ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $state['primaryResponse'] === null ) {
|
|
|
|
|
$provider = $this->getAuthenticationProvider( $state['primary'] );
|
|
|
|
|
if ( !$provider instanceof PrimaryAuthenticationProvider ) {
|
|
|
|
|
// Configuration changed? Force them to start over.
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-authn-not-in-progress' )
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAuthentication',
|
2021-08-05 21:09:06 +00:00
|
|
|
[ $this->userFactory->newFromName( (string)$guessUserName ), $ret ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
$id = $provider->getUniqueId();
|
|
|
|
|
$res = $provider->continuePrimaryAuthentication( $reqs );
|
|
|
|
|
switch ( $res->status ) {
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::PASS:
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['primaryResponse'] = $res;
|
|
|
|
|
$this->logger->debug( "Primary login with $id succeeded" );
|
|
|
|
|
break;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::FAIL:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( "Login failed in primary authentication by $id" );
|
|
|
|
|
if ( $res->createRequest || $state['maybeLink'] ) {
|
|
|
|
|
$res->createRequest = new CreateFromLoginAuthenticationRequest(
|
|
|
|
|
$res->createRequest, $state['maybeLink']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAuthentication',
|
2021-08-05 21:09:06 +00:00
|
|
|
[ $this->userFactory->newFromName( (string)$guessUserName ), $res ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
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->getHookRunner()->onAuthManagerLoginAuthenticateAudit(
|
|
|
|
|
$res, null, $guessUserName, [] );
|
2015-11-22 20:17:00 +00:00
|
|
|
return $res;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::REDIRECT:
|
|
|
|
|
case AuthenticationResponse::UI:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( "Primary login with $id returned $res->status" );
|
2016-06-01 15:58:44 +00:00
|
|
|
$this->fillRequests( $res->neededRequests, self::ACTION_LOGIN, $guessUserName );
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['continueRequests'] = $res->neededRequests;
|
|
|
|
|
$session->setSecret( 'AuthManager::authnState', $state );
|
|
|
|
|
return $res;
|
|
|
|
|
default:
|
|
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . "::continuePrimaryAuthentication() returned $res->status"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-09 09:09:00 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in loop before, if passed
|
2015-11-22 20:17:00 +00:00
|
|
|
$res = $state['primaryResponse'];
|
|
|
|
|
if ( $res->username === null ) {
|
|
|
|
|
$provider = $this->getAuthenticationProvider( $state['primary'] );
|
|
|
|
|
if ( !$provider instanceof PrimaryAuthenticationProvider ) {
|
|
|
|
|
// Configuration changed? Force them to start over.
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-authn-not-in-progress' )
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAuthentication',
|
2021-08-05 21:09:06 +00:00
|
|
|
[ $this->userFactory->newFromName( (string)$guessUserName ), $ret ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $provider->accountCreationType() === PrimaryAuthenticationProvider::TYPE_LINK &&
|
|
|
|
|
$res->linkRequest &&
|
2021-09-03 22:52:31 +00:00
|
|
|
// don't confuse the user with an incorrect message if linking is disabled
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->getAuthenticationProvider( ConfirmLinkSecondaryAuthenticationProvider::class )
|
|
|
|
|
) {
|
|
|
|
|
$state['maybeLink'][$res->linkRequest->getUniqueId()] = $res->linkRequest;
|
|
|
|
|
$msg = 'authmanager-authn-no-local-user-link';
|
|
|
|
|
} else {
|
|
|
|
|
$msg = 'authmanager-authn-no-local-user';
|
|
|
|
|
}
|
|
|
|
|
$this->logger->debug(
|
|
|
|
|
"Primary login with {$provider->getUniqueId()} succeeded, but returned no user"
|
|
|
|
|
);
|
|
|
|
|
$ret = AuthenticationResponse::newRestart( wfMessage( $msg ) );
|
|
|
|
|
$ret->neededRequests = $this->getAuthenticationRequestsInternal(
|
|
|
|
|
self::ACTION_LOGIN,
|
|
|
|
|
[],
|
|
|
|
|
$this->getPrimaryAuthenticationProviders() + $this->getSecondaryAuthenticationProviders()
|
|
|
|
|
);
|
|
|
|
|
if ( $res->createRequest || $state['maybeLink'] ) {
|
|
|
|
|
$ret->createRequest = new CreateFromLoginAuthenticationRequest(
|
|
|
|
|
$res->createRequest, $state['maybeLink']
|
|
|
|
|
);
|
|
|
|
|
$ret->neededRequests[] = $ret->createRequest;
|
|
|
|
|
}
|
2016-06-03 15:33:41 +00:00
|
|
|
$this->fillRequests( $ret->neededRequests, self::ACTION_LOGIN, null, true );
|
2015-11-22 20:17:00 +00:00
|
|
|
$session->setSecret( 'AuthManager::authnState', [
|
|
|
|
|
'reqs' => [], // Will be filled in later
|
|
|
|
|
'primary' => null,
|
|
|
|
|
'primaryResponse' => null,
|
|
|
|
|
'secondary' => [],
|
|
|
|
|
'continueRequests' => $ret->neededRequests,
|
|
|
|
|
] + $state );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 2: Primary authentication succeeded, create the User object
|
|
|
|
|
// (and add the user locally if necessary)
|
|
|
|
|
|
2021-08-05 21:09:06 +00:00
|
|
|
$user = $this->userFactory->newFromName(
|
|
|
|
|
(string)$res->username,
|
2022-03-16 04:08:00 +00:00
|
|
|
UserRigorOptions::RIGOR_USABLE
|
2021-08-05 21:09:06 +00:00
|
|
|
);
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !$user ) {
|
2016-09-08 22:10:19 +00:00
|
|
|
$provider = $this->getAuthenticationProvider( $state['primary'] );
|
2015-11-22 20:17:00 +00:00
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . " returned an invalid username: {$res->username}"
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-04-29 18:32:20 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
// User doesn't exist locally. Create it.
|
|
|
|
|
$this->logger->info( 'Auto-creating {user} on login', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$status = $this->autoCreateUser( $user, $state['primary'], false );
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
Status::wrap( $status )->getMessage( 'authmanager-authn-autocreate-failed' )
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAuthentication', [ $user, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
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->getHookRunner()->onAuthManagerLoginAuthenticateAudit(
|
|
|
|
|
$ret, $user, $user->getName(), [] );
|
2015-11-22 20:17:00 +00:00
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 3: Iterate over all the secondary authentication providers.
|
|
|
|
|
|
|
|
|
|
$beginReqs = $state['reqs'];
|
|
|
|
|
|
|
|
|
|
foreach ( $this->getSecondaryAuthenticationProviders() as $id => $provider ) {
|
|
|
|
|
if ( !isset( $state['secondary'][$id] ) ) {
|
|
|
|
|
// This provider isn't started yet, so we pass it the set
|
|
|
|
|
// of reqs from beginAuthentication instead of whatever
|
|
|
|
|
// might have been used by a previous provider in line.
|
|
|
|
|
$func = 'beginSecondaryAuthentication';
|
|
|
|
|
$res = $provider->beginSecondaryAuthentication( $user, $beginReqs );
|
|
|
|
|
} elseif ( !$state['secondary'][$id] ) {
|
|
|
|
|
$func = 'continueSecondaryAuthentication';
|
|
|
|
|
$res = $provider->continueSecondaryAuthentication( $user, $reqs );
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
switch ( $res->status ) {
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::PASS:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( "Secondary login with $id succeeded" );
|
|
|
|
|
// fall through
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::ABSTAIN:
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['secondary'][$id] = true;
|
|
|
|
|
break;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::FAIL:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( "Login failed in secondary authentication by $id" );
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAuthentication', [ $user, $res ] );
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
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->getHookRunner()->onAuthManagerLoginAuthenticateAudit(
|
|
|
|
|
$res, $user, $user->getName(), [] );
|
2015-11-22 20:17:00 +00:00
|
|
|
return $res;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::REDIRECT:
|
|
|
|
|
case AuthenticationResponse::UI:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( "Secondary login with $id returned " . $res->status );
|
2016-06-01 15:58:44 +00:00
|
|
|
$this->fillRequests( $res->neededRequests, self::ACTION_LOGIN, $user->getName() );
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['secondary'][$id] = false;
|
|
|
|
|
$state['continueRequests'] = $res->neededRequests;
|
|
|
|
|
$session->setSecret( 'AuthManager::authnState', $state );
|
|
|
|
|
return $res;
|
|
|
|
|
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
default:
|
|
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . "::{$func}() returned $res->status"
|
|
|
|
|
);
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 4: Authentication complete! Set the user in the session and
|
|
|
|
|
// clean up.
|
|
|
|
|
|
2018-10-20 12:35:22 +00:00
|
|
|
$this->logger->info( 'Login for {user} succeeded from {clientip}', [
|
2015-11-22 20:17:00 +00:00
|
|
|
'user' => $user->getName(),
|
2018-10-20 12:35:22 +00:00
|
|
|
'clientip' => $this->request->getIP(),
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
2022-04-25 15:19:41 +00:00
|
|
|
$rememberMeConfig = $this->config->get( MainConfigNames::RememberMe );
|
2021-02-22 03:07:19 +00:00
|
|
|
if ( $rememberMeConfig === RememberMeAuthenticationRequest::ALWAYS_REMEMBER ) {
|
|
|
|
|
$rememberMe = true;
|
|
|
|
|
} elseif ( $rememberMeConfig === RememberMeAuthenticationRequest::NEVER_REMEMBER ) {
|
|
|
|
|
$rememberMe = false;
|
|
|
|
|
} else {
|
|
|
|
|
/** @var RememberMeAuthenticationRequest $req */
|
|
|
|
|
$req = AuthenticationRequest::getRequestByClass(
|
|
|
|
|
$beginReqs, RememberMeAuthenticationRequest::class
|
|
|
|
|
);
|
|
|
|
|
$rememberMe = $req && $req->rememberMe;
|
|
|
|
|
}
|
|
|
|
|
$this->setSessionDataForUser( $user, $rememberMe );
|
2015-11-22 20:17:00 +00:00
|
|
|
$ret = AuthenticationResponse::newPass( $user->getName() );
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAuthentication', [ $user, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
|
|
|
|
$this->removeAuthenticationSessionData( null );
|
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->getHookRunner()->onAuthManagerLoginAuthenticateAudit(
|
|
|
|
|
$ret, $user, $user->getName(), [] );
|
2015-11-22 20:17:00 +00:00
|
|
|
return $ret;
|
|
|
|
|
} catch ( \Exception $ex ) {
|
|
|
|
|
$session->remove( 'AuthManager::authnState' );
|
|
|
|
|
throw $ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether security-sensitive operations should proceed.
|
|
|
|
|
*
|
|
|
|
|
* A "security-sensitive operation" is something like a password or email
|
|
|
|
|
* change, that would normally have a "reenter your password to confirm"
|
|
|
|
|
* box if we only supported password-based authentication.
|
|
|
|
|
*
|
|
|
|
|
* @param string $operation Operation being checked. This should be a
|
|
|
|
|
* message-key-like string such as 'change-password' or 'change-email'.
|
|
|
|
|
* @return string One of the SEC_* constants.
|
|
|
|
|
*/
|
|
|
|
|
public function securitySensitiveOperationStatus( $operation ) {
|
|
|
|
|
$status = self::SEC_OK;
|
|
|
|
|
|
|
|
|
|
$this->logger->debug( __METHOD__ . ": Checking $operation" );
|
|
|
|
|
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
$aId = $session->getUser()->getId();
|
|
|
|
|
if ( $aId === 0 ) {
|
|
|
|
|
// User isn't authenticated. DWIM?
|
|
|
|
|
$status = $this->canAuthenticateNow() ? self::SEC_REAUTH : self::SEC_FAIL;
|
|
|
|
|
$this->logger->info( __METHOD__ . ": Not logged in! $operation is $status" );
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $session->canSetUser() ) {
|
|
|
|
|
$id = $session->get( 'AuthManager:lastAuthId' );
|
|
|
|
|
$last = $session->get( 'AuthManager:lastAuthTimestamp' );
|
|
|
|
|
if ( $id !== $aId || $last === null ) {
|
|
|
|
|
$timeSinceLogin = PHP_INT_MAX; // Forever ago
|
|
|
|
|
} else {
|
|
|
|
|
$timeSinceLogin = max( 0, time() - $last );
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-25 15:19:41 +00:00
|
|
|
$thresholds = $this->config->get( MainConfigNames::ReauthenticateTime );
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( isset( $thresholds[$operation] ) ) {
|
|
|
|
|
$threshold = $thresholds[$operation];
|
|
|
|
|
} elseif ( isset( $thresholds['default'] ) ) {
|
|
|
|
|
$threshold = $thresholds['default'];
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException( '$wgReauthenticateTime lacks a default' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $threshold >= 0 && $timeSinceLogin > $threshold ) {
|
|
|
|
|
$status = self::SEC_REAUTH;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$timeSinceLogin = -1;
|
|
|
|
|
|
2022-04-25 15:19:41 +00:00
|
|
|
$pass = $this->config->get(
|
|
|
|
|
MainConfigNames::AllowSecuritySensitiveOperationIfCannotReauthenticate );
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( isset( $pass[$operation] ) ) {
|
|
|
|
|
$status = $pass[$operation] ? self::SEC_OK : self::SEC_FAIL;
|
|
|
|
|
} elseif ( isset( $pass['default'] ) ) {
|
|
|
|
|
$status = $pass['default'] ? self::SEC_OK : self::SEC_FAIL;
|
|
|
|
|
} else {
|
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
|
'$wgAllowSecuritySensitiveOperationIfCannotReauthenticate lacks a default'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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->getHookRunner()->onSecuritySensitiveOperationStatus(
|
|
|
|
|
$status, $operation, $session, $timeSinceLogin );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// If authentication is not possible, downgrade from "REAUTH" to "FAIL".
|
|
|
|
|
if ( !$this->canAuthenticateNow() && $status === self::SEC_REAUTH ) {
|
|
|
|
|
$status = self::SEC_FAIL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-15 07:29:49 +00:00
|
|
|
$this->logger->info( __METHOD__ . ": $operation is $status for '{user}'",
|
|
|
|
|
[
|
|
|
|
|
'user' => $session->getUser()->getName(),
|
|
|
|
|
'clientip' => $this->getRequest()->getIP(),
|
|
|
|
|
]
|
|
|
|
|
);
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine whether a username can authenticate
|
|
|
|
|
*
|
2016-08-27 05:40:37 +00:00
|
|
|
* This is mainly for internal purposes and only takes authentication data into account,
|
|
|
|
|
* not things like blocks that can change without the authentication system being aware.
|
|
|
|
|
*
|
|
|
|
|
* @param string $username MediaWiki username
|
2015-11-22 20:17:00 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function userCanAuthenticate( $username ) {
|
|
|
|
|
foreach ( $this->getPrimaryAuthenticationProviders() as $provider ) {
|
|
|
|
|
if ( $provider->testUserCanAuthenticate( $username ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide normalized versions of the username for security checks
|
|
|
|
|
*
|
|
|
|
|
* Since different providers can normalize the input in different ways,
|
|
|
|
|
* this returns an array of all the different ways the name might be
|
|
|
|
|
* normalized for authentication.
|
|
|
|
|
*
|
|
|
|
|
* The returned strings should not be revealed to the user, as that might
|
|
|
|
|
* leak private information (e.g. an email address might be normalized to a
|
|
|
|
|
* username).
|
|
|
|
|
*
|
|
|
|
|
* @param string $username
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
public function normalizeUsername( $username ) {
|
|
|
|
|
$ret = [];
|
|
|
|
|
foreach ( $this->getPrimaryAuthenticationProviders() as $provider ) {
|
|
|
|
|
$normalized = $provider->providerNormalizeUsername( $username );
|
|
|
|
|
if ( $normalized !== null ) {
|
|
|
|
|
$ret[$normalized] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return array_keys( $ret );
|
|
|
|
|
}
|
|
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
// endregion -- end of Authentication
|
2015-11-22 20:17:00 +00:00
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
/***************************************************************************/
|
|
|
|
|
// region Authentication data changing
|
|
|
|
|
/** @name Authentication data changing */
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Revoke any authentication credentials for a user
|
|
|
|
|
*
|
|
|
|
|
* After this, the user should no longer be able to log in.
|
|
|
|
|
*
|
|
|
|
|
* @param string $username
|
|
|
|
|
*/
|
|
|
|
|
public function revokeAccessForUser( $username ) {
|
|
|
|
|
$this->logger->info( 'Revoking access for {user}', [
|
|
|
|
|
'user' => $username,
|
|
|
|
|
] );
|
|
|
|
|
$this->callMethodOnProviders( 6, 'providerRevokeAccessForUser', [ $username ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validate a change of authentication data (e.g. passwords)
|
|
|
|
|
* @param AuthenticationRequest $req
|
|
|
|
|
* @param bool $checkData If false, $req hasn't been loaded from the
|
|
|
|
|
* submission so checks on user-submitted fields should be skipped. $req->username is
|
|
|
|
|
* considered user-submitted for this purpose, even if it cannot be changed via
|
|
|
|
|
* $req->loadFromSubmission.
|
|
|
|
|
* @return Status
|
|
|
|
|
*/
|
|
|
|
|
public function allowsAuthenticationDataChange( AuthenticationRequest $req, $checkData = true ) {
|
|
|
|
|
$any = false;
|
|
|
|
|
$providers = $this->getPrimaryAuthenticationProviders() +
|
|
|
|
|
$this->getSecondaryAuthenticationProviders();
|
2020-03-10 21:11:50 +00:00
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
foreach ( $providers as $provider ) {
|
|
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, $checkData );
|
|
|
|
|
if ( !$status->isGood() ) {
|
2020-03-10 21:11:50 +00:00
|
|
|
// If status is not good because reset email password last attempt was within
|
|
|
|
|
// $wgPasswordReminderResendTime then return good status with throttled-mailpassword value;
|
|
|
|
|
// otherwise, return the $status wrapped.
|
|
|
|
|
return $status->hasMessage( 'throttled-mailpassword' )
|
|
|
|
|
? Status::newGood( 'throttled-mailpassword' )
|
|
|
|
|
: Status::wrap( $status );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
$any = $any || $status->value !== 'ignored';
|
|
|
|
|
}
|
|
|
|
|
if ( !$any ) {
|
2021-07-30 19:58:25 +00:00
|
|
|
return Status::newGood( 'ignored' )
|
|
|
|
|
->warning( 'authmanager-change-not-supported' );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Change authentication data (e.g. passwords)
|
|
|
|
|
*
|
|
|
|
|
* If $req was returned for AuthManager::ACTION_CHANGE, using $req should
|
|
|
|
|
* result in a successful login in the future.
|
|
|
|
|
*
|
|
|
|
|
* If $req was returned for AuthManager::ACTION_REMOVE, using $req should
|
|
|
|
|
* no longer result in a successful login.
|
|
|
|
|
*
|
2016-08-27 05:40:37 +00:00
|
|
|
* This method should only be called if allowsAuthenticationDataChange( $req, true )
|
|
|
|
|
* returned success.
|
|
|
|
|
*
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param AuthenticationRequest $req
|
2018-07-17 20:18:59 +00:00
|
|
|
* @param bool $isAddition Set true if this represents an addition of
|
|
|
|
|
* credentials rather than a change. The main difference is that additions
|
|
|
|
|
* should not invalidate BotPasswords. If you're not sure, leave it false.
|
2015-11-22 20:17:00 +00:00
|
|
|
*/
|
2018-07-17 20:18:59 +00:00
|
|
|
public function changeAuthenticationData( AuthenticationRequest $req, $isAddition = false ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->info( 'Changing authentication data for {user} class {what}', [
|
|
|
|
|
'user' => is_string( $req->username ) ? $req->username : '<no name>',
|
|
|
|
|
'what' => get_class( $req ),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->callMethodOnProviders( 6, 'providerChangeAuthenticationData', [ $req ] );
|
|
|
|
|
|
|
|
|
|
// When the main account's authentication data is changed, invalidate
|
|
|
|
|
// all BotPasswords too.
|
2018-07-17 20:18:59 +00:00
|
|
|
if ( !$isAddition ) {
|
2021-08-05 21:09:06 +00:00
|
|
|
$this->botPasswordStore->invalidateUserPasswords( (string)$req->username );
|
2018-07-17 20:18:59 +00:00
|
|
|
}
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
// endregion -- end of Authentication data changing
|
2015-11-22 20:17:00 +00:00
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
/***************************************************************************/
|
|
|
|
|
// region Account creation
|
|
|
|
|
/** @name Account creation */
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine whether accounts can be created
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function canCreateAccounts() {
|
|
|
|
|
foreach ( $this->getPrimaryAuthenticationProviders() as $provider ) {
|
|
|
|
|
switch ( $provider->accountCreationType() ) {
|
|
|
|
|
case PrimaryAuthenticationProvider::TYPE_CREATE:
|
|
|
|
|
case PrimaryAuthenticationProvider::TYPE_LINK:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine whether a particular account can be created
|
2016-08-27 05:40:37 +00:00
|
|
|
* @param string $username MediaWiki username
|
2016-06-16 21:43:12 +00:00
|
|
|
* @param array $options
|
|
|
|
|
* - flags: (int) Bitfield of User:READ_* constants, default User::READ_NORMAL
|
|
|
|
|
* - creating: (bool) For internal use only. Never specify this.
|
2015-11-22 20:17:00 +00:00
|
|
|
* @return Status
|
|
|
|
|
*/
|
2016-06-16 21:43:12 +00:00
|
|
|
public function canCreateAccount( $username, $options = [] ) {
|
|
|
|
|
// Back compat
|
|
|
|
|
if ( is_int( $options ) ) {
|
|
|
|
|
$options = [ 'flags' => $options ];
|
|
|
|
|
}
|
|
|
|
|
$options += [
|
|
|
|
|
'flags' => User::READ_NORMAL,
|
|
|
|
|
'creating' => false,
|
|
|
|
|
];
|
2022-03-28 20:10:05 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
|
2016-06-16 21:43:12 +00:00
|
|
|
$flags = $options['flags'];
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !$this->canCreateAccounts() ) {
|
|
|
|
|
return Status::newFatal( 'authmanager-create-disabled' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $this->userExists( $username, $flags ) ) {
|
|
|
|
|
return Status::newFatal( 'userexists' );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 04:08:00 +00:00
|
|
|
$user = $this->userFactory->newFromName( (string)$username, UserRigorOptions::RIGOR_CREATABLE );
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !is_object( $user ) ) {
|
|
|
|
|
return Status::newFatal( 'noname' );
|
|
|
|
|
} else {
|
|
|
|
|
$user->load( $flags ); // Explicitly load with $flags, auto-loading always uses READ_NORMAL
|
2022-04-29 18:32:20 +00:00
|
|
|
if ( $user->isRegistered() ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
return Status::newFatal( 'userexists' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Denied by providers?
|
|
|
|
|
$providers = $this->getPreAuthenticationProviders() +
|
|
|
|
|
$this->getPrimaryAuthenticationProviders() +
|
|
|
|
|
$this->getSecondaryAuthenticationProviders();
|
|
|
|
|
foreach ( $providers as $provider ) {
|
2016-06-16 21:43:12 +00:00
|
|
|
$status = $provider->testUserForCreation( $user, false, $options );
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
return Status::wrap( $status );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-05-24 13:40:37 +00:00
|
|
|
* @param callable $authorizer ( string $action, PageIdentity $target, PermissionStatus $status )
|
|
|
|
|
* @param Authority $creator
|
|
|
|
|
* @return StatusValue
|
2015-11-22 20:17:00 +00:00
|
|
|
*/
|
2021-05-24 13:40:37 +00:00
|
|
|
private function authorizeInternal(
|
|
|
|
|
callable $authorizer,
|
|
|
|
|
Authority $creator
|
|
|
|
|
): StatusValue {
|
2015-11-22 20:17:00 +00:00
|
|
|
// Wiki is read-only?
|
2020-10-10 21:03:11 +00:00
|
|
|
if ( $this->readOnlyMode->isReadOnly() ) {
|
2021-05-24 13:40:37 +00:00
|
|
|
return StatusValue::newFatal( wfMessage( 'readonlytext', $this->readOnlyMode->getReason() ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-22 20:38:43 +00:00
|
|
|
$permStatus = new PermissionStatus();
|
2021-05-24 13:40:37 +00:00
|
|
|
if ( !$authorizer(
|
2020-02-20 09:45:13 +00:00
|
|
|
'createaccount',
|
2021-02-22 20:38:43 +00:00
|
|
|
SpecialPage::getTitleFor( 'CreateAccount' ),
|
|
|
|
|
$permStatus
|
|
|
|
|
) ) {
|
2021-05-24 13:40:37 +00:00
|
|
|
return $permStatus;
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-17 14:54:01 +00:00
|
|
|
$ip = $this->getRequest()->getIP();
|
2020-10-30 10:55:50 +00:00
|
|
|
if ( $this->blockManager->isDnsBlacklisted( $ip, true /* check $wgProxyWhitelist */ ) ) {
|
2021-05-24 13:40:37 +00:00
|
|
|
return StatusValue::newFatal( 'sorbs_create_account_reason' );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-24 13:40:37 +00:00
|
|
|
return StatusValue::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check whether $creator can create accounts.
|
|
|
|
|
*
|
|
|
|
|
* @note this method does not guarantee full permissions check, so it should only
|
|
|
|
|
* be used to to decide whether to show a form. To authorize the account creation
|
|
|
|
|
* action use {@link self::authorizeCreateAccount} instead.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.39
|
|
|
|
|
* @param Authority $creator
|
|
|
|
|
* @return StatusValue
|
|
|
|
|
*/
|
|
|
|
|
public function probablyCanCreateAccount( Authority $creator ): StatusValue {
|
|
|
|
|
return $this->authorizeInternal(
|
|
|
|
|
static function (
|
|
|
|
|
string $action,
|
|
|
|
|
PageIdentity $target,
|
|
|
|
|
PermissionStatus $status
|
|
|
|
|
) use ( $creator ) {
|
|
|
|
|
return $creator->probablyCan( $action, $target, $status );
|
|
|
|
|
},
|
|
|
|
|
$creator
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Authorize the account creation by $creator
|
|
|
|
|
*
|
|
|
|
|
* @note this method should be used right before the account is created.
|
|
|
|
|
* To check whether a current performer has the potential to create accounts,
|
|
|
|
|
* use {@link self::probablyCanCreateAccount} instead.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.39
|
|
|
|
|
* @param Authority $creator
|
|
|
|
|
* @return StatusValue
|
|
|
|
|
*/
|
|
|
|
|
public function authorizeCreateAccount( Authority $creator ): StatusValue {
|
|
|
|
|
return $this->authorizeInternal(
|
|
|
|
|
static function (
|
|
|
|
|
string $action,
|
|
|
|
|
PageIdentity $target,
|
|
|
|
|
PermissionStatus $status
|
|
|
|
|
) use ( $creator ) {
|
|
|
|
|
return $creator->authorizeWrite( $action, $target, $status );
|
|
|
|
|
},
|
|
|
|
|
$creator
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Basic permissions checks on whether a user can create accounts
|
|
|
|
|
*
|
|
|
|
|
* @deprecated since 1.39, use ::authorizeCreateAccount or
|
|
|
|
|
* ::probablyCanCreateAccount instead
|
|
|
|
|
*
|
|
|
|
|
* @param Authority $creator User doing the account creation
|
|
|
|
|
* @return StatusValue
|
|
|
|
|
*/
|
|
|
|
|
public function checkAccountCreatePermissions( Authority $creator ): StatusValue {
|
|
|
|
|
wfDeprecated( __METHOD__, '1.39' );
|
|
|
|
|
return Status::wrap( $this->authorizeCreateAccount( $creator ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start an account creation flow
|
2016-05-26 17:09:14 +00:00
|
|
|
*
|
|
|
|
|
* In addition to the AuthenticationRequests returned by
|
|
|
|
|
* $this->getAuthenticationRequests(), a client might include a
|
|
|
|
|
* CreateFromLoginAuthenticationRequest from a previous login attempt. If
|
|
|
|
|
* <code>
|
|
|
|
|
* $createFromLoginAuthenticationRequest->hasPrimaryStateForAction( AuthManager::ACTION_CREATE )
|
|
|
|
|
* </code>
|
|
|
|
|
* returns true, any AuthenticationRequest::PRIMARY_REQUIRED requests
|
|
|
|
|
* should be omitted. If the CreateFromLoginAuthenticationRequest has a
|
|
|
|
|
* username set, that username must be used for all other requests.
|
|
|
|
|
*
|
2021-04-21 16:25:17 +00:00
|
|
|
* @param Authority $creator User doing the account creation
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param AuthenticationRequest[] $reqs
|
|
|
|
|
* @param string $returnToUrl Url that REDIRECT responses should eventually
|
|
|
|
|
* return to.
|
|
|
|
|
* @return AuthenticationResponse
|
|
|
|
|
*/
|
2021-04-21 16:25:17 +00:00
|
|
|
public function beginAccountCreation( Authority $creator, array $reqs, $returnToUrl ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
if ( !$this->canCreateAccounts() ) {
|
|
|
|
|
// Caller should have called canCreateAccounts()
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
throw new \LogicException( 'Account creation is not possible' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$username = AuthenticationRequest::getUsernameFromRequests( $reqs );
|
|
|
|
|
} catch ( \UnexpectedValueException $ex ) {
|
|
|
|
|
$username = null;
|
|
|
|
|
}
|
|
|
|
|
if ( $username === null ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': No username provided' );
|
|
|
|
|
return AuthenticationResponse::newFail( wfMessage( 'noname' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Permissions check
|
2021-05-24 13:40:37 +00:00
|
|
|
$status = Status::wrap( $this->authorizeCreateAccount( $creator ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': {creator} cannot create users: {reason}', [
|
|
|
|
|
'user' => $username,
|
2021-04-21 16:25:17 +00:00
|
|
|
'creator' => $creator->getUser()->getName(),
|
2021-10-16 20:35:01 +00:00
|
|
|
'reason' => $status->getWikiText( false, false, 'en' )
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
|
|
|
|
return AuthenticationResponse::newFail( $status->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-16 21:43:12 +00:00
|
|
|
$status = $this->canCreateAccount(
|
|
|
|
|
$username, [ 'flags' => User::READ_LOCKING, 'creating' => true ]
|
|
|
|
|
);
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': {user} cannot be created: {reason}', [
|
|
|
|
|
'user' => $username,
|
2021-04-21 16:25:17 +00:00
|
|
|
'creator' => $creator->getUser()->getName(),
|
2021-10-16 20:35:01 +00:00
|
|
|
'reason' => $status->getWikiText( false, false, 'en' )
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
|
|
|
|
return AuthenticationResponse::newFail( $status->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-16 04:08:00 +00:00
|
|
|
$user = $this->userFactory->newFromName( (string)$username, UserRigorOptions::RIGOR_CREATABLE );
|
2015-11-22 20:17:00 +00:00
|
|
|
foreach ( $reqs as $req ) {
|
|
|
|
|
$req->username = $username;
|
|
|
|
|
$req->returnToUrl = $returnToUrl;
|
|
|
|
|
if ( $req instanceof UserDataAuthenticationRequest ) {
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable user should be checked and valid here
|
2015-11-22 20:17:00 +00:00
|
|
|
$status = $req->populateUser( $user );
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$status = Status::wrap( $status );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': UserData is invalid: {reason}', [
|
|
|
|
|
'user' => $user->getName(),
|
2021-04-21 16:25:17 +00:00
|
|
|
'creator' => $creator->getUser()->getName(),
|
2021-10-16 20:35:01 +00:00
|
|
|
'reason' => $status->getWikiText( false, false, 'en' ),
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
|
|
|
|
return AuthenticationResponse::newFail( $status->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->removeAuthenticationSessionData( null );
|
|
|
|
|
|
|
|
|
|
$state = [
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'userid' => 0,
|
2021-04-21 16:25:17 +00:00
|
|
|
'creatorid' => $creator->getUser()->getId(),
|
|
|
|
|
'creatorname' => $creator->getUser()->getName(),
|
2015-11-22 20:17:00 +00:00
|
|
|
'reqs' => $reqs,
|
|
|
|
|
'returnToUrl' => $returnToUrl,
|
|
|
|
|
'primary' => null,
|
|
|
|
|
'primaryResponse' => null,
|
|
|
|
|
'secondary' => [],
|
|
|
|
|
'continueRequests' => [],
|
|
|
|
|
'maybeLink' => [],
|
|
|
|
|
'ranPreTests' => false,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Special case: converting a login to an account creation
|
|
|
|
|
$req = AuthenticationRequest::getRequestByClass(
|
|
|
|
|
$reqs, CreateFromLoginAuthenticationRequest::class
|
|
|
|
|
);
|
|
|
|
|
if ( $req ) {
|
|
|
|
|
$state['maybeLink'] = $req->maybeLink;
|
|
|
|
|
|
|
|
|
|
if ( $req->createRequest ) {
|
2016-05-26 17:09:14 +00:00
|
|
|
$reqs[] = $req->createRequest;
|
|
|
|
|
$state['reqs'][] = $req->createRequest;
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$session->setSecret( 'AuthManager::accountCreationState', $state );
|
|
|
|
|
$session->persist();
|
|
|
|
|
|
|
|
|
|
return $this->continueAccountCreation( $reqs );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Continue an account creation flow
|
|
|
|
|
* @param AuthenticationRequest[] $reqs
|
|
|
|
|
* @return AuthenticationResponse
|
|
|
|
|
*/
|
|
|
|
|
public function continueAccountCreation( array $reqs ) {
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
try {
|
|
|
|
|
if ( !$this->canCreateAccounts() ) {
|
|
|
|
|
// Caller should have called canCreateAccounts()
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
throw new \LogicException( 'Account creation is not possible' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$state = $session->getSecret( 'AuthManager::accountCreationState' );
|
|
|
|
|
if ( !is_array( $state ) ) {
|
|
|
|
|
return AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-create-not-in-progress' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$state['continueRequests'] = [];
|
|
|
|
|
|
|
|
|
|
// Step 0: Prepare and validate the input
|
|
|
|
|
|
2021-08-05 21:09:06 +00:00
|
|
|
$user = $this->userFactory->newFromName(
|
|
|
|
|
(string)$state['username'],
|
2022-03-16 04:08:00 +00:00
|
|
|
UserRigorOptions::RIGOR_CREATABLE
|
2021-08-05 21:09:06 +00:00
|
|
|
);
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !is_object( $user ) ) {
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': Invalid username', [
|
|
|
|
|
'user' => $state['username'],
|
|
|
|
|
] );
|
|
|
|
|
return AuthenticationResponse::newFail( wfMessage( 'noname' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $state['creatorid'] ) {
|
2021-08-05 21:09:06 +00:00
|
|
|
$creator = $this->userFactory->newFromId( (int)$state['creatorid'] );
|
2015-11-22 20:17:00 +00:00
|
|
|
} else {
|
2021-08-05 21:09:06 +00:00
|
|
|
$creator = $this->userFactory->newAnonymous();
|
2015-11-22 20:17:00 +00:00
|
|
|
$creator->setName( $state['creatorname'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid account creation races on double submissions
|
|
|
|
|
$cache = \ObjectCache::getLocalClusterInstance();
|
|
|
|
|
$lock = $cache->getScopedLock( $cache->makeGlobalKey( 'account', md5( $user->getName() ) ) );
|
|
|
|
|
if ( !$lock ) {
|
|
|
|
|
// Don't clear AuthManager::accountCreationState for this code
|
|
|
|
|
// path because the process that won the race owns it.
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': Could not acquire account creation lock', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
return AuthenticationResponse::newFail( wfMessage( 'usernameinprogress' ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Permissions check
|
2021-05-24 13:40:37 +00:00
|
|
|
$status = Status::wrap( $this->authorizeCreateAccount( $creator ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': {creator} cannot create users: {reason}', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
2021-10-16 20:35:01 +00:00
|
|
|
'reason' => $status->getWikiText( false, false, 'en' )
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
|
|
|
|
$ret = AuthenticationResponse::newFail( $status->getMessage() );
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-01 21:04:40 +00:00
|
|
|
// Load from primary DB for existence check
|
2015-11-22 20:17:00 +00:00
|
|
|
$user->load( User::READ_LOCKING );
|
|
|
|
|
|
|
|
|
|
if ( $state['userid'] === 0 ) {
|
2022-04-29 18:32:20 +00:00
|
|
|
if ( $user->isRegistered() ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ': User exists locally', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$ret = AuthenticationResponse::newFail( wfMessage( 'userexists' ) );
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-04-29 18:32:20 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ': User does not exist locally when it should', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
'expected_id' => $state['userid'],
|
|
|
|
|
] );
|
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
|
"User \"{$state['username']}\" should exist now, but doesn't!"
|
|
|
|
|
);
|
|
|
|
|
}
|
2018-10-31 13:22:47 +00:00
|
|
|
if ( $user->getId() !== $state['userid'] ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ': User ID/name mismatch', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
'expected_id' => $state['userid'],
|
|
|
|
|
'actual_id' => $user->getId(),
|
|
|
|
|
] );
|
|
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
|
"User \"{$state['username']}\" exists, but " .
|
2018-10-31 13:22:47 +00:00
|
|
|
"ID {$user->getId()} !== {$state['userid']}!"
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach ( $state['reqs'] as $req ) {
|
|
|
|
|
if ( $req instanceof UserDataAuthenticationRequest ) {
|
|
|
|
|
$status = $req->populateUser( $user );
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
// This should never happen...
|
|
|
|
|
$status = Status::wrap( $status );
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': UserData is invalid: {reason}', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
2021-10-16 20:35:01 +00:00
|
|
|
'reason' => $status->getWikiText( false, false, 'en' ),
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
|
|
|
|
$ret = AuthenticationResponse::newFail( $status->getMessage() );
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $reqs as $req ) {
|
|
|
|
|
$req->returnToUrl = $state['returnToUrl'];
|
|
|
|
|
$req->username = $state['username'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run pre-creation tests, if we haven't already
|
|
|
|
|
if ( !$state['ranPreTests'] ) {
|
|
|
|
|
$providers = $this->getPreAuthenticationProviders() +
|
|
|
|
|
$this->getPrimaryAuthenticationProviders() +
|
|
|
|
|
$this->getSecondaryAuthenticationProviders();
|
|
|
|
|
foreach ( $providers as $id => $provider ) {
|
|
|
|
|
$status = $provider->testForAccountCreation( $user, $creator, $reqs );
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ": Fail in pre-authentication by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
Status::wrap( $status )->getMessage()
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$state['ranPreTests'] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 1: Choose a primary authentication provider and call it until it succeeds.
|
|
|
|
|
|
|
|
|
|
if ( $state['primary'] === null ) {
|
|
|
|
|
// We haven't picked a PrimaryAuthenticationProvider yet
|
|
|
|
|
foreach ( $this->getPrimaryAuthenticationProviders() as $id => $provider ) {
|
|
|
|
|
if ( $provider->accountCreationType() === PrimaryAuthenticationProvider::TYPE_NONE ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$res = $provider->beginPrimaryAccountCreation( $user, $creator, $reqs );
|
|
|
|
|
switch ( $res->status ) {
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::PASS:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Primary creation passed by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$state['primary'] = $id;
|
|
|
|
|
$state['primaryResponse'] = $res;
|
|
|
|
|
break 2;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::FAIL:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Primary creation failed by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $res ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
return $res;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::ABSTAIN:
|
2015-11-22 20:17:00 +00:00
|
|
|
// Continue loop
|
|
|
|
|
break;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::REDIRECT:
|
|
|
|
|
case AuthenticationResponse::UI:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Primary creation $res->status by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
2016-06-01 15:58:44 +00:00
|
|
|
$this->fillRequests( $res->neededRequests, self::ACTION_CREATE, null );
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['primary'] = $id;
|
|
|
|
|
$state['continueRequests'] = $res->neededRequests;
|
|
|
|
|
$session->setSecret( 'AuthManager::accountCreationState', $state );
|
|
|
|
|
return $res;
|
|
|
|
|
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
default:
|
|
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . "::beginPrimaryAccountCreation() returned $res->status"
|
|
|
|
|
);
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-09 09:09:00 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in loop before, if passed
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( $state['primary'] === null ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': Primary creation failed because no provider accepted', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-create-no-primary' )
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( $state['primaryResponse'] === null ) {
|
|
|
|
|
$provider = $this->getAuthenticationProvider( $state['primary'] );
|
|
|
|
|
if ( !$provider instanceof PrimaryAuthenticationProvider ) {
|
|
|
|
|
// Configuration changed? Force them to start over.
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-create-not-in-progress' )
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
$id = $provider->getUniqueId();
|
|
|
|
|
$res = $provider->continuePrimaryAccountCreation( $user, $creator, $reqs );
|
|
|
|
|
switch ( $res->status ) {
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::PASS:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Primary creation passed by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$state['primaryResponse'] = $res;
|
|
|
|
|
break;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::FAIL:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Primary creation failed by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $res ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
return $res;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::REDIRECT:
|
|
|
|
|
case AuthenticationResponse::UI:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Primary creation $res->status by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
2016-06-01 15:58:44 +00:00
|
|
|
$this->fillRequests( $res->neededRequests, self::ACTION_CREATE, null );
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['continueRequests'] = $res->neededRequests;
|
|
|
|
|
$session->setSecret( 'AuthManager::accountCreationState', $state );
|
|
|
|
|
return $res;
|
|
|
|
|
default:
|
|
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . "::continuePrimaryAccountCreation() returned $res->status"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 2: Primary authentication succeeded, create the User object
|
|
|
|
|
// and add the user locally.
|
|
|
|
|
|
|
|
|
|
if ( $state['userid'] === 0 ) {
|
|
|
|
|
$this->logger->info( 'Creating user {user} during account creation', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$status = $user->addToDatabase();
|
2016-09-08 22:10:19 +00:00
|
|
|
if ( !$status->isOK() ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
$ret = AuthenticationResponse::newFail( $status->getMessage() );
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
$this->setDefaultUserOptions( $user, $creator->isAnon() );
|
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->getHookRunner()->onLocalUserCreated( $user, false );
|
2015-11-22 20:17:00 +00:00
|
|
|
$user->saveSettings();
|
|
|
|
|
$state['userid'] = $user->getId();
|
|
|
|
|
|
|
|
|
|
// Update user count
|
2018-02-28 03:15:43 +00:00
|
|
|
\DeferredUpdates::addUpdate( \SiteStatsUpdate::factory( [ 'users' => 1 ] ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// Watch user's userpage and talk page
|
2021-04-13 00:43:46 +00:00
|
|
|
$this->watchlistManager->addWatchIgnoringRights( $user, $user->getUserPage() );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// Inform the provider
|
2022-03-29 18:11:06 +00:00
|
|
|
// @phan-suppress-next-next-line PhanPossiblyUndeclaredVariable
|
2022-05-09 09:09:00 +00:00
|
|
|
// @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in loop before, if passed
|
2015-11-22 20:17:00 +00:00
|
|
|
$logSubtype = $provider->finishAccountCreation( $user, $creator, $state['primaryResponse'] );
|
|
|
|
|
|
|
|
|
|
// Log the creation
|
2022-04-25 15:19:41 +00:00
|
|
|
if ( $this->config->get( MainConfigNames::NewUserLog ) ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$isAnon = $creator->isAnon();
|
|
|
|
|
$logEntry = new \ManualLogEntry(
|
|
|
|
|
'newusers',
|
|
|
|
|
$logSubtype ?: ( $isAnon ? 'create' : 'create2' )
|
|
|
|
|
);
|
|
|
|
|
$logEntry->setPerformer( $isAnon ? $user : $creator );
|
|
|
|
|
$logEntry->setTarget( $user->getUserPage() );
|
2016-09-08 22:10:19 +00:00
|
|
|
/** @var CreationReasonAuthenticationRequest $req */
|
2015-11-22 20:17:00 +00:00
|
|
|
$req = AuthenticationRequest::getRequestByClass(
|
|
|
|
|
$state['reqs'], CreationReasonAuthenticationRequest::class
|
|
|
|
|
);
|
|
|
|
|
$logEntry->setComment( $req ? $req->reason : '' );
|
|
|
|
|
$logEntry->setParameters( [
|
|
|
|
|
'4::userid' => $user->getId(),
|
|
|
|
|
] );
|
|
|
|
|
$logid = $logEntry->insert();
|
|
|
|
|
$logEntry->publish( $logid );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 3: Iterate over all the secondary authentication providers.
|
|
|
|
|
|
|
|
|
|
$beginReqs = $state['reqs'];
|
|
|
|
|
|
|
|
|
|
foreach ( $this->getSecondaryAuthenticationProviders() as $id => $provider ) {
|
|
|
|
|
if ( !isset( $state['secondary'][$id] ) ) {
|
|
|
|
|
// This provider isn't started yet, so we pass it the set
|
|
|
|
|
// of reqs from beginAuthentication instead of whatever
|
|
|
|
|
// might have been used by a previous provider in line.
|
|
|
|
|
$func = 'beginSecondaryAccountCreation';
|
|
|
|
|
$res = $provider->beginSecondaryAccountCreation( $user, $creator, $beginReqs );
|
|
|
|
|
} elseif ( !$state['secondary'][$id] ) {
|
|
|
|
|
$func = 'continueSecondaryAccountCreation';
|
|
|
|
|
$res = $provider->continueSecondaryAccountCreation( $user, $creator, $reqs );
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
switch ( $res->status ) {
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::PASS:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Secondary creation passed by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
// fall through
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::ABSTAIN:
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['secondary'][$id] = true;
|
|
|
|
|
break;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::REDIRECT:
|
|
|
|
|
case AuthenticationResponse::UI:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Secondary creation $res->status by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
2016-06-01 15:58:44 +00:00
|
|
|
$this->fillRequests( $res->neededRequests, self::ACTION_CREATE, null );
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['secondary'][$id] = false;
|
|
|
|
|
$state['continueRequests'] = $res->neededRequests;
|
|
|
|
|
$session->setSecret( 'AuthManager::accountCreationState', $state );
|
|
|
|
|
return $res;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::FAIL:
|
2015-11-22 20:17:00 +00:00
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . "::{$func}() returned $res->status." .
|
|
|
|
|
' Secondary providers are not allowed to fail account creation, that' .
|
|
|
|
|
' should have been done via testForAccountCreation().'
|
|
|
|
|
);
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
default:
|
|
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . "::{$func}() returned $res->status"
|
|
|
|
|
);
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$id = $user->getId();
|
|
|
|
|
$name = $user->getName();
|
|
|
|
|
$req = new CreatedAccountAuthenticationRequest( $id, $name );
|
|
|
|
|
$ret = AuthenticationResponse::newPass( $name );
|
|
|
|
|
$ret->loginRequest = $req;
|
|
|
|
|
$this->createdAccountAuthenticationRequests[] = $req;
|
|
|
|
|
|
|
|
|
|
$this->logger->info( __METHOD__ . ': Account creation succeeded for {user}', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
'creator' => $creator->getName(),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->callMethodOnProviders( 7, 'postAccountCreation', [ $user, $creator, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
$this->removeAuthenticationSessionData( null );
|
|
|
|
|
return $ret;
|
|
|
|
|
} catch ( \Exception $ex ) {
|
|
|
|
|
$session->remove( 'AuthManager::accountCreationState' );
|
|
|
|
|
throw $ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-02-28 15:00:07 +00:00
|
|
|
* Auto-create an account, and optionally log into that account
|
2016-08-27 05:40:37 +00:00
|
|
|
*
|
|
|
|
|
* PrimaryAuthenticationProviders can invoke this method by returning a PASS from
|
|
|
|
|
* beginPrimaryAuthentication/continuePrimaryAuthentication with the username of a
|
|
|
|
|
* non-existing user. SessionProviders can invoke it by returning a SessionInfo with
|
|
|
|
|
* the username of a non-existing user from provideSessionInfo(). Calling this method
|
|
|
|
|
* explicitly (e.g. from a maintenance script) is also fine.
|
|
|
|
|
*
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param User $user User to auto-create
|
2018-12-30 02:41:27 +00:00
|
|
|
* @param string $source What caused the auto-creation? This must be one of:
|
|
|
|
|
* - the ID of a PrimaryAuthenticationProvider,
|
2022-02-28 03:05:58 +00:00
|
|
|
* - one of the self::AUTOCREATE_SOURCE_* constants
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param bool $login Whether to also log the user in
|
2021-02-28 15:00:07 +00:00
|
|
|
* @param bool $log Whether to generate a user creation log entry (since 1.36)
|
2015-11-22 20:17:00 +00:00
|
|
|
* @return Status Good if user was created, Ok if user already existed, otherwise Fatal
|
|
|
|
|
*/
|
2021-02-28 15:00:07 +00:00
|
|
|
public function autoCreateUser( User $user, $source, $login = true, $log = true ) {
|
2022-02-28 03:05:58 +00:00
|
|
|
$validSources = [
|
|
|
|
|
self::AUTOCREATE_SOURCE_SESSION,
|
|
|
|
|
self::AUTOCREATE_SOURCE_MAINT,
|
|
|
|
|
self::AUTOCREATE_SOURCE_TEMP
|
|
|
|
|
];
|
|
|
|
|
if ( !in_array( $source, $validSources, true )
|
|
|
|
|
&& !$this->getAuthenticationProvider( $source ) instanceof PrimaryAuthenticationProvider
|
2015-11-22 20:17:00 +00:00
|
|
|
) {
|
|
|
|
|
throw new \InvalidArgumentException( "Unknown auto-creation source: $source" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$username = $user->getName();
|
|
|
|
|
|
2016-09-05 20:21:26 +00:00
|
|
|
// Try the local user from the replica DB
|
2021-08-05 21:09:06 +00:00
|
|
|
$localUserIdentity = $this->userIdentityLookup->getUserIdentityByName( $username );
|
|
|
|
|
$localId = ( $localUserIdentity && $localUserIdentity->getId() )
|
|
|
|
|
? $localUserIdentity->getId()
|
|
|
|
|
: null;
|
2015-11-22 20:17:00 +00:00
|
|
|
$flags = User::READ_NORMAL;
|
|
|
|
|
|
2021-04-19 01:02:08 +00:00
|
|
|
// Fetch the user ID from the primary, so that we don't try to create the user
|
2015-11-22 20:17:00 +00:00
|
|
|
// when they already exist, due to replication lag
|
|
|
|
|
// @codeCoverageIgnoreStart
|
2018-01-04 16:38:22 +00:00
|
|
|
if (
|
|
|
|
|
!$localId &&
|
2021-07-22 11:38:45 +00:00
|
|
|
$this->loadBalancer->getReaderIndex() !== 0
|
2018-01-04 16:38:22 +00:00
|
|
|
) {
|
2021-08-05 21:09:06 +00:00
|
|
|
$localUserIdentity = $this->userIdentityLookup->getUserIdentityByName(
|
|
|
|
|
$username,
|
|
|
|
|
UserIdentityLookup::READ_LATEST
|
|
|
|
|
);
|
|
|
|
|
$localId = ( $localUserIdentity && $localUserIdentity->getId() )
|
|
|
|
|
? $localUserIdentity->getId()
|
|
|
|
|
: null;
|
2015-11-22 20:17:00 +00:00
|
|
|
$flags = User::READ_LATEST;
|
|
|
|
|
}
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
|
|
|
|
|
if ( $localId ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': {username} already exists locally', [
|
|
|
|
|
'username' => $username,
|
|
|
|
|
] );
|
|
|
|
|
$user->setId( $localId );
|
|
|
|
|
$user->loadFromId( $flags );
|
|
|
|
|
if ( $login ) {
|
|
|
|
|
$this->setSessionDataForUser( $user );
|
|
|
|
|
}
|
2021-07-30 19:58:25 +00:00
|
|
|
return Status::newGood()->warning( 'userexists' );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wiki is read-only?
|
2020-10-10 21:03:11 +00:00
|
|
|
if ( $this->readOnlyMode->isReadOnly() ) {
|
|
|
|
|
$reason = $this->readOnlyMode->getReason();
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': denied because of read only mode: {reason}', [
|
2015-11-22 20:17:00 +00:00
|
|
|
'username' => $username,
|
2020-10-10 21:03:11 +00:00
|
|
|
'reason' => $reason,
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
|
|
|
|
$user->setId( 0 );
|
|
|
|
|
$user->loadFromId();
|
2020-10-10 21:03:11 +00:00
|
|
|
return Status::newFatal( wfMessage( 'readonlytext', $reason ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check the session, if we tried to create this user already there's
|
|
|
|
|
// no point in retrying.
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
if ( $session->get( 'AuthManager::AutoCreateBlacklist' ) ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': blacklisted in session {sessionid}', [
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'sessionid' => $session->getId(),
|
|
|
|
|
] );
|
|
|
|
|
$user->setId( 0 );
|
|
|
|
|
$user->loadFromId();
|
|
|
|
|
$reason = $session->get( 'AuthManager::AutoCreateBlacklist' );
|
|
|
|
|
if ( $reason instanceof StatusValue ) {
|
|
|
|
|
return Status::wrap( $reason );
|
|
|
|
|
} else {
|
|
|
|
|
return Status::newFatal( $reason );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-23 23:24:38 +00:00
|
|
|
// Is the username valid? (Previously isCreatable() was checked here but
|
|
|
|
|
// that doesn't work with auto-creation of TempUser accounts by CentralAuth)
|
|
|
|
|
if ( !$this->userNameUtils->isValid( $username ) ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': name "{username}" is not valid', [
|
2015-11-22 20:17:00 +00:00
|
|
|
'username' => $username,
|
|
|
|
|
] );
|
2016-09-08 22:10:19 +00:00
|
|
|
$session->set( 'AuthManager::AutoCreateBlacklist', 'noname' );
|
2015-11-22 20:17:00 +00:00
|
|
|
$user->setId( 0 );
|
|
|
|
|
$user->loadFromId();
|
|
|
|
|
return Status::newFatal( 'noname' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Is the IP user able to create accounts?
|
2021-08-05 21:09:06 +00:00
|
|
|
$anon = $this->userFactory->newAnonymous();
|
2020-10-16 17:36:29 +00:00
|
|
|
if ( $source !== self::AUTOCREATE_SOURCE_MAINT &&
|
2021-02-22 20:38:43 +00:00
|
|
|
!$anon->isAllowedAny( 'createaccount', 'autocreateaccount' )
|
2019-01-23 20:14:37 +00:00
|
|
|
) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ': IP lacks the ability to create or autocreate accounts', [
|
|
|
|
|
'username' => $username,
|
2020-02-14 17:48:55 +00:00
|
|
|
'clientip' => $anon->getName(),
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
2016-09-08 22:10:19 +00:00
|
|
|
$session->set( 'AuthManager::AutoCreateBlacklist', 'authmanager-autocreate-noperm' );
|
2015-11-22 20:17:00 +00:00
|
|
|
$session->persist();
|
|
|
|
|
$user->setId( 0 );
|
|
|
|
|
$user->loadFromId();
|
|
|
|
|
return Status::newFatal( 'authmanager-autocreate-noperm' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Avoid account creation races on double submissions
|
|
|
|
|
$cache = \ObjectCache::getLocalClusterInstance();
|
|
|
|
|
$lock = $cache->getScopedLock( $cache->makeGlobalKey( 'account', md5( $username ) ) );
|
|
|
|
|
if ( !$lock ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': Could not acquire account creation lock', [
|
|
|
|
|
'user' => $username,
|
|
|
|
|
] );
|
|
|
|
|
$user->setId( 0 );
|
|
|
|
|
$user->loadFromId();
|
|
|
|
|
return Status::newFatal( 'usernameinprogress' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Denied by providers?
|
2016-06-16 21:43:12 +00:00
|
|
|
$options = [
|
|
|
|
|
'flags' => User::READ_LATEST,
|
|
|
|
|
'creating' => true,
|
|
|
|
|
];
|
2015-11-22 20:17:00 +00:00
|
|
|
$providers = $this->getPreAuthenticationProviders() +
|
|
|
|
|
$this->getPrimaryAuthenticationProviders() +
|
|
|
|
|
$this->getSecondaryAuthenticationProviders();
|
|
|
|
|
foreach ( $providers as $provider ) {
|
2016-06-16 21:43:12 +00:00
|
|
|
$status = $provider->testUserForCreation( $user, $source, $options );
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$ret = Status::wrap( $status );
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': Provider denied creation of {username}: {reason}', [
|
|
|
|
|
'username' => $username,
|
2021-10-16 20:35:01 +00:00
|
|
|
'reason' => $ret->getWikiText( false, false, 'en' ),
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
2016-09-08 22:10:19 +00:00
|
|
|
$session->set( 'AuthManager::AutoCreateBlacklist', $status );
|
2015-11-22 20:17:00 +00:00
|
|
|
$user->setId( 0 );
|
|
|
|
|
$user->loadFromId();
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:01:34 +00:00
|
|
|
$backoffKey = $cache->makeKey( 'AuthManager', 'autocreate-failed', md5( $username ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( $cache->get( $backoffKey ) ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': {username} denied by prior creation attempt failures', [
|
|
|
|
|
'username' => $username,
|
|
|
|
|
] );
|
|
|
|
|
$user->setId( 0 );
|
|
|
|
|
$user->loadFromId();
|
|
|
|
|
return Status::newFatal( 'authmanager-autocreate-exception' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Checks passed, create the user...
|
2017-10-06 22:17:58 +00:00
|
|
|
$from = $_SERVER['REQUEST_URI'] ?? 'CLI';
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->info( __METHOD__ . ': creating new user ({username}) - from: {from}', [
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'from' => $from,
|
|
|
|
|
] );
|
|
|
|
|
|
2021-09-01 21:04:40 +00:00
|
|
|
// Ignore warnings about primary connections/writes...hard to avoid here
|
2021-05-15 08:09:28 +00:00
|
|
|
$trxProfilerSilencedScope = \Profiler::instance()->getTransactionProfiler()->silenceForScope();
|
2015-11-22 20:17:00 +00:00
|
|
|
try {
|
|
|
|
|
$status = $user->addToDatabase();
|
2016-09-08 22:10:19 +00:00
|
|
|
if ( !$status->isOK() ) {
|
2016-09-08 22:40:20 +00:00
|
|
|
// Double-check for a race condition (T70012). We make use of the fact that when
|
|
|
|
|
// addToDatabase fails due to the user already existing, the user object gets loaded.
|
|
|
|
|
if ( $user->getId() ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->info( __METHOD__ . ': {username} already exists locally (race)', [
|
|
|
|
|
'username' => $username,
|
|
|
|
|
] );
|
|
|
|
|
if ( $login ) {
|
|
|
|
|
$this->setSessionDataForUser( $user );
|
|
|
|
|
}
|
2021-07-30 19:58:25 +00:00
|
|
|
$status = Status::newGood()->warning( 'userexists' );
|
2015-11-22 20:17:00 +00:00
|
|
|
} else {
|
2016-10-01 01:10:56 +00:00
|
|
|
$this->logger->error( __METHOD__ . ': {username} failed with message {msg}', [
|
2015-11-22 20:17:00 +00:00
|
|
|
'username' => $username,
|
2021-10-16 20:35:01 +00:00
|
|
|
'msg' => $status->getWikiText( false, false, 'en' )
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
|
|
|
|
$user->setId( 0 );
|
|
|
|
|
$user->loadFromId();
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
} catch ( \Exception $ex ) {
|
|
|
|
|
$this->logger->error( __METHOD__ . ': {username} failed with exception {exception}', [
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'exception' => $ex,
|
|
|
|
|
] );
|
|
|
|
|
// Do not keep throwing errors for a while
|
|
|
|
|
$cache->set( $backoffKey, 1, 600 );
|
|
|
|
|
// Bubble up error; which should normally trigger DB rollbacks
|
|
|
|
|
throw $ex;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 16:51:22 +00:00
|
|
|
$this->setDefaultUserOptions( $user, false );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// Inform the providers
|
|
|
|
|
$this->callMethodOnProviders( 6, 'autoCreatedAccount', [ $user, $source ] );
|
|
|
|
|
|
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->getHookRunner()->onLocalUserCreated( $user, true );
|
2015-11-22 20:17:00 +00:00
|
|
|
$user->saveSettings();
|
|
|
|
|
|
|
|
|
|
// Update user count
|
2018-02-28 03:15:43 +00:00
|
|
|
\DeferredUpdates::addUpdate( \SiteStatsUpdate::factory( [ 'users' => 1 ] ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
// Watch user's userpage and talk page
|
2021-04-13 00:43:46 +00:00
|
|
|
\DeferredUpdates::addCallableUpdate( function () use ( $user ) {
|
|
|
|
|
$this->watchlistManager->addWatchIgnoringRights( $user, $user->getUserPage() );
|
2016-08-15 14:14:57 +00:00
|
|
|
} );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// Log the creation
|
2022-04-25 15:19:41 +00:00
|
|
|
if ( $this->config->get( MainConfigNames::NewUserLog ) && $log ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$logEntry = new \ManualLogEntry( 'newusers', 'autocreate' );
|
|
|
|
|
$logEntry->setPerformer( $user );
|
|
|
|
|
$logEntry->setTarget( $user->getUserPage() );
|
|
|
|
|
$logEntry->setComment( '' );
|
|
|
|
|
$logEntry->setParameters( [
|
|
|
|
|
'4::userid' => $user->getId(),
|
|
|
|
|
] );
|
2016-07-23 00:00:13 +00:00
|
|
|
$logEntry->insert();
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
2021-05-15 08:09:28 +00:00
|
|
|
ScopedCallback::consume( $trxProfilerSilencedScope );
|
2016-08-15 14:14:57 +00:00
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( $login ) {
|
2022-02-28 03:05:58 +00:00
|
|
|
$remember = $source === self::AUTOCREATE_SOURCE_TEMP;
|
|
|
|
|
$this->setSessionDataForUser( $user, $remember );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Status::newGood();
|
|
|
|
|
}
|
|
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
// endregion -- end of Account creation
|
2015-11-22 20:17:00 +00:00
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
/***************************************************************************/
|
|
|
|
|
// region Account linking
|
|
|
|
|
/** @name Account linking */
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine whether accounts can be linked
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function canLinkAccounts() {
|
|
|
|
|
foreach ( $this->getPrimaryAuthenticationProviders() as $provider ) {
|
|
|
|
|
if ( $provider->accountCreationType() === PrimaryAuthenticationProvider::TYPE_LINK ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start an account linking flow
|
|
|
|
|
*
|
|
|
|
|
* @param User $user User being linked
|
|
|
|
|
* @param AuthenticationRequest[] $reqs
|
|
|
|
|
* @param string $returnToUrl Url that REDIRECT responses should eventually
|
|
|
|
|
* return to.
|
|
|
|
|
* @return AuthenticationResponse
|
|
|
|
|
*/
|
|
|
|
|
public function beginAccountLink( User $user, array $reqs, $returnToUrl ) {
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
$session->remove( 'AuthManager::accountLinkState' );
|
|
|
|
|
|
|
|
|
|
if ( !$this->canLinkAccounts() ) {
|
|
|
|
|
// Caller should have called canLinkAccounts()
|
|
|
|
|
throw new \LogicException( 'Account linking is not possible' );
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-29 18:32:20 +00:00
|
|
|
if ( !$user->isRegistered() ) {
|
2020-04-08 18:19:49 +00:00
|
|
|
if ( !$this->userNameUtils->isUsable( $user->getName() ) ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$msg = wfMessage( 'noname' );
|
|
|
|
|
} else {
|
|
|
|
|
$msg = wfMessage( 'authmanager-userdoesnotexist', $user->getName() );
|
|
|
|
|
}
|
|
|
|
|
return AuthenticationResponse::newFail( $msg );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $reqs as $req ) {
|
|
|
|
|
$req->username = $user->getName();
|
|
|
|
|
$req->returnToUrl = $returnToUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->removeAuthenticationSessionData( null );
|
|
|
|
|
|
|
|
|
|
$providers = $this->getPreAuthenticationProviders();
|
|
|
|
|
foreach ( $providers as $id => $provider ) {
|
|
|
|
|
$status = $provider->testForAccountLink( $user );
|
|
|
|
|
if ( !$status->isGood() ) {
|
|
|
|
|
$this->logger->debug( __METHOD__ . ": Account linking pre-check failed by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
Status::wrap( $status )->getMessage()
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 3, 'postAccountLink', [ $user, $ret ] );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$state = [
|
|
|
|
|
'username' => $user->getName(),
|
|
|
|
|
'userid' => $user->getId(),
|
|
|
|
|
'returnToUrl' => $returnToUrl,
|
|
|
|
|
'primary' => null,
|
|
|
|
|
'continueRequests' => [],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$providers = $this->getPrimaryAuthenticationProviders();
|
|
|
|
|
foreach ( $providers as $id => $provider ) {
|
|
|
|
|
if ( $provider->accountCreationType() !== PrimaryAuthenticationProvider::TYPE_LINK ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$res = $provider->beginPrimaryAccountLink( $user, $reqs );
|
|
|
|
|
switch ( $res->status ) {
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::PASS:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->info( "Account linked to {user} by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$this->callMethodOnProviders( 3, 'postAccountLink', [ $user, $res ] );
|
|
|
|
|
return $res;
|
|
|
|
|
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::FAIL:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Account linking failed by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$this->callMethodOnProviders( 3, 'postAccountLink', [ $user, $res ] );
|
|
|
|
|
return $res;
|
|
|
|
|
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::ABSTAIN:
|
2015-11-22 20:17:00 +00:00
|
|
|
// Continue loop
|
|
|
|
|
break;
|
|
|
|
|
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::REDIRECT:
|
|
|
|
|
case AuthenticationResponse::UI:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Account linking $res->status by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
2016-06-01 15:58:44 +00:00
|
|
|
$this->fillRequests( $res->neededRequests, self::ACTION_LINK, $user->getName() );
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['primary'] = $id;
|
|
|
|
|
$state['continueRequests'] = $res->neededRequests;
|
|
|
|
|
$session->setSecret( 'AuthManager::accountLinkState', $state );
|
|
|
|
|
$session->persist();
|
|
|
|
|
return $res;
|
|
|
|
|
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
default:
|
|
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . "::beginPrimaryAccountLink() returned $res->status"
|
|
|
|
|
);
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': Account linking failed because no provider accepted', [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-link-no-primary' )
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 3, 'postAccountLink', [ $user, $ret ] );
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Continue an account linking flow
|
|
|
|
|
* @param AuthenticationRequest[] $reqs
|
|
|
|
|
* @return AuthenticationResponse
|
|
|
|
|
*/
|
|
|
|
|
public function continueAccountLink( array $reqs ) {
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
try {
|
|
|
|
|
if ( !$this->canLinkAccounts() ) {
|
|
|
|
|
// Caller should have called canLinkAccounts()
|
|
|
|
|
$session->remove( 'AuthManager::accountLinkState' );
|
|
|
|
|
throw new \LogicException( 'Account linking is not possible' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$state = $session->getSecret( 'AuthManager::accountLinkState' );
|
|
|
|
|
if ( !is_array( $state ) ) {
|
|
|
|
|
return AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-link-not-in-progress' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$state['continueRequests'] = [];
|
|
|
|
|
|
|
|
|
|
// Step 0: Prepare and validate the input
|
|
|
|
|
|
2021-08-05 21:09:06 +00:00
|
|
|
$user = $this->userFactory->newFromName(
|
|
|
|
|
(string)$state['username'],
|
2022-03-16 04:08:00 +00:00
|
|
|
UserRigorOptions::RIGOR_USABLE
|
2021-08-05 21:09:06 +00:00
|
|
|
);
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !is_object( $user ) ) {
|
|
|
|
|
$session->remove( 'AuthManager::accountLinkState' );
|
|
|
|
|
return AuthenticationResponse::newFail( wfMessage( 'noname' ) );
|
|
|
|
|
}
|
2018-10-31 13:22:47 +00:00
|
|
|
if ( $user->getId() !== $state['userid'] ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
throw new \UnexpectedValueException(
|
|
|
|
|
"User \"{$state['username']}\" is valid, but " .
|
2018-10-31 13:22:47 +00:00
|
|
|
"ID {$user->getId()} !== {$state['userid']}!"
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $reqs as $req ) {
|
|
|
|
|
$req->username = $state['username'];
|
|
|
|
|
$req->returnToUrl = $state['returnToUrl'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Step 1: Call the primary again until it succeeds
|
|
|
|
|
|
|
|
|
|
$provider = $this->getAuthenticationProvider( $state['primary'] );
|
|
|
|
|
if ( !$provider instanceof PrimaryAuthenticationProvider ) {
|
|
|
|
|
// Configuration changed? Force them to start over.
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
$ret = AuthenticationResponse::newFail(
|
|
|
|
|
wfMessage( 'authmanager-link-not-in-progress' )
|
|
|
|
|
);
|
|
|
|
|
$this->callMethodOnProviders( 3, 'postAccountLink', [ $user, $ret ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountLinkState' );
|
|
|
|
|
return $ret;
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
}
|
|
|
|
|
$id = $provider->getUniqueId();
|
|
|
|
|
$res = $provider->continuePrimaryAccountLink( $user, $reqs );
|
|
|
|
|
switch ( $res->status ) {
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::PASS:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->info( "Account linked to {user} by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$this->callMethodOnProviders( 3, 'postAccountLink', [ $user, $res ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountLinkState' );
|
|
|
|
|
return $res;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::FAIL:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Account linking failed by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
|
|
|
|
$this->callMethodOnProviders( 3, 'postAccountLink', [ $user, $res ] );
|
|
|
|
|
$session->remove( 'AuthManager::accountLinkState' );
|
|
|
|
|
return $res;
|
2020-10-28 18:58:11 +00:00
|
|
|
case AuthenticationResponse::REDIRECT:
|
|
|
|
|
case AuthenticationResponse::UI:
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->logger->debug( __METHOD__ . ": Account linking $res->status by $id", [
|
|
|
|
|
'user' => $user->getName(),
|
|
|
|
|
] );
|
2016-06-01 15:58:44 +00:00
|
|
|
$this->fillRequests( $res->neededRequests, self::ACTION_LINK, $user->getName() );
|
2015-11-22 20:17:00 +00:00
|
|
|
$state['continueRequests'] = $res->neededRequests;
|
|
|
|
|
$session->setSecret( 'AuthManager::accountLinkState', $state );
|
|
|
|
|
return $res;
|
|
|
|
|
default:
|
|
|
|
|
throw new \DomainException(
|
|
|
|
|
get_class( $provider ) . "::continuePrimaryAccountLink() returned $res->status"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch ( \Exception $ex ) {
|
|
|
|
|
$session->remove( 'AuthManager::accountLinkState' );
|
|
|
|
|
throw $ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
// endregion -- end of Account linking
|
2015-11-22 20:17:00 +00:00
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
/***************************************************************************/
|
|
|
|
|
// region Information methods
|
|
|
|
|
/** @name Information methods */
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the applicable list of AuthenticationRequests
|
|
|
|
|
*
|
|
|
|
|
* Possible values for $action:
|
|
|
|
|
* - ACTION_LOGIN: Valid for passing to beginAuthentication
|
|
|
|
|
* - ACTION_LOGIN_CONTINUE: Valid for passing to continueAuthentication in the current state
|
|
|
|
|
* - ACTION_CREATE: Valid for passing to beginAccountCreation
|
|
|
|
|
* - ACTION_CREATE_CONTINUE: Valid for passing to continueAccountCreation in the current state
|
|
|
|
|
* - ACTION_LINK: Valid for passing to beginAccountLink
|
|
|
|
|
* - ACTION_LINK_CONTINUE: Valid for passing to continueAccountLink in the current state
|
|
|
|
|
* - ACTION_CHANGE: Valid for passing to changeAuthenticationData to change credentials
|
|
|
|
|
* - ACTION_REMOVE: Valid for passing to changeAuthenticationData to remove credentials.
|
|
|
|
|
* - ACTION_UNLINK: Same as ACTION_REMOVE, but limited to linked accounts.
|
|
|
|
|
*
|
|
|
|
|
* @param string $action One of the AuthManager::ACTION_* constants
|
2021-04-21 16:25:17 +00:00
|
|
|
* @param UserIdentity|null $user User being acted on, instead of the current user.
|
2015-11-22 20:17:00 +00:00
|
|
|
* @return AuthenticationRequest[]
|
|
|
|
|
*/
|
2021-04-21 16:25:17 +00:00
|
|
|
public function getAuthenticationRequests( $action, UserIdentity $user = null ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$options = [];
|
|
|
|
|
$providerAction = $action;
|
|
|
|
|
|
|
|
|
|
// Figure out which providers to query
|
|
|
|
|
switch ( $action ) {
|
|
|
|
|
case self::ACTION_LOGIN:
|
|
|
|
|
case self::ACTION_CREATE:
|
|
|
|
|
$providers = $this->getPreAuthenticationProviders() +
|
|
|
|
|
$this->getPrimaryAuthenticationProviders() +
|
|
|
|
|
$this->getSecondaryAuthenticationProviders();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::ACTION_LOGIN_CONTINUE:
|
|
|
|
|
$state = $this->request->getSession()->getSecret( 'AuthManager::authnState' );
|
|
|
|
|
return is_array( $state ) ? $state['continueRequests'] : [];
|
|
|
|
|
|
|
|
|
|
case self::ACTION_CREATE_CONTINUE:
|
|
|
|
|
$state = $this->request->getSession()->getSecret( 'AuthManager::accountCreationState' );
|
|
|
|
|
return is_array( $state ) ? $state['continueRequests'] : [];
|
|
|
|
|
|
|
|
|
|
case self::ACTION_LINK:
|
2020-02-18 06:16:24 +00:00
|
|
|
$providers = [];
|
|
|
|
|
foreach ( $this->getPrimaryAuthenticationProviders() as $p ) {
|
|
|
|
|
if ( $p->accountCreationType() === PrimaryAuthenticationProvider::TYPE_LINK ) {
|
|
|
|
|
$providers[] = $p;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-22 20:17:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::ACTION_UNLINK:
|
2020-02-18 06:16:24 +00:00
|
|
|
$providers = [];
|
|
|
|
|
foreach ( $this->getPrimaryAuthenticationProviders() as $p ) {
|
|
|
|
|
if ( $p->accountCreationType() === PrimaryAuthenticationProvider::TYPE_LINK ) {
|
|
|
|
|
$providers[] = $p;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// To providers, unlink and remove are identical.
|
|
|
|
|
$providerAction = self::ACTION_REMOVE;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::ACTION_LINK_CONTINUE:
|
|
|
|
|
$state = $this->request->getSession()->getSecret( 'AuthManager::accountLinkState' );
|
|
|
|
|
return is_array( $state ) ? $state['continueRequests'] : [];
|
|
|
|
|
|
|
|
|
|
case self::ACTION_CHANGE:
|
|
|
|
|
case self::ACTION_REMOVE:
|
|
|
|
|
$providers = $this->getPrimaryAuthenticationProviders() +
|
|
|
|
|
$this->getSecondaryAuthenticationProviders();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
|
default:
|
|
|
|
|
throw new \DomainException( __METHOD__ . ": Invalid action \"$action\"" );
|
|
|
|
|
}
|
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
|
|
|
|
|
return $this->getAuthenticationRequestsInternal( $providerAction, $options, $providers, $user );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal request lookup for self::getAuthenticationRequests
|
|
|
|
|
*
|
|
|
|
|
* @param string $providerAction Action to pass to providers
|
|
|
|
|
* @param array $options Options to pass to providers
|
|
|
|
|
* @param AuthenticationProvider[] $providers
|
2021-04-21 16:25:17 +00:00
|
|
|
* @param UserIdentity|null $user being acted on
|
2015-11-22 20:17:00 +00:00
|
|
|
* @return AuthenticationRequest[]
|
|
|
|
|
*/
|
|
|
|
|
private function getAuthenticationRequestsInternal(
|
2021-04-21 16:25:17 +00:00
|
|
|
$providerAction, array $options, array $providers, UserIdentity $user = null
|
2015-11-22 20:17:00 +00:00
|
|
|
) {
|
|
|
|
|
$user = $user ?: \RequestContext::getMain()->getUser();
|
2021-04-21 16:25:17 +00:00
|
|
|
$options['username'] = $user->isRegistered() ? $user->getName() : null;
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// Query them and merge results
|
|
|
|
|
$reqs = [];
|
|
|
|
|
foreach ( $providers as $provider ) {
|
|
|
|
|
$isPrimary = $provider instanceof PrimaryAuthenticationProvider;
|
|
|
|
|
foreach ( $provider->getAuthenticationRequests( $providerAction, $options ) as $req ) {
|
|
|
|
|
$id = $req->getUniqueId();
|
|
|
|
|
|
2016-08-04 20:47:57 +00:00
|
|
|
// If a required request if from a Primary, mark it as "primary-required" instead
|
2019-03-29 20:12:24 +00:00
|
|
|
if ( $isPrimary && $req->required ) {
|
|
|
|
|
$req->required = AuthenticationRequest::PRIMARY_REQUIRED;
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
2016-08-04 20:47:57 +00:00
|
|
|
if (
|
|
|
|
|
!isset( $reqs[$id] )
|
|
|
|
|
|| $req->required === AuthenticationRequest::REQUIRED
|
|
|
|
|
|| $reqs[$id] === AuthenticationRequest::OPTIONAL
|
|
|
|
|
) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$reqs[$id] = $req;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AuthManager has its own req for some actions
|
|
|
|
|
switch ( $providerAction ) {
|
|
|
|
|
case self::ACTION_LOGIN:
|
2022-04-25 15:19:41 +00:00
|
|
|
$reqs[] = new RememberMeAuthenticationRequest(
|
|
|
|
|
$this->config->get( MainConfigNames::RememberMe ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::ACTION_CREATE:
|
|
|
|
|
$reqs[] = new UsernameAuthenticationRequest;
|
|
|
|
|
$reqs[] = new UserDataAuthenticationRequest;
|
|
|
|
|
if ( $options['username'] !== null ) {
|
|
|
|
|
$reqs[] = new CreationReasonAuthenticationRequest;
|
|
|
|
|
$options['username'] = null; // Don't fill in the username below
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fill in reqs data
|
2016-06-03 15:33:41 +00:00
|
|
|
$this->fillRequests( $reqs, $providerAction, $options['username'], true );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// For self::ACTION_CHANGE, filter out any that something else *doesn't* allow changing
|
|
|
|
|
if ( $providerAction === self::ACTION_CHANGE || $providerAction === self::ACTION_REMOVE ) {
|
|
|
|
|
$reqs = array_filter( $reqs, function ( $req ) {
|
|
|
|
|
return $this->allowsAuthenticationDataChange( $req, false )->isGood();
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array_values( $reqs );
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 15:58:44 +00:00
|
|
|
/**
|
|
|
|
|
* Set values in an array of requests
|
|
|
|
|
* @param AuthenticationRequest[] &$reqs
|
|
|
|
|
* @param string $action
|
|
|
|
|
* @param string|null $username
|
2017-08-20 11:20:59 +00:00
|
|
|
* @param bool $forceAction
|
2016-06-01 15:58:44 +00:00
|
|
|
*/
|
2016-06-03 15:33:41 +00:00
|
|
|
private function fillRequests( array &$reqs, $action, $username, $forceAction = false ) {
|
2016-06-01 15:58:44 +00:00
|
|
|
foreach ( $reqs as $req ) {
|
2016-06-03 15:33:41 +00:00
|
|
|
if ( !$req->action || $forceAction ) {
|
|
|
|
|
$req->action = $action;
|
|
|
|
|
}
|
2016-06-01 15:58:44 +00:00
|
|
|
if ( $req->username === null ) {
|
|
|
|
|
$req->username = $username;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
/**
|
|
|
|
|
* Determine whether a username exists
|
|
|
|
|
* @param string $username
|
|
|
|
|
* @param int $flags Bitfield of User:READ_* constants
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function userExists( $username, $flags = User::READ_NORMAL ) {
|
|
|
|
|
foreach ( $this->getPrimaryAuthenticationProviders() as $provider ) {
|
|
|
|
|
if ( $provider->testUserExists( $username, $flags ) ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determine whether a user property should be allowed to be changed.
|
|
|
|
|
*
|
|
|
|
|
* Supported properties are:
|
|
|
|
|
* - emailaddress
|
|
|
|
|
* - realname
|
|
|
|
|
* - nickname
|
|
|
|
|
*
|
|
|
|
|
* @param string $property
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function allowsPropertyChange( $property ) {
|
|
|
|
|
$providers = $this->getPrimaryAuthenticationProviders() +
|
|
|
|
|
$this->getSecondaryAuthenticationProviders();
|
|
|
|
|
foreach ( $providers as $provider ) {
|
|
|
|
|
if ( !$provider->providerAllowsPropertyChange( $property ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 18:16:49 +00:00
|
|
|
/**
|
|
|
|
|
* Get a provider by ID
|
|
|
|
|
* @note This is public so extensions can check whether their own provider
|
|
|
|
|
* is installed and so they can read its configuration if necessary.
|
|
|
|
|
* Other uses are not recommended.
|
|
|
|
|
* @param string $id
|
|
|
|
|
* @return AuthenticationProvider|null
|
|
|
|
|
*/
|
|
|
|
|
public function getAuthenticationProvider( $id ) {
|
|
|
|
|
// Fast version
|
|
|
|
|
if ( isset( $this->allAuthenticationProviders[$id] ) ) {
|
|
|
|
|
return $this->allAuthenticationProviders[$id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Slow version: instantiate each kind and check
|
|
|
|
|
$providers = $this->getPrimaryAuthenticationProviders();
|
|
|
|
|
if ( isset( $providers[$id] ) ) {
|
|
|
|
|
return $providers[$id];
|
|
|
|
|
}
|
|
|
|
|
$providers = $this->getSecondaryAuthenticationProviders();
|
|
|
|
|
if ( isset( $providers[$id] ) ) {
|
|
|
|
|
return $providers[$id];
|
|
|
|
|
}
|
|
|
|
|
$providers = $this->getPreAuthenticationProviders();
|
|
|
|
|
if ( isset( $providers[$id] ) ) {
|
|
|
|
|
return $providers[$id];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
// endregion -- end of Information methods
|
2015-11-22 20:17:00 +00:00
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
/***************************************************************************/
|
|
|
|
|
// region Internal methods
|
|
|
|
|
/** @name Internal methods */
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store authentication in the current session
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by AuthenticationProviders only
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @param mixed $data Must be serializable
|
|
|
|
|
*/
|
|
|
|
|
public function setAuthenticationSessionData( $key, $data ) {
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
$arr = $session->getSecret( 'authData' );
|
|
|
|
|
if ( !is_array( $arr ) ) {
|
|
|
|
|
$arr = [];
|
|
|
|
|
}
|
|
|
|
|
$arr[$key] = $data;
|
|
|
|
|
$session->setSecret( 'authData', $arr );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch authentication data from the current session
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by AuthenticationProviders only
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param string $key
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param mixed|null $default
|
2015-11-22 20:17:00 +00:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getAuthenticationSessionData( $key, $default = null ) {
|
|
|
|
|
$arr = $this->request->getSession()->getSecret( 'authData' );
|
|
|
|
|
if ( is_array( $arr ) && array_key_exists( $key, $arr ) ) {
|
|
|
|
|
return $arr[$key];
|
|
|
|
|
} else {
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove authentication data
|
2020-06-26 12:31:16 +00:00
|
|
|
* @note For use by AuthenticationProviders
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param string|null $key If null, all data is removed
|
|
|
|
|
*/
|
|
|
|
|
public function removeAuthenticationSessionData( $key ) {
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
if ( $key === null ) {
|
|
|
|
|
$session->remove( 'authData' );
|
|
|
|
|
} else {
|
|
|
|
|
$arr = $session->getSecret( 'authData' );
|
|
|
|
|
if ( is_array( $arr ) && array_key_exists( $key, $arr ) ) {
|
|
|
|
|
unset( $arr[$key] );
|
|
|
|
|
$session->setSecret( 'authData', $arr );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create an array of AuthenticationProviders from an array of ObjectFactory specs
|
|
|
|
|
* @param string $class
|
|
|
|
|
* @param array[] $specs
|
|
|
|
|
* @return AuthenticationProvider[]
|
|
|
|
|
*/
|
|
|
|
|
protected function providerArrayFromSpecs( $class, array $specs ) {
|
|
|
|
|
$i = 0;
|
|
|
|
|
foreach ( $specs as &$spec ) {
|
|
|
|
|
$spec = [ 'sort2' => $i++ ] + $spec + [ 'sort' => 0 ];
|
|
|
|
|
}
|
|
|
|
|
unset( $spec );
|
Use PHP 7 '<=>' operator in 'sort()' callbacks
`$a <=> $b` returns `-1` if `$a` is lesser, `1` if `$b` is lesser,
and `0` if they are equal, which are exactly the values 'sort()'
callbacks are supposed to return.
It also enables the neat idiom `$a[x] <=> $b[x] ?: $a[y] <=> $b[y]`
to sort arrays of objects first by 'x', and by 'y' if they are equal.
* Replace a common pattern like `return $a < $b ? -1 : 1` with the
new operator (and similar patterns with the variables, the numbers
or the comparison inverted). Some of the uses were previously not
correctly handling the variables being equal; this is now
automatically fixed.
* Also replace `return $a - $b`, which is equivalent to `return
$a <=> $b` if both variables are integers but less intuitive.
* (Do not replace `return strcmp( $a, $b )`. It is also equivalent
when both variables are strings, but if any of the variables is not,
'strcmp()' converts it to a string before comparison, which could
give different results than '<=>', so changing this would require
careful review and isn't worth it.)
* Also replace `return $a > $b`, which presumably sort of works most
of the time (returns `1` if `$b` is lesser, and `0` if they are
equal or `$a` is lesser) but is erroneous.
Change-Id: I19a3d2fc8fcdb208c10330bd7a42c4e05d7f5cf3
2017-10-06 20:39:13 +00:00
|
|
|
// Sort according to the 'sort' field, and if they are equal, according to 'sort2'
|
2021-02-10 22:31:02 +00:00
|
|
|
usort( $specs, static function ( $a, $b ) {
|
Use PHP 7 '<=>' operator in 'sort()' callbacks
`$a <=> $b` returns `-1` if `$a` is lesser, `1` if `$b` is lesser,
and `0` if they are equal, which are exactly the values 'sort()'
callbacks are supposed to return.
It also enables the neat idiom `$a[x] <=> $b[x] ?: $a[y] <=> $b[y]`
to sort arrays of objects first by 'x', and by 'y' if they are equal.
* Replace a common pattern like `return $a < $b ? -1 : 1` with the
new operator (and similar patterns with the variables, the numbers
or the comparison inverted). Some of the uses were previously not
correctly handling the variables being equal; this is now
automatically fixed.
* Also replace `return $a - $b`, which is equivalent to `return
$a <=> $b` if both variables are integers but less intuitive.
* (Do not replace `return strcmp( $a, $b )`. It is also equivalent
when both variables are strings, but if any of the variables is not,
'strcmp()' converts it to a string before comparison, which could
give different results than '<=>', so changing this would require
careful review and isn't worth it.)
* Also replace `return $a > $b`, which presumably sort of works most
of the time (returns `1` if `$b` is lesser, and `0` if they are
equal or `$a` is lesser) but is erroneous.
Change-Id: I19a3d2fc8fcdb208c10330bd7a42c4e05d7f5cf3
2017-10-06 20:39:13 +00:00
|
|
|
return $a['sort'] <=> $b['sort']
|
|
|
|
|
?: $a['sort2'] <=> $b['sort2'];
|
2015-11-22 20:17:00 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
$ret = [];
|
|
|
|
|
foreach ( $specs as $spec ) {
|
2021-04-16 13:17:10 +00:00
|
|
|
/** @var AbstractAuthenticationProvider $provider */
|
2019-11-08 21:24:00 +00:00
|
|
|
$provider = $this->objectFactory->createObject( $spec, [ 'assertClass' => $class ] );
|
2021-04-16 13:17:10 +00:00
|
|
|
$provider->init( $this->logger, $this, $this->getHookContainer(), $this->config, $this->userNameUtils );
|
2015-11-22 20:17:00 +00:00
|
|
|
$id = $provider->getUniqueId();
|
|
|
|
|
if ( isset( $this->allAuthenticationProviders[$id] ) ) {
|
|
|
|
|
throw new \RuntimeException(
|
|
|
|
|
"Duplicate specifications for id $id (classes " .
|
|
|
|
|
get_class( $provider ) . ' and ' .
|
|
|
|
|
get_class( $this->allAuthenticationProviders[$id] ) . ')'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
$this->allAuthenticationProviders[$id] = $provider;
|
|
|
|
|
$ret[$id] = $provider;
|
|
|
|
|
}
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
private function getConfiguration() {
|
2022-04-25 15:19:41 +00:00
|
|
|
return $this->config->get( MainConfigNames::AuthManagerConfig )
|
|
|
|
|
?: $this->config->get( MainConfigNames::AuthManagerAutoConfig );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the list of PreAuthenticationProviders
|
|
|
|
|
* @return PreAuthenticationProvider[]
|
|
|
|
|
*/
|
|
|
|
|
protected function getPreAuthenticationProviders() {
|
|
|
|
|
if ( $this->preAuthenticationProviders === null ) {
|
|
|
|
|
$conf = $this->getConfiguration();
|
|
|
|
|
$this->preAuthenticationProviders = $this->providerArrayFromSpecs(
|
|
|
|
|
PreAuthenticationProvider::class, $conf['preauth']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $this->preAuthenticationProviders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the list of PrimaryAuthenticationProviders
|
|
|
|
|
* @return PrimaryAuthenticationProvider[]
|
|
|
|
|
*/
|
|
|
|
|
protected function getPrimaryAuthenticationProviders() {
|
|
|
|
|
if ( $this->primaryAuthenticationProviders === null ) {
|
|
|
|
|
$conf = $this->getConfiguration();
|
|
|
|
|
$this->primaryAuthenticationProviders = $this->providerArrayFromSpecs(
|
|
|
|
|
PrimaryAuthenticationProvider::class, $conf['primaryauth']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $this->primaryAuthenticationProviders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the list of SecondaryAuthenticationProviders
|
|
|
|
|
* @return SecondaryAuthenticationProvider[]
|
|
|
|
|
*/
|
|
|
|
|
protected function getSecondaryAuthenticationProviders() {
|
|
|
|
|
if ( $this->secondaryAuthenticationProviders === null ) {
|
|
|
|
|
$conf = $this->getConfiguration();
|
|
|
|
|
$this->secondaryAuthenticationProviders = $this->providerArrayFromSpecs(
|
|
|
|
|
SecondaryAuthenticationProvider::class, $conf['secondaryauth']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return $this->secondaryAuthenticationProviders;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-08-27 05:40:37 +00:00
|
|
|
* Log the user in
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param User $user
|
|
|
|
|
* @param bool|null $remember
|
|
|
|
|
*/
|
|
|
|
|
private function setSessionDataForUser( $user, $remember = null ) {
|
|
|
|
|
$session = $this->request->getSession();
|
|
|
|
|
$delay = $session->delaySave();
|
|
|
|
|
|
|
|
|
|
$session->resetId();
|
2016-05-31 19:20:05 +00:00
|
|
|
$session->resetAllTokens();
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( $session->canSetUser() ) {
|
|
|
|
|
$session->setUser( $user );
|
|
|
|
|
}
|
|
|
|
|
if ( $remember !== null ) {
|
|
|
|
|
$session->setRememberUser( $remember );
|
|
|
|
|
}
|
|
|
|
|
$session->set( 'AuthManager:lastAuthId', $user->getId() );
|
|
|
|
|
$session->set( 'AuthManager:lastAuthTimestamp', time() );
|
|
|
|
|
$session->persist();
|
|
|
|
|
|
2016-10-12 05:36:03 +00:00
|
|
|
\Wikimedia\ScopedCallback::consume( $delay );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
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->getHookRunner()->onUserLoggedIn( $user );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param bool $useContextLang Use 'uselang' to set the user's language
|
|
|
|
|
*/
|
|
|
|
|
private function setDefaultUserOptions( User $user, $useContextLang ) {
|
2016-06-02 19:26:14 +00:00
|
|
|
$user->setToken();
|
2015-11-22 20:17:00 +00:00
|
|
|
|
2021-07-22 11:38:45 +00:00
|
|
|
$lang = $useContextLang ? \RequestContext::getMain()->getLanguage() : $this->contentLanguage;
|
2021-08-05 21:09:06 +00:00
|
|
|
$this->userOptionsManager->setOption(
|
|
|
|
|
$user,
|
2021-08-04 00:51:12 +00:00
|
|
|
'language',
|
|
|
|
|
$this->languageConverterFactory->getLanguageConverter( $lang )->getPreferredVariant()
|
|
|
|
|
);
|
2015-11-22 20:17:00 +00:00
|
|
|
|
2021-08-04 00:51:12 +00:00
|
|
|
$contLangConverter = $this->languageConverterFactory->getLanguageConverter( $this->contentLanguage );
|
2020-01-23 18:39:23 +00:00
|
|
|
if ( $contLangConverter->hasVariants() ) {
|
2021-08-05 21:09:06 +00:00
|
|
|
$this->userOptionsManager->setOption(
|
|
|
|
|
$user,
|
|
|
|
|
'variant',
|
|
|
|
|
$contLangConverter->getPreferredVariant()
|
|
|
|
|
);
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $which Bitmask: 1 = pre, 2 = primary, 4 = secondary
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @param array $args
|
|
|
|
|
*/
|
|
|
|
|
private function callMethodOnProviders( $which, $method, array $args ) {
|
|
|
|
|
$providers = [];
|
|
|
|
|
if ( $which & 1 ) {
|
|
|
|
|
$providers += $this->getPreAuthenticationProviders();
|
|
|
|
|
}
|
|
|
|
|
if ( $which & 2 ) {
|
|
|
|
|
$providers += $this->getPrimaryAuthenticationProviders();
|
|
|
|
|
}
|
|
|
|
|
if ( $which & 4 ) {
|
|
|
|
|
$providers += $this->getSecondaryAuthenticationProviders();
|
|
|
|
|
}
|
|
|
|
|
foreach ( $providers as $provider ) {
|
2018-06-05 06:24:34 +00:00
|
|
|
$provider->$method( ...$args );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 HookContainer
|
|
|
|
|
*/
|
|
|
|
|
private function getHookContainer() {
|
|
|
|
|
return $this->hookContainer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return HookRunner
|
|
|
|
|
*/
|
|
|
|
|
private function getHookRunner() {
|
|
|
|
|
return $this->hookRunner;
|
|
|
|
|
}
|
|
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
// endregion -- end of Internal methods
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
Improve custom folding and grouping
PHPStorm can use custom folding regions defined in either the
VisualStudio style or the NetBeans style. The VisualStudio style is more
pleasing to the eye and also works as a vim foldmarker. So get rid of
the previous vim foldmarkers, and use region/endregion.
region/endregion need to be in a single-line comment which is not a doc
comment, and the rest of the comment is used as a region heading (by
both PHPStorm and vim). So to retain Doxygen @name tags, it is
necessary to repeat the section heading, once in a @name and once in a
region. Establish a standard style for this, with a divider and three
spaces before the heading, to better set off the heading name in plain
text.
Besides being the previous vim foldmarker, @{ is also a Doxygen
grouping command. However, almost all prior usages of @{ ... @} in this
sense were broken for one reason or another. It's necessary for the @{
to be in a doc comment, and DISTRIBUTE_GROUP_DOC doesn't work if any of
the individual members in the group are separately documented.
@name alone is sufficient to create a Doxygen section when the sections
are adjacent, but if there is ungrouped content after the section, it
is necessary to use @{ ... @} to avoid having the Doxygen group run on.
So I retained, fixed or added @{ ... @} in certain cases.
I wasn't able to test the changes to the trait documentation in Doxygen
since trait syntax is not recognised and the output is badly broken.
Change-Id: I7d819fdb376c861f40bfc01aed74cd3706141b20
2020-12-22 23:52:00 +00:00
|
|
|
/*
|
|
|
|
|
* This file uses VisualStudio style region/endregion fold markers which are
|
|
|
|
|
* recognised by PHPStorm. If modelines are enabled, the following editor
|
|
|
|
|
* configuration will also enable folding in vim, if it is in the last 5 lines
|
|
|
|
|
* of the file. We also use "@name" which creates sections in Doxygen.
|
|
|
|
|
*
|
|
|
|
|
* vim: foldmarker=//\ region,//\ endregion foldmethod=marker
|
2015-11-22 20:17:00 +00:00
|
|
|
*/
|