Skip to content

Commit fb0cfc8

Browse files
authored
feat: Ability to add custom parser to the chain (tymondesigns#2068)
1 parent 244a7a3 commit fb0cfc8

File tree

5 files changed

+57
-13
lines changed

5 files changed

+57
-13
lines changed

src/Http/Parser/Parser.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ public function getChain()
5353
return $this->chain;
5454
}
5555

56+
/**
57+
* Add a new parser to the chain.
58+
*
59+
* @param array|\Tymon\JWTAuth\Contracts\Http\Parser $parsers
60+
*
61+
* @return $this
62+
*/
63+
public function addParser($parsers)
64+
{
65+
$this->chain = array_merge($this->chain, is_array($parsers) ? $parsers : [$parsers]);
66+
67+
return $this;
68+
}
69+
5670
/**
5771
* Set the order of the parser chain.
5872
*

src/Providers/AbstractServiceProvider.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@
2727
use Tymon\JWTAuth\Http\Middleware\Check;
2828
use Tymon\JWTAuth\Http\Middleware\RefreshToken;
2929
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
30-
use Tymon\JWTAuth\Http\Parser\Cookies;
3130
use Tymon\JWTAuth\Http\Parser\InputSource;
3231
use Tymon\JWTAuth\Http\Parser\Parser;
3332
use Tymon\JWTAuth\Http\Parser\QueryString;
34-
use Tymon\JWTAuth\Http\Parser\RouteParams;
3533
use Tymon\JWTAuth\JWT;
3634
use Tymon\JWTAuth\JWTAuth;
3735
use Tymon\JWTAuth\JWTGuard;
@@ -235,8 +233,6 @@ protected function registerTokenParser()
235233
new AuthHeaders,
236234
new QueryString,
237235
new InputSource,
238-
new RouteParams,
239-
new Cookies($this->config('decrypt_cookies')),
240236
]
241237
);
242238

src/Providers/LaravelServiceProvider.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Tymon\JWTAuth\Providers;
1313

14+
use Tymon\JWTAuth\Http\Parser\Cookies;
15+
use Tymon\JWTAuth\Http\Parser\RouteParams;
16+
1417
class LaravelServiceProvider extends AbstractServiceProvider
1518
{
1619
/**
@@ -26,6 +29,11 @@ public function boot()
2629
$this->aliasMiddleware();
2730

2831
$this->extendAuthGuard();
32+
33+
$this->app['tymon.jwt.parser']->addParser([
34+
new RouteParams,
35+
new Cookies($this->config('decrypt_cookies')),
36+
]);
2937
}
3038

3139
/**

src/Providers/LumenServiceProvider.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111

1212
namespace Tymon\JWTAuth\Providers;
1313

14-
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
15-
use Tymon\JWTAuth\Http\Parser\InputSource;
1614
use Tymon\JWTAuth\Http\Parser\LumenRouteParams;
17-
use Tymon\JWTAuth\Http\Parser\QueryString;
1815

1916
class LumenServiceProvider extends AbstractServiceProvider
2017
{
@@ -32,11 +29,6 @@ public function boot()
3229

3330
$this->extendAuthGuard();
3431

35-
$this->app['tymon.jwt.parser']->setChain([
36-
new AuthHeaders,
37-
new QueryString,
38-
new InputSource,
39-
new LumenRouteParams,
40-
]);
32+
$this->app['tymon.jwt.parser']->addParser(new LumenRouteParams);
4133
}
4234
}

tests/Http/ParserTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Illuminate\Routing\Route;
1616
use Illuminate\Support\Facades\Crypt;
1717
use Mockery;
18+
use Tymon\JWTAuth\Contracts\Http\Parser as ParserContract;
1819
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
1920
use Tymon\JWTAuth\Http\Parser\Cookies;
2021
use Tymon\JWTAuth\Http\Parser\InputSource;
@@ -419,6 +420,39 @@ public function it_should_set_the_cookie_key()
419420
$this->assertInstanceOf(Cookies::class, $cookies);
420421
}
421422

423+
/** @test */
424+
public function it_should_add_custom_parser()
425+
{
426+
$request = Request::create('foo', 'GET', ['foo' => 'bar']);
427+
428+
$customParser = Mockery::mock(ParserContract::class);
429+
$customParser->shouldReceive('parse')->with($request)->andReturn('foobar');
430+
431+
$parser = new Parser($request);
432+
$parser->addParser($customParser);
433+
434+
$this->assertSame($parser->parseToken(), 'foobar');
435+
$this->assertTrue($parser->hasToken());
436+
}
437+
438+
/** @test */
439+
public function it_should_add_multiple_custom_parser()
440+
{
441+
$request = Request::create('foo', 'GET', ['foo' => 'bar']);
442+
443+
$customParser1 = Mockery::mock(ParserContract::class);
444+
$customParser1->shouldReceive('parse')->with($request)->andReturn(false);
445+
446+
$customParser2 = Mockery::mock(ParserContract::class);
447+
$customParser2->shouldReceive('parse')->with($request)->andReturn('foobar');
448+
449+
$parser = new Parser($request);
450+
$parser->addParser([$customParser1, $customParser2]);
451+
452+
$this->assertSame($parser->parseToken(), 'foobar');
453+
$this->assertTrue($parser->hasToken());
454+
}
455+
422456
protected function getRouteMock($expectedParameterValue = null, $expectedParameterName = 'token')
423457
{
424458
return Mockery::mock(Route::class)

0 commit comments

Comments
 (0)