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;
|
2020-04-08 18:19:49 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2019-11-08 21:24:00 +00:00
|
|
|
use Psr\Container\ContainerInterface;
|
2016-10-12 05:36:03 +00:00
|
|
|
use Wikimedia\ScopedCallback;
|
2017-04-19 19:37:35 +00:00
|
|
|
use Wikimedia\TestingAccessWrapper;
|
2016-10-12 05:36:03 +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\TemporaryPasswordPrimaryAuthenticationProvider
|
2015-11-22 20:17:00 +00:00
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class TemporaryPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegrationTestCase {
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
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 array $params
|
|
|
|
|
* @return TemporaryPasswordPrimaryAuthenticationProvider
|
|
|
|
|
*/
|
|
|
|
|
protected function getProvider( $params = [] ) {
|
2020-10-30 10:55:50 +00:00
|
|
|
$mwServices = MediaWikiServices::getInstance();
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( !$this->config ) {
|
|
|
|
|
$this->config = new \HashConfig( [
|
|
|
|
|
'EmailEnabled' => true,
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
$config = new \MultiConfig( [
|
|
|
|
|
$this->config,
|
2020-10-30 10:55:50 +00:00
|
|
|
$mwServices->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
|
|
|
$hookContainer = $this->createHookContainer();
|
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 );
|
2020-04-08 18:19:49 +00:00
|
|
|
$userNameUtils = $this->createNoOpMock( UserNameUtils::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,
|
2020-10-10 21:03:11 +00:00
|
|
|
$hookContainer,
|
2020-10-30 10:55:50 +00:00
|
|
|
$mwServices->getReadOnlyMode(),
|
|
|
|
|
$userNameUtils,
|
|
|
|
|
$mwServices->getBlockManager(),
|
|
|
|
|
$mwServices->getBlockErrorFormatter()
|
2020-02-20 09:45:13 +00:00
|
|
|
);
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
$this->validity = \Status::newGood();
|
|
|
|
|
|
|
|
|
|
$mockedMethods[] = 'checkPasswordValidity';
|
2017-04-05 23:39:50 +00:00
|
|
|
$provider = $this->getMockBuilder( TemporaryPasswordPrimaryAuthenticationProvider::class )
|
|
|
|
|
->setMethods( $mockedMethods )
|
|
|
|
|
->setConstructorArgs( [ $params ] )
|
|
|
|
|
->getMock();
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function hookMailer( $func = null ) {
|
2020-02-10 14:47:46 +00:00
|
|
|
$hookContainer = MediaWikiServices::getInstance()->getHookContainer();
|
2015-11-22 20:17:00 +00:00
|
|
|
if ( $func ) {
|
2020-04-15 19:30:38 +00:00
|
|
|
$reset = $hookContainer->scopedRegister( 'AlternateUserMailer', $func, true );
|
2015-11-22 20:17:00 +00:00
|
|
|
} else {
|
2020-02-10 14:47:46 +00:00
|
|
|
$reset = $hookContainer->scopedRegister( 'AlternateUserMailer', function () {
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->fail( 'AlternateUserMailer hook called unexpectedly' );
|
|
|
|
|
return false;
|
2020-04-15 19:30:38 +00:00
|
|
|
}, true );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
2020-02-10 14:47:46 +00:00
|
|
|
return $reset;
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBasics() {
|
|
|
|
|
$provider = new TemporaryPasswordPrimaryAuthenticationProvider();
|
|
|
|
|
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
PrimaryAuthenticationProvider::TYPE_CREATE,
|
|
|
|
|
$provider->accountCreationType()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $provider->testUserExists( 'UTSysop' ) );
|
|
|
|
|
$this->assertTrue( $provider->testUserExists( 'uTSysop' ) );
|
|
|
|
|
$this->assertFalse( $provider->testUserExists( 'DoesNotExist' ) );
|
|
|
|
|
$this->assertFalse( $provider->testUserExists( '<invalid>' ) );
|
|
|
|
|
|
|
|
|
|
$req = new PasswordAuthenticationRequest;
|
|
|
|
|
$req->action = AuthManager::ACTION_CHANGE;
|
|
|
|
|
$req->username = '<invalid>';
|
|
|
|
|
$provider->providerChangeAuthenticationData( $req );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConfig() {
|
|
|
|
|
$config = new \HashConfig( [
|
|
|
|
|
'EnableEmail' => false,
|
|
|
|
|
'NewPasswordExpiry' => 100,
|
|
|
|
|
'PasswordReminderResendTime' => 101,
|
2019-09-28 00:27:54 +00:00
|
|
|
'AllowRequiringEmailForResets' => false,
|
2015-11-22 20:17:00 +00:00
|
|
|
] );
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$p = TestingAccessWrapper::newFromObject( new TemporaryPasswordPrimaryAuthenticationProvider() );
|
2015-11-22 20:17:00 +00:00
|
|
|
$p->setConfig( $config );
|
|
|
|
|
$this->assertSame( false, $p->emailEnabled );
|
|
|
|
|
$this->assertSame( 100, $p->newPasswordExpiry );
|
|
|
|
|
$this->assertSame( 101, $p->passwordReminderResendTime );
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$p = TestingAccessWrapper::newFromObject( new TemporaryPasswordPrimaryAuthenticationProvider( [
|
2015-11-22 20:17:00 +00:00
|
|
|
'emailEnabled' => true,
|
|
|
|
|
'newPasswordExpiry' => 42,
|
|
|
|
|
'passwordReminderResendTime' => 43,
|
2019-09-28 00:27:54 +00:00
|
|
|
'allowRequiringEmailForResets' => true,
|
2015-11-22 20:17:00 +00:00
|
|
|
] ) );
|
|
|
|
|
$p->setConfig( $config );
|
|
|
|
|
$this->assertSame( true, $p->emailEnabled );
|
|
|
|
|
$this->assertSame( 42, $p->newPasswordExpiry );
|
|
|
|
|
$this->assertSame( 43, $p->passwordReminderResendTime );
|
2019-09-28 00:27:54 +00:00
|
|
|
$this->assertSame( true, $p->allowRequiringEmail );
|
2015-11-22 20:17:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTestUserCanAuthenticate() {
|
2016-09-28 14:32:34 +00:00
|
|
|
$user = self::getMutableTestUser()->getUser();
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2016-10-02 07:41:55 +00:00
|
|
|
$config = MediaWikiServices::getInstance()->getMainConfig();
|
2015-11-22 20:17:00 +00:00
|
|
|
// A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
|
2016-10-02 07:41:55 +00:00
|
|
|
$passwordFactory = new \PasswordFactory( $config->get( 'PasswordConfig' ), 'A' );
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
$pwhash = $passwordFactory->newFromPlaintext( 'password' )->toString();
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider();
|
2017-04-19 19:37:35 +00:00
|
|
|
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$this->assertFalse( $provider->testUserCanAuthenticate( '<invalid>' ) );
|
|
|
|
|
$this->assertFalse( $provider->testUserCanAuthenticate( 'DoesNotExist' ) );
|
|
|
|
|
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[
|
|
|
|
|
'user_newpassword' => \PasswordFactory::newInvalidPassword()->toString(),
|
|
|
|
|
'user_newpass_time' => null,
|
|
|
|
|
],
|
2016-09-28 14:32:34 +00:00
|
|
|
[ 'user_id' => $user->getId() ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
2016-09-28 14:32:34 +00:00
|
|
|
$this->assertFalse( $provider->testUserCanAuthenticate( $user->getName() ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[
|
|
|
|
|
'user_newpassword' => $pwhash,
|
|
|
|
|
'user_newpass_time' => null,
|
|
|
|
|
],
|
2016-09-28 14:32:34 +00:00
|
|
|
[ 'user_id' => $user->getId() ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
2016-09-28 14:32:34 +00:00
|
|
|
$this->assertTrue( $provider->testUserCanAuthenticate( $user->getName() ) );
|
|
|
|
|
$this->assertTrue( $provider->testUserCanAuthenticate( lcfirst( $user->getName() ) ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[
|
|
|
|
|
'user_newpassword' => $pwhash,
|
|
|
|
|
'user_newpass_time' => $dbw->timestamp( time() - 10 ),
|
|
|
|
|
],
|
2016-09-28 14:32:34 +00:00
|
|
|
[ 'user_id' => $user->getId() ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
$providerPriv->newPasswordExpiry = 100;
|
2016-09-28 14:32:34 +00:00
|
|
|
$this->assertTrue( $provider->testUserCanAuthenticate( $user->getName() ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
$providerPriv->newPasswordExpiry = 1;
|
2016-09-28 14:32:34 +00:00
|
|
|
$this->assertFalse( $provider->testUserCanAuthenticate( $user->getName() ) );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[
|
|
|
|
|
'user_newpassword' => \PasswordFactory::newInvalidPassword()->toString(),
|
|
|
|
|
'user_newpass_time' => null,
|
|
|
|
|
],
|
2016-09-28 14:32:34 +00:00
|
|
|
[ 'user_id' => $user->getId() ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideGetAuthenticationRequests
|
|
|
|
|
* @param string $action
|
|
|
|
|
* @param array $options
|
|
|
|
|
* @param array $expected
|
|
|
|
|
*/
|
|
|
|
|
public function testGetAuthenticationRequests( $action, $options, $expected ) {
|
2019-03-26 18:50:28 +00:00
|
|
|
$actual = $this->getProvider( [ 'emailEnabled' => true ] )
|
|
|
|
|
->getAuthenticationRequests( $action, $options );
|
2015-11-22 20:17:00 +00:00
|
|
|
foreach ( $actual as $req ) {
|
|
|
|
|
if ( $req instanceof TemporaryPasswordAuthenticationRequest && $req->password !== null ) {
|
|
|
|
|
$req->password = 'random';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->assertEquals( $expected, $actual );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideGetAuthenticationRequests() {
|
|
|
|
|
$anon = [ 'username' => null ];
|
2020-12-17 23:10:11 +00:00
|
|
|
$registered = [ 'username' => 'UTSysop' ];
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
[ AuthManager::ACTION_LOGIN, $anon, [
|
|
|
|
|
new PasswordAuthenticationRequest
|
|
|
|
|
] ],
|
2020-12-17 23:10:11 +00:00
|
|
|
[ AuthManager::ACTION_LOGIN, $registered, [
|
2015-11-22 20:17:00 +00:00
|
|
|
new PasswordAuthenticationRequest
|
|
|
|
|
] ],
|
|
|
|
|
[ AuthManager::ACTION_CREATE, $anon, [] ],
|
2020-12-17 23:10:11 +00:00
|
|
|
[ AuthManager::ACTION_CREATE, $registered, [
|
2015-11-22 20:17:00 +00:00
|
|
|
new TemporaryPasswordAuthenticationRequest( 'random' )
|
|
|
|
|
] ],
|
|
|
|
|
[ AuthManager::ACTION_LINK, $anon, [] ],
|
2020-12-17 23:10:11 +00:00
|
|
|
[ AuthManager::ACTION_LINK, $registered, [] ],
|
2015-11-22 20:17:00 +00:00
|
|
|
[ AuthManager::ACTION_CHANGE, $anon, [
|
|
|
|
|
new TemporaryPasswordAuthenticationRequest( 'random' )
|
|
|
|
|
] ],
|
2020-12-17 23:10:11 +00:00
|
|
|
[ AuthManager::ACTION_CHANGE, $registered, [
|
2015-11-22 20:17:00 +00:00
|
|
|
new TemporaryPasswordAuthenticationRequest( 'random' )
|
|
|
|
|
] ],
|
|
|
|
|
[ AuthManager::ACTION_REMOVE, $anon, [
|
|
|
|
|
new TemporaryPasswordAuthenticationRequest
|
|
|
|
|
] ],
|
2020-12-17 23:10:11 +00:00
|
|
|
[ AuthManager::ACTION_REMOVE, $registered, [
|
2015-11-22 20:17:00 +00:00
|
|
|
new TemporaryPasswordAuthenticationRequest
|
|
|
|
|
] ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAuthentication() {
|
2016-09-28 14:32:34 +00:00
|
|
|
$user = self::getMutableTestUser()->getUser();
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
$password = 'TemporaryPassword';
|
|
|
|
|
$hash = ':A:' . md5( $password );
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[ 'user_newpassword' => $hash, 'user_newpass_time' => $dbw->timestamp( time() - 10 ) ],
|
2016-09-28 14:32:34 +00:00
|
|
|
[ 'user_id' => $user->getId() ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req = new PasswordAuthenticationRequest();
|
|
|
|
|
$req->action = AuthManager::ACTION_LOGIN;
|
|
|
|
|
$reqs = [ PasswordAuthenticationRequest::class => $req ];
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider();
|
2017-04-19 19:37:35 +00:00
|
|
|
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$providerPriv->newPasswordExpiry = 100;
|
|
|
|
|
|
|
|
|
|
// 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(
|
|
|
|
|
AuthenticationResponse::newAbstain(),
|
|
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Validation failure
|
2016-09-28 14:32:34 +00:00
|
|
|
$req->username = $user->getName();
|
2015-11-22 20:17:00 +00:00
|
|
|
$req->password = $password;
|
|
|
|
|
$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-09-28 14:32:34 +00:00
|
|
|
AuthenticationResponse::newPass( $user->getName() ),
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
$this->assertNotNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
|
|
|
|
|
|
|
|
|
|
$this->manager->removeAuthenticationSessionData( null );
|
|
|
|
|
$this->validity = \Status::newGood();
|
2016-09-28 14:32:34 +00:00
|
|
|
$req->username = lcfirst( $user->getName() );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertEquals(
|
2016-09-28 14:32:34 +00:00
|
|
|
AuthenticationResponse::newPass( $user->getName() ),
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider->beginPrimaryAuthentication( $reqs )
|
|
|
|
|
);
|
|
|
|
|
$this->assertNotNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
|
2016-09-28 14:32:34 +00:00
|
|
|
$req->username = $user->getName();
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
// Expired password
|
|
|
|
|
$providerPriv->newPasswordExpiry = 1;
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'wrongpassword',
|
|
|
|
|
$ret->message->getKey()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Bad password
|
|
|
|
|
$providerPriv->newPasswordExpiry = 100;
|
|
|
|
|
$this->validity = \Status::newGood();
|
|
|
|
|
$req->password = 'Wrong';
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $reqs );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'wrongpassword',
|
|
|
|
|
$ret->message->getKey()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 ||
|
|
|
|
|
$type === TemporaryPasswordAuthenticationRequest::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';
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider();
|
|
|
|
|
$this->validity = $validity;
|
|
|
|
|
$this->assertEquals( $expect1, $provider->providerAllowsAuthenticationDataChange( $req, false ) );
|
|
|
|
|
$this->assertEquals( $expect2, $provider->providerAllowsAuthenticationDataChange( $req, true ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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( 'ignored' ), \StatusValue::newGood( 'ignored' ) ],
|
|
|
|
|
[ TemporaryPasswordAuthenticationRequest::class, 'UTSysop', \Status::newGood(),
|
|
|
|
|
\StatusValue::newGood(), \StatusValue::newGood() ],
|
|
|
|
|
[ TemporaryPasswordAuthenticationRequest::class, 'uTSysop', \Status::newGood(),
|
|
|
|
|
\StatusValue::newGood(), \StatusValue::newGood() ],
|
|
|
|
|
[ TemporaryPasswordAuthenticationRequest::class, 'UTSysop', \Status::wrap( $err ),
|
|
|
|
|
\StatusValue::newGood(), $err ],
|
|
|
|
|
[ TemporaryPasswordAuthenticationRequest::class, 'UTSysop',
|
|
|
|
|
\Status::newFatal( 'arbitrary-error' ), \StatusValue::newGood(),
|
|
|
|
|
\StatusValue::newFatal( 'arbitrary-error' ) ],
|
|
|
|
|
[ TemporaryPasswordAuthenticationRequest::class, 'DoesNotExist', \Status::newGood(),
|
|
|
|
|
\StatusValue::newGood(), \StatusValue::newGood( 'ignored' ) ],
|
|
|
|
|
[ TemporaryPasswordAuthenticationRequest::class, '<invalid>', \Status::newGood(),
|
|
|
|
|
\StatusValue::newGood(), \StatusValue::newGood( 'ignored' ) ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideProviderChangeAuthenticationData
|
|
|
|
|
* @param string $user
|
|
|
|
|
* @param string $type
|
|
|
|
|
* @param bool $changed
|
|
|
|
|
*/
|
|
|
|
|
public function testProviderChangeAuthenticationData( $user, $type, $changed ) {
|
|
|
|
|
$cuser = ucfirst( $user );
|
|
|
|
|
$oldpass = 'OldTempPassword';
|
|
|
|
|
$newpass = 'NewTempPassword';
|
|
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$oldHash = $dbw->selectField( 'user', 'user_newpassword', [ 'user_name' => $cuser ] );
|
2016-10-12 05:36:03 +00:00
|
|
|
$cb = new ScopedCallback( function () use ( $dbw, $cuser, $oldHash ) {
|
2015-11-22 20:17:00 +00:00
|
|
|
$dbw->update( 'user', [ 'user_newpassword' => $oldHash ], [ 'user_name' => $cuser ] );
|
|
|
|
|
} );
|
|
|
|
|
|
2016-09-28 14:32:34 +00:00
|
|
|
$hash = ':A:' . md5( $oldpass );
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[ 'user_newpassword' => $hash, 'user_newpass_time' => $dbw->timestamp( time() + 10 ) ],
|
|
|
|
|
[ 'user_name' => $cuser ]
|
|
|
|
|
);
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
$provider = $this->getProvider();
|
|
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
|
$loginReq = new PasswordAuthenticationRequest();
|
|
|
|
|
$loginReq->action = AuthManager::ACTION_CHANGE;
|
|
|
|
|
$loginReq->username = $user;
|
|
|
|
|
$loginReq->password = $oldpass;
|
|
|
|
|
$loginReqs = [ PasswordAuthenticationRequest::class => $loginReq ];
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newPass( $cuser ),
|
|
|
|
|
$provider->beginPrimaryAuthentication( $loginReqs ),
|
|
|
|
|
'Sanity check'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $type === PasswordAuthenticationRequest::class ||
|
|
|
|
|
$type === TemporaryPasswordAuthenticationRequest::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;
|
|
|
|
|
$resetMailer = $this->hookMailer();
|
|
|
|
|
$provider->providerChangeAuthenticationData( $changeReq );
|
2016-10-12 05:36:03 +00:00
|
|
|
ScopedCallback::consume( $resetMailer );
|
2015-11-22 20:17:00 +00:00
|
|
|
|
|
|
|
|
$loginReq->password = $oldpass;
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $loginReqs );
|
|
|
|
|
$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 ( $changed ) {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::newPass( $cuser ),
|
|
|
|
|
$ret,
|
|
|
|
|
'new password should pass'
|
|
|
|
|
);
|
|
|
|
|
$this->assertNotNull(
|
|
|
|
|
$dbw->selectField( 'user', 'user_newpass_time', [ 'user_name' => $cuser ] )
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
AuthenticationResponse::FAIL,
|
|
|
|
|
$ret->status,
|
|
|
|
|
'new password should fail'
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'wrongpassword',
|
|
|
|
|
$ret->message->getKey(),
|
|
|
|
|
'new password should fail'
|
|
|
|
|
);
|
|
|
|
|
$this->assertNull(
|
|
|
|
|
$dbw->selectField( 'user', 'user_newpass_time', [ 'user_name' => $cuser ] )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideProviderChangeAuthenticationData() {
|
|
|
|
|
return [
|
|
|
|
|
[ 'UTSysop', AuthenticationRequest::class, false ],
|
|
|
|
|
[ 'UTSysop', PasswordAuthenticationRequest::class, false ],
|
|
|
|
|
[ 'UTSysop', TemporaryPasswordAuthenticationRequest::class, true ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testProviderChangeAuthenticationDataEmail() {
|
2016-09-28 14:32:34 +00:00
|
|
|
$user = self::getMutableTestUser()->getUser();
|
|
|
|
|
|
2015-11-22 20:17:00 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[ 'user_newpass_time' => $dbw->timestamp( time() - 5 * 3600 ) ],
|
2016-09-28 14:32:34 +00:00
|
|
|
[ 'user_id' => $user->getId() ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$req = TemporaryPasswordAuthenticationRequest::newRandom();
|
|
|
|
|
$req->username = $user->getName();
|
|
|
|
|
$req->mailpassword = true;
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider( [ 'emailEnabled' => false ] );
|
|
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
|
|
|
|
|
$this->assertEquals( \StatusValue::newFatal( 'passwordreset-emaildisabled' ), $status );
|
|
|
|
|
|
2019-03-26 18:50:28 +00:00
|
|
|
$provider = $this->getProvider( [
|
|
|
|
|
'emailEnabled' => true, 'passwordReminderResendTime' => 10
|
|
|
|
|
] );
|
2015-11-22 20:17:00 +00:00
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
|
|
|
|
|
$this->assertEquals( \StatusValue::newFatal( 'throttled-mailpassword', 10 ), $status );
|
|
|
|
|
|
2019-03-26 18:50:28 +00:00
|
|
|
$provider = $this->getProvider( [
|
|
|
|
|
'emailEnabled' => true, 'passwordReminderResendTime' => 3
|
|
|
|
|
] );
|
2015-11-22 20:17:00 +00:00
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
|
|
|
|
|
$this->assertFalse( $status->hasMessage( 'throttled-mailpassword' ) );
|
|
|
|
|
|
|
|
|
|
$dbw->update(
|
|
|
|
|
'user',
|
|
|
|
|
[ 'user_newpass_time' => $dbw->timestamp( time() + 5 * 3600 ) ],
|
2016-09-28 14:32:34 +00:00
|
|
|
[ 'user_id' => $user->getId() ]
|
2015-11-22 20:17:00 +00:00
|
|
|
);
|
2019-03-26 18:50:28 +00:00
|
|
|
$provider = $this->getProvider( [
|
|
|
|
|
'emailEnabled' => true, 'passwordReminderResendTime' => 0
|
|
|
|
|
] );
|
2015-11-22 20:17:00 +00:00
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
|
|
|
|
|
$this->assertFalse( $status->hasMessage( 'throttled-mailpassword' ) );
|
|
|
|
|
|
|
|
|
|
$req->caller = null;
|
|
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
|
|
|
|
|
$this->assertEquals( \StatusValue::newFatal( 'passwordreset-nocaller' ), $status );
|
|
|
|
|
|
|
|
|
|
$req->caller = '127.0.0.256';
|
|
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
|
|
|
|
|
$this->assertEquals( \StatusValue::newFatal( 'passwordreset-nosuchcaller', '127.0.0.256' ),
|
|
|
|
|
$status );
|
|
|
|
|
|
|
|
|
|
$req->caller = '<Invalid>';
|
|
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
|
|
|
|
|
$this->assertEquals( \StatusValue::newFatal( 'passwordreset-nosuchcaller', '<Invalid>' ),
|
|
|
|
|
$status );
|
|
|
|
|
|
|
|
|
|
$req->caller = '127.0.0.1';
|
|
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
|
|
|
|
|
$this->assertEquals( \StatusValue::newGood(), $status );
|
|
|
|
|
|
2016-09-28 14:32:34 +00:00
|
|
|
$req->caller = $user->getName();
|
2015-11-22 20:17:00 +00:00
|
|
|
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
|
|
|
|
|
$this->assertEquals( \StatusValue::newGood(), $status );
|
|
|
|
|
|
|
|
|
|
$mailed = false;
|
|
|
|
|
$resetMailer = $this->hookMailer( function ( $headers, $to, $from, $subject, $body )
|
2016-09-28 14:32:34 +00:00
|
|
|
use ( &$mailed, $req, $user )
|
2015-11-22 20:17:00 +00:00
|
|
|
{
|
|
|
|
|
$mailed = true;
|
2016-09-28 14:32:34 +00:00
|
|
|
$this->assertSame( $user->getEmail(), $to[0]->address );
|
2019-12-14 12:45:35 +00:00
|
|
|
$this->assertStringContainsString( $req->password, $body );
|
2015-11-22 20:17:00 +00:00
|
|
|
return false;
|
|
|
|
|
} );
|
|
|
|
|
$provider->providerChangeAuthenticationData( $req );
|
2016-10-12 05:36:03 +00:00
|
|
|
ScopedCallback::consume( $resetMailer );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertTrue( $mailed );
|
|
|
|
|
|
2017-04-19 19:37:35 +00:00
|
|
|
$priv = TestingAccessWrapper::newFromObject( $provider );
|
2015-11-22 20:17:00 +00:00
|
|
|
$req->username = '<invalid>';
|
|
|
|
|
$status = $priv->sendPasswordResetEmail( $req );
|
|
|
|
|
$this->assertEquals( \Status::newFatal( 'noname' ), $status );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testTestForAccountCreation() {
|
|
|
|
|
$user = \User::newFromName( 'foo' );
|
|
|
|
|
$req = new TemporaryPasswordAuthenticationRequest();
|
|
|
|
|
$req->username = 'Foo';
|
|
|
|
|
$req->password = 'Bar';
|
|
|
|
|
$reqs = [ TemporaryPasswordAuthenticationRequest::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'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->validity->error( 'arbitrary warning' );
|
|
|
|
|
$expect = \StatusValue::newGood();
|
|
|
|
|
$expect->error( 'arbitrary warning' );
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expect,
|
|
|
|
|
$provider->testForAccountCreation( $user, $user, $reqs ),
|
|
|
|
|
'Password request, not validated'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAccountCreation() {
|
|
|
|
|
$resetMailer = $this->hookMailer();
|
|
|
|
|
|
|
|
|
|
$user = \User::newFromName( 'Foo' );
|
|
|
|
|
|
|
|
|
|
$req = new TemporaryPasswordAuthenticationRequest();
|
|
|
|
|
$reqs = [ TemporaryPasswordAuthenticationRequest::class => $req ];
|
|
|
|
|
|
|
|
|
|
$authreq = new PasswordAuthenticationRequest();
|
|
|
|
|
$authreq->action = AuthManager::ACTION_CREATE;
|
|
|
|
|
$authreqs = [ PasswordAuthenticationRequest::class => $authreq ];
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider();
|
|
|
|
|
|
|
|
|
|
$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 ) );
|
|
|
|
|
$this->assertNull( $this->manager->getAuthenticationSessionData( 'no-email' ) );
|
|
|
|
|
|
2016-09-28 14:32:34 +00:00
|
|
|
$user = self::getMutableTestUser()->getUser();
|
2015-11-22 20:17:00 +00:00
|
|
|
$req->username = $authreq->username = $user->getName();
|
|
|
|
|
$req->password = $authreq->password = 'NewPassword';
|
2016-09-28 14:32:34 +00:00
|
|
|
$expect = AuthenticationResponse::newPass( $user->getName() );
|
2015-11-22 20:17:00 +00:00
|
|
|
$expect->createRequest = $req;
|
|
|
|
|
|
|
|
|
|
$res2 = $provider->beginPrimaryAccountCreation( $user, $user, $reqs );
|
|
|
|
|
$this->assertEquals( $expect, $res2, 'Sanity check' );
|
|
|
|
|
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $authreqs );
|
|
|
|
|
$this->assertEquals( AuthenticationResponse::FAIL, $ret->status, 'sanity check' );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( null, $provider->finishAccountCreation( $user, $user, $res2 ) );
|
|
|
|
|
|
|
|
|
|
$ret = $provider->beginPrimaryAuthentication( $authreqs );
|
|
|
|
|
$this->assertEquals( AuthenticationResponse::PASS, $ret->status, 'new password is set' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testAccountCreationEmail() {
|
|
|
|
|
$creator = \User::newFromName( 'Foo' );
|
|
|
|
|
|
2016-09-28 14:32:34 +00:00
|
|
|
$user = self::getMutableTestUser()->getUser();
|
2015-11-22 20:17:00 +00:00
|
|
|
$user->setEmail( null );
|
|
|
|
|
|
|
|
|
|
$req = TemporaryPasswordAuthenticationRequest::newRandom();
|
|
|
|
|
$req->username = $user->getName();
|
|
|
|
|
$req->mailpassword = true;
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider( [ 'emailEnabled' => false ] );
|
|
|
|
|
$status = $provider->testForAccountCreation( $user, $creator, [ $req ] );
|
|
|
|
|
$this->assertEquals( \StatusValue::newFatal( 'emaildisabled' ), $status );
|
|
|
|
|
|
|
|
|
|
$provider = $this->getProvider( [ 'emailEnabled' => true ] );
|
|
|
|
|
$status = $provider->testForAccountCreation( $user, $creator, [ $req ] );
|
|
|
|
|
$this->assertEquals( \StatusValue::newFatal( 'noemailcreate' ), $status );
|
|
|
|
|
|
|
|
|
|
$user->setEmail( 'test@localhost.localdomain' );
|
|
|
|
|
$status = $provider->testForAccountCreation( $user, $creator, [ $req ] );
|
|
|
|
|
$this->assertEquals( \StatusValue::newGood(), $status );
|
|
|
|
|
|
|
|
|
|
$mailed = false;
|
|
|
|
|
$resetMailer = $this->hookMailer( function ( $headers, $to, $from, $subject, $body )
|
|
|
|
|
use ( &$mailed, $req )
|
|
|
|
|
{
|
|
|
|
|
$mailed = true;
|
|
|
|
|
$this->assertSame( 'test@localhost.localdomain', $to[0]->address );
|
2019-12-14 12:45:35 +00:00
|
|
|
$this->assertStringContainsString( $req->password, $body );
|
2015-11-22 20:17:00 +00:00
|
|
|
return false;
|
|
|
|
|
} );
|
|
|
|
|
|
2016-09-28 14:32:34 +00:00
|
|
|
$expect = AuthenticationResponse::newPass( $user->getName() );
|
2017-06-27 19:44:12 +00:00
|
|
|
$expect->createRequest = clone $req;
|
2016-09-28 14:32:34 +00:00
|
|
|
$expect->createRequest->username = $user->getName();
|
2015-11-22 20:17:00 +00:00
|
|
|
$res = $provider->beginPrimaryAccountCreation( $user, $creator, [ $req ] );
|
|
|
|
|
$this->assertEquals( $expect, $res );
|
|
|
|
|
$this->assertTrue( $this->manager->getAuthenticationSessionData( 'no-email' ) );
|
|
|
|
|
$this->assertFalse( $mailed );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 'byemail', $provider->finishAccountCreation( $user, $creator, $res ) );
|
|
|
|
|
$this->assertTrue( $mailed );
|
|
|
|
|
|
2016-10-12 05:36:03 +00:00
|
|
|
ScopedCallback::consume( $resetMailer );
|
2015-11-22 20:17:00 +00:00
|
|
|
$this->assertTrue( $mailed );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|