From da21fd9ebe7f2c9c3988fdbf21961b70602f20b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Ot=C3=A1vio=20Cobucci=20Oblonczyk?= Date: Tue, 18 Mar 2014 00:11:45 +0000 Subject: [PATCH 1/3] Updating dependencies --- composer.lock | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/composer.lock b/composer.lock index f3320231..2b5c4cb5 100644 --- a/composer.lock +++ b/composer.lock @@ -73,16 +73,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "2.0.1", + "version": "2.0.2", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "23e6ac9513df2af67f9f713347f3e4bf4b59784c" + "reference": "2c20ba4872d09d1d3d7ceda5a0c5f82d9a8ff31f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/23e6ac9513df2af67f9f713347f3e4bf4b59784c", - "reference": "23e6ac9513df2af67f9f713347f3e4bf4b59784c", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/2c20ba4872d09d1d3d7ceda5a0c5f82d9a8ff31f", + "reference": "2c20ba4872d09d1d3d7ceda5a0c5f82d9a8ff31f", "shasum": "" }, "require": { @@ -133,7 +133,7 @@ "testing", "xunit" ], - "time": "2014-03-07 16:03:14" + "time": "2014-03-17 10:25:49" }, { "name": "phpunit/php-file-iterator", @@ -315,16 +315,16 @@ }, { "name": "phpunit/phpunit", - "version": "4.0.3", + "version": "4.0.9", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "6a0c2dbfd79ddb5072d77fb5879c8045976b5686" + "reference": "c7e9e4b1253142acda4cb86a7686db412684fe04" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/6a0c2dbfd79ddb5072d77fb5879c8045976b5686", - "reference": "6a0c2dbfd79ddb5072d77fb5879c8045976b5686", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/c7e9e4b1253142acda4cb86a7686db412684fe04", + "reference": "c7e9e4b1253142acda4cb86a7686db412684fe04", "shasum": "" }, "require": { @@ -386,20 +386,20 @@ "testing", "xunit" ], - "time": "2014-03-07 18:08:58" + "time": "2014-03-17 15:17:30" }, { "name": "phpunit/phpunit-mock-objects", - "version": "2.0.1", + "version": "2.0.3", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit-mock-objects.git", - "reference": "f6942d1da56abcf0ddd11e94a24dfbeb11777a9d" + "reference": "5888b2501d41c34f9132c959b073b23de83235aa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/f6942d1da56abcf0ddd11e94a24dfbeb11777a9d", - "reference": "f6942d1da56abcf0ddd11e94a24dfbeb11777a9d", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/5888b2501d41c34f9132c959b073b23de83235aa", + "reference": "5888b2501d41c34f9132c959b073b23de83235aa", "shasum": "" }, "require": { @@ -443,7 +443,7 @@ "mock", "xunit" ], - "time": "2014-03-07 17:55:21" + "time": "2014-03-11 09:46:18" }, { "name": "sebastian/diff", From 763d8fa6e64323170c6ccd710396246d0b52d3d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Ot=C3=A1vio=20Cobucci=20Oblonczyk?= Date: Tue, 18 Mar 2014 00:12:05 +0000 Subject: [PATCH 2/3] Enabling verbose mode on PHPUnit --- phpunit.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit.xml b/phpunit.xml index ed065180..a4a260a7 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -4,6 +4,7 @@ backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" + verbose="true" strict="true"> From 696633b9584023c024e834fd34d9b7ededfd2c95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Ot=C3=A1vio=20Cobucci=20Oblonczyk?= Date: Tue, 18 Mar 2014 00:12:24 +0000 Subject: [PATCH 3/3] Adding basic token validation --- src/Token.php | 40 +++++++++++++++++++ test/TokenTest.php | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) diff --git a/src/Token.php b/src/Token.php index f7b8d73e..e6ec96d0 100644 --- a/src/Token.php +++ b/src/Token.php @@ -118,6 +118,46 @@ public function verify($key) return $this->signature->verify($this->getPayload(), $key); } + /** + * Validates if the token is valid + * + * @param string $issuer + * @param string $audience + * @param string $subject + * @param int $currentTime + * @return boolean + */ + public function validate( + $issuer = null, + $audience = null, + $subject = null, + $currentTime = null + ) { + $currentTime = $currentTime ?: time(); + + if (isset($this->claims['iss']) && $this->claims['iss'] != $issuer) { + return false; + } + + if (isset($this->claims['aud']) && $this->claims['aud'] != $audience) { + return false; + } + + if (isset($this->claims['sub']) && $this->claims['sub'] != $subject) { + return false; + } + + if (isset($this->claims['nbf']) && $this->claims['nbf'] > $currentTime) { + return false; + } + + if (isset($this->claims['exp']) && $this->claims['exp'] < $currentTime) { + return false; + } + + return true; + } + /** * Returns the token payload * diff --git a/test/TokenTest.php b/test/TokenTest.php index a0cbfe32..7da3ccc4 100644 --- a/test/TokenTest.php +++ b/test/TokenTest.php @@ -138,6 +138,101 @@ public function verifyMustDelegateTheValidationToSignature() $this->assertTrue($token->verify('test')); } + /** + * @test + * @covers ::__construct + * @covers ::validate + */ + public function validateShouldReturnTrueWhenClaimsAreEmpty() + { + $token = new Token(); + + $this->assertTrue($token->validate()); + } + + /** + * @test + * @covers ::__construct + * @covers ::validate + */ + public function validateShouldReturnFalseWhenIssuerIsDiferentThanTheGivenOne() + { + $token = new Token([], ['iss' => 'test']); + + $this->assertFalse($token->validate('test1')); + } + + /** + * @test + * @covers ::__construct + * @covers ::validate + */ + public function validateShouldReturnFalseWhenAudienceIsDiferentThanTheGivenOne() + { + $token = new Token([], ['aud' => 'test']); + + $this->assertFalse($token->validate(null, 'test1')); + } + + /** + * @test + * @covers ::__construct + * @covers ::validate + */ + public function validateShouldReturnFalseWhenSubjectIsDiferentThanTheGivenOne() + { + $token = new Token([], ['sub' => 'test']); + + $this->assertFalse($token->validate(null, null, 'test1')); + } + + /** + * @test + * @covers ::__construct + * @covers ::validate + */ + public function validateShouldReturnFalseWhenTokenCannotYetBeUsed() + { + $token = new Token([], ['nbf' => strtotime('+2 hours')]); + + $this->assertFalse($token->validate(null, null, null, time())); + } + + /** + * @test + * @covers ::__construct + * @covers ::validate + */ + public function validateShouldReturnFalseWhenTokenIsExpired() + { + $token = new Token([], ['exp' => time()]); + + $this->assertFalse($token->validate(null, null, null, strtotime('+2 hours'))); + } + + /** + * @test + * @covers ::__construct + * @covers ::validate + */ + public function validateShouldReturnTrueWhenAllInformationsAreRight() + { + $token = new Token( + [], + [ + 'iss' => 'test0', + 'aud' => 'test1', + 'sub' => 'test2', + 'nbf' => time(), + 'exp' => strtotime('+3 hours') + ] + ); + + $this->assertTrue( + $token->validate('test0', 'test1', 'test2', strtotime('+1 hours')) + ); + } + /** * @test * @covers ::__construct