wiki.techinc.nl/tests/phpunit/includes/auth/TemporaryPasswordPrimaryAuthenticationProviderTest.php

741 lines
24 KiB
PHP
Raw Normal View History

<?php
namespace MediaWiki\Auth;
use MediaWiki\MediaWikiServices;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\User\UserNameUtils;
use Psr\Container\ContainerInterface;
use Wikimedia\ScopedCallback;
use Wikimedia\TestingAccessWrapper;
/**
* @group AuthManager
* @group Database
* @covers \MediaWiki\Auth\TemporaryPasswordPrimaryAuthenticationProvider
*/
class TemporaryPasswordPrimaryAuthenticationProviderTest extends \MediaWikiIntegrationTestCase {
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 = [] ) {
$mwServices = MediaWikiServices::getInstance();
if ( !$this->config ) {
$this->config = new \HashConfig( [
'EmailEnabled' => true,
] );
}
$config = new \MultiConfig( [
$this->config,
$mwServices->getMainConfig()
] );
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();
if ( !$this->manager ) {
$services = $this->createNoOpAbstractMock( ContainerInterface::class );
$objectFactory = new \Wikimedia\ObjectFactory( $services );
$permManager = $this->createNoOpMock( PermissionManager::class );
$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
$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,
$mwServices->getReadOnlyMode(),
$userNameUtils,
$mwServices->getBlockManager(),
$mwServices->getBlockErrorFormatter()
);
}
$this->validity = \Status::newGood();
$mockedMethods[] = 'checkPasswordValidity';
$provider = $this->getMockBuilder( TemporaryPasswordPrimaryAuthenticationProvider::class )
->setMethods( $mockedMethods )
->setConstructorArgs( [ $params ] )
->getMock();
$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 );
return $provider;
}
protected function hookMailer( $func = null ) {
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
$hookContainer = MediaWikiServices::getInstance()->getHookContainer();
if ( $func ) {
$reset = $hookContainer->scopedRegister( 'AlternateUserMailer', $func, true );
} else {
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
$reset = $hookContainer->scopedRegister( 'AlternateUserMailer', function () {
$this->fail( 'AlternateUserMailer hook called unexpectedly' );
return false;
}, true );
}
Hook Container New classes and modificatons to existing classes to support the new Hooks system. All changes are documented in RFC https://phabricator.wikimedia.org/T240307. - HookContainer.php: Class for doing much of what Hooks.php has historically done, but enabling new-style hooks to be processed and registered. Changes include new ways of defining hook handler functions as an object with defined dependencies in extension.json, removing runWithoutAbort() and addit it to an $options parameter to be passed to HookContainer::run(), being able to decipher whether a hook handler is legacy or non-legacy style and run them in the appropriate way, etc. - DeprecatedHooks.php: For marking hooks deprecated and verifying if one is deprecated - DeprecatedHooksTest.php: Unit tests for DeprecatedHooks.php - Hooks.php: register() will now additionally register hooks with handlers in new HooksContainer.php. getHandlers() will be a legacy wrapper for calling the newer HookContainer::getHandlers() - MediaWikiServices.php: Added getHookContainer() for retrieving HookContainer singleton - ExtensionProcessor.php: modified extractHooks() to be able to extract new style handler objects being registered in extension.json - ServiceWiring.php: Added HookContainer to list of services to return - HookContainerTest.php: Unit tests for HookContainer.php - ExtensionProcessorTest.php: Moved file out of /unit folder and now extends MediaWikiTestCase instead of MediaWikiUnitTestCase (as the tests are not truly unit tests). Modified existing tests for ExtensionProcessor::extractHooks() to include a test case for new style handler Bug: T240307 Change-Id: I432861d8995cfd7180e77e115251d8055b7eceec
2020-02-10 14:47:46 +00:00
return $reset;
}
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,
'AllowRequiringEmailForResets' => false,
] );
$p = TestingAccessWrapper::newFromObject( new TemporaryPasswordPrimaryAuthenticationProvider() );
$p->setConfig( $config );
$this->assertSame( false, $p->emailEnabled );
$this->assertSame( 100, $p->newPasswordExpiry );
$this->assertSame( 101, $p->passwordReminderResendTime );
$p = TestingAccessWrapper::newFromObject( new TemporaryPasswordPrimaryAuthenticationProvider( [
'emailEnabled' => true,
'newPasswordExpiry' => 42,
'passwordReminderResendTime' => 43,
'allowRequiringEmailForResets' => true,
] ) );
$p->setConfig( $config );
$this->assertSame( true, $p->emailEnabled );
$this->assertSame( 42, $p->newPasswordExpiry );
$this->assertSame( 43, $p->passwordReminderResendTime );
$this->assertSame( true, $p->allowRequiringEmail );
}
public function testTestUserCanAuthenticate() {
$user = self::getMutableTestUser()->getUser();
$dbw = wfGetDB( DB_MASTER );
$config = MediaWikiServices::getInstance()->getMainConfig();
// A is unsalted MD5 (thus fast) ... we don't care about security here, this is test only
$passwordFactory = new \PasswordFactory( $config->get( 'PasswordConfig' ), 'A' );
$pwhash = $passwordFactory->newFromPlaintext( 'password' )->toString();
$provider = $this->getProvider();
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$this->assertFalse( $provider->testUserCanAuthenticate( '<invalid>' ) );
$this->assertFalse( $provider->testUserCanAuthenticate( 'DoesNotExist' ) );
$dbw->update(
'user',
[
'user_newpassword' => \PasswordFactory::newInvalidPassword()->toString(),
'user_newpass_time' => null,
],
[ 'user_id' => $user->getId() ]
);
$this->assertFalse( $provider->testUserCanAuthenticate( $user->getName() ) );
$dbw->update(
'user',
[
'user_newpassword' => $pwhash,
'user_newpass_time' => null,
],
[ 'user_id' => $user->getId() ]
);
$this->assertTrue( $provider->testUserCanAuthenticate( $user->getName() ) );
$this->assertTrue( $provider->testUserCanAuthenticate( lcfirst( $user->getName() ) ) );
$dbw->update(
'user',
[
'user_newpassword' => $pwhash,
'user_newpass_time' => $dbw->timestamp( time() - 10 ),
],
[ 'user_id' => $user->getId() ]
);
$providerPriv->newPasswordExpiry = 100;
$this->assertTrue( $provider->testUserCanAuthenticate( $user->getName() ) );
$providerPriv->newPasswordExpiry = 1;
$this->assertFalse( $provider->testUserCanAuthenticate( $user->getName() ) );
$dbw->update(
'user',
[
'user_newpassword' => \PasswordFactory::newInvalidPassword()->toString(),
'user_newpass_time' => null,
],
[ 'user_id' => $user->getId() ]
);
}
/**
* @dataProvider provideGetAuthenticationRequests
* @param string $action
* @param array $options
* @param array $expected
*/
public function testGetAuthenticationRequests( $action, $options, $expected ) {
$actual = $this->getProvider( [ 'emailEnabled' => true ] )
->getAuthenticationRequests( $action, $options );
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 ];
$registered = [ 'username' => 'UTSysop' ];
return [
[ AuthManager::ACTION_LOGIN, $anon, [
new PasswordAuthenticationRequest
] ],
[ AuthManager::ACTION_LOGIN, $registered, [
new PasswordAuthenticationRequest
] ],
[ AuthManager::ACTION_CREATE, $anon, [] ],
[ AuthManager::ACTION_CREATE, $registered, [
new TemporaryPasswordAuthenticationRequest( 'random' )
] ],
[ AuthManager::ACTION_LINK, $anon, [] ],
[ AuthManager::ACTION_LINK, $registered, [] ],
[ AuthManager::ACTION_CHANGE, $anon, [
new TemporaryPasswordAuthenticationRequest( 'random' )
] ],
[ AuthManager::ACTION_CHANGE, $registered, [
new TemporaryPasswordAuthenticationRequest( 'random' )
] ],
[ AuthManager::ACTION_REMOVE, $anon, [
new TemporaryPasswordAuthenticationRequest
] ],
[ AuthManager::ACTION_REMOVE, $registered, [
new TemporaryPasswordAuthenticationRequest
] ],
];
}
public function testAuthentication() {
$user = self::getMutableTestUser()->getUser();
$password = 'TemporaryPassword';
$hash = ':A:' . md5( $password );
$dbw = wfGetDB( DB_MASTER );
$dbw->update(
'user',
[ 'user_newpassword' => $hash, 'user_newpass_time' => $dbw->timestamp( time() - 10 ) ],
[ 'user_id' => $user->getId() ]
);
$req = new PasswordAuthenticationRequest();
$req->action = AuthManager::ACTION_LOGIN;
$reqs = [ PasswordAuthenticationRequest::class => $req ];
$provider = $this->getProvider();
$providerPriv = TestingAccessWrapper::newFromObject( $provider );
$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
$req->username = $user->getName();
$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(
AuthenticationResponse::newPass( $user->getName() ),
$provider->beginPrimaryAuthentication( $reqs )
);
$this->assertNotNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
$this->manager->removeAuthenticationSessionData( null );
$this->validity = \Status::newGood();
$req->username = lcfirst( $user->getName() );
$this->assertEquals(
AuthenticationResponse::newPass( $user->getName() ),
$provider->beginPrimaryAuthentication( $reqs )
);
$this->assertNotNull( $this->manager->getAuthenticationSessionData( 'reset-pass' ) );
$req->username = $user->getName();
// 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 {
$req = $this->createMock( $type );
}
$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 ] );
$cb = new ScopedCallback( function () use ( $dbw, $cuser, $oldHash ) {
$dbw->update( 'user', [ 'user_newpassword' => $oldHash ], [ 'user_name' => $cuser ] );
} );
$hash = ':A:' . md5( $oldpass );
$dbw->update(
'user',
[ 'user_newpassword' => $hash, 'user_newpass_time' => $dbw->timestamp( time() + 10 ) ],
[ 'user_name' => $cuser ]
);
$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 {
$changeReq = $this->createMock( $type );
}
$changeReq->action = AuthManager::ACTION_CHANGE;
$changeReq->username = $user;
$changeReq->password = $newpass;
$resetMailer = $this->hookMailer();
$provider->providerChangeAuthenticationData( $changeReq );
ScopedCallback::consume( $resetMailer );
$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() {
$user = self::getMutableTestUser()->getUser();
$dbw = wfGetDB( DB_MASTER );
$dbw->update(
'user',
[ 'user_newpass_time' => $dbw->timestamp( time() - 5 * 3600 ) ],
[ 'user_id' => $user->getId() ]
);
$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 );
$provider = $this->getProvider( [
'emailEnabled' => true, 'passwordReminderResendTime' => 10
] );
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
$this->assertEquals( \StatusValue::newFatal( 'throttled-mailpassword', 10 ), $status );
$provider = $this->getProvider( [
'emailEnabled' => true, 'passwordReminderResendTime' => 3
] );
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
$this->assertFalse( $status->hasMessage( 'throttled-mailpassword' ) );
$dbw->update(
'user',
[ 'user_newpass_time' => $dbw->timestamp( time() + 5 * 3600 ) ],
[ 'user_id' => $user->getId() ]
);
$provider = $this->getProvider( [
'emailEnabled' => true, 'passwordReminderResendTime' => 0
] );
$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 );
$req->caller = $user->getName();
$status = $provider->providerAllowsAuthenticationDataChange( $req, true );
$this->assertEquals( \StatusValue::newGood(), $status );
$mailed = false;
$resetMailer = $this->hookMailer( function ( $headers, $to, $from, $subject, $body )
use ( &$mailed, $req, $user )
{
$mailed = true;
$this->assertSame( $user->getEmail(), $to[0]->address );
$this->assertStringContainsString( $req->password, $body );
return false;
} );
$provider->providerChangeAuthenticationData( $req );
ScopedCallback::consume( $resetMailer );
$this->assertTrue( $mailed );
$priv = TestingAccessWrapper::newFromObject( $provider );
$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' );
$expect->createRequest = clone $req;
$expect->createRequest->username = 'Foo';
$this->assertEquals( $expect, $provider->beginPrimaryAccountCreation( $user, $user, $reqs ) );
$this->assertNull( $this->manager->getAuthenticationSessionData( 'no-email' ) );
$user = self::getMutableTestUser()->getUser();
$req->username = $authreq->username = $user->getName();
$req->password = $authreq->password = 'NewPassword';
$expect = AuthenticationResponse::newPass( $user->getName() );
$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' );
$user = self::getMutableTestUser()->getUser();
$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 );
$this->assertStringContainsString( $req->password, $body );
return false;
} );
$expect = AuthenticationResponse::newPass( $user->getName() );
$expect->createRequest = clone $req;
$expect->createRequest->username = $user->getName();
$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 );
ScopedCallback::consume( $resetMailer );
$this->assertTrue( $mailed );
}
}