Skip to content

Commit

Permalink
Maintain | Merge branch 1.4 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Dec 7, 2021
1 parent 28ce4a7 commit ab1bf5f
Show file tree
Hide file tree
Showing 28 changed files with 73 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ protected function createEntryPoint($container, $id, $config, ?string $defaultEn
*/
protected function createListener(ContainerBuilder $container, string $id, array $config, string $userProvider): string
{
// @phpstan-ignore-next-line Symfony <5.4 BC layer
$listenerId = parent::createListener($container, $id, $config, $userProvider);

$checkPaths = $config['resource_owners'];
Expand Down
4 changes: 2 additions & 2 deletions HWIOAuthBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public function build(ContainerBuilder $container)
/** @var SecurityExtension $extension */
$extension = $container->getExtension('security');

// Symfony < 5.4 BC layer
if (interface_exists(AuthenticationProviderInterface::class)) {
$extension->addSecurityListenerFactory(new OAuthFactory()); // @phpstan-ignore-this-line Symfony < 5.4 BC layer
// @phpstan-ignore-next-line Symfony < 5.4 BC layer
$extension->addSecurityListenerFactory(new OAuthFactory());
} else {
$extension->addAuthenticatorFactory(new OAuthAuthenticatorFactory());
}
Expand Down
2 changes: 1 addition & 1 deletion Security/Core/Authentication/Token/AbstractOAuthToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct($accessToken, array $roles = [])

// required for compatibility with Symfony 5.4
if (method_exists($this, 'setAuthenticated')) {
parent::setAuthenticated(\count($roles) > 0, false);
$this->setAuthenticated(\count($roles) > 0, false);
}
}

Expand Down
10 changes: 8 additions & 2 deletions Security/Core/User/EntityUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ public function refreshUser(UserInterface $user)
}

$userId = $accessor->getValue($user, $identifier);
$username = $user->getUsername();

// @phpstan-ignore-next-line Symfony <5.4 BC layer
$username = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();

if (null === $user = $this->findUser([$identifier => $userId])) {
throw $this->createUserNotFoundException($username, sprintf('User with ID "%d" could not be reloaded.', $userId));
Expand Down Expand Up @@ -148,9 +150,13 @@ private function createUserNotFoundException(string $username, string $message)
$exception = new UserNotFoundException($message);
$exception->setUserIdentifier($username);
} else {
if (!class_exists(UsernameNotFoundException::class)) {
throw new \RuntimeException('Unsupported Symfony version used!');
}

$exception = new UsernameNotFoundException($message);
if (method_exists($exception, 'setUsername')) {
$exception->setUsername($username);
$exception->setUsername($username); // @phpstan-ignore-this-line Symfony <5.4 BC layer
}
}

Expand Down
23 changes: 4 additions & 19 deletions Security/Core/User/OAuthUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ public function __construct(string $username)
$this->username = $username;
}

/**
* @return string
*/
public function getUserIdentifier(): string
{
return $this->username;
Expand All @@ -41,43 +38,31 @@ public function getRoles(): array
return ['ROLE_USER', 'ROLE_OAUTH_USER'];
}

/**
* @return string|null
*/
public function getPassword(): ?string
{
return null;
}

/**
* @return string|null
*/
public function getSalt(): ?string
{
return null;
}

/**
* @return string
*/
public function getUsername(): string
{
return $this->getUserIdentifier();
}

/**
* @return bool
*/
public function eraseCredentials(): bool
{
return true;
}

/**
* @return bool
*/
public function equals(UserInterface $user): bool
{
return $user->getUsername() === $this->username;
// @phpstan-ignore-next-line Symfony <5.4 BC layer
$username = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();

return $username === $this->username;
}
}
5 changes: 4 additions & 1 deletion Security/Core/User/OAuthUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function refreshUser(UserInterface $user)
throw new UnsupportedUserException(sprintf('Unsupported user class "%s"', \get_class($user)));
}

return $this->loadUserByUsername($user->getUsername());
// @phpstan-ignore-next-line Symfony <5.4 BC layer
$username = method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername();

return $this->loadUserByUsername($username);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions Security/Http/Authenticator/OAuthAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,14 @@ public function authenticate(Request $request): Passport
return new SelfValidatingPassport(
class_exists(UserBadge::class)
? new UserBadge(
// @phpstan-ignore-next-line Symfony <5.4 BC layer
method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(),
static function () use ($user) { return $user; }
)
: $user
);
}

public function createToken(PassportInterface $passport, string $firewallName): TokenInterface
{
return $this->createAuthenticatedToken($passport, $firewallName);
}

/**
* @param Passport|SelfValidatingPassport $passport
*/
Expand Down
1 change: 1 addition & 0 deletions Templating/Helper/OAuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ private function getMainRequest(): ?Request
return $this->requestStack->getMainRequest(); // Symfony 5.3+
}

