MediaWiki  1.28.0
AbstractPasswordPrimaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
10  public function testConstructor() {
11  $provider = $this->getMockForAbstractClass(
13  );
14  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
15  $this->assertTrue( $providerPriv->authoritative );
16 
17  $provider = $this->getMockForAbstractClass(
19  [ [ 'authoritative' => false ] ]
20  );
21  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
22  $this->assertFalse( $providerPriv->authoritative );
23  }
24 
25  public function testGetPasswordFactory() {
26  $provider = $this->getMockForAbstractClass(
28  );
29  $provider->setConfig( \ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
30  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
31 
32  $obj = $providerPriv->getPasswordFactory();
33  $this->assertInstanceOf( 'PasswordFactory', $obj );
34  $this->assertSame( $obj, $providerPriv->getPasswordFactory() );
35  }
36 
37  public function testGetPassword() {
38  $provider = $this->getMockForAbstractClass(
40  );
41  $provider->setConfig( \ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
42  $provider->setLogger( new \Psr\Log\NullLogger() );
43  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
44 
45  $obj = $providerPriv->getPassword( null );
46  $this->assertInstanceOf( 'Password', $obj );
47 
48  $obj = $providerPriv->getPassword( 'invalid' );
49  $this->assertInstanceOf( 'Password', $obj );
50  }
51 
52  public function testGetNewPasswordExpiry() {
53  $config = new \HashConfig;
54  $provider = $this->getMockForAbstractClass(
56  );
57  $provider->setConfig( new \MultiConfig( [
58  $config,
59  \ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
60  ] ) );
61  $provider->setLogger( new \Psr\Log\NullLogger() );
62  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
63 
64  $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'ResetPasswordExpiration' => [] ] );
65 
66  $config->set( 'PasswordExpirationDays', 0 );
67  $this->assertNull( $providerPriv->getNewPasswordExpiry( 'UTSysop' ) );
68 
69  $config->set( 'PasswordExpirationDays', 5 );
70  $this->assertEquals(
71  time() + 5 * 86400,
72  wfTimestamp( TS_UNIX, $providerPriv->getNewPasswordExpiry( 'UTSysop' ) ),
73  '',
74  2 /* Fuzz */
75  );
76 
77  $this->mergeMwGlobalArrayValue( 'wgHooks', [
78  'ResetPasswordExpiration' => [ function ( $user, &$expires ) {
79  $this->assertSame( 'UTSysop', $user->getName() );
80  $expires = '30001231235959';
81  } ]
82  ] );
83  $this->assertEquals( '30001231235959', $providerPriv->getNewPasswordExpiry( 'UTSysop' ) );
84  }
85 
86  public function testCheckPasswordValidity() {
87  $uppCalled = 0;
88  $uppStatus = \Status::newGood();
89  $this->setMwGlobals( [
90  'wgPasswordPolicy' => [
91  'policies' => [
92  'default' => [
93  'Check' => true,
94  ],
95  ],
96  'checks' => [
97  'Check' => function () use ( &$uppCalled, &$uppStatus ) {
98  $uppCalled++;
99  return $uppStatus;
100  },
101  ],
102  ]
103  ] );
104 
105  $provider = $this->getMockForAbstractClass(
107  );
108  $provider->setConfig( \ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
109  $provider->setLogger( new \Psr\Log\NullLogger() );
110  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
111 
112  $this->assertEquals( $uppStatus, $providerPriv->checkPasswordValidity( 'foo', 'bar' ) );
113 
114  $uppStatus->fatal( 'arbitrary-warning' );
115  $this->assertEquals( $uppStatus, $providerPriv->checkPasswordValidity( 'foo', 'bar' ) );
116  }
117 
118  public function testSetPasswordResetFlag() {
119  $config = new \HashConfig( [
120  'InvalidPasswordReset' => true,
121  ] );
122 
123  $manager = new AuthManager(
124  new \FauxRequest(), \ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
125  );
126 
127  $provider = $this->getMockForAbstractClass(
129  );
130  $provider->setConfig( $config );
131  $provider->setLogger( new \Psr\Log\NullLogger() );
132  $provider->setManager( $manager );
133  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
134 
135  $manager->removeAuthenticationSessionData( null );
137  $providerPriv->setPasswordResetFlag( 'Foo', $status );
138  $this->assertNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
139 
140  $manager->removeAuthenticationSessionData( null );
142  $status->error( 'testing' );
143  $providerPriv->setPasswordResetFlag( 'Foo', $status );
144  $ret = $manager->getAuthenticationSessionData( 'reset-pass' );
145  $this->assertNotNull( $ret );
146  $this->assertSame( 'resetpass-validity-soft', $ret->msg->getKey() );
147  $this->assertFalse( $ret->hard );
148 
149  $config->set( 'InvalidPasswordReset', false );
150  $manager->removeAuthenticationSessionData( null );
151  $providerPriv->setPasswordResetFlag( 'Foo', $status );
152  $ret = $manager->getAuthenticationSessionData( 'reset-pass' );
153  $this->assertNull( $ret );
154  }
155 
156  public function testFailResponse() {
157  $provider = $this->getMockForAbstractClass(
159  [ [ 'authoritative' => false ] ]
160  );
161  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
162 
164 
165  $ret = $providerPriv->failResponse( $req );
166  $this->assertSame( AuthenticationResponse::ABSTAIN, $ret->status );
167 
168  $provider = $this->getMockForAbstractClass(
170  [ [ 'authoritative' => true ] ]
171  );
172  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
173 
174  $req->password = '';
175  $ret = $providerPriv->failResponse( $req );
176  $this->assertSame( AuthenticationResponse::FAIL, $ret->status );
177  $this->assertSame( 'wrongpasswordempty', $ret->message->getKey() );
178 
179  $req->password = 'X';
180  $ret = $providerPriv->failResponse( $req );
181  $this->assertSame( AuthenticationResponse::FAIL, $ret->status );
182  $this->assertSame( 'wrongpassword', $ret->message->getKey() );
183  }
184 
191  $provider = $this->getMockForAbstractClass(
193  );
194 
195  $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
196  }
197 
198  public static function provideGetAuthenticationRequests() {
199  return [
202  [ AuthManager::ACTION_LINK, [] ],
205  ];
206  }
207 
211  $req->username = 'foo';
212  $req->password = null;
213 
214  $provider = $this->getMockForAbstractClass(
216  );
217  $provider->expects( $this->once() )
218  ->method( 'providerChangeAuthenticationData' )
219  ->with( $this->equalTo( $req ) );
220 
221  $provider->providerRevokeAccessForUser( 'foo' );
222  }
223 
224 }
Config $config
Definition: MediaWiki.php:38
mergeMwGlobalArrayValue($name, $values)
Merges the given values into a MW global array variable.
const ABSTAIN
Indicates that the authentication provider does not handle this request.
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:1936
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
this hook is for auditing only $response
Definition: hooks.txt:802
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
const FAIL
Indicates that the authentication failed.
AuthManager MediaWiki\Auth\AbstractPasswordPrimaryAuthenticationProvider.
const ACTION_CHANGE
Change a user's credentials.
Definition: AuthManager.php:98
static newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
This serves as the entry point to the authentication system.
Definition: AuthManager.php:81
Provides a fallback sequence for Config objects.
Definition: MultiConfig.php:28
const ACTION_LINK
Link an existing user to a third-party account.
Definition: AuthManager.php:93
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 local account $user
Definition: hooks.txt:242
This is a value object for authentication requests with a username and password.
String $action
Cache what action this request is.
Definition: MediaWiki.php:43
static getDefaultInstance()
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
this hook is for auditing only $req
Definition: hooks.txt:1007
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
const ACTION_REMOVE
Remove a user's credentials.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1046
const ACTION_CREATE
Create a new user.
Definition: AuthManager.php:88
static newFromObject($object)
Return the same object, without access restrictions.
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
Definition: AuthManager.php:83
setMwGlobals($pairs, $value=null)