-
-
Notifications
You must be signed in to change notification settings - Fork 799
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1867 from gassan/bugfix-lazy-refresh-token-listener
Bugfix: Refresh token listener should not be lazy.
- Loading branch information
Showing
12 changed files
with
220 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/DependencyInjection/CompilerPass/EnableRefreshOAuthTokenListenerCompilerPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the HWIOAuthBundle package. | ||
* | ||
* (c) Hardware Info <opensource@hardware.info> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace HWI\Bundle\OAuthBundle\DependencyInjection\CompilerPass; | ||
|
||
use HWI\Bundle\OAuthBundle\DependencyInjection\HWIOAuthExtension; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
final class EnableRefreshOAuthTokenListenerCompilerPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
/** @var HWIOAuthExtension $extension */ | ||
$extension = $container->getExtension('hwi_oauth'); | ||
|
||
if ($extension->isRefreshTokenListenerEnabled()) { | ||
foreach ($extension->getFirewallNames() as $firewallName => $_) { | ||
$container->getDefinition('hwi_oauth.context_listener.token_refresher.'.$firewallName) | ||
->addMethodCall('enable'); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,6 @@ login_landing: | |
|
||
google_login: | ||
path: /check-login/google | ||
|
||
yahoo_login: | ||
path: /check-login/yahoo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
tests/Functional/Security/Http/Firewall/RefreshTokenListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the HWIOAuthBundle package. | ||
* | ||
* (c) Hardware Info <opensource@hardware.info> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace HWI\Bundle\OAuthBundle\Tests\Functional\Security\Http\Firewall; | ||
|
||
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken; | ||
use HWI\Bundle\OAuthBundle\Tests\App\AppKernel; | ||
use HWI\Bundle\OAuthBundle\Tests\Fixtures\User; | ||
use HWI\Bundle\OAuthBundle\Tests\Functional\AuthenticationHelperTrait; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
use Symfony\Component\HttpClient\MockHttpClient; | ||
use Symfony\Component\HttpClient\Response\MockResponse; | ||
use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
|
||
final class RefreshTokenListenerTest extends WebTestCase | ||
{ | ||
use AuthenticationHelperTrait; | ||
|
||
private string $tokenResponse = <<<json | ||
{ | ||
"access_token": "valid-access-token", | ||
"refresh_token": "valid-refresh-token", | ||
"expires_in": 666 | ||
} | ||
json; | ||
|
||
private string $userResponse = <<<json | ||
{ | ||
"response": { | ||
"user": { | ||
"id": "1", | ||
"firstName": "bar", | ||
"lastName": "foo" | ||
} | ||
} | ||
} | ||
json; | ||
|
||
private MockHttpClient $httpClient; | ||
|
||
private KernelBrowser $client; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->httpClient = new MockHttpClient([ | ||
new MockResponse($this->tokenResponse, [ | ||
'response_headers' => ['content-type' => 'application/json'], | ||
]), | ||
new MockResponse($this->userResponse, [ | ||
'response_headers' => ['content-type' => 'application/json'], | ||
]), | ||
]); | ||
|
||
$this->client = self::createClient(); | ||
$this->client->getContainer()->set('hwi_oauth.http_client', $this->httpClient); | ||
} | ||
|
||
public static function getKernelClass(): string | ||
{ | ||
return AppKernel::class; | ||
} | ||
|
||
public function testExpiredTokenWillNotBeRefreshed(): void | ||
{ | ||
// refresh_on_expire not set | ||
$session = $this->createExpiredTokenAndStoreToSession('google'); | ||
|
||
$this->client->request('GET', '/'); | ||
|
||
$this->assertEquals(0, $this->httpClient->getRequestsCount()); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
|
||
$securityContext = $session->get('_security_hwi_context'); | ||
|
||
$this->assertNotNull($securityContext); | ||
$newToken = unserialize($securityContext); | ||
$this->assertInstanceOf(OAuthToken::class, $newToken); | ||
$this->assertTrue($newToken->isExpired()); | ||
// same old expired token | ||
$this->assertEquals(1000, $newToken->getCreatedAt()); | ||
} | ||
|
||
public function testExpiredTokenWillBeRefreshed(): void | ||
{ | ||
// refresh_on_expire: true | ||
$session = $this->createExpiredTokenAndStoreToSession('yahoo'); | ||
|
||
$this->client->request('GET', '/'); | ||
|
||
$this->assertEquals(2, $this->httpClient->getRequestsCount()); | ||
|
||
$this->assertResponseIsSuccessful(); | ||
|
||
$securityContext = $session->get('_security_hwi_context'); | ||
|
||
$this->assertNotNull($securityContext); | ||
$newToken = unserialize($securityContext); | ||
$this->assertInstanceOf(OAuthToken::class, $newToken); | ||
$this->assertFalse($newToken->isExpired()); | ||
} | ||
|
||
private function createExpiredTokenAndStoreToSession(string $resourceOwnerName): SessionInterface | ||
{ | ||
$expectedToken = [ | ||
'access_token' => 'access_token', | ||
'refresh_token' => 'refresh_token', | ||
'expires_in' => 666, | ||
'oauth_token_secret' => 'secret', | ||
]; | ||
|
||
$user = new User(); | ||
$oauthToken = new OAuthToken($expectedToken, $user->getRoles()); | ||
$oauthToken->setUser($user); | ||
$oauthToken->setResourceOwnerName($resourceOwnerName); | ||
$oauthToken->setCreatedAt(1000); | ||
|
||
$this->assertTrue($oauthToken->isExpired()); | ||
|
||
$session = $this->getSession($this->client); | ||
$session->set('_security_hwi_context', serialize($oauthToken)); | ||
$this->saveSession($this->client, $session); | ||
|
||
return $session; | ||
} | ||
} |