Skip to content

Commit 5b57dbc

Browse files
authored
Add support for EncryptCookies middleware (#1628)
1 parent 31ed569 commit 5b57dbc

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

src/Guards/TokenGuard.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,12 @@ protected function getTokenViaCookie($request)
295295
*/
296296
protected function decodeJwtTokenCookie($request)
297297
{
298+
$jwt = $request->cookie(Passport::cookie());
299+
298300
return (array) JWT::decode(
299-
CookieValuePrefix::remove($this->encrypter->decrypt($request->cookie(Passport::cookie()), Passport::$unserializesCookies)),
301+
Passport::$decryptsCookies
302+
? CookieValuePrefix::remove($this->encrypter->decrypt($jwt, Passport::$unserializesCookies))
303+
: $jwt,
300304
new Key(Passport::tokenEncryptionKey($this->encrypter), 'HS256')
301305
);
302306
}

src/Passport.php

+31
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ class Passport
133133
*/
134134
public static $unserializesCookies = false;
135135

136+
/**
137+
* Indicates if Passport should decrypt cookies.
138+
*
139+
* @var bool
140+
*/
141+
public static $decryptsCookies = true;
142+
136143
/**
137144
* Indicates if client secrets will be hashed.
138145
*
@@ -684,4 +691,28 @@ public static function withoutCookieSerialization()
684691

685692
return new static;
686693
}
694+
695+
/**
696+
* Instruct Passport to enable cookie encryption.
697+
*
698+
* @return static
699+
*/
700+
public static function withCookieEncryption()
701+
{
702+
static::$decryptsCookies = true;
703+
704+
return new static;
705+
}
706+
707+
/**
708+
* Instruct Passport to disable cookie encryption.
709+
*
710+
* @return static
711+
*/
712+
public static function withoutCookieEncryption()
713+
{
714+
static::$decryptsCookies = false;
715+
716+
return new static;
717+
}
687718
}

tests/Unit/TokenGuardTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,48 @@ public function test_users_may_be_retrieved_from_cookies_with_xsrf_token_header_
304304
Passport::encryptTokensUsing(null);
305305
}
306306

307+
public function test_users_may_be_retrieved_from_cookies_without_encryption()
308+
{
309+
Passport::withoutCookieEncryption();
310+
Passport::encryptTokensUsing(function (EncrypterContract $encrypter) {
311+
return $encrypter->getKey().'.mykey';
312+
});
313+
314+
$resourceServer = m::mock(ResourceServer::class);
315+
$userProvider = m::mock(PassportUserProvider::class);
316+
$tokens = m::mock(TokenRepository::class);
317+
$clients = m::mock(ClientRepository::class);
318+
$encrypter = new Encrypter(str_repeat('a', 16));
319+
320+
$clients->shouldReceive('findActive')
321+
->with(1)
322+
->andReturn(new TokenGuardTestClient);
323+
324+
$request = Request::create('/');
325+
$request->headers->set('X-XSRF-TOKEN', $encrypter->encrypt(CookieValuePrefix::create('X-XSRF-TOKEN', $encrypter->getKey()).'token', false));
326+
$request->cookies->set('laravel_token',
327+
JWT::encode([
328+
'sub' => 1,
329+
'aud' => 1,
330+
'csrf' => 'token',
331+
'expiry' => Carbon::now()->addMinutes(10)->getTimestamp(),
332+
], Passport::tokenEncryptionKey($encrypter), 'HS256')
333+
);
334+
335+
$guard = new TokenGuard($resourceServer, $userProvider, $tokens, $clients, $encrypter, $request);
336+
337+
$userProvider->shouldReceive('retrieveById')->with(1)->andReturn($expectedUser = new TokenGuardTestUser);
338+
$userProvider->shouldReceive('getProviderName')->andReturn(null);
339+
340+
$user = $guard->user();
341+
342+
$this->assertEquals($expectedUser, $user);
343+
344+
// Revert to the default encryption method
345+
Passport::withCookieEncryption();
346+
Passport::encryptTokensUsing(null);
347+
}
348+
307349
public function test_xsrf_token_cookie_without_a_token_header_is_not_accepted()
308350
{
309351
$resourceServer = m::mock(ResourceServer::class);

0 commit comments

Comments
 (0)