2015-11-22 20:17:00 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Auth;
|
|
|
|
|
|
2016-11-22 23:39:22 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2020-02-20 09:45:13 +00:00
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2019-11-08 21:24:00 +00:00
|
|
|
use Psr\Container\ContainerInterface;
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2016-11-22 23:39:22 +00:00
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
/**
|
|
|
|
|
* @group AuthManager
|
|
|
|
|
* @group Database
|
2018-11-01 11:48:52 +00:00
|
|
|
* @covers \MediaWiki\Auth\LocalPasswordPrimaryAuthenticationProvider
|
2015-11-22 20:17:00 +00:00
|
|
|
*/
|
|
|
|
|
class LocalPasswordPrimaryAuthenticationProviderTest extends \MediaWikiTestCase {
|
|
|
|
|
|
|
|
|
|
private $manager = null;
|
|
|
|
|
private $config = null;
|
|
|
|
|
private $validity = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get an instance of the provider
|
|
|
|
|
*
|
|
|
|
|
* $provider->checkPasswordValidity is mocked to return $this->validity,
|
|
|
|
|
* because we don't need to test that here.
|
|
|
|
|
*
|
|
|
|
|
* @param bool $loginOnly
|
|
|
|
|
* @return LocalPasswordPrimaryAuthenticationProvider
|
|
|
|
|
*/
|
|
|
|
|
protected function getProvider( $loginOnly = false ) {
|
|
|
|
|
if ( !$this->config ) {
|
|
|
|
|
$this->config = new \HashConfig();
|
|
|
|
|
}
|
|
|
|
|
$config = new \MultiConfig( [
|
|
|
|
|
$this->config,
|
2016-11-22 23:39:22 +00:00
|
|
|
MediaWikiServices::getInstance()->getMainConfig()
|
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
|
|
|
// We need a real HookContainer since testProviderChangeAuthenticationData()
|
|
|
|
|
// modifies $wgHooks
|
|
|
|
|
$hookContainer = MediaWikiServices::getInstance()->getHookContainer();
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !$this->manager ) {
|
2019-11-08 21:24:00 +00:00
|
|
|
$services = $this->createNoOpAbstractMock( ContainerInterface::class );
|
|
|
|
|
$objectFactory = new \Wikimedia\ObjectFactory( $services );
|
2020-02-20 09:45:13 +00:00
|
|
|
$permManager = $this->createNoOpMock( PermissionManager::class );
|
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
|
|
|
|
2020-02-20 09:45:13 +00:00
|
|
|
$this->manager = new AuthManager(
|
|
|
|
|
new \FauxRequest(),
|
|
|
|
|
$config,
|
|
|
|
|
$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
|
|
|
$permManager,
|
|
|
|
|
$hookContainer
|
2020-02-20 09:45:13 +00:00
|
|
|
);
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
$this->validity = \Status::newGood();
|
2017-04-05 23:39:50 +00:00
|
|
|
$provider = $this->getMockBuilder( LocalPasswordPrimaryAuthenticationProvider::class )
|
|
|
|
|
->setMethods( [ 'checkPasswordValidity' ] )
|
|
|
|
|
->setConstructorArgs( [ [ 'loginOnly' => $loginOnly ] ] )
|
|
|
|
|
->getMock();
|
2019-02-20 06:02:33 +00:00
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider->expects( $this->any() )->method( 'checkPasswordValidity' )
|
|
|
|
|
->will( $this->returnCallback( function () {
|
|
|
|
|
return $this->validity;
|
|
|
|
|
} ) );
|
|
|
|
|
$provider->setConfig( $config );
|
|
|
|
|
$provider->setLogger( new \Psr\Log\NullLogger() );
|
|
|
|
|
$provider->setManager( $this->manager );
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$provider->setHookContainer( $hookContainer );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
return $provider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBasics() {
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
|
|
|
$userName = $user->getName();
|
|
|
|
|
$lowerInitialUserName = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider = new LocalPasswordPrimaryAuthenticationProvider();
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
PrimaryAuthenticationProvider::TYPE_CREATE,
|
|
|
|
|
$provider->accountCreationType()
|
|
|
|
|
);
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$this->assertTrue( $provider->testUserExists( $userName ) );
|
|
|
|
|
$this->assertTrue( $provider->testUserExists( $lowerInitialUserName ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertFalse( $provider->testUserExists( 'DoesNotExist' ) );
|
|
|
|
|
$this->assertFalse( $provider->testUserExists( '<invalid>' ) );
|
|
|
|
|
|
|
|
|
|
$provider = new LocalPasswordPrimaryAuthenticationProvider( [ 'loginOnly' => true ] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
PrimaryAuthenticationProvider::TYPE_NONE,
|
|
|
|
|
$provider->accountCreationType()
|
|
|
|
|
);
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$this->assertTrue( $provider->testUserExists( $userName ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertFalse( $provider->testUserExists( 'DoesNotExist' ) );
|
|
|
|
|
|
|
|
|
|
$req = new PasswordAuthenticationRequest;
|
|
|
|
|
$req->action = AuthManager::ACTION_CHANGE;
|
|
|
|
|
$req->username = '<invalid>';
|
|
|
|
|
$provider->providerChangeAuthenticationData( $req );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTestUserCanAuthenticate() {
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
|
|
|
$userName = $user->getName();
|
2015-11-22 20:17:00 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider();
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $provider->testUserCanAuthenticate( '<invalid>' ) );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $provider->testUserCanAuthenticate( 'DoesNotExist' ) );
|
|
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$this->assertTrue( $provider->testUserCanAuthenticate( $userName ) );
|
|
|
|
|
$lowerInitialUserName = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
|
|
|
|
|
$this->assertTrue( $provider->testUserCanAuthenticate( $lowerInitialUserName ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[ 'user_password' => \PasswordFactory::newInvalidPassword()->toString() ],
|
2016-05-18 09:19:20 +00:00
|
|
|
[ 'user_name' => $userName ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
2016-05-18 09:19:20 +00:00
|
|
|
$this->assertFalse( $provider->testUserCanAuthenticate( $userName ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// Really old format
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[ 'user_password' => '0123456789abcdef0123456789abcdef' ],
|
2016-05-18 09:19:20 +00:00
|
|
|
[ 'user_name' => $userName ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
2016-05-18 09:19:20 +00:00
|
|
|
$this->assertTrue( $provider->testUserCanAuthenticate( $userName ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSetPasswordResetFlag() {
|
|
|
|
|
// Set instance vars
|
|
|
|
|
$this->getProvider();
|
|
|
|
|
|
|
|
|
|
/// @todo: Because we're currently using User, which uses the global config...
|
|
|
|
|
$this->setMwGlobals( [ 'wgPasswordExpireGrace' => 100 ] );
|
|
|
|
|
|
|
|
|
|
$this->config->set( 'PasswordExpireGrace', 100 );
|
|
|
|
|
$this->config->set( 'InvalidPasswordReset', true );
|
|
|
|
|
|
|
|
|
|
$provider = new LocalPasswordPrimaryAuthenticationProvider();
|
|
|
|
|
$provider->setConfig( $this->config );
|
|
|
|
|
$provider->setLogger( new \Psr\Log\NullLogger() );
|
|
|
|
|
$provider->setManager( $this->manager );
|
2017-04-19 19:37:35 +00:00
|
|
|
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
2016-05-18 09:19:20 +00:00
|
|
|
$user = $this->getMutableTestUser()->getUser();
|
|
|
|
|
$userName = $user->getName();
|
2015-11-22 20:17:00 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$row = $dbw->selectRow(
|
|
|
|
|
'user',
|
|
|
|
|
'*',
|
2016-05-18 09:19:20 +00:00
|
|
|
[ 'user_name' => $userName ],
|
2015-11-22 20:17:00 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
|
|
|
|
$row->user_password_expires = wfTimestamp( TS_MW, time() + 200 );
|
2016-05-18 09:19:20 +00:00
|
|
|
$providerPriv->setPasswordResetFlag( $userName, \Status::newGood(), $row );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
|
|
|
|
|
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
|
|
|
|
$row->user_password_expires = wfTimestamp( TS_MW, time() - 200 );
|
2016-05-18 09:19:20 +00:00
|
|
|
$providerPriv->setPasswordResetFlag( $userName, \Status::newGood(), $row );
|
2015-11-22 20:17:00 +00:00
|
|
|
$ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
|
|
|
|
|
$this->assertNotNull( $ret );
|
|
|
|
|
$this->assertSame( 'resetpass-expired', $ret->msg->getKey() );
|
|
|
|
|
$this->assertTrue( $ret->hard );
|
|
|
|
|
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
|
|
|
|
$row->user_password_expires = wfTimestamp( TS_MW, time() - 1 );
|
2016-05-18 09:19:20 +00:00
|
|
|
$providerPriv->setPasswordResetFlag( $userName, \Status::newGood(), $row );
|
2015-11-22 20:17:00 +00:00
|
|
|
$ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
|
|
|
|
|
$this->assertNotNull( $ret );
|
|
|
|
|
$this->assertSame( 'resetpass-expired-soft', $ret->msg->getKey() );
|
|
|
|
|
$this->assertFalse( $ret->hard );
|
|
|
|
|
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
|
|
|
|
$row->user_password_expires = null;
|
2019-02-20 06:02:33 +00:00
|
|
|
$status = \Status::newGood( [ 'suggestChangeOnLogin' => true ] );
|
2015-11-22 20:17:00 +00:00
|
|
|
$status->error( 'testing' );
|
2016-05-18 09:19:20 +00:00
|
|
|
$providerPriv->setPasswordResetFlag( $userName, $status, $row );
|
2015-11-22 20:17:00 +00:00
|
|
|
$ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
|
|
|
|
|
$this->assertNotNull( $ret );
|
|
|
|
|
$this->assertSame( 'resetpass-validity-soft', $ret->msg->getKey() );
|
|
|
|
|
$this->assertFalse( $ret->hard );
|
2019-01-02 06:17:30 +00:00
|
|
|
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
|
|
|
|
$row->user_password_expires = null;
|
|
|
|
|
$status = \Status::newGood( [ 'forceChange' => true ] );
|
|
|
|
|
$status->error( 'testing' );
|
|
|
|
|
$providerPriv->setPasswordResetFlag( $userName, $status, $row );
|
|
|
|
|
$ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
|
|
|
|
|
$this->assertNotNull( $ret );
|
|
|
|
|
$this->assertSame( 'resetpass-validity', $ret->msg->getKey() );
|
|
|
|
|
$this->assertTrue( $ret->hard );
|
2019-02-20 06:02:33 +00:00
|
|
|
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
|
|
|
|
$row->user_password_expires = null;
|
|
|
|
|
$status = \Status::newGood( [ 'suggestChangeOnLogin' => false, ] );
|
|
|
|
|
$status->error( 'testing' );
|
|
|
|
|
$providerPriv->setPasswordResetFlag( $userName, $status, $row );
|
|
|
|
|
$ret = $this->manager->getAuthenticationSessionData( 'reset-pass' );
|
|
|
|
|
$this->assertNull( $ret );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAuthentication() {
|
2016-05-18 09:19:20 +00:00
|
|
|
$testUser = $this->getMutableTestUser();
|
|
|
|
|
$userName = $testUser->getUser()->getName();
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2016-05-18 09:19:20 +00:00
|
|
|
$id = \User::idFromName( $userName );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$req = new PasswordAuthenticationRequest();
|
|
|
|
|
$req->action = AuthManager::ACTION_LOGIN;
|
|
|
|
|
$reqs = [ PasswordAuthenticationRequest::class => $req ];
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider();
|
|
|
|
|
|
|
|
|
|
// General failures
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginPrimaryAuthentication( [] )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req->username = 'foo';
|
|
|
|
|
$req->password = null;
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req->username = null;
|
|
|
|
|
$req->password = 'bar';
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req->username = '<invalid>';
|
|
|
|
|
$req->password = 'WhoCares';
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req->username = 'DoesNotExist';
|
|
|
|
|
$req->password = 'DoesNotExist';
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals(
|
2017-11-15 01:58:49 +00:00
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'wrongpassword',
|
|
|
|
|
$ret->message->getKey()
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Validation failure
|
2016-05-18 09:19:20 +00:00
|
|
|
$req->username = $userName;
|
|
|
|
|
$req->password = $testUser->getPassword();
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->validity = \Status::newFatal( 'arbitrary-failure' );
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'arbitrary-failure',
|
|
|
|
|
$ret->message->getKey()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Successful auth
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
|
|
|
|
$this->validity = \Status::newGood();
|
|
|
|
|
$this->assertEquals(
|
2016-05-18 09:19:20 +00:00
|
|
|
AuthenticationResponse::newPass( $userName ),
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
$this->assertNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
|
|
|
|
|
|
|
|
|
|
// Successful auth after normalizing name
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
|
|
|
|
$this->validity = \Status::newGood();
|
2016-05-18 09:19:20 +00:00
|
|
|
$req->username = mb_strtolower( $userName[0] ) . substr( $userName, 1 );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertEquals(
|
2016-05-18 09:19:20 +00:00
|
|
|
AuthenticationResponse::newPass( $userName ),
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
$this->assertNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
|
2016-05-18 09:19:20 +00:00
|
|
|
$req->username = $userName;
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// Successful auth with reset
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
2019-02-20 06:02:33 +00:00
|
|
|
$this->validity = \Status::newGood( [ 'suggestChangeOnLogin' => true ] );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->validity->error( 'arbitrary-warning' );
|
|
|
|
|
$this->assertEquals(
|
2016-05-18 09:19:20 +00:00
|
|
|
AuthenticationResponse::newPass( $userName ),
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
$this->assertNotNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
|
|
|
|
|
|
|
|
|
|
// Wrong password
|
|
|
|
|
$this->validity = \Status::newGood();
|
|
|
|
|
$req->password = 'Wrong';
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'wrongpassword',
|
|
|
|
|
$ret->message->getKey()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Correct handling of legacy encodings
|
|
|
|
|
$password = ':B:salt:' . md5( 'salt-' . md5( "\xe1\xe9\xed\xf3\xfa" ) );
|
2016-05-18 09:19:20 +00:00
|
|
|
$dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => $userName ] );
|
2015-11-22 20:17:00 +00:00
|
|
|
$req->password = 'áéíóú';
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'wrongpassword',
|
|
|
|
|
$ret->message->getKey()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->config->set( 'LegacyEncoding', true );
|
|
|
|
|
$this->assertEquals(
|
2016-05-18 09:19:20 +00:00
|
|
|
AuthenticationResponse::newPass( $userName ),
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req->password = 'áéíóú Wrong';
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'wrongpassword',
|
|
|
|
|
$ret->message->getKey()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Correct handling of really old password hashes
|
|
|
|
|
$this->config->set( 'PasswordSalt', true );
|
|
|
|
|
$password = md5( "$id-" . md5( 'FooBar' ) );
|
2016-05-18 09:19:20 +00:00
|
|
|
$dbw->update( 'user', [ 'user_password' => $password ], [ 'user_name' => $userName ] );
|
2015-11-22 20:17:00 +00:00
|
|
|
$req->password = 'FooBar';
|
|
|
|
|
$this->assertEquals(
|
2016-05-18 09:19:20 +00:00
|
|
|
AuthenticationResponse::newPass( $userName ),
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideProviderAllowsAuthenticationDataChange
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @param string $user
|
|
|
|
|
* @param \Status $validity Result of the password validity check
|
|
|
|
|
* @param \StatusValue $expect1 Expected result with $checkData = false
|
|
|
|
|
* @param \StatusValue $expect2 Expected result with $checkData = true
|
|
|
|
|
*/
|
|
|
|
|
public function testProviderAllowsAuthenticationDataChange( $type, $user, \Status $validity,
|
|
|
|
|
\StatusValue $expect1, \StatusValue $expect2
|
|
|
|
|
) {
|
|
|
|
|
if ( $type === PasswordAuthenticationRequest::class ) {
|
|
|
|
|
$req = new $type();
|
|
|
|
|
} elseif ( $type === PasswordDomainAuthenticationRequest::class ) {
|
|
|
|
|
$req = new $type( [] );
|
|
|
|
|
} else {
|
2017-04-05 23:39:50 +00:00
|
|
|
$req = $this->createMock( $type );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
$req->action = AuthManager::ACTION_CHANGE;
|
|
|
|
|
$req->username = $user;
|
|
|
|
|
$req->password = 'NewPassword';
|
|
|
|
|
$req->retype = 'NewPassword';
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider();
|
|
|
|
|
$this->validity = $validity;
|
|
|
|
|
$this->assertEquals( $expect1, $provider->providerAllowsAuthenticationDataChange( $req, false ) );
|
|
|
|
|
$this->assertEquals( $expect2, $provider->providerAllowsAuthenticationDataChange( $req, true ) );
|
|
|
|
|
|
|
|
|
|
$req->retype = 'BadRetype';
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expect1,
|
|
|
|
|
$provider->providerAllowsAuthenticationDataChange( $req, false )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expect2->getValue() === 'ignored' ? $expect2 : \StatusValue::newFatal( 'badretype' ),
|
|
|
|
|
$provider->providerAllowsAuthenticationDataChange( $req, true )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider( true );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
\StatusValue::newGood( 'ignored' ),
|
|
|
|
|
$provider->providerAllowsAuthenticationDataChange( $req, true ),
|
|
|
|
|
'loginOnly mode should claim to ignore all changes'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideProviderAllowsAuthenticationDataChange() {
|
|
|
|
|
$err = \StatusValue::newGood();
|
|
|
|
|
$err->error( 'arbitrary-warning' );
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
[ AuthenticationRequest::class, 'UTSysop', \Status::newGood(),
|
|
|
|
|
\StatusValue::newGood( 'ignored' ), \StatusValue::newGood( 'ignored' ) ],
|
|
|
|
|
[ PasswordAuthenticationRequest::class, 'UTSysop', \Status::newGood(),
|
|
|
|
|
\StatusValue::newGood(), \StatusValue::newGood() ],
|
|
|
|
|
[ PasswordAuthenticationRequest::class, 'uTSysop', \Status::newGood(),
|
|
|
|
|
\StatusValue::newGood(), \StatusValue::newGood() ],
|
|
|
|
|
[ PasswordAuthenticationRequest::class, 'UTSysop', \Status::wrap( $err ),
|
|
|
|
|
\StatusValue::newGood(), $err ],
|
|
|
|
|
[ PasswordAuthenticationRequest::class, 'UTSysop', \Status::newFatal( 'arbitrary-error' ),
|
|
|
|
|
\StatusValue::newGood(), \StatusValue::newFatal( 'arbitrary-error' ) ],
|
|
|
|
|
[ PasswordAuthenticationRequest::class, 'DoesNotExist', \Status::newGood(),
|
|
|
|
|
\StatusValue::newGood(), \StatusValue::newGood( 'ignored' ) ],
|
|
|
|
|
[ PasswordDomainAuthenticationRequest::class, 'UTSysop', \Status::newGood(),
|
|
|
|
|
\StatusValue::newGood( 'ignored' ), \StatusValue::newGood( 'ignored' ) ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideProviderChangeAuthenticationData
|
2016-05-18 09:19:20 +00:00
|
|
|
* @param callable|bool $usernameTransform
|
2015-11-22 20:17:00 +00:00
|
|
|
* @param string $type
|
|
|
|
|
* @param bool $loginOnly
|
|
|
|
|
* @param bool $changed
|
|
|
|
|
*/
|
2016-05-18 09:19:20 +00:00
|
|
|
public function testProviderChangeAuthenticationData(
|
|
|
|
|
$usernameTransform, $type, $loginOnly, $changed ) {
|
|
|
|
|
$testUser = $this->getMutableTestUser();
|
|
|
|
|
$user = $testUser->getUser()->getName();
|
|
|
|
|
if ( is_callable( $usernameTransform ) ) {
|
|
|
|
|
$user = call_user_func( $usernameTransform, $user );
|
|
|
|
|
}
|
2015-11-22 20:17:00 +00:00
|
|
|
$cuser = ucfirst( $user );
|
2016-05-18 09:19:20 +00:00
|
|
|
$oldpass = $testUser->getPassword();
|
2015-11-22 20:17:00 +00:00
|
|
|
$newpass = 'NewPassword';
|
|
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$oldExpiry = $dbw->selectField( 'user', 'user_password_expires', [ 'user_name' => $cuser ] );
|
|
|
|
|
|
|
|
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [
|
|
|
|
|
'ResetPasswordExpiration' => [ function ( $user, &$expires ) {
|
|
|
|
|
$expires = '30001231235959';
|
|
|
|
|
} ]
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider( $loginOnly );
|
|
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
|
$loginReq = new PasswordAuthenticationRequest();
|
|
|
|
|
$loginReq->action = AuthManager::ACTION_LOGIN;
|
|
|
|
|
$loginReq->username = $user;
|
|
|
|
|
$loginReq->password = $oldpass;
|
|
|
|
|
$loginReqs = [ PasswordAuthenticationRequest::class => $loginReq ];
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newPass( $cuser ),
|
|
|
|
|
$provider->beginPrimaryAuthentication( $loginReqs ),
|
|
|
|
|
'Sanity check'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $type === PasswordAuthenticationRequest::class ) {
|
|
|
|
|
$changeReq = new $type();
|
|
|
|
|
} else {
|
2017-04-05 23:39:50 +00:00
|
|
|
$changeReq = $this->createMock( $type );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
$changeReq->action = AuthManager::ACTION_CHANGE;
|
|
|
|
|
$changeReq->username = $user;
|
|
|
|
|
$changeReq->password = $newpass;
|
|
|
|
|
$provider->providerChangeAuthenticationData( $changeReq );
|
|
|
|
|
|
2016-11-15 04:04:24 +00:00
|
|
|
if ( $loginOnly && $changed ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$old = 'fail';
|
|
|
|
|
$new = 'fail';
|
|
|
|
|
$expectExpiry = null;
|
|
|
|
|
} elseif ( $changed ) {
|
|
|
|
|
$old = 'fail';
|
|
|
|
|
$new = 'pass';
|
|
|
|
|
$expectExpiry = '30001231235959';
|
|
|
|
|
} else {
|
|
|
|
|
$old = 'pass';
|
|
|
|
|
$new = 'fail';
|
|
|
|
|
$expectExpiry = $oldExpiry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$loginReq->password = $oldpass;
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $loginReqs );
|
|
|
|
|
if ( $old === 'pass' ) {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newPass( $cuser ),
|
|
|
|
|
$ret,
|
|
|
|
|
'old password should pass'
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status,
|
|
|
|
|
'old password should fail'
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'wrongpassword',
|
|
|
|
|
$ret->message->getKey(),
|
|
|
|
|
'old password should fail'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$loginReq->password = $newpass;
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $loginReqs );
|
|
|
|
|
if ( $new === 'pass' ) {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newPass( $cuser ),
|
|
|
|
|
$ret,
|
|
|
|
|
'new password should pass'
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status,
|
|
|
|
|
'new password should fail'
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'wrongpassword',
|
|
|
|
|
$ret->message->getKey(),
|
|
|
|
|
'new password should fail'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
$expectExpiry,
|
2017-05-10 02:32:24 +00:00
|
|
|
wfTimestampOrNull(
|
|
|
|
|
TS_MW,
|
|
|
|
|
$dbw->selectField( 'user', 'user_password_expires', [ 'user_name' => $cuser ] )
|
|
|
|
|
)
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideProviderChangeAuthenticationData() {
|
|
|
|
|
return [
|
2016-05-18 09:19:20 +00:00
|
|
|
[ false, AuthenticationRequest::class, false, false ],
|
|
|
|
|
[ false, PasswordAuthenticationRequest::class, false, true ],
|
|
|
|
|
[ false, AuthenticationRequest::class, true, false ],
|
|
|
|
|
[ false, PasswordAuthenticationRequest::class, true, true ],
|
|
|
|
|
[ 'ucfirst', PasswordAuthenticationRequest::class, false, true ],
|
|
|
|
|
[ 'ucfirst', PasswordAuthenticationRequest::class, true, true ],
|
2015-11-22 20:17:00 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTestForAccountCreation() {
|
|
|
|
|
$user = \User::newFromName( 'foo' );
|
|
|
|
|
$req = new PasswordAuthenticationRequest();
|
|
|
|
|
$req->action = AuthManager::ACTION_CREATE;
|
|
|
|
|
$req->username = 'Foo';
|
|
|
|
|
$req->password = 'Bar';
|
|
|
|
|
$req->retype = 'Bar';
|
|
|
|
|
$reqs = [ PasswordAuthenticationRequest::class => $req ];
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider();
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
\StatusValue::newGood(),
|
|
|
|
|
$provider->testForAccountCreation( $user, $user, [] ),
|
|
|
|
|
'No password request'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
\StatusValue::newGood(),
|
|
|
|
|
$provider->testForAccountCreation( $user, $user, $reqs ),
|
|
|
|
|
'Password request, validated'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req->retype = 'Baz';
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
\StatusValue::newFatal( 'badretype' ),
|
|
|
|
|
$provider->testForAccountCreation( $user, $user, $reqs ),
|
|
|
|
|
'Password request, bad retype'
|
|
|
|
|
);
|
|
|
|
|
$req->retype = 'Bar';
|
|
|
|
|
|
|
|
|
|
$this->validity->error( 'arbitrary warning' );
|
|
|
|
|
$expect = \StatusValue::newGood();
|
|
|
|
|
$expect->error( 'arbitrary warning' );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expect,
|
|
|
|
|
$provider->testForAccountCreation( $user, $user, $reqs ),
|
|
|
|
|
'Password request, not validated'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider( true );
|
|
|
|
|
$this->validity->error( 'arbitrary warning' );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
\StatusValue::newGood(),
|
|
|
|
|
$provider->testForAccountCreation( $user, $user, $reqs ),
|
|
|
|
|
'Password request, not validated, loginOnly'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAccountCreation() {
|
|
|
|
|
$user = \User::newFromName( 'Foo' );
|
|
|
|
|
|
|
|
|
|
$req = new PasswordAuthenticationRequest();
|
|
|
|
|
$req->action = AuthManager::ACTION_CREATE;
|
|
|
|
|
$reqs = [ PasswordAuthenticationRequest::class => $req ];
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider( true );
|
|
|
|
|
try {
|
|
|
|
|
$provider->beginPrimaryAccountCreation( $user, $user, [] );
|
|
|
|
|
$this->fail( 'Expected exception was not thrown' );
|
|
|
|
|
} catch ( \BadMethodCallException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'Shouldn\'t call this when accountCreationType() is NONE', $ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$provider->finishAccountCreation( $user, $user, AuthenticationResponse::newPass() );
|
|
|
|
|
$this->fail( 'Expected exception was not thrown' );
|
|
|
|
|
} catch ( \BadMethodCallException $ex ) {
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
'Shouldn\'t call this when accountCreationType() is NONE', $ex->getMessage()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider( false );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginPrimaryAccountCreation( $user, $user, [] )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req->username = 'foo';
|
|
|
|
|
$req->password = null;
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginPrimaryAccountCreation( $user, $user, $reqs )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req->username = null;
|
|
|
|
|
$req->password = 'bar';
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginPrimaryAccountCreation( $user, $user, $reqs )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req->username = 'foo';
|
|
|
|
|
$req->password = 'bar';
|
|
|
|
|
|
|
|
|
|
$expect = AuthenticationResponse::newPass( 'Foo' );
|
2017-06-27 19:44:12 +00:00
|
|
|
$expect->createRequest = clone $req;
|
2015-11-22 20:17:00 +00:00
|
|
|
$expect->createRequest->username = 'Foo';
|
|
|
|
|
$this->assertEquals( $expect, $provider->beginPrimaryAccountCreation( $user, $user, $reqs ) );
|
|
|
|
|
|
|
|
|
|
// We have to cheat a bit to avoid having to add a new user to
|
|
|
|
|
// the database to test the actual setting of the password works right
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
|
|
|
|
|
$user = \User::newFromName( 'UTSysop' );
|
|
|
|
|
$req->username = $user->getName();
|
|
|
|
|
$req->password = 'NewPassword';
|
|
|
|
|
$expect = AuthenticationResponse::newPass( 'UTSysop' );
|
|
|
|
|
$expect->createRequest = $req;
|
|
|
|
|
|
|
|
|
|
$res2 = $provider->beginPrimaryAccountCreation( $user, $user, $reqs );
|
|
|
|
|
$this->assertEquals( $expect, $res2, 'Sanity check' );
|
|
|
|
|
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals( AuthenticationResponse::FAIL, $ret->status, 'sanity check' );
|
|
|
|
|
|
|
|
|
|
$this->assertNull( $provider->finishAccountCreation( $user, $user, $res2 ) );
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals( AuthenticationResponse::PASS, $ret->status, 'new password is set' );
|
|
|
|
|
}
|
|
|
|
|
}
|