|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\OpenID\ValueAbstracts\Factories; |
| 6 | + |
| 7 | +use Jose\Component\Core\JWK as StandardJwk; |
| 8 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 9 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use SimpleSAML\OpenID\Algorithms\SignatureAlgorithmEnum; |
| 12 | +use SimpleSAML\OpenID\Codebooks\ClaimsEnum; |
| 13 | +use SimpleSAML\OpenID\Codebooks\HashAlgorithmsEnum; |
| 14 | +use SimpleSAML\OpenID\Jwk; |
| 15 | +use SimpleSAML\OpenID\Jwk\Factories\JwkDecoratorFactory; |
| 16 | +use SimpleSAML\OpenID\Jwk\JwkDecorator; |
| 17 | +use SimpleSAML\OpenID\ValueAbstracts\Factories\SignatureKeyPairFactory; |
| 18 | +use SimpleSAML\OpenID\ValueAbstracts\KeyPair; |
| 19 | +use SimpleSAML\OpenID\ValueAbstracts\KeyPairConfigInterface; |
| 20 | +use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPair; |
| 21 | +use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPairConfig; |
| 22 | + |
| 23 | +#[CoversClass(SignatureKeyPairFactory::class)] |
| 24 | +#[UsesClass(SignatureKeyPairConfig::class)] |
| 25 | +#[UsesClass(SignatureKeyPair::class)] |
| 26 | +#[UsesClass(KeyPair::class)] |
| 27 | +#[UsesClass(HashAlgorithmsEnum::class)] |
| 28 | +final class SignatureKeyPairFactoryTest extends TestCase |
| 29 | +{ |
| 30 | + /** @var \SimpleSAML\OpenID\Jwk&\PHPUnit\Framework\MockObject\MockObject */ |
| 31 | + protected \PHPUnit\Framework\MockObject\MockObject $jwkMock; |
| 32 | + |
| 33 | + /** @var \SimpleSAML\OpenID\Jwk\Factories\JwkDecoratorFactory&\PHPUnit\Framework\MockObject\MockObject */ |
| 34 | + protected \PHPUnit\Framework\MockObject\MockObject $jwkDecoratorFactoryMock; |
| 35 | + |
| 36 | + /** @var \SimpleSAML\OpenID\Jwk\JwkDecorator&\PHPUnit\Framework\MockObject\MockObject */ |
| 37 | + protected \PHPUnit\Framework\MockObject\MockObject $publicKeyJwkDecoratorMock; |
| 38 | + |
| 39 | + /** @var \SimpleSAML\OpenID\Jwk\JwkDecorator&\PHPUnit\Framework\MockObject\Stub */ |
| 40 | + protected \PHPUnit\Framework\MockObject\Stub $privateKeyJwkDecoratorMock; |
| 41 | + |
| 42 | + |
| 43 | + protected function setUp(): void |
| 44 | + { |
| 45 | + $this->jwkMock = $this->createMock(Jwk::class); |
| 46 | + $this->jwkDecoratorFactoryMock = $this->createMock(JwkDecoratorFactory::class); |
| 47 | + $this->publicKeyJwkDecoratorMock = $this->createMock(JwkDecorator::class); |
| 48 | + $this->privateKeyJwkDecoratorMock = $this->createStub(JwkDecorator::class); |
| 49 | + |
| 50 | + $this->jwkMock->method('jwkDecoratorFactory')->willReturn($this->jwkDecoratorFactoryMock); |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + protected function sut(): SignatureKeyPairFactory |
| 55 | + { |
| 56 | + return new SignatureKeyPairFactory($this->jwkMock); |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + public function testFromConfigWithProvidedKeyId(): void |
| 61 | + { |
| 62 | + $keyPairConfigMock = $this->createMock(KeyPairConfigInterface::class); |
| 63 | + $keyPairConfigMock->method('getPublicKeyString')->willReturn('pub_key_string'); |
| 64 | + $keyPairConfigMock->method('getPrivateKeyString')->willReturn('priv_key_string'); |
| 65 | + $keyPairConfigMock->method('getKeyId')->willReturn('provided_key_id'); |
| 66 | + $keyPairConfigMock->method('getPrivateKeyPassword')->willReturn('priv_password'); |
| 67 | + |
| 68 | + $signatureKeyPairConfig = new SignatureKeyPairConfig( |
| 69 | + SignatureAlgorithmEnum::RS256, |
| 70 | + $keyPairConfigMock, |
| 71 | + ); |
| 72 | + |
| 73 | + $this->jwkDecoratorFactoryMock->expects($this->exactly(2)) |
| 74 | + ->method('fromPkcs1Or8Key') |
| 75 | + ->willReturnCallback(function ( |
| 76 | + string $key, |
| 77 | + ?string $password, |
| 78 | + array $additionalData, |
| 79 | + ): \PHPUnit\Framework\MockObject\MockObject|\PHPUnit\Framework\MockObject\Stub|null { |
| 80 | + if ($key === 'pub_key_string') { |
| 81 | + return $this->publicKeyJwkDecoratorMock; |
| 82 | + } |
| 83 | + |
| 84 | + if ($key === 'priv_key_string') { |
| 85 | + return $this->privateKeyJwkDecoratorMock; |
| 86 | + } |
| 87 | + |
| 88 | + return null; |
| 89 | + }); |
| 90 | + |
| 91 | + $this->publicKeyJwkDecoratorMock->expects($this->once()) |
| 92 | + ->method('addAdditionalData') |
| 93 | + ->with(ClaimsEnum::Kid->value, 'provided_key_id'); |
| 94 | + |
| 95 | + $factory = $this->sut(); |
| 96 | + $result = $factory->fromConfig($signatureKeyPairConfig); |
| 97 | + |
| 98 | + $this->assertInstanceOf(SignatureKeyPair::class, $result); |
| 99 | + $this->assertSame(SignatureAlgorithmEnum::RS256, $result->getSignatureAlgorithm()); |
| 100 | + $this->assertSame('provided_key_id', $result->getKeyPair()->getKeyId()); |
| 101 | + $this->assertSame('priv_password', $result->getKeyPair()->getPrivateKeyPassword()); |
| 102 | + $this->assertSame($this->publicKeyJwkDecoratorMock, $result->getKeyPair()->getPublicKey()); |
| 103 | + $this->assertSame($this->privateKeyJwkDecoratorMock, $result->getKeyPair()->getPrivateKey()); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + public function testFromConfigWithGeneratedKeyIdThumbprint(): void |
| 108 | + { |
| 109 | + $keyPairConfigMock = $this->createMock(KeyPairConfigInterface::class); |
| 110 | + $keyPairConfigMock->method('getPublicKeyString')->willReturn('pub_key_string'); |
| 111 | + $keyPairConfigMock->method('getPrivateKeyString')->willReturn('priv_key_string'); |
| 112 | + $keyPairConfigMock->method('getKeyId')->willReturn(null); |
| 113 | + $keyPairConfigMock->method('getPrivateKeyPassword')->willReturn(null); |
| 114 | + |
| 115 | + $signatureKeyPairConfig = new SignatureKeyPairConfig( |
| 116 | + SignatureAlgorithmEnum::ES256, |
| 117 | + $keyPairConfigMock, |
| 118 | + ); |
| 119 | + |
| 120 | + $this->jwkDecoratorFactoryMock->expects($this->exactly(2)) |
| 121 | + ->method('fromPkcs1Or8Key') |
| 122 | + ->willReturnCallback(function ( |
| 123 | + string $key, |
| 124 | + ?string $password, |
| 125 | + array $additionalData, |
| 126 | + ): \PHPUnit\Framework\MockObject\MockObject|\PHPUnit\Framework\MockObject\Stub|null { |
| 127 | + if ($key === 'pub_key_string') { |
| 128 | + return $this->publicKeyJwkDecoratorMock; |
| 129 | + } |
| 130 | + |
| 131 | + if ($key === 'priv_key_string') { |
| 132 | + return $this->privateKeyJwkDecoratorMock; |
| 133 | + } |
| 134 | + |
| 135 | + return null; |
| 136 | + }); |
| 137 | + |
| 138 | + $standardJwkMock = $this->createMock(StandardJwk::class); |
| 139 | + $standardJwkMock->method('thumbprint') |
| 140 | + ->with(HashAlgorithmsEnum::SHA_256->phpName()) |
| 141 | + ->willReturn('generated_thumbprint_key_id'); |
| 142 | + |
| 143 | + $this->publicKeyJwkDecoratorMock->method('jwk')->willReturn($standardJwkMock); |
| 144 | + |
| 145 | + $this->publicKeyJwkDecoratorMock->expects($this->once()) |
| 146 | + ->method('addAdditionalData') |
| 147 | + ->with(ClaimsEnum::Kid->value, 'generated_thumbprint_key_id'); |
| 148 | + |
| 149 | + $factory = $this->sut(); |
| 150 | + $result = $factory->fromConfig($signatureKeyPairConfig, HashAlgorithmsEnum::SHA_256); |
| 151 | + |
| 152 | + $this->assertInstanceOf(SignatureKeyPair::class, $result); |
| 153 | + $this->assertSame(SignatureAlgorithmEnum::ES256, $result->getSignatureAlgorithm()); |
| 154 | + $this->assertSame('generated_thumbprint_key_id', $result->getKeyPair()->getKeyId()); |
| 155 | + $this->assertNull($result->getKeyPair()->getPrivateKeyPassword()); |
| 156 | + $this->assertSame($this->publicKeyJwkDecoratorMock, $result->getKeyPair()->getPublicKey()); |
| 157 | + $this->assertSame($this->privateKeyJwkDecoratorMock, $result->getKeyPair()->getPrivateKey()); |
| 158 | + } |
| 159 | +} |
0 commit comments