Add JSON Web Token class
git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5283 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
133
Tests/jwtTest.php
Normal file
133
Tests/jwtTest.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/** DomFramework - Tests
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
*/
|
||||
|
||||
/** Test the jwt.php file */
|
||||
class test_jwt extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function test_createKey_1 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$res = $jwt->createKey ();
|
||||
$this->assertSame (40, strlen ($res));
|
||||
}
|
||||
|
||||
public function test_sign_1 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$res = $jwt->sign ("TEXT TO SIGN", "KEY TO USE", "HS384");
|
||||
$this->assertSame (
|
||||
"cQB+yNVvIER+Nw53MZfU/PGPAJlkKUnjMikmXAwVB9tcaINQH5a88LCDi0PmI5mZ",
|
||||
base64_encode ($res));
|
||||
}
|
||||
|
||||
public function test_sign_2 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$res = $jwt->sign ("text to sign", "KEY TO USE", "HS384");
|
||||
$this->assertSame (
|
||||
"FLSkslsUGIpkP3xsJx5ephnCtH7K4jZSNxRxxCn3m7fsPK/MMfEIVr+h3heap80x",
|
||||
base64_encode ($res));
|
||||
}
|
||||
|
||||
public function test_sign_3 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$res = $jwt->sign ("text to sign", "key to use", "HS384");
|
||||
$this->assertSame (
|
||||
"lBLlXb5Xo3z9zoEuO0obZdhqGNUKr8DaEsL991TpSPWIdB2067ckR+AJ1FW6in2B",
|
||||
base64_encode ($res));
|
||||
}
|
||||
|
||||
public function test_encode_1 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$res = $jwt->encode (array ("payload" => "value"), "key to use", "HS384");
|
||||
$this->assertSame (
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.".
|
||||
"eyJwYXlsb2FkIjoidmFsdWUifQ.".
|
||||
"0ByHaODQQjYEvmgU2u5LI034RRMc7CKJQ752ys19Fqj7QiTJO7-trerYKCxCyuge", $res);
|
||||
}
|
||||
|
||||
public function test_decode_1 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$res = $jwt->decode (
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.".
|
||||
"eyJwYXlsb2FkIjoidmFsdWUifQ.".
|
||||
"0ByHaODQQjYEvmgU2u5LI034RRMc7CKJQ752ys19Fqj7QiTJO7-trerYKCxCyuge",
|
||||
"key to use");
|
||||
$this->assertSame ((object) (array ("payload" => "value")), $res);
|
||||
}
|
||||
|
||||
public function test_decode_2 ()
|
||||
{
|
||||
$GLOBALS["hash_equals"] = false;
|
||||
$jwt = new jwt ();
|
||||
$res = $jwt->decode (
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.".
|
||||
"eyJwYXlsb2FkIjoidmFsdWUifQ.".
|
||||
"0ByHaODQQjYEvmgU2u5LI034RRMc7CKJQ752ys19Fqj7QiTJO7-trerYKCxCyuge",
|
||||
"key to use");
|
||||
$this->assertSame ((object) (array ("payload" => "value")), $res);
|
||||
}
|
||||
|
||||
public function test_decode_3 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$this->expectException ("Exception", "JWT Header not readable");
|
||||
$res = $jwt->decode (
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUXXXXXJ9.".
|
||||
"eyJwYXlsb2FkIjoidmFsdWUifQ.".
|
||||
"0ByHaODQQjYEvmgU2u5LI034RRMc7CKJQ752ys19Fqj7QiTJO7-trerYKCxCyuge",
|
||||
"key to use");
|
||||
}
|
||||
|
||||
public function test_decode_4 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$this->expectException ("Exception", "JWT Payload not readable");
|
||||
$res = $jwt->decode (
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.".
|
||||
"eyJwYXlsb2FkIjoiXXXXXXXXfQ.".
|
||||
"0ByHaODQQjYEvmgU2u5LI034RRMc7CKJQ752ys19Fqj7QiTJO7-trerYKCxCyuge",
|
||||
"key to use");
|
||||
}
|
||||
|
||||
public function test_decode_5 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$this->expectException ("Exception",
|
||||
"JWT Signature verification failed");
|
||||
$res = $jwt->decode (
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.".
|
||||
"eyJwYXlsb2FkIjoidmFsdWUifQ.".
|
||||
"1ByHaODQQjYEvmgU2u5LI034RRMc7CKJQ752ys19Fqj7QiTJO7-trerYKCxCyuge",
|
||||
"key to use");
|
||||
}
|
||||
|
||||
public function test_decode_6 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$this->expectException ("Exception",
|
||||
"JWT Signature not readable");
|
||||
$res = $jwt->decode (
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.".
|
||||
"eyJwYXlsb2FkIjoidmFsdWUifQ.".
|
||||
"0",
|
||||
"key to use");
|
||||
}
|
||||
|
||||
public function test_decode_7 ()
|
||||
{
|
||||
$jwt = new jwt ();
|
||||
$this->expectException ("Exception",
|
||||
"Malformed JWT Token");
|
||||
$res = $jwt->decode (
|
||||
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzM4NCJ9.".
|
||||
"eyJwYXlsb2FkIjoidmFsdWUifQ",
|
||||
"key to use");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user