Fixed capitalization, tests
This commit is contained in:
@ -1,17 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt;
|
||||
namespace NoccyLabs\SimpleJWT;
|
||||
|
||||
use NoccyLabs\SimpleJwt\Key\JwtPlaintextKey;
|
||||
use NoccyLabs\SimpleJWT\Key\JWTPlaintextKey;
|
||||
|
||||
class JwtTokenTest extends \PhpUnit\Framework\TestCase
|
||||
class JWTTokenTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @covers
|
||||
*/
|
||||
public function testGeneratingTokens()
|
||||
{
|
||||
$key = new JwtPlaintextKey("test");
|
||||
$key = new JWTPlaintextKey("test");
|
||||
|
||||
$tok = new JwtToken($key);
|
||||
$tok = new JWTToken($key);
|
||||
$tok->addClaim("foo", true);
|
||||
|
||||
$token = $tok->getSignedToken();
|
||||
@ -20,19 +23,22 @@ class JwtTokenTest extends \PhpUnit\Framework\TestCase
|
||||
$this->assertTrue($tok->isGenerated());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers
|
||||
*/
|
||||
public function testParsingTokens()
|
||||
{
|
||||
$key = new JwtPlaintextKey("test");
|
||||
$key = new JWTPlaintextKey("test");
|
||||
|
||||
$tok = new JwtToken($key);
|
||||
$tok = new JWTToken($key);
|
||||
$tok->addClaim("foo", true);
|
||||
|
||||
$token = $tok->getSignedToken();
|
||||
|
||||
$parsed = new JwtToken($key, $token);
|
||||
$parsed = new JWTToken($key, $token);
|
||||
|
||||
$this->assertTrue($parsed->isValid());
|
||||
$this->assertFalse($parsed->isGenerated());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,18 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt;
|
||||
namespace NoccyLabs\SimpleJWT;
|
||||
|
||||
|
||||
class JwtUtilTest extends \PhpUnit\Framework\TestCase
|
||||
class JWTUtilTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @covers
|
||||
*/
|
||||
public function testTheEncodingShouldBeSymmetric()
|
||||
{
|
||||
$v1a = "HelloWorld";
|
||||
$v1b = JwtUtil::encode($v1a);
|
||||
$v1c = JwtUtil::decode($v1b);
|
||||
$v1b = JWTUtil::encode($v1a);
|
||||
$v1c = JWTUtil::decode($v1b);
|
||||
|
||||
$this->assertEquals($v1a, $v1c);
|
||||
$this->assertNotEquals($v1a, $v1b);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt\Key;
|
||||
namespace NoccyLabs\SimpleJWT\Key;
|
||||
|
||||
|
||||
class JwtDerivedKeyTest extends \PhpUnit\Framework\TestCase
|
||||
class JWTDerivedKeyTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @covers
|
||||
*/
|
||||
public function testTheDerivedKeysShouldBeConsistent()
|
||||
{
|
||||
$key1a = new JwtDerivedKey("foo", "foosalt");
|
||||
$key1b = new JwtDerivedKey("foo", "foosalt");
|
||||
$key1a = new JWTDerivedKey("foo", "foosalt");
|
||||
$key1b = new JWTDerivedKey("foo", "foosalt");
|
||||
$this->assertNotNull($key1a);
|
||||
$this->assertEquals($key1a->getBinaryKey(), $key1b->getBinaryKey());
|
||||
|
||||
$key2a = new JwtDerivedKey("bar", "foosalt");
|
||||
$key2b = new JwtDerivedKey("bar", "barsalt");
|
||||
$key2c = new JwtDerivedKey("bar", "barsalt");
|
||||
$key2a = new JWTDerivedKey("bar", "foosalt");
|
||||
$key2b = new JWTDerivedKey("bar", "barsalt");
|
||||
$key2c = new JWTDerivedKey("bar", "barsalt");
|
||||
$this->assertNotNull($key2a);
|
||||
$this->assertNotEquals($key2a->getBinaryKey(), $key2b->getBinaryKey());
|
||||
$this->assertEquals($key2b->getBinaryKey(), $key2c->getBinaryKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers
|
||||
*/
|
||||
public function testTheDerivedKeysShouldBeUnique()
|
||||
{
|
||||
$keys = [];
|
||||
$keys[] = (new JwtDerivedKey("foo", "foosalt"))->getBinaryKey();
|
||||
$keys[] = (new JwtDerivedKey("foo", "barsalt"))->getBinaryKey();
|
||||
$keys[] = (new JwtDerivedKey("foo", "bazsalt"))->getBinaryKey();
|
||||
$keys[] = (new JwtDerivedKey("bar", "foosalt"))->getBinaryKey();
|
||||
$keys[] = (new JwtDerivedKey("bar", "barsalt"))->getBinaryKey();
|
||||
$keys[] = (new JwtDerivedKey("bar", "bazsalt"))->getBinaryKey();
|
||||
$keys[] = (new JWTDerivedKey("foo", "foosalt"))->getBinaryKey();
|
||||
$keys[] = (new JWTDerivedKey("foo", "barsalt"))->getBinaryKey();
|
||||
$keys[] = (new JWTDerivedKey("foo", "bazsalt"))->getBinaryKey();
|
||||
$keys[] = (new JWTDerivedKey("bar", "foosalt"))->getBinaryKey();
|
||||
$keys[] = (new JWTDerivedKey("bar", "barsalt"))->getBinaryKey();
|
||||
$keys[] = (new JWTDerivedKey("bar", "bazsalt"))->getBinaryKey();
|
||||
|
||||
$unique = array_unique($keys);
|
||||
$this->assertEquals(count($keys), count($unique));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,16 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt\Key;
|
||||
namespace NoccyLabs\SimpleJWT\Key;
|
||||
|
||||
|
||||
class JwtPlaintextKeyTest extends \PhpUnit\Framework\TestCase
|
||||
class JWTPlaintextKeyTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @covers
|
||||
*/
|
||||
public function testThePlaintextKeyShouldBeReturned()
|
||||
{
|
||||
$key = new JwtPlaintextKey("foo");
|
||||
$key = new JWTPlaintextKey("foo");
|
||||
$this->assertEquals("foo", $key->getBinaryKey());
|
||||
|
||||
$key = new JwtPlaintextKey("bar");
|
||||
$key = new JWTPlaintextKey("bar");
|
||||
$this->assertEquals("bar", $key->getBinaryKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,49 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\SimpleJwt\Validator;
|
||||
namespace NoccyLabs\SimpleJWT\Validator;
|
||||
|
||||
use NoccyLabs\SimpleJwt\JwtToken;
|
||||
use NoccyLabs\SimpleJwt\Key\JwtPlaintextKey;
|
||||
use NoccyLabs\SimpleJWT\JWTToken;
|
||||
use NoccyLabs\SimpleJWT\Key\JWTPlaintextKey;
|
||||
|
||||
class JwtValidatorTest extends \PhpUnit\Framework\TestCase
|
||||
class JWTValidatorTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @covers
|
||||
*/
|
||||
public function testValidTokensShouldPassWithDefaultConfiguration()
|
||||
{
|
||||
$key = new JwtPlaintextKey("key");
|
||||
$token = new JwtToken($key);
|
||||
$key = new JWTPlaintextKey("key");
|
||||
$token = new JWTToken($key);
|
||||
|
||||
$validator = new JwtValidator();
|
||||
$validator = new JWTValidator();
|
||||
$valid = $validator->validateToken($token);
|
||||
$this->assertEquals(true, $valid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers
|
||||
*/
|
||||
public function testExpiredTokensShouldFailWithException()
|
||||
{
|
||||
$key = new JwtPlaintextKey("key");
|
||||
$token = new JwtToken($key);
|
||||
$key = new JWTPlaintextKey("key");
|
||||
$token = new JWTToken($key);
|
||||
$token->header->set("exp", 0);
|
||||
|
||||
$token = new JwtToken($key, $token->getSignedToken());
|
||||
$token = new JWTToken($key, $token->getSignedToken());
|
||||
|
||||
$validator = new JwtValidator();
|
||||
$this->expectException(JwtTokenException::class);
|
||||
$validator = new JWTValidator();
|
||||
$this->expectException(JWTTokenException::class);
|
||||
$valid = $validator->validateToken($token);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers
|
||||
* @dataProvider tokenGenerator
|
||||
*/
|
||||
public function testPinningIssuer($issuer,$audience,$key,$token)
|
||||
{
|
||||
$goodIssuer = "a-dom.tld";
|
||||
$jwtKey = new JwtPlaintextKey($key);
|
||||
$jwtToken = new JwtToken($jwtKey, $token);
|
||||
$jwtKey = new JWTPlaintextKey($key);
|
||||
$jwtToken = new JWTToken($jwtKey, $token);
|
||||
|
||||
$validator = new JwtValidator();
|
||||
$validator = new JWTValidator();
|
||||
$validator->requireIssuer($goodIssuer);
|
||||
if ($goodIssuer != $issuer) {
|
||||
$this->expectException(JwtTokenException::class);
|
||||
$this->expectException(JWTTokenException::class);
|
||||
}
|
||||
$valid = $validator->validateToken($jwtToken);
|
||||
if ($goodIssuer == $issuer) {
|
||||
@ -52,18 +59,19 @@ class JwtValidatorTest extends \PhpUnit\Framework\TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers
|
||||
* @dataProvider tokenGenerator
|
||||
*/
|
||||
public function testPinningAudience($issuer,$audience,$key,$token)
|
||||
{
|
||||
$goodAudience = [ "a-dom.tld", "app.a-dom.tld" ];
|
||||
$jwtKey = new JwtPlaintextKey($key);
|
||||
$jwtToken = new JwtToken($jwtKey, $token);
|
||||
$jwtKey = new JWTPlaintextKey($key);
|
||||
$jwtToken = new JWTToken($jwtKey, $token);
|
||||
|
||||
$validator = new JwtValidator();
|
||||
$validator = new JWTValidator();
|
||||
$validator->requireAudience($goodAudience);
|
||||
if (!in_array($audience, $goodAudience)) {
|
||||
$this->expectException(JwtTokenException::class);
|
||||
$this->expectException(JWTTokenException::class);
|
||||
}
|
||||
$valid = $validator->validateToken($jwtToken);
|
||||
if (in_array($audience, $goodAudience)) {
|
||||
@ -72,20 +80,21 @@ class JwtValidatorTest extends \PhpUnit\Framework\TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers
|
||||
* @dataProvider tokenGenerator
|
||||
*/
|
||||
public function testPinningBoth($issuer,$audience,$key,$token)
|
||||
{
|
||||
$goodIssuer = "a-dom.tld";
|
||||
$goodAudience = [ "a-dom.tld", "app.a-dom.tld" ];
|
||||
$jwtKey = new JwtPlaintextKey($key);
|
||||
$jwtToken = new JwtToken($jwtKey, $token);
|
||||
$jwtKey = new JWTPlaintextKey($key);
|
||||
$jwtToken = new JWTToken($jwtKey, $token);
|
||||
|
||||
$validator = new JwtValidator();
|
||||
$validator = new JWTValidator();
|
||||
$validator->requireIssuer($goodIssuer);
|
||||
$validator->requireAudience($goodAudience);
|
||||
if (($goodIssuer != $issuer) || (!in_array($audience, $goodAudience))) {
|
||||
$this->expectException(JwtTokenException::class);
|
||||
$this->expectException(JWTTokenException::class);
|
||||
}
|
||||
$valid = $validator->validateToken($jwtToken);
|
||||
if (($goodIssuer == $issuer) && (in_array($audience, $goodAudience))) {
|
||||
@ -93,21 +102,21 @@ class JwtValidatorTest extends \PhpUnit\Framework\TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function tokenGenerator()
|
||||
public static function tokenGenerator()
|
||||
{
|
||||
$keyrand = function () {
|
||||
return substr(sha1(microtime(true).rand(0,65535)), 5, 10);
|
||||
};
|
||||
$token = function ($head,$claims,$key) {
|
||||
$jwtKey = new JwtPlaintextKey($key);
|
||||
$tok = new JwtToken($jwtKey);
|
||||
$jwtKey = new JWTPlaintextKey($key);
|
||||
$tok = new JWTToken($jwtKey);
|
||||
$tok->header->setAll($head);
|
||||
$tok->claims->setAll($claims);
|
||||
return $tok->getSignedToken();
|
||||
};
|
||||
$row = function ($iss, $aud, array $claims) use ($keyrand, $token) {
|
||||
$key = $keyrand();
|
||||
$jwtKey = new JwtPlaintextKey($key);
|
||||
$jwtKey = new JWTPlaintextKey($key);
|
||||
return [
|
||||
$iss,
|
||||
$aud,
|
||||
|
Reference in New Issue
Block a user