MediaWiki  master
AbstractPasswordPrimaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
10  protected function setUp() {
12 
13  parent::setUp();
14  if ( $wgDisableAuthManager ) {
15  $this->markTestSkipped( '$wgDisableAuthManager is set' );
16  }
17  }
18 
19  public function testConstructor() {
20  $provider = $this->getMockForAbstractClass(
22  );
23  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
24  $this->assertTrue( $providerPriv->authoritative );
25 
26  $provider = $this->getMockForAbstractClass(
28  [ [ 'authoritative' => false ] ]
29  );
30  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
31  $this->assertFalse( $providerPriv->authoritative );
32  }
33 
34  public function testGetPasswordFactory() {
35  $provider = $this->getMockForAbstractClass(
37  );
38  $provider->setConfig( \ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
39  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
40 
41  $obj = $providerPriv->getPasswordFactory();
42  $this->assertInstanceOf( 'PasswordFactory', $obj );
43  $this->assertSame( $obj, $providerPriv->getPasswordFactory() );
44  }
45 
46  public function testGetPassword() {
47  $provider = $this->getMockForAbstractClass(
49  );
50  $provider->setConfig( \ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
51  $provider->setLogger( new \Psr\Log\NullLogger() );
52  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
53 
54  $obj = $providerPriv->getPassword( null );
55  $this->assertInstanceOf( 'Password', $obj );
56 
57  $obj = $providerPriv->getPassword( 'invalid' );
58  $this->assertInstanceOf( 'Password', $obj );
59  }
60 
61  public function testGetNewPasswordExpiry() {
62  $config = new \HashConfig;
63  $provider = $this->getMockForAbstractClass(
65  );
66  $provider->setConfig( new \MultiConfig( [
67  $config,
68  \ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
69  ] ) );
70  $provider->setLogger( new \Psr\Log\NullLogger() );
71  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
72 
73  $this->mergeMwGlobalArrayValue( 'wgHooks', [ 'ResetPasswordExpiration' => [] ] );
74 
75  $config->set( 'PasswordExpirationDays', 0 );
76  $this->assertNull( $providerPriv->getNewPasswordExpiry( 'UTSysop' ) );
77 
78  $config->set( 'PasswordExpirationDays', 5 );
79  $this->assertEquals(
80  time() + 5 * 86400,
81  wfTimestamp( TS_UNIX, $providerPriv->getNewPasswordExpiry( 'UTSysop' ) ),
82  '',
83  2 /* Fuzz */
84  );
85 
86  $this->mergeMwGlobalArrayValue( 'wgHooks', [
87  'ResetPasswordExpiration' => [ function ( $user, &$expires ) {
88  $this->assertSame( 'UTSysop', $user->getName() );
89  $expires = '30001231235959';
90  } ]
91  ] );
92  $this->assertEquals( '30001231235959', $providerPriv->getNewPasswordExpiry( 'UTSysop' ) );
93  }
94 
95  public function testCheckPasswordValidity() {
96  $uppCalled = 0;
97  $uppStatus = \Status::newGood();
98  $this->setMwGlobals( [
99  'wgPasswordPolicy' => [
100  'policies' => [
101  'default' => [
102  'Check' => true,
103  ],
104  ],
105  'checks' => [
106  'Check' => function () use ( &$uppCalled, &$uppStatus ) {
107  $uppCalled++;
108  return $uppStatus;
109  },
110  ],
111  ]
112  ] );
113 
114  $provider = $this->getMockForAbstractClass(
116  );
117  $provider->setConfig( \ConfigFactory::getDefaultInstance()->makeConfig( 'main' ) );
118  $provider->setLogger( new \Psr\Log\NullLogger() );
119  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
120 
121  $this->assertEquals( $uppStatus, $providerPriv->checkPasswordValidity( 'foo', 'bar' ) );
122 
123  $uppStatus->fatal( 'arbitrary-warning' );
124  $this->assertEquals( $uppStatus, $providerPriv->checkPasswordValidity( 'foo', 'bar' ) );
125  }
126 
127  public function testSetPasswordResetFlag() {
128  $config = new \HashConfig( [
129  'InvalidPasswordReset' => true,
130  ] );
131 
132  $manager = new AuthManager(
133  new \FauxRequest(), \ConfigFactory::getDefaultInstance()->makeConfig( 'main' )
134  );
135 
136  $provider = $this->getMockForAbstractClass(
138  );
139  $provider->setConfig( $config );
140  $provider->setLogger( new \Psr\Log\NullLogger() );
141  $provider->setManager( $manager );
142  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
143 
144  $manager->removeAuthenticationSessionData( null );
146  $providerPriv->setPasswordResetFlag( 'Foo', $status );
147  $this->assertNull( $manager->getAuthenticationSessionData( 'reset-pass' ) );
148 
149  $manager->removeAuthenticationSessionData( null );
151  $status->error( 'testing' );
152  $providerPriv->setPasswordResetFlag( 'Foo', $status );
153  $ret = $manager->getAuthenticationSessionData( 'reset-pass' );
154  $this->assertNotNull( $ret );
155  $this->assertSame( 'resetpass-validity-soft', $ret->msg->getKey() );
156  $this->assertFalse( $ret->hard );
157 
158  $config->set( 'InvalidPasswordReset', false );
159  $manager->removeAuthenticationSessionData( null );
160  $providerPriv->setPasswordResetFlag( 'Foo', $status );
161  $ret = $manager->getAuthenticationSessionData( 'reset-pass' );
162  $this->assertNull( $ret );
163  }
164 
165  public function testFailResponse() {
166  $provider = $this->getMockForAbstractClass(
168  [ [ 'authoritative' => false ] ]
169  );
170  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
171 
173 
174  $ret = $providerPriv->failResponse( $req );
175  $this->assertSame( AuthenticationResponse::ABSTAIN, $ret->status );
176 
177  $provider = $this->getMockForAbstractClass(
179  [ [ 'authoritative' => true ] ]
180  );
181  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
182 
183  $req->password = '';
184  $ret = $providerPriv->failResponse( $req );
185  $this->assertSame( AuthenticationResponse::FAIL, $ret->status );
186  $this->assertSame( 'wrongpasswordempty', $ret->message->getKey() );
187 
188  $req->password = 'X';
189  $ret = $providerPriv->failResponse( $req );
190  $this->assertSame( AuthenticationResponse::FAIL, $ret->status );
191  $this->assertSame( 'wrongpassword', $ret->message->getKey() );
192  }
193 
200  $provider = $this->getMockForAbstractClass(
202  );
203 
204  $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
205  }
206 
207  public static function provideGetAuthenticationRequests() {
208  return [
211  [ AuthManager::ACTION_LINK, [] ],
214  ];
215  }
216 
220  $req->username = 'foo';
221  $req->password = null;
222 
223  $provider = $this->getMockForAbstractClass(
225  );
226  $provider->expects( $this->once() )
227  ->method( 'providerChangeAuthenticationData' )
228  ->with( $this->equalTo( $req ) );
229 
230  $provider->providerRevokeAccessForUser( 'foo' );
231  }
232 
233 }
Config $config
Definition: MediaWiki.php:37
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.
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:776
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
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:60
This serves as the entry point to the authentication system.
Definition: AuthManager.php:43
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:1816
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:55
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:42
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:981
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
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
const ACTION_REMOVE
Remove a user's credentials.
Definition: AuthManager.php:62
$wgDisableAuthManager
Disable AuthManager.
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:1020
const ACTION_CREATE
Create a new user.
Definition: AuthManager.php:50
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
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:45
setMwGlobals($pairs, $value=null)
static newGood($value=null)
Factory function for good results.
Definition: Status.php:101