// @phpstan-ignore-next-line
return $this->requestStack->getMasterRequest();
}
}
2 changes: 1 addition & 1 deletion Test/Fixtures/ResourceOwner/OAuth1ResourceOwnerStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner;
namespace HWI\Bundle\OAuthBundle\Test\Fixtures\ResourceOwner;

use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth1ResourceOwner;

Expand Down
2 changes: 1 addition & 1 deletion Test/Fixtures/ResourceOwner/OAuth2ResourceOwnerStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* file that was distributed with this source code.
*/

namespace HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner;
namespace HWI\Bundle\OAuthBundle\Test\Fixtures\ResourceOwner;

use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;

Expand Down
10 changes: 5 additions & 5 deletions Test/OAuth/ResourceOwner/GenericOAuth2ResourceOwnerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ public function testGetState(): void
->willReturn(serialize(new State(['state' => 'some-state'])));

$state = $resourceOwner->getState();
self::assertEquals($state->get('initial_state_param_1'), 'value');
self::assertEquals($state->get('state'), 'some-state');
self::assertEquals('value', $state->get('initial_state_param_1'));
self::assertEquals('some-state', $state->get('state'));
}

public function testGetStateWithoutStoredValues(): void
Expand Down Expand Up @@ -207,7 +207,7 @@ public function testGetAuthorizationUrlWithEnabledCsrf(): void
/**
* @dataProvider provideAccessTokenData
*/
public function testGetAccessToken(string $response, string $contentType)
public function testGetAccessToken(string $response, string $contentType): void
{
$resourceOwner = $this->createResourceOwner(
[],
Expand Down Expand Up @@ -293,7 +293,7 @@ public function testGetAccessTokenErrorResponse(): void
/**
* @dataProvider provideRefreshToken
*/
public function testRefreshAccessToken($response, $contentType)
public function testRefreshAccessToken($response, $contentType): void
{
$resourceOwner = $this->createResourceOwner(
[],
Expand All @@ -320,7 +320,7 @@ public function provideRefreshToken(): iterable
/**
* @dataProvider provideInvalidRefreshToken
*/
public function testRefreshAccessTokenInvalid(string $response, string $exceptionClass)
public function testRefreshAccessTokenInvalid(string $response, string $exceptionClass): void
{
$this->expectException($exceptionClass);

Expand Down
2 changes: 2 additions & 0 deletions Test/OAuth/ResourceOwner/ResourceOwnerTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth1ResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
use HWI\Bundle\OAuthBundle\Test\Fixtures\ResourceOwner\OAuth1ResourceOwnerStub;
use HWI\Bundle\OAuthBundle\Test\Fixtures\ResourceOwner\OAuth2ResourceOwnerStub;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
Expand Down
2 changes: 1 addition & 1 deletion Tests/App/Form/RegistrationFormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function configureOptions(OptionsResolver $resolver)
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
public function getBlockPrefix(): string
{
return 'registration';
}
Expand Down
1 change: 1 addition & 0 deletions Tests/Controller/AbstractConnectControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMapLocator;
use HWI\Bundle\OAuthBundle\Security\OAuthUtils;
use HWI\Bundle\OAuthBundle\Test\Fixtures\CustomUserResponse;
use HWI\Bundle\OAuthBundle\Tests\App\Form\RegistrationFormType;
use HWI\Bundle\OAuthBundle\Tests\Fixtures\CustomOAuthToken;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down
1 change: 1 addition & 0 deletions Tests/OAuth/ResourceOwner/AppleResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner;

use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\AppleResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\Response\AbstractUserResponse;
use HWI\Bundle\OAuthBundle\Test\Fixtures\CustomUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;
use Symfony\Component\HttpFoundation\Request;
Expand Down
3 changes: 1 addition & 2 deletions Tests/OAuth/ResourceOwner/Auth0ResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@
namespace HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner;

use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\Auth0ResourceOwner;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
use Symfony\Component\HttpFoundation\Request;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;
use Symfony\Component\Security\Http\HttpUtils;

final class Auth0ResourceOwnerTest extends GenericOAuth2ResourceOwnerTestCase
Expand Down
2 changes: 1 addition & 1 deletion Tests/OAuth/ResourceOwner/AzureResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use HWI\Bundle\OAuthBundle\OAuth\Exception\HttpTransportException;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\AzureResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\Response\AbstractUserResponse;
use HWI\Bundle\OAuthBundle\Tests\Fixtures\CustomUserResponse;
use HWI\Bundle\OAuthBundle\Test\Fixtures\CustomUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;

final class AzureResourceOwnerTest extends GenericOAuth2ResourceOwnerTestCase
Expand Down
2 changes: 1 addition & 1 deletion Tests/OAuth/ResourceOwner/GitHubResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GitHubResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\Response\AbstractUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;
use HWI\Bundle\OAuthBundle\Test\Fixtures\CustomUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;

final class GitHubResourceOwnerTest extends GenericOAuth2ResourceOwnerTestCase
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/OAuth/ResourceOwner/LinkedinResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\LinkedinResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\Response\AbstractUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;
use HWI\Bundle\OAuthBundle\Test\Fixtures\CustomUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;

final class LinkedinResourceOwnerTest extends GenericOAuth2ResourceOwnerTestCase
{
Expand Down
2 changes: 1 addition & 1 deletion Tests/OAuth/ResourceOwner/SinaWeiboResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use HWI\Bundle\OAuthBundle\OAuth\Exception\HttpTransportException;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\SinaWeiboResourceOwner;
use HWI\Bundle\OAuthBundle\OAuth\Response\AbstractUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;
use HWI\Bundle\OAuthBundle\Test\Fixtures\CustomUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;

final class SinaWeiboResourceOwnerTest extends GenericOAuth2ResourceOwnerTestCase
{
Expand Down
4 changes: 2 additions & 2 deletions Tests/OAuth/ResourceOwner/YahooResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\YahooResourceOwner;
use HWI\Bundle\OAuthBundle\Test\Fixtures\CustomUserResponse;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth1ResourceOwnerTestCase;
use HWI\Bundle\OAuthBundle\Test\OAuth\ResourceOwner\GenericOAuth2ResourceOwnerTestCase;

final class YahooResourceOwnerTest extends GenericOAuth1ResourceOwnerTestCase
final class YahooResourceOwnerTest extends GenericOAuth2ResourceOwnerTestCase
{
protected string $resourceOwnerClass = YahooResourceOwner::class;
protected string $userResponse = <<<json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\HttpUtils;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
use HWI\Bundle\OAuthBundle\Tests\Fixtures\User;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\User\UserInterface;

final class OAuthTokenTest extends TestCase
{
Expand Down
35 changes: 17 additions & 18 deletions Tests/Security/Core/User/EntityUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,7 @@ public function testLoadUserByIdentifierThrowsExceptionWhenUserIsNotFound(): voi

public function testLoadUserByUsernameThrowsExceptionWhenUserIsNull(): void
{
if (class_exists(UserNotFoundException::class)) {
$this->expectException(UserNotFoundException::class);
} else {
$this->expectException(UsernameNotFoundException::class);
}

$this->assertUserNotFoundException();
$this->expectExceptionMessage("User 'asm89' not found.");

$provider = $this->createEntityUserProvider();
Expand All @@ -74,12 +69,7 @@ public function testLoadUserByOAuthUserResponseThrowsExceptionWhenNoPropertyIsCo

public function testLoadUserByOAuthUserResponseThrowsExceptionWhenUserIsNull(): void
{
if (class_exists(UserNotFoundException::class)) {
$this->expectException(UserNotFoundException::class);
} else {
$this->expectException(UsernameNotFoundException::class);
}

$this->assertUserNotFoundException();
$this->expectExceptionMessage("User 'asm89' not found.");

$userResponseMock = $this->createUserResponseMock('asm89', 'github');
Expand All @@ -102,12 +92,7 @@ public function testLoadUserByOAuthUserResponse(): void

public function testRefreshUserThrowsExceptionWhenUserIsNull(): void
{
if (class_exists(UserNotFoundException::class)) {
$this->expectException(UserNotFoundException::class);
} else {
$this->expectException(UsernameNotFoundException::class);
}

$this->assertUserNotFoundException();
$this->expectExceptionMessage('User with ID "1" could not be reloaded.');

$provider = $this->createEntityUserProvider();
Expand Down Expand Up @@ -197,4 +182,18 @@ private function createUserResponseMock($username = null, $resourceOwnerName = n

return $responseMock;
}

private function assertUserNotFoundException(): void
{
if (class_exists(UserNotFoundException::class)) {
$this->expectException(UserNotFoundException::class);
} else {
if (!class_exists(UsernameNotFoundException::class)) {
$this->markTestSkipped('Requires Symfony <5.4');
}

// @phpstan-ignore-next-line Symfony <5.4 BC layer
$this->expectException(UsernameNotFoundException::class);
}
}
}
2 changes: 2 additions & 0 deletions Tests/Security/Core/User/OAuthUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function testLoadUserByUsernameOrIdentifier(): void
$user = $this->provider->loadUserByUsername('asm89');

$this->assertInstanceOf(OAuthUser::class, $user);
// @phpstan-ignore-next-line Symfony < 5.4 BC layer
$this->assertEquals('asm89', $user->getUsername());
} else {
$user = $this->provider->loadUserByIdentifier('asm89');
Expand Down Expand Up @@ -89,6 +90,7 @@ public function testLoadUserByOAuthUserResponse(): void
$this->assertInstanceOf(OAuthUser::class, $user);

if (method_exists($this->provider, 'loadUserByUsername')) {
// @phpstan-ignore-next-line Symfony < 5.4 BC layer
$this->assertEquals('asm89', $user->getUsername());
} else {
$this->assertEquals('asm89', $user->getUserIdentifier());
Expand Down
Loading

0 comments on commit ab1bf5f

Please sign in to comment.