From 15d706e650e09b2c2fce2a334eaf55906180458e Mon Sep 17 00:00:00 2001 From: Lowenna Baggaley Date: Wed, 11 Oct 2023 17:06:05 +0100 Subject: [PATCH 1/4] implemented caching --- .../Service/Authentication/IssuerBuilder.php | 31 +++++++++++++++++++ .../OneLoginAuthorisationRequestService.php | 13 ++++++-- ...neLoginAuthorisationRequestServiceTest.php | 25 ++++++++++----- 3 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php diff --git a/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php b/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php new file mode 100644 index 0000000000..3c48a1d5a2 --- /dev/null +++ b/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php @@ -0,0 +1,31 @@ +issuerBuilder = new FacileIssuerBuilder(); + } + + public function setMetadataProviderBuilder(?MetadataProviderBuilder $metadataProviderBuilder): self + { + $this->issuerBuilder->setMetadataProviderBuilder($metadataProviderBuilder); + return $this; + } + + public function build(string $resource): IssuerInterface + { + return $this->issuerBuilder->build($resource); + } +} diff --git a/service-api/app/src/App/src/Service/Authentication/OneLoginAuthorisationRequestService.php b/service-api/app/src/App/src/Service/Authentication/OneLoginAuthorisationRequestService.php index a470462491..fa0cfc4180 100644 --- a/service-api/app/src/App/src/Service/Authentication/OneLoginAuthorisationRequestService.php +++ b/service-api/app/src/App/src/Service/Authentication/OneLoginAuthorisationRequestService.php @@ -4,9 +4,11 @@ namespace App\Service\Authentication; +use App\Service\Cache\CacheFactory; use Facile\OpenIDClient\Client\ClientBuilder; use Facile\OpenIDClient\Client\Metadata\ClientMetadata; use Facile\OpenIDClient\Issuer\IssuerBuilderInterface; +use Facile\OpenIDClient\Issuer\Metadata\Provider\MetadataProviderBuilder; use Facile\OpenIDClient\Service\Builder\AuthorizationServiceBuilder; use function Facile\OpenIDClient\base64url_encode; @@ -14,16 +16,21 @@ class OneLoginAuthorisationRequestService { public function __construct( - private JWKFactory $JWKFactory, + private JWKFactory $jwkFactory, private IssuerBuilderInterface $issuerBuilder, + private CacheFactory $cacheFactory ) { } public function createAuthorisationRequest(string $uiLocale): string { - //TODO UML-3080 Configure cache + + $cachedBuilder = new MetadataProviderBuilder(); + $cachedBuilder->setCache(($this->cacheFactory)('cache')) + ->setCacheTtl(3600); $issuer = $this->issuerBuilder + ->setMetadataProviderBuilder($cachedBuilder) ->build('http://mock-one-login:8080/.well-known/openid-configuration'); @@ -33,7 +40,7 @@ public function createAuthorisationRequest(string $uiLocale): string 'token_endpoint_auth_method' => 'private_key_jwt', 'jwks' => [ 'keys' => [ - ($this->JWKFactory)(), + ($this->jwkFactory)(), ], ], ]); diff --git a/service-api/app/test/AppTest/Service/Authentication/OneLoginAuthorisationRequestServiceTest.php b/service-api/app/test/AppTest/Service/Authentication/OneLoginAuthorisationRequestServiceTest.php index 6ff1501f7d..651bbcb22d 100644 --- a/service-api/app/test/AppTest/Service/Authentication/OneLoginAuthorisationRequestServiceTest.php +++ b/service-api/app/test/AppTest/Service/Authentication/OneLoginAuthorisationRequestServiceTest.php @@ -6,34 +6,44 @@ use App\Service\Authentication\OneLoginAuthorisationRequestService; use App\Service\Authentication\JWKFactory; -use Facile\OpenIDClient\Issuer\IssuerBuilder; +use App\Service\Cache\CacheFactory; +use App\Service\Authentication\IssuerBuilder; use Facile\OpenIDClient\Issuer\IssuerBuilderInterface; use Facile\OpenIDClient\Issuer\IssuerInterface; use Facile\OpenIDClient\Issuer\Metadata\IssuerMetadataInterface; +use Facile\OpenIDClient\Issuer\Metadata\Provider\MetadataProviderBuilder; +use Interop\Container\Containerinterface; use Jose\Component\Core\JWK; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; +use Psr\SimpleCache\CacheInterface; class OneLoginAuthorisationRequestServiceTest extends TestCase { use ProphecyTrait; - private ObjectProphecy|JWKFactory $JWKFactory; + private ObjectProphecy|JWKFactory $jwkFactory; private ObjectProphecy|IssuerBuilder $issuerBuilder; + private ObjectProphecy|CacheFactory $cacheFactory; public function setup(): void { $jwk = $this->prophesize(JWK::class); - $this->JWKFactory = $this->prophesize(JWKFactory::class); - $this->issuerBuilder = $this->prophesize(IssuerBuilderInterface::class); + $this->jwkFactory = $this->prophesize(JWKFactory::class); + $this->issuerBuilder = $this->prophesize(IssuerBuilder::class); $issuer = $this->prophesize(IssuerInterface::class); $issuerMetaData = $this->prophesize(IssuerMetadataInterface::class); + $this->cacheFactory = $this->prophesize(CacheFactory::class); + $cacheInterface = $this->prophesize(CacheInterface::class); - $this->JWKFactory->__invoke()->willReturn($jwk); + $this->jwkFactory->__invoke()->willReturn($jwk); $issuer->getMetadata()->willReturn($issuerMetaData); $issuerMetaData->getAuthorizationEndpoint()->willReturn('fake endpoint'); + $this->issuerBuilder->setMetadataProviderBuilder(Argument::any())->willReturn($this->issuerBuilder); $this->issuerBuilder->build('http://mock-one-login:8080/.well-known/openid-configuration')->willReturn($issuer); + $this->cacheFactory->__invoke('cache')->willReturn($cacheInterface); } /** @@ -42,8 +52,9 @@ public function setup(): void public function create_authorisation_request(): void { $authorisationRequestService = new OneLoginAuthorisationRequestService( - $this->JWKFactory->reveal(), - $this->issuerBuilder->reveal() + $this->jwkFactory->reveal(), + $this->issuerBuilder->reveal(), + $this->cacheFactory->reveal(), ); $authorisationRequest = $authorisationRequestService->createAuthorisationRequest('en'); $this->assertStringContainsString('client_id=client-id', $authorisationRequest); From aa92096d95ad998dd67847f51d6ca6a52716eefe Mon Sep 17 00:00:00 2001 From: Lowenna Baggaley Date: Thu, 12 Oct 2023 09:21:27 +0100 Subject: [PATCH 2/4] fix linting error --- .../app/src/App/src/Service/Authentication/IssuerBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php b/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php index 3c48a1d5a2..8f19df8108 100644 --- a/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php +++ b/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php @@ -7,7 +7,7 @@ use Facile\OpenIDClient\Issuer\IssuerBuilderInterface; use Facile\OpenIDClient\Issuer\IssuerInterface; use Facile\OpenIDClient\Issuer\Metadata\Provider\MetadataProviderBuilder; -use \Facile\OpenIDClient\Issuer\IssuerBuilder as FacileIssuerBuilder; +use Facile\OpenIDClient\Issuer\IssuerBuilder as FacileIssuerBuilder; class IssuerBuilder implements IssuerBuilderInterface { @@ -28,4 +28,4 @@ public function build(string $resource): IssuerInterface { return $this->issuerBuilder->build($resource); } -} +} \ No newline at end of file From ca62110e24b731579d84d43ed86b3acaeaaf11c3 Mon Sep 17 00:00:00 2001 From: Mishkat Najam Date: Thu, 12 Oct 2023 14:24:32 +0100 Subject: [PATCH 3/4] Add unit test coverage for IssuerBuilder setMetaDataProviderBuilder() --- .../Service/Authentication/IssuerBuilder.php | 2 +- .../Authentication/IssuerBuilderTest.php | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 service-api/app/test/AppTest/Service/Authentication/IssuerBuilderTest.php diff --git a/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php b/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php index 8f19df8108..e92a261623 100644 --- a/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php +++ b/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php @@ -28,4 +28,4 @@ public function build(string $resource): IssuerInterface { return $this->issuerBuilder->build($resource); } -} \ No newline at end of file +} diff --git a/service-api/app/test/AppTest/Service/Authentication/IssuerBuilderTest.php b/service-api/app/test/AppTest/Service/Authentication/IssuerBuilderTest.php new file mode 100644 index 0000000000..192825afe6 --- /dev/null +++ b/service-api/app/test/AppTest/Service/Authentication/IssuerBuilderTest.php @@ -0,0 +1,25 @@ +issuerBuilder = new IssuerBuilder(); + } + + /** @test */ + public function can_set_metadata_provider_builder(): void + { + $issuerBuilder = $this->issuerBuilder->setMetadataProviderBuilder(new MetadataProviderBuilder()); + self::assertInstanceOf(IssuerBuilder::class, $issuerBuilder); + } +} \ No newline at end of file From 48115ba89fea50c58a8ffadaeca2b80da4e910ff Mon Sep 17 00:00:00 2001 From: Mishkat Najam Date: Thu, 12 Oct 2023 14:36:46 +0100 Subject: [PATCH 4/4] fix linting and ignore code coverage for build method --- .../app/src/App/src/Service/Authentication/IssuerBuilder.php | 3 +++ .../test/AppTest/Service/Authentication/IssuerBuilderTest.php | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php b/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php index e92a261623..5e74f20c63 100644 --- a/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php +++ b/service-api/app/src/App/src/Service/Authentication/IssuerBuilder.php @@ -24,6 +24,9 @@ public function setMetadataProviderBuilder(?MetadataProviderBuilder $metadataPro return $this; } + /** + * @codeCoverageIgnore + */ public function build(string $resource): IssuerInterface { return $this->issuerBuilder->build($resource); diff --git a/service-api/app/test/AppTest/Service/Authentication/IssuerBuilderTest.php b/service-api/app/test/AppTest/Service/Authentication/IssuerBuilderTest.php index 192825afe6..cbd2ccd7f1 100644 --- a/service-api/app/test/AppTest/Service/Authentication/IssuerBuilderTest.php +++ b/service-api/app/test/AppTest/Service/Authentication/IssuerBuilderTest.php @@ -11,6 +11,7 @@ class IssuerBuilderTest extends TestCase { private IssuerBuilder $issuerBuilder; + public function setUp(): void { $this->issuerBuilder = new IssuerBuilder(); @@ -22,4 +23,4 @@ public function can_set_metadata_provider_builder(): void $issuerBuilder = $this->issuerBuilder->setMetadataProviderBuilder(new MetadataProviderBuilder()); self::assertInstanceOf(IssuerBuilder::class, $issuerBuilder); } -} \ No newline at end of file +}