File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 11
11
12
12
namespace Tymon \JWTAuth \Http \Parser ;
13
13
14
+ use Illuminate \Contracts \Encryption \DecryptException ;
14
15
use Illuminate \Http \Request ;
15
16
use Illuminate \Support \Facades \Crypt ;
16
17
use Tymon \JWTAuth \Contracts \Http \Parser as ParserContract ;
18
+ use Tymon \JWTAuth \Exceptions \TokenInvalidException ;
17
19
18
20
class Cookies implements ParserContract
19
21
{
@@ -41,7 +43,11 @@ public function __construct($decrypt = true)
41
43
public function parse (Request $ request )
42
44
{
43
45
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
+ }
45
51
}
46
52
47
53
return $ request ->cookie ($ this ->key );
Original file line number Diff line number Diff line change 11
11
12
12
namespace Tymon \JWTAuth \Test \Http ;
13
13
14
+ use Illuminate \Contracts \Encryption \DecryptException ;
14
15
use Illuminate \Http \Request ;
15
16
use Illuminate \Routing \Route ;
16
17
use Illuminate \Support \Facades \Crypt ;
17
18
use Mockery ;
18
19
use Tymon \JWTAuth \Contracts \Http \Parser as ParserContract ;
20
+ use Tymon \JWTAuth \Exceptions \TokenInvalidException ;
19
21
use Tymon \JWTAuth \Http \Parser \AuthHeaders ;
20
22
use Tymon \JWTAuth \Http \Parser \Cookies ;
21
23
use Tymon \JWTAuth \Http \Parser \InputSource ;
@@ -314,6 +316,29 @@ public function it_should_return_the_token_from_a_crypted_cookie()
314
316
$ this ->assertTrue ($ parser ->hasToken ());
315
317
}
316
318
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
+
317
342
/** @test */
318
343
public function it_should_return_the_token_from_route ()
319
344
{
You can’t perform that action at this time.
0 commit comments