Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrated PR2109: Handling of invalid encrypted cookies #22

Merged
merged 5 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 PHPOpenSourceSaver\JWTAuth\Http\Parser;

use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use PHPOpenSourceSaver\JWTAuth\Contracts\Http\Parser as ParserContract;
use PHPOpenSourceSaver\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 PHPOpenSourceSaver\JWTAuth\Test\Http;

use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Crypt;
use Mockery;
use PHPOpenSourceSaver\JWTAuth\Contracts\Http\Parser as ParserContract;
use PHPOpenSourceSaver\JWTAuth\Exceptions\TokenInvalidException;
use PHPOpenSourceSaver\JWTAuth\Http\Parser\AuthHeaders;
use PHPOpenSourceSaver\JWTAuth\Http\Parser\Cookies;
use PHPOpenSourceSaver\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