Skip to content

Commit

Permalink
feat: Ability to add custom parser to the chain (#2068)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssi-anik authored Dec 10, 2020
1 parent 244a7a3 commit fb0cfc8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
14 changes: 14 additions & 0 deletions src/Http/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ public function getChain()
return $this->chain;
}

/**
* Add a new parser to the chain.
*
* @param array|\Tymon\JWTAuth\Contracts\Http\Parser $parsers
*
* @return $this
*/
public function addParser($parsers)
{
$this->chain = array_merge($this->chain, is_array($parsers) ? $parsers : [$parsers]);

return $this;
}

/**
* Set the order of the parser chain.
*
Expand Down
4 changes: 0 additions & 4 deletions src/Providers/AbstractServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@
use Tymon\JWTAuth\Http\Middleware\Check;
use Tymon\JWTAuth\Http\Middleware\RefreshToken;
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
use Tymon\JWTAuth\Http\Parser\Cookies;
use Tymon\JWTAuth\Http\Parser\InputSource;
use Tymon\JWTAuth\Http\Parser\Parser;
use Tymon\JWTAuth\Http\Parser\QueryString;
use Tymon\JWTAuth\Http\Parser\RouteParams;
use Tymon\JWTAuth\JWT;
use Tymon\JWTAuth\JWTAuth;
use Tymon\JWTAuth\JWTGuard;
Expand Down Expand Up @@ -235,8 +233,6 @@ protected function registerTokenParser()
new AuthHeaders,
new QueryString,
new InputSource,
new RouteParams,
new Cookies($this->config('decrypt_cookies')),
]
);

Expand Down
8 changes: 8 additions & 0 deletions src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Tymon\JWTAuth\Providers;

use Tymon\JWTAuth\Http\Parser\Cookies;
use Tymon\JWTAuth\Http\Parser\RouteParams;

class LaravelServiceProvider extends AbstractServiceProvider
{
/**
Expand All @@ -26,6 +29,11 @@ public function boot()
$this->aliasMiddleware();

$this->extendAuthGuard();

$this->app['tymon.jwt.parser']->addParser([
new RouteParams,
new Cookies($this->config('decrypt_cookies')),
]);
}

/**
Expand Down
10 changes: 1 addition & 9 deletions src/Providers/LumenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@

namespace Tymon\JWTAuth\Providers;

use Tymon\JWTAuth\Http\Parser\AuthHeaders;
use Tymon\JWTAuth\Http\Parser\InputSource;
use Tymon\JWTAuth\Http\Parser\LumenRouteParams;
use Tymon\JWTAuth\Http\Parser\QueryString;

class LumenServiceProvider extends AbstractServiceProvider
{
Expand All @@ -32,11 +29,6 @@ public function boot()

$this->extendAuthGuard();

$this->app['tymon.jwt.parser']->setChain([
new AuthHeaders,
new QueryString,
new InputSource,
new LumenRouteParams,
]);
$this->app['tymon.jwt.parser']->addParser(new LumenRouteParams);
}
}
34 changes: 34 additions & 0 deletions tests/Http/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Crypt;
use Mockery;
use Tymon\JWTAuth\Contracts\Http\Parser as ParserContract;
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
use Tymon\JWTAuth\Http\Parser\Cookies;
use Tymon\JWTAuth\Http\Parser\InputSource;
Expand Down Expand Up @@ -419,6 +420,39 @@ public function it_should_set_the_cookie_key()
$this->assertInstanceOf(Cookies::class, $cookies);
}

/** @test */
public function it_should_add_custom_parser()
{
$request = Request::create('foo', 'GET', ['foo' => 'bar']);

$customParser = Mockery::mock(ParserContract::class);
$customParser->shouldReceive('parse')->with($request)->andReturn('foobar');

$parser = new Parser($request);
$parser->addParser($customParser);

$this->assertSame($parser->parseToken(), 'foobar');
$this->assertTrue($parser->hasToken());
}

/** @test */
public function it_should_add_multiple_custom_parser()
{
$request = Request::create('foo', 'GET', ['foo' => 'bar']);

$customParser1 = Mockery::mock(ParserContract::class);
$customParser1->shouldReceive('parse')->with($request)->andReturn(false);

$customParser2 = Mockery::mock(ParserContract::class);
$customParser2->shouldReceive('parse')->with($request)->andReturn('foobar');

$parser = new Parser($request);
$parser->addParser([$customParser1, $customParser2]);

$this->assertSame($parser->parseToken(), 'foobar');
$this->assertTrue($parser->hasToken());
}

protected function getRouteMock($expectedParameterValue = null, $expectedParameterName = 'token')
{
return Mockery::mock(Route::class)
Expand Down

0 comments on commit fb0cfc8

Please sign in to comment.