PSR12
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/** DomFramework - Tests
|
||||
* @package domframework
|
||||
* @author Dominique Fournier <dominique@fournier38.fr>
|
||||
@@ -12,234 +13,254 @@ use Domframework\Authjwt;
|
||||
/** Test the Authjwt.php file */
|
||||
class AuthjwtTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function __construct ()
|
||||
{
|
||||
$this->cacheDir = "/tmp/testDFWJWT-".time ();
|
||||
$this->serverKey = "123456789012345678901234";
|
||||
$this->cipherKey = "EC17kIvjD66fBJHbQRkPguhu";
|
||||
$this->token = null;
|
||||
}
|
||||
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");
|
||||
}
|
||||
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 testJWT1 ()
|
||||
public function testJWT1()
|
||||
// {{{
|
||||
{
|
||||
$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);
|
||||
}
|
||||
{
|
||||
$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);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Check if the authentication work
|
||||
*/
|
||||
public function testAuthValid1 ()
|
||||
public function testAuthValid1()
|
||||
// {{{
|
||||
{
|
||||
$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 ();
|
||||
$this->assertSame ($res,
|
||||
["email" => "toto@example.com", "password" => "ToTo"]);
|
||||
}
|
||||
{
|
||||
$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();
|
||||
$this->assertSame(
|
||||
$res,
|
||||
["email" => "toto@example.com", "password" => "ToTo"]
|
||||
);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Invalid Token : reject with invalid signature
|
||||
*/
|
||||
public function testInvalidToken1 ()
|
||||
public function testInvalidToken1()
|
||||
// {{{
|
||||
{
|
||||
$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");
|
||||
$res = $authjwt->getdetails ();
|
||||
}
|
||||
{
|
||||
$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");
|
||||
$res = $authjwt->getdetails();
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Invalid Token : reject with bad algorithm
|
||||
*/
|
||||
public function testInvalidToken2 ()
|
||||
public function testInvalidToken2()
|
||||
// {{{
|
||||
{
|
||||
$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");
|
||||
$res = $authjwt->getdetails ();
|
||||
}
|
||||
{
|
||||
$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");
|
||||
$res = $authjwt->getdetails();
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Invalid Token : No token provided
|
||||
*/
|
||||
public function testInvalidToken3 ()
|
||||
public function testInvalidToken3()
|
||||
// {{{
|
||||
{
|
||||
$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");
|
||||
$res = $authjwt->getdetails ();
|
||||
}
|
||||
{
|
||||
$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");
|
||||
$res = $authjwt->getdetails();
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Invalid Token : No Bearer authentication
|
||||
*/
|
||||
public function testInvalidToken4 ()
|
||||
public function testInvalidToken4()
|
||||
// {{{
|
||||
{
|
||||
$this->expectException ("Exception", "No Bearer Authentication available",
|
||||
401);
|
||||
$authjwt = new Authjwt ();
|
||||
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer";
|
||||
$authjwt->cacheDir = $this->cacheDir;
|
||||
$authjwt->serverKey = $this->serverKey;
|
||||
$authjwt->cipherKey = $this->cipherKey;
|
||||
$authjwt->authentication ("unused", "unused");
|
||||
$res = $authjwt->getdetails ();
|
||||
}
|
||||
{
|
||||
$this->expectException(
|
||||
"Exception",
|
||||
"No Bearer Authentication available",
|
||||
401
|
||||
);
|
||||
$authjwt = new Authjwt();
|
||||
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer";
|
||||
$authjwt->cacheDir = $this->cacheDir;
|
||||
$authjwt->serverKey = $this->serverKey;
|
||||
$authjwt->cipherKey = $this->cipherKey;
|
||||
$authjwt->authentication("unused", "unused");
|
||||
$res = $authjwt->getdetails();
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Invalid Token : no email in it
|
||||
*/
|
||||
public function testInvalidToken5 ()
|
||||
public function testInvalidToken5()
|
||||
// {{{
|
||||
{
|
||||
$this->expectException ("Exception",
|
||||
"AuthJWT : No email available in auth", 403);
|
||||
$auth = ["password" => "ToTo"];
|
||||
$authjwt = new Authjwt ();
|
||||
$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->expectException(
|
||||
"Exception",
|
||||
"AuthJWT : No email available in auth",
|
||||
403
|
||||
);
|
||||
$auth = ["password" => "ToTo"];
|
||||
$authjwt = new Authjwt();
|
||||
$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();
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Anonymous payload
|
||||
*/
|
||||
public function testAnonymous1 ()
|
||||
public function testAnonymous1()
|
||||
// {{{
|
||||
{
|
||||
$this->expectException ("Exception",
|
||||
"AuthJWT : can not create token for anonymous", 403);
|
||||
$auth = ["email" => "anonymous"];
|
||||
$authjwt = new Authjwt ();
|
||||
$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->expectException(
|
||||
"Exception",
|
||||
"AuthJWT : can not create token for anonymous",
|
||||
403
|
||||
);
|
||||
$auth = ["email" => "anonymous"];
|
||||
$authjwt = new Authjwt();
|
||||
$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();
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Logout
|
||||
*/
|
||||
public function testLogout1 ()
|
||||
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 ();
|
||||
$this->assertSame ($res, true);
|
||||
}
|
||||
{
|
||||
$authjwt = new Authjwt();
|
||||
$authjwt->cacheDir = $this->cacheDir;
|
||||
$authjwt->serverKey = $this->serverKey;
|
||||
$authjwt->cipherKey = $this->cipherKey;
|
||||
$_SERVER["HTTP_AUTHENTICATION"] = "Bearer $this->token";
|
||||
$res = $authjwt->logout();
|
||||
$this->assertSame($res, true);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Logout : No Auth provided
|
||||
*/
|
||||
public function testLogout2 ()
|
||||
public function testLogout2()
|
||||
// {{{
|
||||
{
|
||||
$this->expectException ("Exception", "No Authentication available", 401);
|
||||
$authjwt = new Authjwt ();
|
||||
$authjwt->cacheDir = $this->cacheDir;
|
||||
$authjwt->serverKey = $this->serverKey;
|
||||
$authjwt->cipherKey = $this->cipherKey;
|
||||
unset ($_SERVER["HTTP_AUTHENTICATION"]);
|
||||
$res = $authjwt->logout ();
|
||||
}
|
||||
{
|
||||
$this->expectException("Exception", "No Authentication available", 401);
|
||||
$authjwt = new Authjwt();
|
||||
$authjwt->cacheDir = $this->cacheDir;
|
||||
$authjwt->serverKey = $this->serverKey;
|
||||
$authjwt->cipherKey = $this->cipherKey;
|
||||
unset($_SERVER["HTTP_AUTHENTICATION"]);
|
||||
$res = $authjwt->logout();
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Logout : No Bearer available
|
||||
*/
|
||||
public function testLogout3 ()
|
||||
public function testLogout3()
|
||||
// {{{
|
||||
{
|
||||
$this->expectException ("Exception", "No Bearer Authentication available",
|
||||
401);
|
||||
$authjwt = new Authjwt ();
|
||||
$authjwt->cacheDir = $this->cacheDir;
|
||||
$authjwt->serverKey = $this->serverKey;
|
||||
$authjwt->cipherKey = $this->cipherKey;
|
||||
$_SERVER["HTTP_AUTHENTICATION"] = "Another auth";
|
||||
$res = $authjwt->logout ();
|
||||
}
|
||||
{
|
||||
$this->expectException(
|
||||
"Exception",
|
||||
"No Bearer Authentication available",
|
||||
401
|
||||
);
|
||||
$authjwt = new Authjwt();
|
||||
$authjwt->cacheDir = $this->cacheDir;
|
||||
$authjwt->serverKey = $this->serverKey;
|
||||
$authjwt->cipherKey = $this->cipherKey;
|
||||
$_SERVER["HTTP_AUTHENTICATION"] = "Another auth";
|
||||
$res = $authjwt->logout();
|
||||
}
|
||||
// }}}
|
||||
|
||||
|
||||
/** Not needed function connect
|
||||
*/
|
||||
public function testUnusedFunctions1 ()
|
||||
public function testUnusedFunctions1()
|
||||
// {{{
|
||||
{
|
||||
$authjwt = new Authjwt ();
|
||||
$res = $authjwt->connect ();
|
||||
$this->assertSame ($res, true);
|
||||
}
|
||||
{
|
||||
$authjwt = new Authjwt();
|
||||
$res = $authjwt->connect();
|
||||
$this->assertSame($res, true);
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Not needed function changepassword
|
||||
*/
|
||||
public function testUnusedFunctions2 ()
|
||||
public function testUnusedFunctions2()
|
||||
// {{{
|
||||
{
|
||||
$this->expectException ("Exception",
|
||||
"The password can't be change for JWT users", 405);
|
||||
$authjwt = new Authjwt ();
|
||||
$res = $authjwt->changepassword ("unused", "unused");
|
||||
}
|
||||
{
|
||||
$this->expectException(
|
||||
"Exception",
|
||||
"The password can't be change for JWT users",
|
||||
405
|
||||
);
|
||||
$authjwt = new Authjwt();
|
||||
$res = $authjwt->changepassword("unused", "unused");
|
||||
}
|
||||
// }}}
|
||||
|
||||
/** Not needed function overwritepassword
|
||||
*/
|
||||
public function testUnusedFunctions3 ()
|
||||
public function testUnusedFunctions3()
|
||||
// {{{
|
||||
{
|
||||
$this->expectException ("Exception",
|
||||
"The password can't be overwrite for JWT users", 405);
|
||||
$authjwt = new Authjwt ();
|
||||
$res = $authjwt->overwritepassword ("unused", "unused");
|
||||
}
|
||||
{
|
||||
$this->expectException(
|
||||
"Exception",
|
||||
"The password can't be overwrite for JWT users",
|
||||
405
|
||||
);
|
||||
$authjwt = new Authjwt();
|
||||
$res = $authjwt->overwritepassword("unused", "unused");
|
||||
}
|
||||
// }}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user