|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\OpenID\Utils; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\Attributes\UsesClass; |
| 9 | +use PHPUnit\Framework\MockObject\MockObject; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Psr\Log\LoggerInterface; |
| 12 | +use SimpleSAML\OpenID\Algorithms\SignatureAlgorithmEnum; |
| 13 | +use SimpleSAML\OpenID\Exceptions\OpenIdException; |
| 14 | +use SimpleSAML\OpenID\Helpers; |
| 15 | +use SimpleSAML\OpenID\Helpers\Type; |
| 16 | +use SimpleSAML\OpenID\Utils\KeyPairResolver; |
| 17 | +use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPair; |
| 18 | +use SimpleSAML\OpenID\ValueAbstracts\SignatureKeyPairBag; |
| 19 | + |
| 20 | +#[CoversClass(KeyPairResolver::class)] |
| 21 | +#[UsesClass(Type::class)] |
| 22 | +final class KeyPairResolverTest extends TestCase |
| 23 | +{ |
| 24 | + private MockObject $helpersMock; |
| 25 | + |
| 26 | + private MockObject $loggerMock; |
| 27 | + |
| 28 | + private MockObject $signatureKeyPairBagMock; |
| 29 | + |
| 30 | + private MockObject $defaultKeyPairMock; |
| 31 | + |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->helpersMock = $this->createMock(\SimpleSAML\OpenID\Helpers::class); |
| 36 | + $typeHelper = new Type(); |
| 37 | + $this->helpersMock->method('type')->willReturn($typeHelper); |
| 38 | + $this->loggerMock = $this->createMock(LoggerInterface::class); |
| 39 | + |
| 40 | + $this->signatureKeyPairBagMock = $this->createMock(SignatureKeyPairBag::class); |
| 41 | + |
| 42 | + $this->defaultKeyPairMock = $this->createMock(SignatureKeyPair::class); |
| 43 | + $this->defaultKeyPairMock->method('getSignatureAlgorithm') |
| 44 | + ->willReturn(SignatureAlgorithmEnum::RS256); |
| 45 | + |
| 46 | + $this->signatureKeyPairBagMock->method('getFirstOrFail') |
| 47 | + ->willReturn($this->defaultKeyPairMock); |
| 48 | + } |
| 49 | + |
| 50 | + |
| 51 | + protected function sut( |
| 52 | + ?Helpers $helpers = null, |
| 53 | + ?LoggerInterface $logger = null, |
| 54 | + ): KeyPairResolver { |
| 55 | + $helpers ??= $this->helpersMock; |
| 56 | + $logger ??= $this->loggerMock; |
| 57 | + |
| 58 | + return new KeyPairResolver($helpers, $logger); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + public function testResolveWithReceiverDesignatedAlgorithm(): void |
| 63 | + { |
| 64 | + $receiverMetadata = [ |
| 65 | + 'receiver_alg' => 'RS256', |
| 66 | + ]; |
| 67 | + |
| 68 | + $keyPairMock = $this->createMock(SignatureKeyPair::class); |
| 69 | + $keyPairMock->method('getSignatureAlgorithm') |
| 70 | + ->willReturn(SignatureAlgorithmEnum::RS256); |
| 71 | + $this->signatureKeyPairBagMock->expects($this->once()) |
| 72 | + ->method('getFirstByAlgorithmOrFail') |
| 73 | + ->with(SignatureAlgorithmEnum::RS256) |
| 74 | + ->willReturn($keyPairMock); |
| 75 | + |
| 76 | + $result = $this->sut()->resolveSignatureKeyPairByAlgorithm( |
| 77 | + $this->signatureKeyPairBagMock, |
| 78 | + $receiverMetadata, |
| 79 | + [], |
| 80 | + 'receiver_alg', |
| 81 | + ); |
| 82 | + |
| 83 | + $this->assertSame($keyPairMock, $result); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public function testResolveWithSenderDesignatedAlgorithm(): void |
| 88 | + { |
| 89 | + $senderMetadata = [ |
| 90 | + 'sender_alg' => 'ES256', |
| 91 | + ]; |
| 92 | + |
| 93 | + $keyPairMock = $this->createMock(SignatureKeyPair::class); |
| 94 | + $keyPairMock->method('getSignatureAlgorithm') |
| 95 | + ->willReturn(SignatureAlgorithmEnum::ES256); |
| 96 | + |
| 97 | + $this->signatureKeyPairBagMock->expects($this->once()) |
| 98 | + ->method('getFirstByAlgorithmOrFail') |
| 99 | + ->with(SignatureAlgorithmEnum::ES256) |
| 100 | + ->willReturn($keyPairMock); |
| 101 | + |
| 102 | + $result = $this->sut()->resolveSignatureKeyPairByAlgorithm( |
| 103 | + $this->signatureKeyPairBagMock, |
| 104 | + [], |
| 105 | + $senderMetadata, |
| 106 | + null, |
| 107 | + null, |
| 108 | + 'sender_alg', |
| 109 | + ); |
| 110 | + |
| 111 | + $this->assertSame($keyPairMock, $result); |
| 112 | + } |
| 113 | + |
| 114 | + |
| 115 | + public function testResolveConflictDesignatedAlgorithmsThrows(): void |
| 116 | + { |
| 117 | + $receiverMetadata = ['alg' => 'RS256']; |
| 118 | + $senderMetadata = ['alg' => 'ES256']; |
| 119 | + |
| 120 | + $this->expectException(OpenIdException::class); |
| 121 | + $this->expectExceptionMessage('Conflict in designated signature algorithms'); |
| 122 | + |
| 123 | + $this->sut()->resolveSignatureKeyPairByAlgorithm( |
| 124 | + $this->signatureKeyPairBagMock, |
| 125 | + $receiverMetadata, |
| 126 | + $senderMetadata, |
| 127 | + 'alg', |
| 128 | + null, |
| 129 | + 'alg', |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + public function testResolveCommonlySupportedAlgorithms(): void |
| 135 | + { |
| 136 | + $receiverMetadata = ['supported' => ['RS256', 'ES256']]; |
| 137 | + $senderMetadata = ['supported' => ['ES256', 'PS256']]; |
| 138 | + |
| 139 | + $this->signatureKeyPairBagMock->method('getAllAlgorithmNamesUnique') |
| 140 | + ->willReturn(['RS256', 'ES256', 'PS256']); |
| 141 | + |
| 142 | + $keyPairMock = $this->createMock(SignatureKeyPair::class); |
| 143 | + $keyPairMock->method('getSignatureAlgorithm') |
| 144 | + ->willReturn(SignatureAlgorithmEnum::ES256); |
| 145 | + $this->signatureKeyPairBagMock->expects($this->once()) |
| 146 | + ->method('getFirstByAlgorithmOrFail') |
| 147 | + ->with(SignatureAlgorithmEnum::ES256) |
| 148 | + ->willReturn($keyPairMock); |
| 149 | + |
| 150 | + $result = $this->sut()->resolveSignatureKeyPairByAlgorithm( |
| 151 | + $this->signatureKeyPairBagMock, |
| 152 | + $receiverMetadata, |
| 153 | + $senderMetadata, |
| 154 | + null, |
| 155 | + 'supported', |
| 156 | + null, |
| 157 | + 'supported', |
| 158 | + ); |
| 159 | + |
| 160 | + $this->assertSame($keyPairMock, $result); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + public function testResolveFallbackToDefaultWhenNoMatch(): void |
| 165 | + { |
| 166 | + $receiverMetadata = ['supported' => ['RS256']]; |
| 167 | + $senderMetadata = ['supported' => ['ES256']]; |
| 168 | + |
| 169 | + $this->signatureKeyPairBagMock->method('getAllAlgorithmNamesUnique') |
| 170 | + ->willReturn(['PS256']); |
| 171 | + |
| 172 | + |
| 173 | + $result = $this->sut()->resolveSignatureKeyPairByAlgorithm( |
| 174 | + $this->signatureKeyPairBagMock, |
| 175 | + $receiverMetadata, |
| 176 | + $senderMetadata, |
| 177 | + null, |
| 178 | + 'supported', |
| 179 | + null, |
| 180 | + 'supported', |
| 181 | + ); |
| 182 | + |
| 183 | + $this->assertSame($this->defaultKeyPairMock, $result); |
| 184 | + } |
| 185 | +} |
0 commit comments