authjwt : Update to use the local cache to store the identifier data.

git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@5814 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2019-12-09 13:13:40 +00:00
parent f3337996b9
commit 1ab8462266
2 changed files with 129 additions and 53 deletions

View File

@@ -7,18 +7,32 @@
/** Test the authjwt.php file */
class authjwtTest extends PHPUnit_Framework_TestCase
{
/** Generate a JWT valid token
public function __construct ()
{
$this->cacheDir = "/tmp/testDFWJWT-".time ();
$this->serverKey = "123456789012345678901234";
$this->cipherKey = "EC17kIvjD66fBJHbQRkPguhu";
$this->token = null;
}
public function __destruct ()
{
exec ("rm -rf $this->cacheDir");
}
/** Create a valid token as email is provided
* payload = ["email" => "toto@example.com", "password" => "ToTo"];
*/
public function testCreateKey1 ()
public function testJWT1 ()
// {{{
{
$jwt = new jwt ();
$this->serverKey = $jwt->createKey ();
$payload = ["email" => "toto@example.com", "password" => "ToTo"];
$this->cipherKey = "123456789012345678901234";
$this->token = $jwt->encode ($payload, $this->serverKey, "HS256",
$this->cipherKey);
$authjwt = new authjwt ();
$authjwt->cacheDir = $this->cacheDir;
$authjwt->serverKey = $this->serverKey;
$authjwt->cipherKey = $this->cipherKey;
$auth = ["email" => "toto@example.com", "password" => "ToTo"];
$this->token = $authjwt->createJwtToken ($auth);
$this->assertSame (strlen ($this->token), 145);
}
// }}}
@@ -29,11 +43,11 @@ class authjwtTest extends PHPUnit_Framework_TestCase
{
$authjwt = new authjwt ();
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer ".$this->token;
$authjwt->cacheDir = $this->cacheDir;
$authjwt->serverKey = $this->serverKey;
$authjwt->cipherKey = $this->cipherKey;
$authjwt->authentication ("unused", "unused");
$res = $authjwt->getdetails ();
unset ($res["bearer"]);
$this->assertSame ($res,
["email" => "toto@example.com", "password" => "ToTo"]);
}
@@ -47,6 +61,7 @@ class authjwtTest extends PHPUnit_Framework_TestCase
$this->expectException ("Exception", "JWT Signature not readable", 403);
$authjwt = new authjwt ();
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer ".$this->token."NO";
$authjwt->cacheDir = $this->cacheDir;
$authjwt->serverKey = $this->serverKey;
$authjwt->cipherKey = $this->cipherKey;
$authjwt->authentication ("unused", "unused");
@@ -62,6 +77,7 @@ class authjwtTest extends PHPUnit_Framework_TestCase
$this->expectException ("Exception", "JWT with Empty algorithm", 403);
$authjwt = new authjwt ();
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer "."NO".$this->token;
$authjwt->cacheDir = $this->cacheDir;
$authjwt->serverKey = $this->serverKey;
$authjwt->cipherKey = $this->cipherKey;
$authjwt->authentication ("unused", "unused");
@@ -77,6 +93,7 @@ class authjwtTest extends PHPUnit_Framework_TestCase
$this->expectException ("Exception", "No Authentication available", 401);
$authjwt = new authjwt ();
unset ($_SERVER["HTTP_AUTHENTICATION"]);
$authjwt->cacheDir = $this->cacheDir;
$authjwt->serverKey = $this->serverKey;
$authjwt->cipherKey = $this->cipherKey;
$authjwt->authentication ("unused", "unused");
@@ -93,6 +110,7 @@ class authjwtTest extends PHPUnit_Framework_TestCase
401);
$authjwt = new authjwt ();
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer";
$authjwt->cacheDir = $this->cacheDir;
$authjwt->serverKey = $this->serverKey;
$authjwt->cipherKey = $this->cipherKey;
$authjwt->authentication ("unused", "unused");
@@ -105,15 +123,15 @@ class authjwtTest extends PHPUnit_Framework_TestCase
public function testInvalidToken5 ()
// {{{
{
$this->expectException ("Exception", "No email available in Bearer", 403);
$jwt = new jwt ();
$payload = ["password" => "ToTo"];
$token = $jwt->encode ($payload, $this->serverKey, "HS256",
$this->cipherKey);
$this->expectException ("Exception",
"AuthJWT : No email available in auth", 403);
$auth = ["password" => "ToTo"];
$authjwt = new authjwt ();
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer $token";
$authjwt->cacheDir = $this->cacheDir;
$authjwt->serverKey = $this->serverKey;
$authjwt->cipherKey = $this->cipherKey;
$token = $authjwt->createJwtToken ($auth);
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer $token";
$authjwt->authentication ("unused", "unused");
$res = $authjwt->getdetails ();
}
@@ -124,20 +142,31 @@ class authjwtTest extends PHPUnit_Framework_TestCase
public function testAnonymous1 ()
// {{{
{
$jwt = new jwt ();
$payload = ["email" => "anonymous"];
$token = $jwt->encode ($payload, $this->serverKey, "HS256",
$this->cipherKey);
$this->expectException ("Exception",
"AuthJWT : can not create token for anonymous", 403);
$auth = ["email" => "anonymous"];
$authjwt = new authjwt ();
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer $token";
$authjwt->cacheDir = $this->cacheDir;
$authjwt->serverKey = $this->serverKey;
$authjwt->cipherKey = $this->cipherKey;
$token = $authjwt->createJwtToken ($auth);
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer $token";
$authjwt->authentication ("unused", "unused");
$res = $authjwt->getdetails ();
$this->assertSame ($res,
array ("lastname" => "anonymous",
"firstname" => "",
"email" => "anonymous"));
}
// }}}
/** Logout
*/
public function testLogout1 ()
// {{{
{
$authjwt = new authjwt ();
$authjwt->cacheDir = $this->cacheDir;
$authjwt->serverKey = $this->serverKey;
$authjwt->cipherKey = $this->cipherKey;
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer $this->token";
$res = $authjwt->logout ();
}
// }}}
@@ -175,16 +204,4 @@ class authjwtTest extends PHPUnit_Framework_TestCase
$res = $authjwt->overwritepassword ("unused", "unused");
}
// }}}
/** Not needed function logout
*/
public function testUnusedFunctions4 ()
// {{{
{
$this->expectException ("Exception",
"The logout is not available for JWT users", 405);
$authjwt = new authjwt ();
$res = $authjwt->logout ();
}
// }}}
}