JWT : allow to encrypt the payload

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5782 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2019-12-04 09:44:15 +00:00
parent 23d54f577d
commit 0bd6889427
2 changed files with 120 additions and 4 deletions

View File

@@ -133,4 +133,44 @@ class test_jwt extends PHPUnit_Framework_TestCase
"eyJwYXlsb2FkIjoidmFsdWUifQ",
"key to use");
}
///////////////////////////////
// ENCRYPT THE PAYLOAD //
///////////////////////////////
/** Check the length of the otken with cipher
*/
public function testEncrypt1 ()
{
$jwt = new jwt ();
$key = $jwt->createKey ();
$res = $jwt->encode (
["email" => "toto@example.com", "password" => "ToTo"],
$key, "HS256", "123456789012345678901234");
$this->assertSame (strlen ($res), 156);
}
/** Check if the encrypt/decrypt process return the same result
*/
public function testEncrypyt2 ()
{
$jwt = new jwt ();
$key = $jwt->createKey ();
$payload = (object)["email" => "toto@example.com", "password" => "ToTo"];
$token = $jwt->encode ($payload, $key, "HS256", "123456789012345678901234");
$res = $jwt->decode ($token, $key, null, "123456789012345678901234");
$this->assertSame ($res, $payload);
}
/** Check if the encrypted part is well unreadable
*/
public function testEncrypt3 ()
{
$jwt = new jwt ();
$key = $jwt->createKey ();
$payload = (object)["email" => "toto@example.com", "password" => "ToTo"];
$token = $jwt->encode ($payload, $key, "HS256", "123456789012345678901234");
list ($header, $payload, $signature) = explode (".", $token);
$res = strpos (base64_decode ($payload), "email");
$this->assertSame ($res, false);
}
}