Skip to content

Commit

Permalink
uml-3080 caching one login (#2366)
Browse files Browse the repository at this point in the history
* implemented caching

* fix linting error

* Add unit test coverage for IssuerBuilder setMetaDataProviderBuilder()

* fix linting and ignore code coverage for build method

---------

Co-authored-by: Mishkat Najam <[email protected]>
  • Loading branch information
Lbagg1 and MishNajam authored Oct 12, 2023
1 parent db36d37 commit b0a11f8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Service\Authentication;

use Facile\OpenIDClient\Issuer\IssuerBuilderInterface;
use Facile\OpenIDClient\Issuer\IssuerInterface;
use Facile\OpenIDClient\Issuer\Metadata\Provider\MetadataProviderBuilder;
use Facile\OpenIDClient\Issuer\IssuerBuilder as FacileIssuerBuilder;

class IssuerBuilder implements IssuerBuilderInterface
{
private FacileIssuerBuilder $issuerBuilder;

public function __construct()
{
$this->issuerBuilder = new FacileIssuerBuilder();
}

public function setMetadataProviderBuilder(?MetadataProviderBuilder $metadataProviderBuilder): self
{
$this->issuerBuilder->setMetadataProviderBuilder($metadataProviderBuilder);
return $this;
}

/**
* @codeCoverageIgnore
*/
public function build(string $resource): IssuerInterface
{
return $this->issuerBuilder->build($resource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,33 @@

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;

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');


Expand All @@ -33,7 +40,7 @@ public function createAuthorisationRequest(string $uiLocale): string
'token_endpoint_auth_method' => 'private_key_jwt',
'jwks' => [
'keys' => [
($this->JWKFactory)(),
($this->jwkFactory)(),
],
],
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace AppTest\Service\Authentication;

use App\Service\Authentication\IssuerBuilder;
use Facile\OpenIDClient\Issuer\Metadata\Provider\MetadataProviderBuilder;
use PHPUnit\Framework\TestCase;

class IssuerBuilderTest extends TestCase
{
private IssuerBuilder $issuerBuilder;

public function setUp(): void
{
$this->issuerBuilder = new IssuerBuilder();
}

/** @test */
public function can_set_metadata_provider_builder(): void
{
$issuerBuilder = $this->issuerBuilder->setMetadataProviderBuilder(new MetadataProviderBuilder());
self::assertInstanceOf(IssuerBuilder::class, $issuerBuilder);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
Expand Down

0 comments on commit b0a11f8

Please sign in to comment.