Skip to content

chore: test cleanup #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2021
Merged
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
63 changes: 28 additions & 35 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ public function setExpectedException($exceptionName, $message = '', $code = null
}
}

public function testEncodeDecode()
{
$msg = JWT::encode('abc', 'my_key');
$this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
}

public function testDecodeFromPython()
{
$msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg';
Expand Down Expand Up @@ -217,18 +211,6 @@ public function testEmptyKeyFails()
JWT::decode($encoded, '', array('HS256'));
}

public function testRSEncodeDecode()
{
$privKey = openssl_pkey_new(array('digest_alg' => 'sha256',
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA));
$msg = JWT::encode('abc', $privKey, 'RS256');
$pubKey = openssl_pkey_get_details($privKey);
$pubKey = $pubKey['key'];
$decoded = JWT::decode($msg, $pubKey, array('RS256'));
$this->assertEquals($decoded, 'abc');
}

public function testKIDChooser()
{
$keys = array('1' => 'my_key', '2' => 'my_key2');
Expand Down Expand Up @@ -285,35 +267,46 @@ public function testInvalidSignatureEncoding()
JWT::decode($msg, 'secret', array('HS256'));
}

/**
* @runInSeparateProcess
*/
public function testEncodeAndDecodeEcdsaToken()
public function testHSEncodeDecode()
{
$privateKey = file_get_contents(__DIR__ . '/ecdsa-private.pem');
$payload = array('foo' => 'bar');
$encoded = JWT::encode($payload, $privateKey, 'ES256');

// Verify decoding succeeds
$publicKey = file_get_contents(__DIR__ . '/ecdsa-public.pem');
$decoded = JWT::decode($encoded, $publicKey, array('ES256'));
$msg = JWT::encode('abc', 'my_key');
$this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
}

$this->assertEquals('bar', $decoded->foo);
public function testRSEncodeDecode()
{
$privKey = openssl_pkey_new(array('digest_alg' => 'sha256',
'private_key_bits' => 1024,
'private_key_type' => OPENSSL_KEYTYPE_RSA));
$msg = JWT::encode('abc', $privKey, 'RS256');
$pubKey = openssl_pkey_get_details($privKey);
$pubKey = $pubKey['key'];
$decoded = JWT::decode($msg, $pubKey, array('RS256'));
$this->assertEquals($decoded, 'abc');
}

/**
* @runInSeparateProcess
* @dataProvider provideEncodeDecode
*/
public function testEncodeAndDecodeEcdsa384Token()
public function testEncodeDecode($privateKeyFile, $publicKeyFile, $alg)
{
$privateKey = file_get_contents(__DIR__ . '/ecdsa384-private.pem');
$privateKey = file_get_contents($privateKeyFile);
$payload = array('foo' => 'bar');
$encoded = JWT::encode($payload, $privateKey, 'ES384');
$encoded = JWT::encode($payload, $privateKey, $alg);

// Verify decoding succeeds
$publicKey = file_get_contents(__DIR__ . '/ecdsa384-public.pem');
$decoded = JWT::decode($encoded, $publicKey, array('ES384'));
$publicKey = file_get_contents($publicKeyFile);
$decoded = JWT::decode($encoded, $publicKey, array($alg));

$this->assertEquals('bar', $decoded->foo);
}

public function provideEncodeDecode()
{
return array(
array(__DIR__ . '/ecdsa-private.pem', __DIR__ . '/ecdsa-public.pem', 'ES256'),
array(__DIR__ . '/ecdsa384-private.pem', __DIR__ . '/ecdsa384-public.pem', 'ES384'),
);
}
}