Skip to content

Commit 36501ea

Browse files
committed
fix: should throw TokenInvalidException if received an invalid encrypted cookie token.
1 parent ab00f2d commit 36501ea

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Http/Parser/Cookies.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace Tymon\JWTAuth\Http\Parser;
1313

14+
use Illuminate\Contracts\Encryption\DecryptException;
1415
use Illuminate\Http\Request;
1516
use Illuminate\Support\Facades\Crypt;
1617
use Tymon\JWTAuth\Contracts\Http\Parser as ParserContract;
18+
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
1719

1820
class Cookies implements ParserContract
1921
{
@@ -41,7 +43,11 @@ public function __construct($decrypt = true)
4143
public function parse(Request $request)
4244
{
4345
if ($this->decrypt && $request->hasCookie($this->key)) {
44-
return Crypt::decrypt($request->cookie($this->key));
46+
try {
47+
return Crypt::decrypt($request->cookie($this->key));
48+
} catch (DecryptException $ex) {
49+
throw new TokenInvalidException('Token has not decrypted successfully.');
50+
}
4551
}
4652

4753
return $request->cookie($this->key);

tests/Http/ParserTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111

1212
namespace Tymon\JWTAuth\Test\Http;
1313

14+
use Illuminate\Contracts\Encryption\DecryptException;
1415
use Illuminate\Http\Request;
1516
use Illuminate\Routing\Route;
1617
use Illuminate\Support\Facades\Crypt;
1718
use Mockery;
1819
use Tymon\JWTAuth\Contracts\Http\Parser as ParserContract;
20+
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
1921
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
2022
use Tymon\JWTAuth\Http\Parser\Cookies;
2123
use Tymon\JWTAuth\Http\Parser\InputSource;
@@ -314,6 +316,29 @@ public function it_should_return_the_token_from_a_crypted_cookie()
314316
$this->assertTrue($parser->hasToken());
315317
}
316318

319+
/** @test */
320+
public function it_should_throw_token_invalid_exception_from_a_invalid_encrypted_cookie()
321+
{
322+
$request = Request::create('foo', 'POST', [], ['token' => 'foobar']);
323+
324+
$parser = new Parser($request);
325+
$parser->setChain([
326+
new AuthHeaders,
327+
new QueryString,
328+
new InputSource,
329+
new RouteParams,
330+
new Cookies(true),
331+
]);
332+
333+
Crypt::shouldReceive('decrypt')
334+
->with('foobar')
335+
->andThrow(new DecryptException());
336+
337+
$this->expectException(TokenInvalidException::class);
338+
339+
$parser->parseToken();
340+
}
341+
317342
/** @test */
318343
public function it_should_return_the_token_from_route()
319344
{

0 commit comments

Comments
 (0)