Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:tymondesigns/jwt-auth into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
tymondesigns committed Jan 15, 2016
2 parents d985022 + 1b547a9 commit 2f6a539
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/Http/LumenRouteParams.php
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of jwt-auth
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* 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];
}
}
24 changes: 23 additions & 1 deletion src/Http/Parser.php
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/Http/RouteParams.php
Expand Up @@ -32,7 +32,15 @@ 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);
}

/**
Expand Down
73 changes: 73 additions & 0 deletions tests/Http/ParserTest.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -96,6 +97,63 @@ 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_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()
{
Expand All @@ -115,6 +173,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')
Expand Down

0 comments on commit 2f6a539

Please sign in to comment.