MediaWiki  1.29.2
AbstractPasswordPrimaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
6 use Wikimedia\TestingAccessWrapper;
7 
13  public function testConstructor() {
14  $provider = $this->getMockForAbstractClass(
16  );
17  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
18  $this->assertTrue( $providerPriv->authoritative );
19 
20  $provider = $this->getMockForAbstractClass(
22  [ [ 'authoritative' => false ] ]
23  );
24  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
25  $this->assertFalse( $providerPriv->authoritative );
26  }
27 
28  public function testGetPasswordFactory() {
29  $provider = $this->getMockForAbstractClass(
31  );
32  $provider->setConfig( MediaWikiServices::getInstance()->getMainConfig() );
33  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
34 
35  $obj = $providerPriv->getPasswordFactory();
36  $this->assertInstanceOf( 'PasswordFactory', $obj );
37  $this->assertSame( $obj, $providerPriv->getPasswordFactory() );
38  }
39 
40  public function testGetPassword() {
41  $provider = $this->getMockForAbstractClass(
43  );
44  $provider->setConfig( MediaWikiServices::getInstance()->getMainConfig() );
45  $provider->setLogger( new \Psr\Log\NullLogger() );
46  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
47 
48  $obj = $providerPriv->getPassword( null );
49  $this->assertInstanceOf( 'Password', $obj );
50 
51  $obj = $providerPriv->getPassword( 'invalid' );
52  $this->assertInstanceOf( 'Password', $obj );
53  }
54 
55  public function testGetNewPasswordExpiry() {
56  $config = new \HashConfig;
57  $provider = $this->getMockForAbstractClass(
59  );
60  $provider->setConfig( new \MultiConfig( [
61  $config,
62  MediaWikiServices::getInstance()->getMainConfig()
63  ] ) );
64  $provider->setLogger( new \Psr\Log\NullLogger() );
65  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
66 
67  $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'ResetPasswordExpiration' => [] ] );
68 
69  $config->set( 'PasswordExpirationDays', 0 );
70  $this->assertNull( $providerPriv->getNewPasswordExpiry( 'UTSysop' ) );
71 
72  $config->set( 'PasswordExpirationDays', 5 );
73  $this->assertEquals(
74  time() + 5 * 86400,
75  wfTimestamp( TS_UNIX, $providerPriv->getNewPasswordExpiry( 'UTSysop' ) ),
76  '',
77  2 /* Fuzz */
78  );
79 
80  $this->mergeMwGlobalArrayValue( 'wgHooks', [
81  'ResetPasswordExpiration' => [ function ( $user, &$expires ) {
82  $this->assertSame( 'UTSysop', $user->getName() );
83  $expires = '30001231235959';
84  } ]
85  ] );
86  $this->assertEquals( '30001231235959', $providerPriv->getNewPasswordExpiry( 'UTSysop' ) );
87  }
88 
89  public function testCheckPasswordValidity() {
90  $uppCalled = 0;
91  $uppStatus = \Status::newGood();
92  $this->setMwGlobals( [
93  'wgPasswordPolicy' => [
94  'policies' => [
95  'default' => [
96  'Check' => true,
97  ],
98  ],
99  'checks' => [
100  'Check' => function () use ( &$uppCalled, &$uppStatus ) {
101  $uppCalled++;
102  return $uppStatus;
103  },
104  ],
105  ]
106  ] );
107 
108  $provider = $this->getMockForAbstractClass(
110  );
111  $provider->setConfig( MediaWikiServices::getInstance()->getMainConfig() );
112  $provider->setLogger( new \Psr\Log\NullLogger() );
113  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
114 
115  $this->assertEquals( $uppStatus, $providerPriv->checkPasswordValidity( 'foo', 'bar' ) );
116 
117  $uppStatus->fatal( 'arbitrary-warning' );
118  $this->assertEquals( $uppStatus, $providerPriv->checkPasswordValidity( 'foo', 'bar' ) );
119  }
120 
121  public function testSetPasswordResetFlag() {
122  $config = new \HashConfig( [
123  'InvalidPasswordReset' => true,
124  ] );
125 
126  $manager = new AuthManager(
127  new \FauxRequest(),
128  MediaWikiServices::getInstance()->getMainConfig()
129  );
130 
131  $provider = $this->getMockForAbstractClass(
133  );
134  $provider->setConfig( $config );
135  $provider->setLogger( new \Psr\Log\NullLogger() );
136  $provider->setManager( $manager );
137  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
138 
139  $manager->removeAuthenticationSessionData( null );
141  $providerPriv->setPasswordResetFlag( 'Foo', $status );
142  $this->assertNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
143 
144  $manager->removeAuthenticationSessionData( null );
146  $status->error( 'testing' );
147  $providerPriv->setPasswordResetFlag( 'Foo', $status );
148  $ret = $manager->getAuthenticationSessionData( 'reset-pass' );
149  $this->assertNotNull( $ret );
150  $this->assertSame( 'resetpass-validity-soft', $ret->msg->getKey() );
151  $this->assertFalse( $ret->hard );
152 
153  $config->set( 'InvalidPasswordReset', false );
154  $manager->removeAuthenticationSessionData( null );
155  $providerPriv->setPasswordResetFlag( 'Foo', $status );
156  $ret = $manager->getAuthenticationSessionData( 'reset-pass' );
157  $this->assertNull( $ret );
158  }
159 
160  public function testFailResponse() {
161  $provider = $this->getMockForAbstractClass(
163  [ [ 'authoritative' => false ] ]
164  );
165  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
166 
168 
169  $ret = $providerPriv->failResponse( $req );
170  $this->assertSame( AuthenticationResponse::ABSTAIN, $ret->status );
171 
172  $provider = $this->getMockForAbstractClass(
174  [ [ 'authoritative' => true ] ]
175  );
176  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
177 
178  $req->password = '';
179  $ret = $providerPriv->failResponse( $req );
180  $this->assertSame( AuthenticationResponse::FAIL, $ret->status );
181  $this->assertSame( 'wrongpasswordempty', $ret->message->getKey() );
182 
183  $req->password = 'X';
184  $ret = $providerPriv->failResponse( $req );
185  $this->assertSame( AuthenticationResponse::FAIL, $ret->status );
186  $this->assertSame( 'wrongpassword', $ret->message->getKey() );
187  }
188 
195  $provider = $this->getMockForAbstractClass(
197  );
198 
199  $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
200  }
201 
202  public static function provideGetAuthenticationRequests() {
203  return [
206  [ AuthManager::ACTION_LINK, [] ],
209  ];
210  }
211 
215  $req->username = 'foo';
216  $req->password = null;
217 
218  $provider = $this->getMockForAbstractClass(
220  );
221  $provider->expects( $this->once() )
222  ->method( 'providerChangeAuthenticationData' )
223  ->with( $this->equalTo( $req ) );
224 
225  $provider->providerRevokeAccessForUser( 'foo' );
226  }
227 
228 }
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
MediaWiki\$action
String $action
Cache what action this request is.
Definition: MediaWiki.php:47
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:766
MultiConfig
Provides a fallback sequence for Config objects.
Definition: MultiConfig.php:28
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1994
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
$req
this hook is for auditing only $req
Definition: hooks.txt:990
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\testSetPasswordResetFlag
testSetPasswordResetFlag()
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:121
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Auth\PasswordAuthenticationRequest
This is a value object for authentication requests with a username and password.
Definition: PasswordAuthenticationRequest.php:29
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:658
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
MediaWiki\Auth\AuthenticationResponse\ABSTAIN
const ABSTAIN
Indicates that the authentication provider does not handle this request.
Definition: AuthenticationResponse.php:52
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest
AuthManager MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider.
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:12
MediaWiki\Auth\AuthenticationResponse\FAIL
const FAIL
Indicates that the authentication failed.
Definition: AuthenticationResponse.php:42
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:97
MediaWiki\Auth\AuthManager\ACTION_CREATE
const ACTION_CREATE
Create a new user.
Definition: AuthManager.php:89
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:76
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\testCheckPasswordValidity
testCheckPasswordValidity()
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:89
MediaWiki\Auth\AuthManager\ACTION_CHANGE
const ACTION_CHANGE
Change a user's credentials.
Definition: AuthManager.php:99
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1956
MediaWiki\Auth\AuthManager\ACTION_LINK
const ACTION_LINK
Link an existing user to a third-party account.
Definition: AuthManager.php:94
$response
this hook is for auditing only $response
Definition: hooks.txt:783
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:82
MediaWiki\Auth\AuthManager\ACTION_REMOVE
const ACTION_REMOVE
Remove a user's credentials.
Definition: AuthManager.php:101
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\testConstructor
testConstructor()
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:13
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\testProviderRevokeAccessForUser
testProviderRevokeAccessForUser()
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:212
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\testGetPassword
testGetPassword()
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:40
MediaWiki\$config
Config $config
Definition: MediaWiki.php:42
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\testFailResponse
testFailResponse()
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:160
MediaWiki\Auth\AuthManager\ACTION_LOGIN
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
Definition: AuthManager.php:84
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\provideGetAuthenticationRequests
static provideGetAuthenticationRequests()
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:202
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
MediaWiki\Auth
Definition: AbstractAuthenticationProvider.php:22
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\testGetAuthenticationRequests
testGetAuthenticationRequests( $action, $response)
provideGetAuthenticationRequests
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:194
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\testGetNewPasswordExpiry
testGetNewPasswordExpiry()
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:55
MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProviderTest\testGetPasswordFactory
testGetPasswordFactory()
Definition: AbstractPasswordPrimaryAuthenticationProviderTest.php:28