@@ -389,6 +389,65 @@ public function testCanFindByIdFromCache(): void
389389 $ this ->assertInstanceOf (ClientEntityInterface::class, $ sut ->findById ('clientid ' ));
390390 }
391391
392+ /**
393+ * The protocol endpoints cache every client they look up, with no owner. A user managing their own clients
394+ * (the `client` permission) must not be given such a client by its ID when it is not theirs.
395+ */
396+ public function testACachedClientIsNotReturnedToALookupForAnotherOwner (): void
397+ {
398+ $ client = self ::getClient ('clientid ' , owner: 'otherUser ' );
399+ $ this ->repository ->add ($ client );
400+ $ this ->clientEntityFactoryMock ->method ('fromState ' )->willReturn ($ client );
401+
402+ $ sut = $ this ->repositoryWithCacheHolding (['state ' ]);
403+
404+ $ this ->assertNull ($ sut ->findById ('clientid ' , 'homer@example.com ' ));
405+ $ this ->assertSame ($ client , $ sut ->findById ('clientid ' , 'otherUser ' ));
406+ $ this ->assertSame ($ client , $ sut ->findById ('clientid ' ));
407+ }
408+
409+ public function testACachedClientIsNotReturnedToALookupByEntityIdentifierForAnotherOwner (): void
410+ {
411+ $ client = self ::getClient ('clientid ' , owner: 'otherUser ' , entityId: 'entityId ' );
412+ $ this ->repository ->add ($ client );
413+ $ this ->clientEntityFactoryMock ->method ('fromState ' )->willReturn ($ client );
414+
415+ $ sut = $ this ->repositoryWithCacheHolding (['state ' ]);
416+
417+ $ this ->assertNull ($ sut ->findByEntityIdentifier ('entityId ' , 'homer@example.com ' ));
418+ $ this ->assertSame ($ client , $ sut ->findByEntityIdentifier ('entityId ' , 'otherUser ' ));
419+ $ this ->assertSame ($ client , $ sut ->findByEntityIdentifier ('entityId ' ));
420+ }
421+
422+ /**
423+ * A cached client with another owner is looked up in the database, which has the owner the client has now.
424+ */
425+ public function testTheDatabaseAnswersForAnOwnerTheCachedClientDoesNotHave (): void
426+ {
427+ $ staleClient = self ::getClient ('clientid ' , owner: 'otherUser ' );
428+ $ currentClient = self ::getClient ('clientid ' , owner: 'homer@example.com ' );
429+ $ this ->repository ->add ($ currentClient );
430+ $ this ->clientEntityFactoryMock ->expects ($ this ->exactly (2 ))->method ('fromState ' )
431+ ->willReturnOnConsecutiveCalls ($ staleClient , $ currentClient );
432+
433+ $ sut = $ this ->repositoryWithCacheHolding (['state ' ]);
434+
435+ $ this ->assertSame ($ currentClient , $ sut ->findById ('clientid ' , 'homer@example.com ' ));
436+ }
437+
438+ protected function repositoryWithCacheHolding (array $ state ): ClientRepository
439+ {
440+ $ protocolCacheMock = $ this ->createMock (ProtocolCache::class);
441+ $ protocolCacheMock ->method ('get ' )->willReturn ($ state );
442+
443+ return new ClientRepository (
444+ new ModuleConfig (),
445+ Database ::getInstance (),
446+ $ protocolCacheMock ,
447+ $ this ->clientEntityFactoryMock ,
448+ );
449+ }
450+
392451 public function testCanFindByEntityIdentifier (): void
393452 {
394453 $ client = self ::getClient (id: 'clientId ' , entityId: 'entityId ' );
@@ -444,6 +503,27 @@ public function testCanFindAllFederated(): void
444503 $ this ->assertCount (1 , $ this ->repository ->findAllFederated ());
445504 }
446505
506+ /**
507+ * Their queries have a line break after WHERE, so the owner filter has to be added to them with AND.
508+ */
509+ public function testTheQueriesWrittenOverSeveralLinesTakeAnOwner (): void
510+ {
511+ $ client = self ::getClient (
512+ id: 'clientId ' ,
513+ owner: 'otherUser ' ,
514+ entityId: 'entityId ' ,
515+ isFederated: true ,
516+ federationJwks: [],
517+ );
518+ $ this ->repository ->add ($ client );
519+ $ this ->clientEntityFactoryMock ->method ('fromState ' )->willReturn ($ client );
520+
521+ $ this ->assertSame ($ client , $ this ->repository ->findByEntityIdentifier ('entityId ' , 'otherUser ' ));
522+ $ this ->assertNull ($ this ->repository ->findByEntityIdentifier ('entityId ' , 'homer@example.com ' ));
523+ $ this ->assertCount (1 , $ this ->repository ->findAllFederated ('otherUser ' ));
524+ $ this ->assertCount (0 , $ this ->repository ->findAllFederated ('homer@example.com ' ));
525+ }
526+
447527 public function testCanFindByEntityIdFromCache (): void
448528 {
449529 $ protocolCacheMock = $ this ->createMock (ProtocolCache::class);
0 commit comments