Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UML-3056 initial code and working test #2360

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
f69f404
UML-3056 initial code and working test
MishNajam Oct 4, 2023
db49c38
UML-3056 fix php linting
MishNajam Oct 4, 2023
002ce7e
Merge branch 'main' into UML-3056-implement-service-api-endpoint-to-h…
MishNajam Oct 4, 2023
13e66cc
get mock onelogin from ecr and spin that up locally and in CI
nickdavis2001 Oct 4, 2023
059d423
comment our test for now
nickdavis2001 Oct 4, 2023
1be79d2
tell docker where image is
nickdavis2001 Oct 4, 2023
920a0c5
move ecr login earlier as its needed to pull down mock login container
nickdavis2001 Oct 4, 2023
ee72d44
use variable for registry
nickdavis2001 Oct 4, 2023
feb16f3
add docker logs
nickdavis2001 Oct 4, 2023
9f2202e
force a rebuild
nickdavis2001 Oct 4, 2023
573b863
docker inspect to get ip addr of mock
nickdavis2001 Oct 4, 2023
855ff53
try explicit port
nickdavis2001 Oct 4, 2023
86de2d9
ecr login
nickdavis2001 Oct 5, 2023
86bbc86
add jwks to authService and test, and check ui_locale set to en or cy
MishNajam Oct 5, 2023
5cec7df
Test setting test as acceptance test
MishNajam Oct 5, 2023
4d8a2a6
Test setting test as integration test
MishNajam Oct 5, 2023
4c7d815
Fix linting error
MishNajam Oct 5, 2023
2469e34
Confirm test runs in pipeline
MishNajam Oct 5, 2023
7935932
revert to unit test
MishNajam Oct 5, 2023
8027ab4
Turn AuthenticationServiceTest into unit test
MishNajam Oct 5, 2023
62db8b7
Update catch Exception type
MishNajam Oct 10, 2023
65dab71
rename classes, methods and test names
MishNajam Oct 10, 2023
498af55
amend handler name
MishNajam Oct 10, 2023
ba00d22
Remove try catch in createAuthorisationRequest
MishNajam Oct 10, 2023
a2d6e5d
Merge branch 'main' into UML-3056-implement-service-api-endpoint-to-h…
MishNajam Oct 10, 2023
91c037a
Fix lint error
MishNajam Oct 10, 2023
94f1772
Merge branch 'main' into UML-3056-implement-service-api-endpoint-to-h…
MishNajam Oct 10, 2023
51f1958
Merge branch 'main' into UML-3056-implement-service-api-endpoint-to-h…
Lbagg1 Oct 11, 2023
c074f90
Merge branch 'main' into UML-3056-implement-service-api-endpoint-to-h…
Lbagg1 Oct 11, 2023
709c994
remove superfluous comments and move redirect_uri from the client met…
MishNajam Oct 11, 2023
f93f7ba
Merge branch 'main' into UML-3056-implement-service-api-endpoint-to-h…
nickdavis2001 Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions service-api/app/src/App/src/Handler/AuthRedirectHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Handler;

use App\Exception\BadRequestException;
use App\Service\Authentication\AuthenticationService;
use Exception;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
* Class AuthRedirectHandler
*
* @package App\Handler
* @codeCoverageIgnore
*/
class AuthRedirectHandler implements RequestHandlerInterface
{
public function __construct(
private AuthenticationService $authenticationService,
) {
}

/**
* Handles a request and produces a response.
*
* May call other collaborating code to generate the response.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
* @throws Exception
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
$params = $request->getParsedBody();

if (empty($params['ui_locale'])) {
throw new BadRequestException('Ui locale must be provided');
}

$authorisationUri = $this->authenticationService->redirect($params['ui_locale']);
MishNajam marked this conversation as resolved.
Show resolved Hide resolved

return new JsonResponse($authorisationUri);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace App\Service\Authentication;

use function Facile\OpenIDClient\base64url_encode;
use Facile\OpenIDClient\Client\ClientBuilder;
use Facile\OpenIDClient\Client\Metadata\ClientMetadata;
use Facile\OpenIDClient\Issuer\IssuerBuilder;
use Facile\OpenIDClient\Service\Builder\AuthorizationServiceBuilder;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;

class AuthenticationService
{
public function __construct(private JWKFactory $JWKFactory, private LoggerInterface $logger)
{
}

public function redirect(string $uiLocale): string
{
//TODO UML-3080 Configure cache

$issuer = (new IssuerBuilder())
->build('http://mock-one-login:8080/.well-known/openid-configuration');


// $key = $this->JWKFactory->invoke();

$clientMetadata = ClientMetadata::fromArray([
'client_id' => 'client-id',
'client_secret' => 'my-client-secret',
'token_endpoint_auth_method' => 'private_key_jwt',
'redirect_uri' => '/lpa/dashboard',
// 'jwks' => [
// 'keys' => [
//// $key,
// ],
// ],
]);

$client = (new ClientBuilder())
->setIssuer($issuer)
->setClientMetadata($clientMetadata)
->build();

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

$redirectAuthorisationUri = '';
try {
$redirectAuthorisationUri = $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 (InvalidArgumentException $e) {
$this->logger->error('Unable to get authorisation uri: ' . $e->getMessage());
throw $e;
}

return $redirectAuthorisationUri;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace AppTest\Service\Authentication;

use App\Service\Authentication\AuthenticationService;
use App\Service\Authentication\JWKFactory;
use App\Service\Authentication\KeyPairManager;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;

class AuthenticationServiceTest extends TestCase
{
use ProphecyTrait;

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

public function setup(): void
{
$this->keyPairManager = $this->prophesize(KeyPairManager::class);
$this->JWKFactory = new JWKFactory($this->keyPairManager->reveal());
$this->logger = $this->prophesize(LoggerInterface::class);
}

/**
* @test
*/
public function getRedirectUri(): void
{
$authenticationService = new AuthenticationService($this->JWKFactory, $this->logger->reveal());
$redirectUri = $authenticationService->redirect('en');
$this->assertStringContainsString('client_id=client-id', $redirectUri);
$this->assertStringContainsString('scope=openid+email', $redirectUri);
$this->assertStringContainsString('vtr=%5B%22Cl.Cm.P2%22%5D', $redirectUri);
}
}