-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* UML-3056 initial code and working test * UML-3056 fix php linting * get mock onelogin from ecr and spin that up locally and in CI * tell docker where image is * move ecr login earlier as its needed to pull down mock login container * use variable for registry * add docker logs * force a rebuild * docker inspect to get ip addr of mock * ecr login * add jwks to authService and test, and check ui_locale set to en or cy * Turn AuthenticationServiceTest into unit test * Remove try catch in createAuthorisationRequest --------- Co-authored-by: Nick Davis <[email protected]> Co-authored-by: Lbagg1 <[email protected]> Co-authored-by: Nick Davis <[email protected]>
- Loading branch information
1 parent
d7694e9
commit b87582a
Showing
6 changed files
with
234 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,9 +146,19 @@ jobs: | |
matrix.run_unit_tests == 'true' && | ||
(inputs.specific_path == 'all' || inputs.specific_path == matrix.svc_prefix) | ||
- name: ecr login | ||
id: login_ecr | ||
uses: aws-actions/amazon-ecr-login@b5f9031d829ac39b7fd26092019aeca3ee6b3ebd # [email protected] | ||
with: | ||
registries: 311462405659 | ||
if: | | ||
inputs.push_to_ecr == 'true' && | ||
(inputs.specific_path == 'all' || inputs.specific_path == matrix.svc_prefix) | ||
- name: docker integration and acceptance tests | ||
env: | ||
SVC_PREFIX: ${{ matrix.svc_prefix }} | ||
ECR_REGISTRY: ${{ steps.login_ecr.outputs.registry }} | ||
run: | | ||
if [[ "${SVC_PREFIX}" = "api" ]]; then | ||
docker run -d --rm --name lpa-codes-pact-mock pactfoundation/pact-cli:latest mock-service -p "80" --host "0.0.0.0" \ | ||
|
@@ -157,10 +167,15 @@ jobs: | |
--pact-dir /tmp/pacts --consumer use_a_lasting_power_of_attorney --provider api-gateway | ||
docker run -d --rm --name iap-images-mock pactfoundation/pact-cli:latest mock-service -p "80" --host "0.0.0.0" \ | ||
--pact-dir /tmp/pacts --consumer use_a_lasting_power_of_attorney --provider iap-api-gateway | ||
docker run -d --rm --name mock-one-login $ECR_REGISTRY/use_an_lpa/mock_onelogin_app | ||
export DOCKER_REMOTE_LPA_PACT_IP="$(docker inspect --format='{{.NetworkSettings.IPAddress}}' lpa-codes-pact-mock)" | ||
export DOCKER_REMOTE_API_PACT_IP="$(docker inspect --format='{{.NetworkSettings.IPAddress}}' api-gateway-pact-mock)" | ||
export DOCKER_REMOTE_IAP_PACT_IP="$(docker inspect --format='{{.NetworkSettings.IPAddress}}' iap-images-mock)" | ||
export DOCKER_REMOTE_MOCK_ONELOGIN_IP="$(docker inspect --format='{{.NetworkSettings.IPAddress}}' mock-one-login)" | ||
curl -XGET $DOCKER_REMOTE_MOCK_ONELOGIN_IP:8080/token | ||
docker logs mock-one-login | ||
docker run -d --name behattests \ | ||
--add-host lpa-codes-pact-mock:$DOCKER_REMOTE_LPA_PACT_IP \ | ||
|
@@ -174,6 +189,7 @@ jobs: | |
docker stop lpa-codes-pact-mock | ||
docker stop api-gateway-pact-mock | ||
docker stop iap-images-mock | ||
docker stop mock-one-login | ||
elif [[ "${SVC_PREFIX}" = "front" ]]; then | ||
docker run -d --name behattests front-app:latest | ||
|
@@ -256,15 +272,6 @@ jobs: | |
with: | ||
sarif_file: 'trivy-results.sarif' | ||
|
||
- name: ecr login | ||
id: login_ecr | ||
uses: aws-actions/amazon-ecr-login@b5f9031d829ac39b7fd26092019aeca3ee6b3ebd # [email protected] | ||
with: | ||
registries: 311462405659 | ||
if: | | ||
inputs.push_to_ecr == 'true' && | ||
(inputs.specific_path == 'all' || inputs.specific_path == matrix.svc_prefix) | ||
- name: tag and push container | ||
env: | ||
ECR_REGISTRY: ${{ steps.login_ecr.outputs.registry }} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
service-api/app/src/App/src/Handler/OneLoginAuthorisationRequestHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Handler; | ||
|
||
use App\Exception\BadRequestException; | ||
use App\Service\Authentication\OneLoginAuthorisationRequestService; | ||
use Exception; | ||
use Laminas\Diactoros\Response\JsonResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
class OneLoginAuthorisationRequestHandler implements RequestHandlerInterface | ||
{ | ||
public function __construct( | ||
private OneLoginAuthorisationRequestService $authorisationRequestService, | ||
) { | ||
} | ||
|
||
/** | ||
* @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'); | ||
} | ||
|
||
$ui_locale = strtolower($params['ui_locale']); | ||
if ($ui_locale !== 'en' and $ui_locale !== 'cy') { | ||
throw new BadRequestException('ui_locale is not set to en or cy'); | ||
} | ||
|
||
$authorisationUri = $this->authorisationRequestService->createAuthorisationRequest($params['ui_locale']); | ||
|
||
return new JsonResponse($authorisationUri); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
service-api/app/src/App/src/Service/Authentication/OneLoginAuthorisationRequestService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Service\Authentication; | ||
|
||
use Facile\OpenIDClient\Client\ClientBuilder; | ||
use Facile\OpenIDClient\Client\Metadata\ClientMetadata; | ||
use Facile\OpenIDClient\Issuer\IssuerBuilderInterface; | ||
use Facile\OpenIDClient\Service\Builder\AuthorizationServiceBuilder; | ||
|
||
use function Facile\OpenIDClient\base64url_encode; | ||
|
||
class OneLoginAuthorisationRequestService | ||
{ | ||
public function __construct( | ||
private JWKFactory $JWKFactory, | ||
private IssuerBuilderInterface $issuerBuilder, | ||
) { | ||
} | ||
|
||
public function createAuthorisationRequest(string $uiLocale): string | ||
{ | ||
//TODO UML-3080 Configure cache | ||
|
||
$issuer = $this->issuerBuilder | ||
->build('http://mock-one-login:8080/.well-known/openid-configuration'); | ||
|
||
|
||
$clientMetadata = ClientMetadata::fromArray([ | ||
'client_id' => 'client-id', | ||
'client_secret' => 'my-client-secret', | ||
'token_endpoint_auth_method' => 'private_key_jwt', | ||
'jwks' => [ | ||
'keys' => [ | ||
($this->JWKFactory)(), | ||
], | ||
], | ||
]); | ||
|
||
$client = (new ClientBuilder()) | ||
->setIssuer($issuer) | ||
->setClientMetadata($clientMetadata) | ||
->build(); | ||
|
||
$authorisationService = (new AuthorizationServiceBuilder())->build(); | ||
|
||
return $authorisationService->getAuthorizationUri( | ||
$client, | ||
[ | ||
'scope' => 'openid email', | ||
'state' => base64url_encode(random_bytes(12)), | ||
'redirect_uri' => '/lpa/dashboard', | ||
'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}}', | ||
] | ||
); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...e-api/app/test/AppTest/Service/Authentication/OneLoginAuthorisationRequestServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AppTest\Service\Authentication; | ||
|
||
use App\Service\Authentication\OneLoginAuthorisationRequestService; | ||
use App\Service\Authentication\JWKFactory; | ||
use Facile\OpenIDClient\Issuer\IssuerBuilder; | ||
use Facile\OpenIDClient\Issuer\IssuerBuilderInterface; | ||
use Facile\OpenIDClient\Issuer\IssuerInterface; | ||
use Facile\OpenIDClient\Issuer\Metadata\IssuerMetadataInterface; | ||
use Jose\Component\Core\JWK; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
|
||
class OneLoginAuthorisationRequestServiceTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private ObjectProphecy|JWKFactory $JWKFactory; | ||
private ObjectProphecy|IssuerBuilder $issuerBuilder; | ||
|
||
public function setup(): void | ||
{ | ||
$jwk = $this->prophesize(JWK::class); | ||
$this->JWKFactory = $this->prophesize(JWKFactory::class); | ||
$this->issuerBuilder = $this->prophesize(IssuerBuilderInterface::class); | ||
$issuer = $this->prophesize(IssuerInterface::class); | ||
$issuerMetaData = $this->prophesize(IssuerMetadataInterface::class); | ||
|
||
$this->JWKFactory->__invoke()->willReturn($jwk); | ||
$issuer->getMetadata()->willReturn($issuerMetaData); | ||
$issuerMetaData->getAuthorizationEndpoint()->willReturn('fake endpoint'); | ||
$this->issuerBuilder->build('http://mock-one-login:8080/.well-known/openid-configuration')->willReturn($issuer); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function create_authorisation_request(): void | ||
{ | ||
$authorisationRequestService = new OneLoginAuthorisationRequestService( | ||
$this->JWKFactory->reveal(), | ||
$this->issuerBuilder->reveal() | ||
); | ||
$authorisationRequest = $authorisationRequestService->createAuthorisationRequest('en'); | ||
$this->assertStringContainsString('client_id=client-id', $authorisationRequest); | ||
$this->assertStringContainsString('scope=openid+email', $authorisationRequest); | ||
$this->assertStringContainsString('vtr=%5B%22Cl.Cm.P2%22%5D', $authorisationRequest); | ||
$this->assertStringContainsString('ui_locales=en', $authorisationRequest); | ||
$this->assertStringContainsString('redirect_uri=%2Flpa%2Fdashboard', $authorisationRequest); | ||
} | ||
} |