1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
<?php
declare(strict_types=1);
namespace Lcobucci\JWT\Tests\Signer\Rsa;
use Lcobucci\JWT\Signer\CannotSignPayload;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\OpenSSL;
use PHPUnit\Framework\Attributes as PHPUnit;
use PHPUnit\Framework\TestCase;
use function openssl_error_string;
use const OPENSSL_ALGO_SHA256;
use const PHP_EOL;
#[PHPUnit\CoversClass(OpenSSL::class)]
#[PHPUnit\CoversClass(CannotSignPayload::class)]
#[PHPUnit\UsesClass(InMemory::class)]
final class KeyValidationTest extends TestCase
{
#[PHPUnit\After]
public function clearOpenSSLErrors(): void
{
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedWhile
while (openssl_error_string()) {
}
}
#[PHPUnit\Test]
public function signShouldRaiseAnExceptionWhenKeyIsInvalid(): void
{
$key = <<<'KEY'
-----BEGIN RSA PRIVATE KEY-----
MGECAQACEQC4MRKSVsq5XnRBrJoX6+rnAgMBAAECECO8SZkgw6Yg66A6SUly/3kC
CQDtPXZtCQWJuwIJAMbBu17GDOrFAggopfhNlFcjkwIIVjb7G+U0/TECCEERyvxP
TWdN
-----END RSA PRIVATE KEY-----
KEY;
$this->expectException(CannotSignPayload::class);
$this->expectExceptionMessage('There was an error while creating the signature:' . PHP_EOL . '* error:');
$this->algorithm()->sign('testing', InMemory::plainText($key));
}
private function algorithm(): OpenSSL
{
return new class () extends OpenSSL
{
// phpcs:ignore SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
protected function guardAgainstIncompatibleKey(int $type, int $lengthInBits): void
{
}
public function algorithm(): int
{
return OPENSSL_ALGO_SHA256;
}
public function algorithmId(): string
{
return 'RS256';
}
public function sign(string $payload, Key $key): string
{
return $this->createSignature($key->contents(), $key->passphrase(), $payload);
}
public function verify(string $expected, string $payload, Key $key): bool
{
return $this->verifySignature($expected, $payload, $key->contents());
}
};
}
}
|