Skip to content

Commit

Permalink
fix: should throw TokenInvalidException if received an invalid encryp…
Browse files Browse the repository at this point in the history
…ted cookie token.
  • Loading branch information
chris-lee-lb committed Mar 27, 2021
1 parent ab00f2d commit 36501ea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Http/Parser/Cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace Tymon\JWTAuth\Http\Parser;

use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Tymon\JWTAuth\Contracts\Http\Parser as ParserContract;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;

class Cookies implements ParserContract
{
Expand Down Expand Up @@ -41,7 +43,11 @@ public function __construct($decrypt = true)
public function parse(Request $request)
{
if ($this->decrypt && $request->hasCookie($this->key)) {
return Crypt::decrypt($request->cookie($this->key));
try {
return Crypt::decrypt($request->cookie($this->key));
} catch (DecryptException $ex) {
throw new TokenInvalidException('Token has not decrypted successfully.');
}
}

return $request->cookie($this->key);
Expand Down
25 changes: 25 additions & 0 deletions tests/Http/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

namespace Tymon\JWTAuth\Test\Http;

use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Crypt;
use Mockery;
use Tymon\JWTAuth\Contracts\Http\Parser as ParserContract;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
use Tymon\JWTAuth\Http\Parser\AuthHeaders;
use Tymon\JWTAuth\Http\Parser\Cookies;
use Tymon\JWTAuth\Http\Parser\InputSource;
Expand Down Expand Up @@ -314,6 +316,29 @@ public function it_should_return_the_token_from_a_crypted_cookie()
$this->assertTrue($parser->hasToken());
}

/** @test */
public function it_should_throw_token_invalid_exception_from_a_invalid_encrypted_cookie()
{
$request = Request::create('foo', 'POST', [], ['token' => 'foobar']);

$parser = new Parser($request);
$parser->setChain([
new AuthHeaders,
new QueryString,
new InputSource,
new RouteParams,
new Cookies(true),
]);

Crypt::shouldReceive('decrypt')
->with('foobar')
->andThrow(new DecryptException());

$this->expectException(TokenInvalidException::class);

$parser->parseToken();
}

/** @test */
public function it_should_return_the_token_from_route()
{
Expand Down

0 comments on commit 36501ea

Please sign in to comment.