diff --git a/Changelog.md b/Changelog.md index ae56ab6..9f0308d 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Changelog +## v1.10.11 2026-11-25 + +Mutation testing is now up to 98% + ## v1.10.10 2026-11-25 ## v1.10.9 2026-11-25 diff --git a/infection.json5 b/infection.json5 index 8b0da52..fa213ba 100644 --- a/infection.json5 +++ b/infection.json5 @@ -17,8 +17,8 @@ } }, "tmpDir": "build/cache/infection", - "minMsi": 95, - "minCoveredMsi": 95, + "minMsi": 97, + "minCoveredMsi": 97, "mutators": { "@default": true, }, diff --git a/src/Rfc9421/Dto/ComponentItemDto.php b/src/Rfc9421/Dto/ComponentItemDto.php index 743fb29..de1131b 100644 --- a/src/Rfc9421/Dto/ComponentItemDto.php +++ b/src/Rfc9421/Dto/ComponentItemDto.php @@ -73,9 +73,14 @@ private static function validFieldName(Item $item): string private static function ensureStringValue( Bytes|Token|DisplayString|DateTimeImmutable|string|int|float|bool $v, ): string { - return is_string($v) ? $v : throw new UnexpectedValue( + if (is_string($v)) { + return $v; + } + // @codeCoverageIgnoreStart + throw new UnexpectedValue( sprintf('Expected string, got "%s"', gettype($v)), ); + // @codeCoverageIgnoreEnd } public function getItem(): Item diff --git a/src/Rfc9421/Enums/DigestAlgorithmsEnum.php b/src/Rfc9421/Enums/DigestAlgorithmsEnum.php index 02c28a9..42479c9 100644 --- a/src/Rfc9421/Enums/DigestAlgorithmsEnum.php +++ b/src/Rfc9421/Enums/DigestAlgorithmsEnum.php @@ -1,4 +1,5 @@ algorithm diff --git a/src/Rfc9421/Signer/Signatures/ReadLines.php b/src/Rfc9421/Signer/Signatures/ReadLines.php index 34b8b5e..960db83 100644 --- a/src/Rfc9421/Signer/Signatures/ReadLines.php +++ b/src/Rfc9421/Signer/Signatures/ReadLines.php @@ -11,6 +11,8 @@ namespace Filefabrik\Rfc9421\Signer\Signatures; +use Filefabrik\Rfc9421\Exceptions\UnexpectedValue; + /** * after verify, so nothing can be written into the ReadLines */ @@ -18,6 +20,8 @@ final class ReadLines extends AbstractLines { /** * @param array|array{}|\Filefabrik\Rfc9421\Signer\Signatures\WriteLines $lines + * + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue */ public function __construct(array|WriteLines $lines) { @@ -26,6 +30,8 @@ public function __construct(array|WriteLines $lines) /** * @param array|array{}|\Filefabrik\Rfc9421\Signer\Signatures\WriteLines $lines + * + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue */ private function setup(array|WriteLines $lines): void { @@ -36,10 +42,12 @@ private function setup(array|WriteLines $lines): void foreach ($lines as $key => $line) { if ($line instanceof Line && is_int($key)) { $this->protectedAdd($line); - } else { - /* @var string $key */ - /* @var string $line */ + } elseif ((is_string($line) || is_int($line)) && is_string($key)) { $this->protectedAddEntry($key, $line); + } else { + throw new UnexpectedValue( + sprintf('Unexpected value of type %s or key of type %s', gettype($line), gettype($key)), + ); } } } diff --git a/src/Rfc9421/Signer/TraitSigner.php b/src/Rfc9421/Signer/TraitSigner.php index 37b0bc4..d8128f3 100644 --- a/src/Rfc9421/Signer/TraitSigner.php +++ b/src/Rfc9421/Signer/TraitSigner.php @@ -170,13 +170,11 @@ private function updateHeadersBeforePrepare(): void /** * Generate the base signature * - * - * * @throws \Filefabrik\Rfc9421\Exceptions\EmptyException * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue */ - public function crypt(string $signingParametersString): string + private function crypt(string $signingParametersString): string { $signature = SignatureRegistry::signing( signingEnumInterface: SigningEnum::isOwnTypeOrThrow($this->signingConfiguration->getMetadata()->alg), @@ -193,7 +191,7 @@ public function crypt(string $signingParametersString): string * @return string $signatureKeyName:$signature: * */ - public function makeSignatureString(string $signature): string + private function makeSignatureString(string $signature): string { return sprintf($this->signingConfiguration->getSignatureKeyName().':%s:', $signature); } @@ -203,7 +201,11 @@ public function makeSignatureString(string $signature): string */ public function getSigningParametersStringOrThrow(): string { - return $this->signingParametersString ?? throw ProcessingException::signingParametersString(); + if (is_string($this->signingParametersString)) { + return $this->signingParametersString; + } + + return throw ProcessingException::signingParametersString(); } /** diff --git a/src/Rfc9421/StructuredFields/Processing/Bs.php b/src/Rfc9421/StructuredFields/Processing/Bs.php index acd533e..975facc 100644 --- a/src/Rfc9421/StructuredFields/Processing/Bs.php +++ b/src/Rfc9421/StructuredFields/Processing/Bs.php @@ -39,7 +39,7 @@ public function handle( if (0 < count($values)) { $valuesSegments = []; foreach ($values as $value) { - $valuesSegments [] = ':'.base64_encode(trim($value)).':'; + $valuesSegments [] = ':'.base64_encode($value).':'; } // all in a line diff --git a/src/Rfc9421/Support/AbstractMetadata.php b/src/Rfc9421/Support/AbstractMetadata.php index df14491..385f448 100644 --- a/src/Rfc9421/Support/AbstractMetadata.php +++ b/src/Rfc9421/Support/AbstractMetadata.php @@ -77,7 +77,7 @@ public function hasTag(): bool * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue */ - public function set(string $key, int|string|null $value): static + public function set(string $key, int|string|null|SigningEnumInterface $value): static { $methodName = 'set'.ucfirst($key); @@ -122,6 +122,7 @@ public function setNonce(?string $nonce): static */ private function setStringOrNull(mixed $value): ?string { + // todo signature exception or metadata exception return null === $value || is_string($value) ? $value : throw new UnexpectedValue('$value must be string|null'); } @@ -148,8 +149,10 @@ public function setAlg(string|SigningEnumInterface $alg): static */ private function setStringOrSigningEnum(mixed $value): string|SigningEnumInterface { - return is_string($value) || $value instanceof SigningEnumInterface ? $value : - throw SignatureException::wrongFormat(); + if (is_string($value) || $value instanceof SigningEnumInterface) { + return $value; + } + throw SignatureException::wrongFormat(); } public function setTag(?string $tag): static diff --git a/src/Rfc9421/Verify/AbstractVerifyPrepare.php b/src/Rfc9421/Verify/AbstractVerifyPrepare.php index 058451d..5728803 100644 --- a/src/Rfc9421/Verify/AbstractVerifyPrepare.php +++ b/src/Rfc9421/Verify/AbstractVerifyPrepare.php @@ -12,7 +12,6 @@ namespace Filefabrik\Rfc9421\Verify; -use Filefabrik\Rfc9421\Exceptions\UnexpectedValue; use Filefabrik\Rfc9421\Exceptions\VerifyHeaderSignatureException; use Filefabrik\Rfc9421\Extractors\ContentDigest; use Filefabrik\Rfc9421\Extractors\HeaderSignature; @@ -79,7 +78,7 @@ public function prepareVerification(): bool */ Metadata::bindMetadata( instance: $this->signingConfiguration->getMetadata(), - metadata: $this->getExtractedMetadataArray(), + metadata: $this->getSignaturInputInstance()->extractedMetadata->toArray(), ); /* @@ -103,23 +102,6 @@ public function getHeaderSignatureExtractor(): HeaderSignature return $this->headerSignatureExtractor; } - /** - * - * @throws \Filefabrik\Rfc9421\Exceptions\AlreadySetException - * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue - * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException - * @throws \Filefabrik\Rfc9421\Exceptions\SignatureExtractionException - * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue - * @throws \Filefabrik\Rfc9421\Exceptions\VerifyHeaderSignatureException - * @return array{}|array{created?: int, expires?: int, nonce?: string, keyid?: string, alg?: string, tag?: string} - */ - protected function getExtractedMetadataArray(): array - { - return $this->getSignaturInputInstance()->extractedMetadata?->toArray() ?? throw new UnexpectedValue( - 'No Metadata Found', - ); - } - /** * @throws \Filefabrik\Rfc9421\Exceptions\AlreadySetException * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException @@ -127,7 +109,7 @@ protected function getExtractedMetadataArray(): array * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue * @throws \Filefabrik\Rfc9421\Exceptions\VerifyHeaderSignatureException */ - protected function getSignaturInputInstance(): HeaderSignatureInput + private function getSignaturInputInstance(): HeaderSignatureInput { $headerSignatureInstance = new HeaderSignatureInput( headerSignatureInputValue: $this->headerHelper diff --git a/src/Rfc9421/Verify/AbstractVerifying.php b/src/Rfc9421/Verify/AbstractVerifying.php index cea814e..ed597c3 100644 --- a/src/Rfc9421/Verify/AbstractVerifying.php +++ b/src/Rfc9421/Verify/AbstractVerifying.php @@ -54,8 +54,8 @@ public function verifying(): bool $preparedSigningConfiguration = $this->getVerifySigningConfiguration(); $preparedSigningConfiguration->setSigningComponents( components: $this->preparedVerify - ->getSignatureInputInstance() - ->getExtractedComponents(), + ->getSignatureInputInstance() + ->getExtractedComponents(), ); $contentDigestResult = $this->verifyContentDigestIfNeed($preparedSigningConfiguration); @@ -100,22 +100,19 @@ private function verifyContentDigestIfNeed(SigningConfiguration $signingConfigur // Validates the Content digest if needed from signature-line sig1=("content-digest") if ($signingConfiguration->hasContentDigestHashAlgo()) { if (!$this->validateContentDigest()) { - $this->hasSignedBody = false; - // todo logging the error that validate content digest is wrong - return false; + return $this->hasSignedBody = false; } // was signed and is valid $this->hasSignedBody = true; } - if(null===$this->hasSignedBody){ + if (null === $this->hasSignedBody) { // content-digest was not part of the signing process $this->hasSignedBody = false; } - // return true so tell we have no validation error return true; } @@ -128,8 +125,8 @@ private function verifyContentDigestIfNeed(SigningConfiguration $signingConfigur private function validateContentDigest(): bool { $contentDigestLine = $this->preparedVerify - ->getHeaderHelper() - ->getContentDigestLine() + ->getHeaderHelper() + ->getContentDigestLine() ; $verifyInstance = new VerifyContentDigest( @@ -149,8 +146,8 @@ abstract protected function getSigner(): RequestSigner|ResponseSigner; private function getBinarySignatureValue(): string { return $this->preparedVerify - ->getHeaderSignatureExtractor() - ->getBinarySignatureValue() + ->getHeaderSignatureExtractor() + ->getBinarySignatureValue() ; } @@ -159,7 +156,10 @@ private function getBinarySignatureValue(): string */ public function getSignedHeaderLines(): ReadLines { - return $this->signedHeaderLines ?? throw ProcessingException::SignedHeaderLines(); + if ($this->signedHeaderLines instanceof ReadLines) { + return $this->signedHeaderLines; + } + throw ProcessingException::SignedHeaderLines(); } abstract public function getMessage(): MessageInterface; diff --git a/src/Rfc9421/Verify/HeaderSignatureInput.php b/src/Rfc9421/Verify/HeaderSignatureInput.php index e9e14fc..5355cf9 100644 --- a/src/Rfc9421/Verify/HeaderSignatureInput.php +++ b/src/Rfc9421/Verify/HeaderSignatureInput.php @@ -42,7 +42,7 @@ public function __construct( * if the signatureNameKey not set/null, then detect it during the extract * */ - private(set) readonly string $signatureNameKey + private(set) readonly string $signatureNameKey, ) { } @@ -103,7 +103,11 @@ private function extractSignatureString(): string */ public function getExtractedComponents(): SigningComponents { - return $this->extractedComponents ?? throw SignatureExtractionException::notProceeded(); + if ($this->extractedComponents instanceof SigningComponents) { + return $this->extractedComponents; + } + + throw SignatureExtractionException::notProceeded(); } } diff --git a/src/Rfc9421/Verify/VerifyContentDigest.php b/src/Rfc9421/Verify/VerifyContentDigest.php index 25636f8..a714d9a 100644 --- a/src/Rfc9421/Verify/VerifyContentDigest.php +++ b/src/Rfc9421/Verify/VerifyContentDigest.php @@ -27,8 +27,8 @@ public function __construct( } /** - * @return bool true if valid * @throws \Filefabrik\Rfc9421\Exceptions\ContentDigestException + * @return bool true if valid */ public function verify(): bool { @@ -40,7 +40,7 @@ public function verify(): bool /** * @throws \Filefabrik\Rfc9421\Exceptions\ContentDigestException */ - public function getContentDigestVerifier(): ContentDigest + private function getContentDigestVerifier(): ContentDigest { if (null === $this->contentDigestVerifier) { $this->contentDigestVerifier = $this->prepare(); diff --git a/tests/Rfc9421/Enums/InitialContentsEnumTest.php b/tests/Rfc9421/Enums/InitialContentsEnumTest.php index 61278c7..c25c9d5 100644 --- a/tests/Rfc9421/Enums/InitialContentsEnumTest.php +++ b/tests/Rfc9421/Enums/InitialContentsEnumTest.php @@ -54,6 +54,14 @@ public function testIsOwnTypeOrNullOrThrowException(): void InitialContentsEnum::isOwnTypeOrNullOrThrow('nothing'); } + /** + * @throws \Filefabrik\Rfc9421\Exceptions\EnumTypeException + */ + public function testIsOwnTypeOrNullOrThrowNull(): void + { + self::assertNull(InitialContentsEnum::isOwnTypeOrNullOrThrow(null)); + } + /** * @throws \Filefabrik\Rfc9421\Exceptions\EnumTypeException */ diff --git a/tests/Rfc9421/RequestSignerTest.php b/tests/Rfc9421/RequestSignerTest.php index 2036382..139371b 100644 --- a/tests/Rfc9421/RequestSignerTest.php +++ b/tests/Rfc9421/RequestSignerTest.php @@ -669,4 +669,46 @@ public function testSignThrowsReq(): void $signer = new RequestSigner(message: $request, signingConfiguration: $signingConfiguration); $signer->sign(); } + + /** + * @throws \Filefabrik\Rfc9421\Exceptions\ProcessingException + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + */ + public function testHasNoSigningParametersString(): void + { + $this->expectExceptionObject(ProcessingException::signingParametersString()); + + $targetUrl = 'http://localhost:8001/mastodon?param=value&foo=bar&baz=bat%2Dman'; + $certInstance = new CertHelper('rsa'); + $currentDateTime = new DateTime()->getTimestamp(); + $expiredDate = $currentDateTime + 7200; + $signingConfiguration = new SigningConfiguration() + ->setSigningAlgorithm(SigningEnum::rsa_v_1_5_sha_512) + ->setCreated($currentDateTime) + ->setExpires($expiredDate) + ->setTag('mastodon') + ->setKeyid('http://127.0.0.1:8001/actor#main-key') + ->withNonce('nonce-now') + + // content digest signer key + ->setSignatureKeyName('testValue=') + // for signing needs private + ->setPrivatePem($certInstance->getPrivatPemContent()) + ; + + /** + * Prepare the Request + */ + $request = new Request( + method : 'POST', + uri : $targetUrl, + headers: ['x-origin-host' => 'mytest.com'], + body : '{"recipient":"Alex","amount":900000000,"text":"fdawsfs"}', + ); + + $signer = new RequestSigner(message: $request, signingConfiguration: $signingConfiguration); + + $signer->getSigningParametersStringOrThrow(); + } } diff --git a/tests/Rfc9421/Signer/MetadataTest.php b/tests/Rfc9421/Signer/MetadataTest.php index 307c93e..53f7dd6 100644 --- a/tests/Rfc9421/Signer/MetadataTest.php +++ b/tests/Rfc9421/Signer/MetadataTest.php @@ -14,6 +14,8 @@ use BadMethodCallException; use DateTime; use Filefabrik\Rfc9421\Enums\SigningEnum; +use Filefabrik\Rfc9421\Exceptions\SignatureException; +use Filefabrik\Rfc9421\Exceptions\UnexpectedValue; use Filefabrik\Rfc9421\Signer\Metadata; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; @@ -128,7 +130,11 @@ public function testSetCreated(): void { $ts = new DateTime()->getTimestamp(); - self::assertEquals($ts, new Metadata()->setCreated($ts)->getCreated()); + self::assertEquals( + $ts, + new Metadata()->setCreated($ts) + ->getCreated(), + ); } /** @@ -157,4 +163,77 @@ public function testHasTag(): void ); self::assertFalse(new Metadata()->hasTag()); } + + /** + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException + */ + public function testSetStringOrNullThrow(): void + { + $this->expectExceptionObject(new UnexpectedValue('$value must be string|null')); + $md = new Metadata(); + $md->set('keyid', 1); + } + + /** + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException + */ + public function testsetStringOrSigningEnumThrow(): void + { + $this->expectExceptionObject( + new SignatureException('The signature algorithm must be a string|SigningEnumInterface.'), + ); + $md = new Metadata(); + $md->set('alg', 1); + } + + /** + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException + */ + public function testSetStringOrSigningEnumValid(): void + { + $md = new Metadata(); + $md->set('alg', SigningEnum::rsa_v_1_5_sha_256); + self::assertSame($md->alg, SigningEnum::rsa_v_1_5_sha_256); + + $md = new Metadata(); + $md->set('alg', SigningEnum::rsa_v_1_5_sha_256->value); + self::assertSame($md->alg, SigningEnum::rsa_v_1_5_sha_256); + + $md = new Metadata(); + $md->set('alg', SigningEnum::rsa_v_1_5_sha_256->name); + self::assertSame($md->alg, SigningEnum::rsa_v_1_5_sha_256); + } + + /** + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException + */ + public function testSetIntOrNullThrow(): void + { + $this->expectExceptionObject(new UnexpectedValue('$value must be int|null')); + $md = new Metadata(); + $md->set('created', 'string'); + } + + /** + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + */ + #[DataProvider('dataProviderToArray')] + public function testBindMetadata(...$datas): void + { + $inputMeta = Metadata::fromArray($datas) + ->toArray() + ; + $bondage = Metadata::bindMetadata(new Metadata(), $inputMeta); + self::assertSame( + $datas, + $bondage->toArray(), + + ); + } + } diff --git a/tests/Rfc9421/Signer/PrepareComponentsTest.php b/tests/Rfc9421/Signer/PrepareComponentsTest.php index 4e8ec50..67150de 100644 --- a/tests/Rfc9421/Signer/PrepareComponentsTest.php +++ b/tests/Rfc9421/Signer/PrepareComponentsTest.php @@ -206,6 +206,23 @@ public function testProcessAuthority(): void ); } + /** + * @throws \Filefabrik\Rfc9421\Exceptions\EnumTypeException + * @throws \Filefabrik\Rfc9421\Exceptions\PrepareMethodsException + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + */ + public function testProcessSignatureParamsThrow(): void + { + $this->expectExceptionObject( + new PrepareMethodsException( + 'Error during calling the method with key: "@signature-params": remove before processing signatureParams', + ), + ); + $r = new Request('post', 'https://www.Test.com/var/pathy/?qer=var', ['']); + + new PrepareComponents($r)->processValue('@signature-params'); + } + /** * @throws \Filefabrik\Rfc9421\Exceptions\EnumTypeException * @throws \Filefabrik\Rfc9421\Exceptions\PrepareMethodsException diff --git a/tests/Rfc9421/Signer/Signatures/ReadLinesTest.php b/tests/Rfc9421/Signer/Signatures/ReadLinesTest.php index d04bd2d..9f20f6e 100644 --- a/tests/Rfc9421/Signer/Signatures/ReadLinesTest.php +++ b/tests/Rfc9421/Signer/Signatures/ReadLinesTest.php @@ -11,6 +11,7 @@ namespace Filefabrik\Rfc9421Test\Signer\Signatures; +use Filefabrik\Rfc9421\Exceptions\UnexpectedValue; use Filefabrik\Rfc9421\Signer\Signatures\Line; use Filefabrik\Rfc9421\Signer\Signatures\ReadLines; use Filefabrik\Rfc9421\Signer\Signatures\WriteLines; @@ -20,6 +21,9 @@ #[CoversClass(ReadLines::class)] final class ReadLinesTest extends TestCase { + /** + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + */ public function testSetupArray(): void { $rl = new ReadLines(['foo' => 'bar', 'play' => 'book']); @@ -27,6 +31,9 @@ public function testSetupArray(): void self::assertEquals(['foo' => 'bar', 'play' => 'book'], $rl->toKeyValueArray()); } + /** + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + */ public function testReadWriteLines(): void { $wl = new WriteLines()->addEntry('foo', 'bar') @@ -38,6 +45,9 @@ public function testReadWriteLines(): void self::assertEquals(['foo' => 'bar', 'play' => 'book'], $rl->toKeyValueArray()); } + /** + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + */ public function testSetup(): void { $rl = new ReadLines([new Line('foo', 'bar'), new Line('play', 'book')]); @@ -45,10 +55,22 @@ public function testSetup(): void self::assertEquals(['foo' => 'bar', 'play' => 'book'], $rl->toKeyValueArray()); } + /** + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + */ public function testToLineString(): void { $rl = new ReadLines([new Line('"foo"', 'bar'), new Line('"play"', 'book')]); self::assertEquals(['"foo"' => '"foo": bar', '"play"' => '"play": book'], $rl->toLineString()); } + + /** + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + */ + public function testWrongFormat(): void + { + $this->expectExceptionObject(new UnexpectedValue('Unexpected value of type string or key of type integer')); + new ReadLines(['bar', 'book']); + } } diff --git a/tests/Rfc9421/Support/PreparedMethodsTest.php b/tests/Rfc9421/Support/PreparedMethodsTest.php index 378bdcc..ff59750 100644 --- a/tests/Rfc9421/Support/PreparedMethodsTest.php +++ b/tests/Rfc9421/Support/PreparedMethodsTest.php @@ -36,9 +36,12 @@ public function testGetMethod(): void [ 'texst' => fn (MessageInterface $message): string => $message->getMethod(), 'poster' => fn (MessageInterface $message): string => $message->getMethod(), + 'UpperCase' => fn (MessageInterface $message): string => $message->getMethod(), ], ); self::assertInstanceOf(Closure::class, $i->getMethod('poster')); + self::assertInstanceOf(Closure::class, $i->getMethod('uppercase')); + self::assertInstanceOf(Closure::class, $i->getMethod('UpperCase')); } /** diff --git a/tests/Rfc9421/Verify/HeaderSignatureInputTest.php b/tests/Rfc9421/Verify/HeaderSignatureInputTest.php index b88c953..dde4a0d 100644 --- a/tests/Rfc9421/Verify/HeaderSignatureInputTest.php +++ b/tests/Rfc9421/Verify/HeaderSignatureInputTest.php @@ -11,6 +11,7 @@ namespace Filefabrik\Rfc9421Test\Verify; +use Filefabrik\Rfc9421\Exceptions\SignatureExtractionException; use Filefabrik\Rfc9421\Exceptions\VerifyHeaderSignatureException; use Filefabrik\Rfc9421\SigningComponents; use Filefabrik\Rfc9421\Verify\HeaderSignatureInput; @@ -27,6 +28,7 @@ final class HeaderSignatureInputTest extends TestCase * @throws \Filefabrik\Rfc9421\Exceptions\SignatureExtractionException * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + * @throws \Filefabrik\Rfc9421\Exceptions\EnumTypeException */ public function testExtract(): void { @@ -55,6 +57,7 @@ public function testExtract(): void * @throws \Filefabrik\Rfc9421\Exceptions\SignatureExtractionException * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + * @throws \Filefabrik\Rfc9421\Exceptions\EnumTypeException */ public function testExtractWithoutKey(): void { @@ -136,4 +139,18 @@ public function testExtractInvalidSignature(): void $headerSignatureInput->extract(); } + + /** + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureExtractionException + */ + public function testGetExtractedComponents(): void + { + $this->expectExceptionObject(SignatureExtractionException::notProceeded()); + $expectedComponents = new SigningComponents(); + + $sig = 'someSig=()'; + $headerSignatureInput = new HeaderSignatureInput($sig, 'someSig='); + + self::assertEquals($expectedComponents, $headerSignatureInput->getExtractedComponents()); + } } diff --git a/tests/Rfc9421/VerifyRequestPrepareTest.php b/tests/Rfc9421/VerifyRequestPrepareTest.php index d62b5dd..2eeba86 100644 --- a/tests/Rfc9421/VerifyRequestPrepareTest.php +++ b/tests/Rfc9421/VerifyRequestPrepareTest.php @@ -16,6 +16,7 @@ use Filefabrik\Rfc9421\Enums\DigestAlgorithmsEnum; use Filefabrik\Rfc9421\Enums\SigningEnum; use Filefabrik\Rfc9421\Exceptions\VerifyHeaderSignatureException; +use Filefabrik\Rfc9421\Extractors\HeaderSignature; use Filefabrik\Rfc9421\RequestSigner; use Filefabrik\Rfc9421\SigningComponents; use Filefabrik\Rfc9421\SigningConfiguration; @@ -78,13 +79,13 @@ public static function withSigningMethods(): array */ #[DataProvider('withSigningMethods')] public function testPrepareVerify(string|SigningEnum $signingAlgo, string $cert): void - { require_once dirname(__DIR__, 2).'/src/register-openssl-signing-verify.php'; + { + require_once dirname(__DIR__, 2).'/src/register-openssl-signing-verify.php'; $certInstance = new CertHelper($cert); $signatureKeyName = 'testing='; $signingComponents = new SigningComponents()->sign('@method') ->sign('@authority') - ; /** * Prepare signingConfiguration @@ -167,8 +168,6 @@ public function testPrepareVerifyWithContentDigest(string|SigningEnum $signingAl $signatureKeyName = 'testing='; $signatureComponents = new SigningComponents()->sign('@method') ->sign('@authority') - - ; /** * Prepare signingConfiguration @@ -176,15 +175,15 @@ public function testPrepareVerifyWithContentDigest(string|SigningEnum $signingAl $currentDateTime = new DateTime()->getTimestamp(); $configuration = new SigningConfiguration(signingComponents: $signatureComponents); $configuration - ->setSigningAlgorithm($signingAlgo) - ->setCreated($currentDateTime) - ->setExpires($currentDateTime + 7200) - ->setTag('mastodon') - ->withNonce('nonce-now') - ->setSignatureKeyName($signatureKeyName) - ->withContentDigest(DigestAlgorithmsEnum::sha512) + ->setSigningAlgorithm($signingAlgo) + ->setCreated($currentDateTime) + ->setExpires($currentDateTime + 7200) + ->setTag('mastodon') + ->withNonce('nonce-now') + ->setSignatureKeyName($signatureKeyName) + ->withContentDigest(DigestAlgorithmsEnum::sha512) // for signing needs private - ->setPrivatePem($certInstance->getPrivatPemContent()) + ->setPrivatePem($certInstance->getPrivatPemContent()) ; /** @@ -221,6 +220,10 @@ public function testPrepareVerifyWithContentDigest(string|SigningEnum $signingAl * now prepare the verify process */ self::assertTrue($prepareVerifierInstance->prepareVerification()); + self::assertSame( + DigestAlgorithmsEnum::sha512, + $prepareVerifierInstance->signingConfiguration->contentDigestAlgo, + ); } /** @@ -252,6 +255,74 @@ public function testValidateThrowMissingSignatureInput(): void self::assertTrue($prepareVerifierInstance->prepareVerification()); } + /** + * @throws \Filefabrik\Rfc9421\Exceptions\AlreadySetException + * @throws \Filefabrik\Rfc9421\Exceptions\ContentDigestException + * @throws \Filefabrik\Rfc9421\Exceptions\EmptyException + * @throws \Filefabrik\Rfc9421\Exceptions\EnumTypeException + * @throws \Filefabrik\Rfc9421\Exceptions\InvalidHeaderKeyException + * @throws \Filefabrik\Rfc9421\Exceptions\MethodException + * @throws \Filefabrik\Rfc9421\Exceptions\PrepareMethodsException + * @throws \Filefabrik\Rfc9421\Exceptions\ProcessingException + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureExtractionException + * @throws \Filefabrik\Rfc9421\Exceptions\StructuredFieldException + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + * @throws \Filefabrik\Rfc9421\Exceptions\VerifyHeaderSignatureException + * @throws JsonException + */ + public function testValidateThrowMissingSignatureLines(): void + { + $this->expectExceptionObject(new VerifyHeaderSignatureException('Missing signature')); + $signingAlgo = SigningEnum::rsa_v_1_5_sha_512->name; + $cert = 'rsa'; + $certInstance = new CertHelper($cert); + + $signatureKeyName = 'testing='; + $signatureComponents = new SigningComponents()->sign('@method') + ->sign('@authority') + ; + /** + * Prepare signingConfiguration + */ + $currentDateTime = new DateTime()->getTimestamp(); + $configuration = new SigningConfiguration(signingComponents: $signatureComponents); + $configuration + ->setSigningAlgorithm($signingAlgo) + ->setCreated($currentDateTime) + ->setExpires($currentDateTime + 7200) + ->setTag('mastodon') + ->withNonce('nonce-now') + ->setSignatureKeyName($signatureKeyName) + ->withContentDigest(DigestAlgorithmsEnum::sha512) + // for signing needs private + ->setPrivatePem($certInstance->getPrivatPemContent()) + ; + + /** + * Prepare the Request + */ + $bodyParams = ['recipient' => 'Alex', 'amount' => 900, 'currency' => 'EUR']; + $bodyJson = json_encode($bodyParams, JSON_THROW_ON_ERROR); + $request = new Request( + method : 'POST', + uri : 'https://target-host.com', + headers: ['x-origin-host' => 'target-host.com'], + body : $bodyJson, + ); + + $signer = new RequestSigner($request, $configuration); + $signedRequest = $signer->sign(); + + // Kills signature to provocate error + $signedRequest = $signedRequest->withoutHeader('signature'); + + $prepareVerifierInstance = new VerifyRequestPrepare( + message : $signedRequest, + signingConfiguration: new SigningConfiguration(), + )->prepareVerification(); + } + /** * @throws \Filefabrik\Rfc9421\Exceptions\VerifyHeaderSignatureException * @throws \Filefabrik\Rfc9421\Exceptions\ContentDigestException @@ -323,8 +394,7 @@ public function testGetSignatureInputInstance(): void $certInstance = new CertHelper('rsa'); $signatureComponents = new SigningComponents()->sign('@method') - ->sign('@authority') - /* + ->sign('@authority')/* * todo check duplicate content-digest * ->withHeader('content-digest') */ @@ -363,6 +433,8 @@ public function testGetSignatureInputInstance(): void self::assertInstanceOf(HeaderSignatureInput::class, $prepareVerifierInstance->getSignatureInputInstance()); } + + public function testGetHeaderHelper(): void { $prepareVerifierInstance = new VerifyRequestPrepare( @@ -372,4 +444,64 @@ public function testGetHeaderHelper(): void self::assertInstanceOf(HeaderHelper::class, $prepareVerifierInstance->getHeaderHelper()); } + /** + * @throws \Filefabrik\Rfc9421\Exceptions\MethodException + * @throws \Filefabrik\Rfc9421\Exceptions\ProcessingException + * @throws \Filefabrik\Rfc9421\Exceptions\StructuredFieldException + * @throws \Filefabrik\Rfc9421\Exceptions\VerifyHeaderSignatureException + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureExtractionException + * @throws \Filefabrik\Rfc9421\Exceptions\EnumTypeException + * @throws \Filefabrik\Rfc9421\Exceptions\InvalidHeaderKeyException + * @throws \Filefabrik\Rfc9421\Exceptions\PrepareMethodsException + * @throws \Filefabrik\Rfc9421\Exceptions\UnexpectedValue + * @throws \Filefabrik\Rfc9421\Exceptions\EmptyException + * @throws JsonException + * @throws \Filefabrik\Rfc9421\Exceptions\ContentDigestException + * @throws \Filefabrik\Rfc9421\Exceptions\AlreadySetException + * @throws \Filefabrik\Rfc9421\Exceptions\SignatureException + */ + public function testGetHeaderSignatureExtractor(): void + { + $certInstance = new CertHelper('rsa'); + + $signatureComponents = new SigningComponents()->sign('@method') + ->sign('@authority')/* + * todo check duplicate content-digest + * ->withHeader('content-digest') + */ + + ; + $signatureKeyName = 'testing='; + $bodyParams = ['recipient' => 'Alex', 'amount' => 900, 'currency' => 'EUR']; + $bodyJson = json_encode($bodyParams, JSON_THROW_ON_ERROR); + $request = new Request( + method : 'POST', + uri : 'https://target-host.com', + headers: ['x-origin-host' => 'target-host.com'], + body : $bodyJson, + ); + $currentDateTime = new DateTime()->getTimestamp(); + $configuration = new SigningConfiguration(signingComponents: $signatureComponents); + $configuration + ->setSigningAlgorithm(SigningEnum::rsa_v_1_5_sha_256) + ->setCreated($currentDateTime) + ->setExpires($currentDateTime + 7200) + ->setTag('mastodon') + ->withNonce('nonce-now') + ->setSignatureKeyName($signatureKeyName) + ->setContentDigestAlgo(DigestAlgorithmsEnum::sha512) + // for signing needs private + ->setPrivatePem($certInstance->getPrivatPemContent()) + ; + + $signer = new RequestSigner($request, $configuration); + $signedRequest = $signer->sign(); + $prepareVerifierInstance = new VerifyRequestPrepare( + message : $signedRequest, + signingConfiguration: new SigningConfiguration(), + ); + $prepareVerifierInstance->prepareVerification(); + $hs = $prepareVerifierInstance->getHeaderSignatureExtractor(); + self::assertTrue($hs instanceof HeaderSignature); + } } diff --git a/tests/Rfc9421/VerifyRequestTest.php b/tests/Rfc9421/VerifyRequestTest.php index 1ac0e25..ad1cd33 100644 --- a/tests/Rfc9421/VerifyRequestTest.php +++ b/tests/Rfc9421/VerifyRequestTest.php @@ -17,6 +17,7 @@ use Filefabrik\Rfc9421\Enums\SigningEnum; use Filefabrik\Rfc9421\Exceptions\ProcessingException; use Filefabrik\Rfc9421\RequestSigner; +use Filefabrik\Rfc9421\Signer\Signatures\ReadLines; use Filefabrik\Rfc9421\SigningComponents; use Filefabrik\Rfc9421\SigningConfiguration; use Filefabrik\Rfc9421\VerifyRequest; @@ -117,6 +118,7 @@ public function testValidate(string|SigningEnum $signingAlgo, string $cert): voi self::assertEquals(1, $verifier->verifying()); // must be true because body(content-digest) was part of the signing process self::assertTrue($verifier->hasSignedBody()); + self::assertInstanceOf(ReadLines::class,$verifier->getSignedHeaderLines()); } /**