Skip to content

Commit

Permalink
Remove try catch in createAuthorisationRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
MishNajam committed Oct 10, 2023
1 parent 498af55 commit ba00d22
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@

namespace App\Service\Authentication;

use Exception;
use Facile\OpenIDClient\Client\ClientBuilder;
use Facile\OpenIDClient\Client\Metadata\ClientMetadata;
use Facile\OpenIDClient\Issuer\IssuerBuilderInterface;
use Facile\OpenIDClient\Service\Builder\AuthorizationServiceBuilder;
use Psr\Log\LoggerInterface;

use RuntimeException;

use function Facile\OpenIDClient\base64url_encode;

class OneLoginAuthorisationRequestService
{
public function __construct(
private JWKFactory $JWKFactory,
private LoggerInterface $logger,
private IssuerBuilderInterface $issuerBuilder,
) {
}
Expand Down Expand Up @@ -51,24 +46,16 @@ public function createAuthorisationRequest(string $uiLocale): string

$authorisationService = (new AuthorizationServiceBuilder())->build();

$authorisationRequest = '';
try {
$authorisationRequest = $authorisationService->getAuthorizationUri(
$client,
[
'scope' => 'openid email',
'state' => base64url_encode(random_bytes(12)),
'nonce' => openssl_digest(base64url_encode(random_bytes(12)), 'sha256'),
'vtr' => '["Cl.Cm.P2"]',
'ui_locales' => $uiLocale,
'claims' => '{"userinfo":{"https://vocab.account.gov.uk/v1/coreIdentityJWT": null}}',
]
);
} catch (Exception $e) {
$this->logger->error('Unable to get authorisation uri: ' . $e->getMessage());
throw new RuntimeException('Could not create authorisation uri');
}

return $authorisationRequest;
return $authorisationService->getAuthorizationUri(
$client,
[
'scope' => 'openid email',
'state' => base64url_encode(random_bytes(12)),
'nonce' => openssl_digest(base64url_encode(random_bytes(12)), 'sha256'),
'vtr' => '["Cl.Cm.P2"]',
'ui_locales' => $uiLocale,
'claims' => '{"userinfo":{"https://vocab.account.gov.uk/v1/coreIdentityJWT": null}}',
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;

class OneLoginAuthorisationRequestServiceTest extends TestCase
{
use ProphecyTrait;

private ObjectProphecy|JWKFactory $JWKFactory;
private ObjectProphecy|LoggerInterface $logger;
private ObjectProphecy|IssuerBuilder $issuerBuilder;

public function setup(): void
{
$jwk = $this->prophesize(JWK::class);
$this->JWKFactory = $this->prophesize(JWKFactory::class);
$this->logger = $this->prophesize(LoggerInterface::class);
$this->issuerBuilder = $this->prophesize(IssuerBuilderInterface::class);
$issuer = $this->prophesize(IssuerInterface::class);
$issuerMetaData = $this->prophesize(IssuerMetadataInterface::class);
Expand All @@ -46,7 +43,6 @@ public function create_authorisation_request(): void
{
$authorisationRequestService = new OneLoginAuthorisationRequestService(
$this->JWKFactory->reveal(),
$this->logger->reveal(),
$this->issuerBuilder->reveal()
);
$authorisationRequest = $authorisationRequestService->createAuthorisationRequest('en');
Expand All @@ -55,4 +51,4 @@ public function create_authorisation_request(): void
$this->assertStringContainsString('vtr=%5B%22Cl.Cm.P2%22%5D', $authorisationRequest);
$this->assertStringContainsString('ui_locales=en', $authorisationRequest);
}
}
}

0 comments on commit ba00d22

Please sign in to comment.