-
Notifications
You must be signed in to change notification settings - Fork 114
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 #168 from trikoder/make-token-firewall-context-aware
Make the token aware of the firewall context
- Loading branch information
Showing
8 changed files
with
235 additions
and
12 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
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Trikoder\Bundle\OAuth2Bundle\Tests\Unit; | ||
|
||
use League\OAuth2\Server\ResourceServer; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Symfony\Component\Security\Core\User\UserProviderInterface; | ||
use Trikoder\Bundle\OAuth2Bundle\Security\Authentication\Provider\OAuth2Provider; | ||
use Trikoder\Bundle\OAuth2Bundle\Security\Authentication\Token\OAuth2Token; | ||
use Trikoder\Bundle\OAuth2Bundle\Security\Authentication\Token\OAuth2TokenFactory; | ||
use Trikoder\Bundle\OAuth2Bundle\Tests\Fixtures\FixtureFactory; | ||
use Trikoder\Bundle\OAuth2Bundle\Tests\Fixtures\User; | ||
|
||
final class OAuth2ProviderTest extends TestCase | ||
{ | ||
public function testItSupportsOnlyOAuthTokenWithSameProviderKey(): void | ||
{ | ||
$providerKey = 'foo'; | ||
|
||
$tokenFactory = new OAuth2TokenFactory('ROLE_OAUTH2_'); | ||
|
||
$provider = new OAuth2Provider( | ||
$this->createMock(UserProviderInterface::class), | ||
$this->createMock(ResourceServer::class), | ||
$tokenFactory, | ||
$providerKey | ||
); | ||
|
||
$this->assertTrue($provider->supports($this->createToken($tokenFactory, $providerKey))); | ||
$this->assertFalse($provider->supports($this->createToken($tokenFactory, $providerKey . 'bar'))); | ||
} | ||
|
||
private function createToken(OAuth2TokenFactory $tokenFactory, string $providerKey): OAuth2Token | ||
{ | ||
$scopes = [FixtureFactory::FIXTURE_SCOPE_FIRST]; | ||
$serverRequest = $this->createMock(ServerRequestInterface::class); | ||
$serverRequest->expects($this->once()) | ||
->method('getAttribute') | ||
->with('oauth_scopes', []) | ||
->willReturn($scopes); | ||
|
||
$user = new User(); | ||
|
||
return $tokenFactory->createOAuth2Token($serverRequest, $user, $providerKey); | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Trikoder\Bundle\OAuth2Bundle\Tests\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Trikoder\Bundle\OAuth2Bundle\Security\Authentication\Token\OAuth2Token; | ||
use Trikoder\Bundle\OAuth2Bundle\Security\Authentication\Token\OAuth2TokenFactory; | ||
use Trikoder\Bundle\OAuth2Bundle\Tests\Fixtures\FixtureFactory; | ||
use Trikoder\Bundle\OAuth2Bundle\Tests\Fixtures\User; | ||
|
||
final class OAuth2TokenFactoryTest extends TestCase | ||
{ | ||
public function testCreatingToken(): void | ||
{ | ||
$rolePrefix = 'ROLE_OAUTH2_'; | ||
$factory = new OAuth2TokenFactory($rolePrefix); | ||
|
||
$scopes = [FixtureFactory::FIXTURE_SCOPE_FIRST]; | ||
$serverRequest = $this->createMock(ServerRequestInterface::class); | ||
$serverRequest->expects($this->once()) | ||
->method('getAttribute') | ||
->with('oauth_scopes', []) | ||
->willReturn($scopes); | ||
|
||
$user = new User(); | ||
$providerKey = 'main'; | ||
|
||
$token = $factory->createOAuth2Token($serverRequest, $user, $providerKey); | ||
|
||
$this->assertInstanceOf(OAuth2Token::class, $token); | ||
|
||
$roles = $token->getRoles(); | ||
$this->assertCount(1, $roles); | ||
$this->assertSame($rolePrefix . strtoupper($scopes[0]), $roles[0]->getRole()); | ||
|
||
$this->assertFalse($token->isAuthenticated()); | ||
$this->assertSame($user, $token->getUser()); | ||
$this->assertSame($providerKey, $token->getProviderKey()); | ||
} | ||
} |
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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Trikoder\Bundle\OAuth2Bundle\Tests\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Trikoder\Bundle\OAuth2Bundle\Security\Authentication\Token\OAuth2Token; | ||
use Trikoder\Bundle\OAuth2Bundle\Tests\Fixtures\FixtureFactory; | ||
use Trikoder\Bundle\OAuth2Bundle\Tests\Fixtures\User; | ||
|
||
final class OAuth2TokenTest extends TestCase | ||
{ | ||
public function testTokenSerialization(): void | ||
{ | ||
$scopes = [FixtureFactory::FIXTURE_SCOPE_FIRST]; | ||
$serverRequest = $this->createMock(ServerRequestInterface::class); | ||
$serverRequest->expects($this->once()) | ||
->method('getAttribute') | ||
->with('oauth_scopes', []) | ||
->willReturn($scopes); | ||
|
||
$user = new User(); | ||
$rolePrefix = 'ROLE_OAUTH2_'; | ||
$providerKey = 'main'; | ||
$token = new OAuth2Token($serverRequest, $user, $rolePrefix, $providerKey); | ||
|
||
/** @var OAuth2Token $unserializedToken */ | ||
$unserializedToken = unserialize(serialize($token)); | ||
|
||
$this->assertSame($providerKey, $unserializedToken->getProviderKey()); | ||
|
||
$roles = $unserializedToken->getRoles(); | ||
$this->assertCount(1, $roles); | ||
$expectedRole = $rolePrefix . strtoupper($scopes[0]); | ||
$this->assertSame($expectedRole, $roles[0]->getRole()); | ||
|
||
$this->assertSame($user->getUsername(), $unserializedToken->getUser()->getUsername()); | ||
$this->assertFalse($unserializedToken->isAuthenticated()); | ||
|
||
if (method_exists($token, 'getRoleNames')) { | ||
$this->assertSame([$expectedRole], $token->getRoleNames()); | ||
} | ||
} | ||
} |