Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions tests/JWKTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
class JWKTest extends TestCase
{
private static $keys;
private static $privKey1;
private static $privKey2;

public static function setUpBeforeClass(): void
{
$jwkSet = json_decode(
file_get_contents(__DIR__ . '/data/rsa-jwkset.json'),
true
);
self::$keys = JWK::parseKeySet($jwkSet);
}

public function testMissingKty()
{
Expand Down Expand Up @@ -84,16 +91,33 @@ public function testParseKeyWithEmptyDValue()
$this->assertTrue(\is_array($keys));
}

public function testParseJwkKeySet()
/** @dataProvider provideParseJwkKeySet */
public function testParseJwkKeySet($jwkFile, $keyId, $pubkeyFile)
{
$jwkSet = json_decode(
file_get_contents(__DIR__ . '/data/rsa-jwkset.json'),
file_get_contents(__DIR__ . '/data/' . $jwkFile),
true
);
$keys = JWK::parseKeySet($jwkSet);
$this->assertTrue(\is_array($keys));
$this->assertArrayHasKey('jwk1', $keys);
self::$keys = $keys;
$this->assertArrayHasKey($keyId, $keys);

// verify public key
$keyMaterial = $keys[$keyId]->getKeyMaterial();
$publicKey = openssl_pkey_get_details($keyMaterial)['key'];

$this->assertEquals(
file_get_contents(__DIR__ . '/data/' . $pubkeyFile),
$publicKey
);
Comment on lines +109 to +112
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Comparing PEM strings directly can be brittle due to differences in line endings (CRLF vs LF) or trailing newlines across different operating systems and OpenSSL versions. It is recommended to trim() both the expected and actual strings to make the test more robust and avoid failures caused by trailing whitespace.

        $this->assertEquals(
            trim(file_get_contents(__DIR__ . '/data/' . $pubkeyFile)),
            trim($publicKey)
        );

}

public function provideParseJwkKeySet()
{
return [
['rsa-jwkset.json', 'jwk1', 'rsa1-public.pub'],
['rsa-jwkset-2.json', 'jwk2', 'rsa-jwk2-public.pub'],
];
}

public function testParseJwkKey_empty()
Expand Down
9 changes: 9 additions & 0 deletions tests/data/rsa-jwk2-public.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyKqGRQyJtqxRm/Mo2YTC
CAkPSDb7uNgC7tXjgVzNv2/XB8r4vMibBpZFPbwyVUk0wGhPk8qLjrIj/K8IMu/I
Ytkq87pc1/1FAOub7e3xUrMx66GCq8QG94xROSfDWuMy7twILwjbkzNEU6bNibM0
IQbCvdybFPhq4YHvlwOjfuMl2mNUma8wT1/l2MZenV1dmeLTg/kYGe9PGmn9JiY4
t01Nj1FJQj9rH863KAa3LadQ4l8aBOpaIZwjANo3GCJJd4uSB67G+p0wuuDDYbiU
GtN55degXjDKrv3v5bLgpPMX6ynvt2bi0olb/QZfovTnUaLfsZpCXTk/CvUXr2Q2
KwIDAQAB
-----END PUBLIC KEY-----
11 changes: 11 additions & 0 deletions tests/data/rsa-jwkset-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"keys": [
{
"kty": "RSA",
"e": "AQAB",
"kid": "jwk2",
"alg": "RS256",
"n": "yKqGRQyJtqxRm_Mo2YTCCAkPSDb7uNgC7tXjgVzNv2_XB8r4vMibBpZFPbwyVUk0wGhPk8qLjrIj_K8IMu_IYtkq87pc1_1FAOub7e3xUrMx66GCq8QG94xROSfDWuMy7twILwjbkzNEU6bNibM0IQbCvdybFPhq4YHvlwOjfuMl2mNUma8wT1_l2MZenV1dmeLTg_kYGe9PGmn9JiY4t01Nj1FJQj9rH863KAa3LadQ4l8aBOpaIZwjANo3GCJJd4uSB67G-p0wuuDDYbiUGtN55degXjDKrv3v5bLgpPMX6ynvt2bi0olb_QZfovTnUaLfsZpCXTk_CvUXr2Q2Kw"
}
]
}
Loading