From 1556b507bb28e7e4bf9ee2eac1e5fdebfdc0ff07 Mon Sep 17 00:00:00 2001 From: Sean Tymon Date: Mon, 11 Jan 2016 19:12:11 -0500 Subject: [PATCH 1/7] Applied fixes from StyleCI --- src/JWTGuard.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/JWTGuard.php b/src/JWTGuard.php index 6da4cb39a..9d4b4ec66 100644 --- a/src/JWTGuard.php +++ b/src/JWTGuard.php @@ -81,7 +81,6 @@ public function validate(array $credentials = [], $login = true) $this->lastAttempted = $user = $this->provider->retrieveByCredentials($credentials); if ($this->hasValidCredentials($user, $credentials)) { - if ($login) { $this->setUser($user); From a79976609318fcd9be153b8c18e0cc73673d72ba Mon Sep 17 00:00:00 2001 From: Taylor Smith Date: Wed, 13 Jan 2016 17:23:55 -0600 Subject: [PATCH 2/7] Add tests for null- and array-valued `$request->route()` --- tests/Http/ParserTest.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/Http/ParserTest.php b/tests/Http/ParserTest.php index ee515eefa..5c1cc6e4b 100644 --- a/tests/Http/ParserTest.php +++ b/tests/Http/ParserTest.php @@ -96,6 +96,44 @@ public function it_should_return_the_token_from_route() $this->assertTrue($parser->hasToken()); } + /** @test */ + public function it_should_ignore_routeless_requests() + { + $request = Request::create('foo', 'GET', ['foo' => 'bar']); + $request->setRouteResolver(function () { + return null; + }); + + $parser = new Parser($request); + $parser->setChainOrder([ + new AuthHeaders, + new QueryString, + new RouteParams + ]); + + $this->assertNull($parser->parseToken()); + $this->assertFalse($parser->hasToken()); + } + + /** @test */ + public function it_should_ignore_lumen_request_arrays() + { + $request = Request::create('foo', 'GET', ['foo' => 'bar']); + $request->setRouteResolver(function () { + return [false, ['uses'=>'someController'], ['token'=>'foobar']]; + }); + + $parser = new Parser($request); + $parser->setChainOrder([ + new AuthHeaders, + new QueryString, + new RouteParams + ]); + + $this->assertNull($parser->parseToken()); + $this->assertFalse($parser->hasToken()); + } + /** @test */ public function it_should_return_null_if_no_token_in_request() { From e5c75a771dd13f97ca6d226e70ffcc82e35a4779 Mon Sep 17 00:00:00 2001 From: Taylor Smith Date: Wed, 13 Jan 2016 17:25:42 -0600 Subject: [PATCH 3/7] RouteParams should not crash when route has no `parameter()` method. --- src/Http/RouteParams.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Http/RouteParams.php b/src/Http/RouteParams.php index 946bc7835..4299b8cc7 100644 --- a/src/Http/RouteParams.php +++ b/src/Http/RouteParams.php @@ -32,7 +32,14 @@ class RouteParams implements ParserContract */ public function parse(Request $request) { - return $request->route($this->key); + $route = $request->route(); + + if (! is_callable([$route, 'parameter'])) { + // Route may not be an instance of Illuminate\Routing\Route (it's an array + // in Lumen <5.2) or not exist at all (if the request was never dispatched) + return null; + } + return $route->parameter($this->key); } /** From 40f63ca58cdd9aa896fa9479d47b9833c849f1ac Mon Sep 17 00:00:00 2001 From: Taylor Smith Date: Wed, 13 Jan 2016 17:32:20 -0600 Subject: [PATCH 4/7] Allow chains to be retrieved from the Parser --- src/Http/Parser.php | 24 +++++++++++++++++++++++- tests/Http/ParserTest.php | 15 +++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/Http/Parser.php b/src/Http/Parser.php index 35a7e061f..267ce38f4 100644 --- a/src/Http/Parser.php +++ b/src/Http/Parser.php @@ -35,17 +35,39 @@ public function __construct(Request $request, array $chain = []) $this->chain = $chain; } + /** + * Get the parser chain + * + * @return array The chain of ParserContracts that the parser evaluates. + */ + public function getChain() + { + return $this->chain; + } + /** * Set the order of the parser chain * * @param array $chain */ - public function setChainOrder(array $chain) + public function setChain(array $chain) { $this->chain = $chain; return $this; } + + /** + * Alias for setting the order of the chain + * + * @param array $chain + */ + public function setChainOrder(array $chain) + { + $this->setChain($chain); + + return $this; + } /** * Iterate throught the parsers and attempt to retrieve diff --git a/tests/Http/ParserTest.php b/tests/Http/ParserTest.php index 5c1cc6e4b..76f87393c 100644 --- a/tests/Http/ParserTest.php +++ b/tests/Http/ParserTest.php @@ -153,6 +153,21 @@ public function it_should_return_null_if_no_token_in_request() $this->assertFalse($parser->hasToken()); } + /** @test */ + public function it_should_retrieve_the_chain() + { + $chain = [ + new AuthHeaders, + new QueryString, + new RouteParams + ]; + + $parser = new Parser(Mockery::mock('Illuminate\Http\Request')); + $parser->setChain($chain); + + $this->assertEquals($parser->getChain(), $chain); + } + protected function getRouteMock($expectedParameterValue = null) { return Mockery::mock('Illuminate\Routing\Route') From 61cbfe3132da2338015ae73ab149eb56ecda9a0f Mon Sep 17 00:00:00 2001 From: Taylor Smith Date: Wed, 13 Jan 2016 17:44:17 -0600 Subject: [PATCH 5/7] Add special LumenRouteParams --- src/Http/LumenRouteParams.php | 38 +++++++++++++++++++++++++++++++++++ tests/Http/ParserTest.php | 20 ++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/Http/LumenRouteParams.php diff --git a/src/Http/LumenRouteParams.php b/src/Http/LumenRouteParams.php new file mode 100644 index 000000000..a675eb847 --- /dev/null +++ b/src/Http/LumenRouteParams.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tymon\JWTAuth\Http; + +use Illuminate\Http\Request; + +class LumenRouteParams extends RouteParams +{ + /** + * Try to get the token from the route parameters + * + * @param \Illuminate\Http\Request + * + * @return null|string + */ + public function parse(Request $request) + { + // WARNING: Only use this parser if you know what you're doing! + // It will only work with poorly-specified aspects of certain Lumen releases. + $route = $request->route(); + + if (! is_array($route) || ! array_has($route, '2.'.$this->key)) { + // Route is not the expected kind of array, or does not have a parameter + // with the key we want. + return null; + } + return $route[2][$this->key]; + } +} diff --git a/tests/Http/ParserTest.php b/tests/Http/ParserTest.php index 76f87393c..777321d6f 100644 --- a/tests/Http/ParserTest.php +++ b/tests/Http/ParserTest.php @@ -17,6 +17,7 @@ use Tymon\JWTAuth\Http\AuthHeaders; use Tymon\JWTAuth\Http\QueryString; use Tymon\JWTAuth\Http\RouteParams; +use Tymon\JWTAuth\Http\LumenRouteParams; class ParserTest extends \PHPUnit_Framework_TestCase { @@ -133,6 +134,25 @@ public function it_should_ignore_lumen_request_arrays() $this->assertNull($parser->parseToken()); $this->assertFalse($parser->hasToken()); } + + /** @test */ + public function it_should_accept_lumen_request_arrays_with_special_class() + { + $request = Request::create('foo', 'GET', ['foo' => 'bar']); + $request->setRouteResolver(function () { + return [false, ['uses'=>'someController'], ['token'=>'foobar']]; + }); + + $parser = new Parser($request); + $parser->setChainOrder([ + new AuthHeaders, + new QueryString, + new LumenRouteParams + ]); + + $this->assertEquals($parser->parseToken(), 'foobar'); + $this->assertTrue($parser->hasToken()); + } /** @test */ public function it_should_return_null_if_no_token_in_request() From c7259bac99ae0405c8a6c1b83426d5d2404fbbb4 Mon Sep 17 00:00:00 2001 From: Sean Tymon Date: Thu, 14 Jan 2016 22:10:23 +0000 Subject: [PATCH 6/7] cs --- src/Http/RouteParams.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Http/RouteParams.php b/src/Http/RouteParams.php index 4299b8cc7..75bbeb8ba 100644 --- a/src/Http/RouteParams.php +++ b/src/Http/RouteParams.php @@ -39,6 +39,7 @@ public function parse(Request $request) // in Lumen <5.2) or not exist at all (if the request was never dispatched) return null; } + return $route->parameter($this->key); } From 1b547a9f9c1a66720233127a455a3cc8eb73197f Mon Sep 17 00:00:00 2001 From: Sean Tymon Date: Thu, 14 Jan 2016 22:10:41 +0000 Subject: [PATCH 7/7] cs --- src/Http/LumenRouteParams.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Http/LumenRouteParams.php b/src/Http/LumenRouteParams.php index a675eb847..4a2e02cc5 100644 --- a/src/Http/LumenRouteParams.php +++ b/src/Http/LumenRouteParams.php @@ -33,6 +33,7 @@ public function parse(Request $request) // with the key we want. return null; } + return $route[2][$this->key]; } }