Skip to content

Commit

Permalink
UML-3076 JWKFactory provdes JWK with test coverage (#2353)
Browse files Browse the repository at this point in the history
Co-authored-by: Lbagg1 <[email protected]>
  • Loading branch information
MishNajam and Lbagg1 authored Oct 2, 2023
1 parent a7bb3ae commit e727f8b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
28 changes: 28 additions & 0 deletions service-api/app/src/App/src/Service/Authentication/JWKFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Service\Authentication;

use Jose\Component\KeyManagement\JWKFactory as KeyFactory;
use Jose\Component\Core\JWK;

class JWKFactory
{
public function __construct(private KeyPairManager $keyPairManager)
{
}

public function __invoke(): JWK
{
return KeyFactory::createFromKey(
$this->keyPairManager->getKeyPair()->private->getString(),
null,
[
//TODO UML-3056 These may need revisiting
'alg' => 'RS256',
'use' => 'sig',
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace AppTest\Service\Authentication;

use App\Service\Authentication\JWKFactory;
use App\Service\Authentication\KeyPair;
use App\Service\Authentication\KeyPairManager;
use InvalidArgumentException;
use Jose\Component\Core\JWK;
use ParagonIE\HiddenString\HiddenString;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;

class JWKFactoryTest extends TestCase
{
use ProphecyTrait;

private string $key;
private ObjectProphecy|KeyPairManager $keyPairManager;

public function setUp(): void
{
$key = openssl_pkey_new(
[
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
]
);
if ($key === false) {
throw new InvalidArgumentException('Unable to create the key');
}
$details = openssl_pkey_get_details($key);
if (! is_array($details)) {
throw new InvalidArgumentException('Unable to get key details');
}

$this->key = '';
$success = openssl_pkey_export($key, $this->key);

if (!$success) {
throw new InvalidArgumentException('Unable to export key to string');
}
$keyPair = new KeyPair('public', new HiddenString($this->key, false, true));

$this->keyPairManager = $this->prophesize(KeyPairManager::class);
$this->keyPairManager->getKeyPair()->willReturn($keyPair)->shouldBeCalled();
}

/** @test */
public function can_create_jwk(): void
{
$JWKFactory = new JWKFactory($this->keyPairManager->reveal());
$JWK = ($JWKFactory)();
self::assertNotNull($JWK);
self::assertInstanceOf(JWK::class, $JWK);
self::assertTrue($JWK->has('alg'));
self::assertTrue($JWK->has('use'));
self::assertEquals('RS256', $JWK->get('alg'));
self::assertEquals('sig', $JWK->get('use'));
}
}

0 comments on commit e727f8b

Please sign in to comment.