3 namespace MediaWiki\Tests\Session
;
5 use InvalidArgumentException
;
6 use MediaWiki\Config\HashConfig
;
7 use MediaWiki\Config\MultiConfig
;
8 use MediaWiki\MainConfigNames
;
9 use MediaWiki\Request\FauxRequest
;
10 use MediaWiki\Session\BotPasswordSessionProvider
;
11 use MediaWiki\Session\Session
;
12 use MediaWiki\Session\SessionInfo
;
13 use MediaWiki\Session\SessionManager
;
14 use MediaWiki\Session\UserInfo
;
15 use MediaWiki\User\BotPassword
;
16 use MediaWikiIntegrationTestCase
;
18 use Psr\Log\NullLogger
;
20 use Wikimedia\TestingAccessWrapper
;
25 * @covers \MediaWiki\Session\BotPasswordSessionProvider
27 class BotPasswordSessionProviderTest
extends MediaWikiIntegrationTestCase
{
28 use SessionProviderTestTrait
;
30 /** @var HashConfig */
35 private function getProvider( $name = null, $prefix = null, $isApiRequest = true ) {
36 global $wgSessionProviders;
40 'sessionCookieName' => $name,
41 'sessionCookieOptions' => [],
42 'isApiRequest' => $isApiRequest,
44 if ( $prefix !== null ) {
45 $params['sessionCookieOptions']['prefix'] = $prefix;
48 $configHash = json_encode( [ $name, $prefix, $isApiRequest ] );
49 if ( !$this->config ||
$this->configHash
!== $configHash ) {
50 $this->config
= new HashConfig( [
51 MainConfigNames
::CookiePrefix
=> 'wgCookiePrefix',
52 MainConfigNames
::EnableBotPasswords
=> true,
53 MainConfigNames
::SessionProviders
=> $wgSessionProviders +
[
54 BotPasswordSessionProvider
::class => [
55 'class' => BotPasswordSessionProvider
::class,
56 'args' => [ $params ],
57 'services' => [ 'GrantsInfo' ],
61 $this->configHash
= $configHash;
63 $manager = new SessionManager( [
64 'config' => new MultiConfig( [ $this->config
, $this->getServiceContainer()->getMainConfig() ] ),
65 'logger' => new NullLogger
,
66 'store' => new TestBagOStuff
,
69 return $manager->getProvider( BotPasswordSessionProvider
::class );
72 protected function setUp(): void
{
75 $this->overrideConfigValues( [
76 MainConfigNames
::EnableBotPasswords
=> true,
77 MainConfigNames
::CentralIdLookupProvider
=> 'local',
78 MainConfigNames
::GrantPermissions
=> [
79 'test' => [ 'read' => true ],
84 public function addDBDataOnce() {
85 $passwordFactory = $this->getServiceContainer()->getPasswordFactory();
86 $passwordHash = $passwordFactory->newFromPlaintext( 'foobaz' );
88 $sysop = static::getTestSysop()->getUser();
89 $userId = $this->getServiceContainer()
90 ->getCentralIdLookupFactory()
91 ->getLookup( 'local' )
92 ->centralIdFromName( $sysop->getName() );
94 $dbw = $this->getDb();
95 $dbw->newDeleteQueryBuilder()
96 ->deleteFrom( 'bot_passwords' )
97 ->where( [ 'bp_user' => $userId, 'bp_app_id' => 'BotPasswordSessionProvider' ] )
98 ->caller( __METHOD__
)->execute();
99 $dbw->newInsertQueryBuilder()
100 ->insertInto( 'bot_passwords' )
102 'bp_user' => $userId,
103 'bp_app_id' => 'BotPasswordSessionProvider',
104 'bp_password' => $passwordHash->toString(),
105 'bp_token' => 'token!',
106 'bp_restrictions' => '{"IPAddresses":["127.0.0.0/8"]}',
107 'bp_grants' => '["test"]',
109 ->caller( __METHOD__
)
113 public function testConstructor() {
114 $grantsInfo = $this->getServiceContainer()->getGrantsInfo();
117 $provider = new BotPasswordSessionProvider( $grantsInfo );
118 $this->fail( 'Expected exception not thrown' );
119 } catch ( InvalidArgumentException
$ex ) {
121 'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: priority must be specified',
127 $provider = new BotPasswordSessionProvider(
130 'priority' => SessionInfo
::MIN_PRIORITY
- 1
133 $this->fail( 'Expected exception not thrown' );
134 } catch ( InvalidArgumentException
$ex ) {
136 'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: Invalid priority',
142 $provider = new BotPasswordSessionProvider(
145 'priority' => SessionInfo
::MAX_PRIORITY +
1
148 $this->fail( 'Expected exception not thrown' );
149 } catch ( InvalidArgumentException
$ex ) {
151 'MediaWiki\\Session\\BotPasswordSessionProvider::__construct: Invalid priority',
156 $provider = new BotPasswordSessionProvider(
160 $priv = TestingAccessWrapper
::newFromObject( $provider );
161 $this->assertSame( 40, $priv->priority
);
162 $this->assertSame( '_BPsession', $priv->sessionCookieName
);
163 $this->assertSame( [], $priv->sessionCookieOptions
);
165 $provider = new BotPasswordSessionProvider(
169 'sessionCookieName' => null,
172 $priv = TestingAccessWrapper
::newFromObject( $provider );
173 $this->assertSame( '_BPsession', $priv->sessionCookieName
);
175 $provider = new BotPasswordSessionProvider(
179 'sessionCookieName' => 'Foo',
180 'sessionCookieOptions' => [ 'Bar' ],
183 $priv = TestingAccessWrapper
::newFromObject( $provider );
184 $this->assertSame( 'Foo', $priv->sessionCookieName
);
185 $this->assertSame( [ 'Bar' ], $priv->sessionCookieOptions
);
188 public function testBasics() {
189 $provider = $this->getProvider();
191 $this->assertTrue( $provider->persistsSessionId() );
192 $this->assertFalse( $provider->canChangeUser() );
194 $this->assertNull( $provider->newSessionInfo() );
195 $this->assertNull( $provider->newSessionInfo( 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) );
198 public function testProvideSessionInfo() {
199 $request = new FauxRequest
;
200 $request->setCookie( '_BPsession', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'wgCookiePrefix' );
202 $provider = $this->getProvider( null, null, false );
203 $this->assertNull( $provider->provideSessionInfo( $request ) );
205 $provider = $this->getProvider();
207 $info = $provider->provideSessionInfo( $request );
208 $this->assertInstanceOf( SessionInfo
::class, $info );
209 $this->assertSame( 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', $info->getId() );
211 $this->config
->set( MainConfigNames
::EnableBotPasswords
, false );
212 $this->assertNull( $provider->provideSessionInfo( $request ) );
213 $this->config
->set( MainConfigNames
::EnableBotPasswords
, true );
215 $this->assertNull( $provider->provideSessionInfo( new FauxRequest
) );
218 public function testNewSessionInfoForRequest() {
219 $provider = $this->getProvider();
220 $user = static::getTestSysop()->getUser();
221 $request = $this->getMockBuilder( FauxRequest
::class )
222 ->onlyMethods( [ 'getIP' ] )->getMock();
223 $request->method( 'getIP' )
224 ->willReturn( '127.0.0.1' );
225 $bp = BotPassword
::newFromUser( $user, 'BotPasswordSessionProvider' );
227 $session = $provider->newSessionForRequest( $user, $bp, $request );
228 $this->assertInstanceOf( Session
::class, $session );
230 $this->assertEquals( $session->getId(), $request->getSession()->getId() );
231 $this->assertEquals( $user->getName(), $session->getUser()->getName() );
233 $this->assertEquals( [
234 'centralId' => $bp->getUserCentralId(),
235 'appId' => $bp->getAppId(),
236 'token' => $bp->getToken(),
237 'rights' => [ 'read' ],
238 'restrictions' => $bp->getRestrictions()->toJson(),
239 ], $session->getProviderMetadata() );
241 $this->assertEquals( [ 'read' ], $session->getAllowedUserRights() );
244 public function testCheckSessionInfo() {
245 $logger = new TestLogger( true );
246 $provider = $this->getProvider();
247 $this->initProvider( $provider, $logger );
249 $user = static::getTestSysop()->getUser();
250 $request = $this->getMockBuilder( FauxRequest
::class )
251 ->onlyMethods( [ 'getIP' ] )->getMock();
252 $request->method( 'getIP' )
253 ->willReturn( '127.0.0.1' );
254 $bp = BotPassword
::newFromUser( $user, 'BotPasswordSessionProvider' );
257 'provider' => $provider,
258 'id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
259 'userInfo' => UserInfo
::newFromUser( $user, true ),
260 'persisted' => false,
262 'centralId' => $bp->getUserCentralId(),
263 'appId' => $bp->getAppId(),
264 'token' => $bp->getToken(),
267 $dataMD = $data['metadata'];
269 foreach ( $data['metadata'] as $key => $_ ) {
270 $data['metadata'] = $dataMD;
271 unset( $data['metadata'][$key] );
272 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, $data );
273 $metadata = $info->getProviderMetadata();
275 $this->assertFalse( $provider->refreshSessionInfo( $info, $request, $metadata ) );
277 [ LogLevel
::INFO
, 'Session "{session}": Missing metadata: {missing}' ]
278 ], $logger->getBuffer() );
279 $logger->clearBuffer();
282 $data['metadata'] = $dataMD;
283 $data['metadata']['appId'] = 'Foobar';
284 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, $data );
285 $metadata = $info->getProviderMetadata();
286 $this->assertFalse( $provider->refreshSessionInfo( $info, $request, $metadata ) );
288 [ LogLevel
::INFO
, 'Session "{session}": No BotPassword for {centralId} {appId}' ],
289 ], $logger->getBuffer() );
290 $logger->clearBuffer();
292 $data['metadata'] = $dataMD;
293 $data['metadata']['token'] = 'Foobar';
294 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, $data );
295 $metadata = $info->getProviderMetadata();
296 $this->assertFalse( $provider->refreshSessionInfo( $info, $request, $metadata ) );
298 [ LogLevel
::INFO
, 'Session "{session}": BotPassword token check failed' ],
299 ], $logger->getBuffer() );
300 $logger->clearBuffer();
302 $request2 = $this->getMockBuilder( FauxRequest
::class )
303 ->onlyMethods( [ 'getIP' ] )->getMock();
304 $request2->method( 'getIP' )
305 ->willReturn( '10.0.0.1' );
306 $data['metadata'] = $dataMD;
307 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, $data );
308 $metadata = $info->getProviderMetadata();
309 $this->assertFalse( $provider->refreshSessionInfo( $info, $request2, $metadata ) );
311 [ LogLevel
::INFO
, 'Session "{session}": Restrictions check failed' ],
312 ], $logger->getBuffer() );
313 $logger->clearBuffer();
315 $info = new SessionInfo( SessionInfo
::MIN_PRIORITY
, $data );
316 $metadata = $info->getProviderMetadata();
317 $this->assertTrue( $provider->refreshSessionInfo( $info, $request, $metadata ) );
318 $this->assertSame( [], $logger->getBuffer() );
319 $this->assertEquals( $dataMD +
[ 'rights' => [ 'read' ] ], $metadata );
322 public function testGetAllowedUserRights() {
323 $logger = new TestLogger( true );
324 $provider = $this->getProvider();
325 $this->initProvider( $provider, $logger );
327 $backend = TestUtils
::getDummySessionBackend();
328 $backendPriv = TestingAccessWrapper
::newFromObject( $backend );
331 $provider->getAllowedUserRights( $backend );
332 $this->fail( 'Expected exception not thrown' );
333 } catch ( InvalidArgumentException
$ex ) {
334 $this->assertSame( 'Backend\'s provider isn\'t $this', $ex->getMessage() );
337 $backendPriv->provider
= $provider;
338 $backendPriv->providerMetadata
= [ 'rights' => [ 'foo', 'bar', 'baz' ] ];
339 $this->assertSame( [ 'foo', 'bar', 'baz' ], $provider->getAllowedUserRights( $backend ) );
340 $this->assertSame( [], $logger->getBuffer() );
342 $backendPriv->providerMetadata
= [ 'foo' => 'bar' ];
343 $this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
347 'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
348 'No provider metadata, returning no rights allowed'
350 ], $logger->getBuffer() );
351 $logger->clearBuffer();
353 $backendPriv->providerMetadata
= [ 'rights' => 'bar' ];
354 $this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
358 'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
359 'No provider metadata, returning no rights allowed'
361 ], $logger->getBuffer() );
362 $logger->clearBuffer();
364 $backendPriv->providerMetadata
= null;
365 $this->assertSame( [], $provider->getAllowedUserRights( $backend ) );
369 'MediaWiki\\Session\\BotPasswordSessionProvider::getAllowedUserRights: ' .
370 'No provider metadata, returning no rights allowed'
372 ], $logger->getBuffer() );
373 $logger->clearBuffer();