Skip to content

Commit

Permalink
Bugfix | Adjust OAuthAuthenticator to be compatible with new Symfon…
Browse files Browse the repository at this point in the history
…y Security
  • Loading branch information
stloyd committed Dec 6, 2021
1 parent 4b4c25a commit 4f1675d
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function createAuthenticator(
->addArgument($config['resource_owners'])
->addArgument(new Reference($this->createAuthenticationSuccessHandler($container, $firewallName, $config)))
->addArgument(new Reference($this->createAuthenticationFailureHandler($container, $firewallName, $config)))
->addArgument(array_intersect_key($config, $this->options))
;

return $authenticatorId;
Expand Down
17 changes: 17 additions & 0 deletions DependencyInjection/Security/Factory/OAuthFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ public function getPosition(): string
return 'http';
}

public function getPriority(): int
{
return 1;
}

/**
* {@inheritDoc}
*/
public function createAuthenticator(
ContainerBuilder $container,
string $firewallName,
array $config,
string $userProviderId
): string {
throw new \RuntimeException('Deprecated "OAuthFactory" cannot create new Symfony Authenticator!');
}

/**
* Gets a reference to the resource owner map.
*/
Expand Down
15 changes: 12 additions & 3 deletions Security/Http/Authenticator/OAuthAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use Symfony\Component\Security\Http\HttpUtils;

/**
* @author Vadim Borodavko <vadim.borodavko@gmail.com>
*/
final class OAuthAuthenticator implements AuthenticatorInterface
final class OAuthAuthenticator implements AuthenticatorInterface, AuthenticationEntryPointInterface
{
private HttpUtils $httpUtils;
private OAuthAwareUserProviderInterface $userProvider;
Expand All @@ -53,21 +54,24 @@ final class OAuthAuthenticator implements AuthenticatorInterface
private ?string $resourceOwnerName = null;
private ?string $refreshToken = null;
private ?int $createdAt = null;
private array $options;

public function __construct(
HttpUtils $httpUtils,
OAuthAwareUserProviderInterface $userProvider,
ResourceOwnerMapInterface $resourceOwnerMap,
array $checkPaths,
AuthenticationSuccessHandlerInterface $successHandler,
AuthenticationFailureHandlerInterface $failureHandler
AuthenticationFailureHandlerInterface $failureHandler,
array $options
) {
$this->failureHandler = $failureHandler;
$this->successHandler = $successHandler;
$this->checkPaths = $checkPaths;
$this->resourceOwnerMap = $resourceOwnerMap;
$this->userProvider = $userProvider;
$this->httpUtils = $httpUtils;
$this->options = $options;
}

public function supports(Request $request): bool
Expand All @@ -81,6 +85,11 @@ public function supports(Request $request): bool
return false;
}

public function start(Request $request, AuthenticationException $authException = null): Response
{
return new RedirectResponse($this->httpUtils->generateUri($request, $this->options['login_path']));
}

/**
* @throws AuthenticationException
* @throws LazyResponseException
Expand Down Expand Up @@ -170,7 +179,7 @@ public function onAuthenticationSuccess(Request $request, TokenInterface $token,
return $this->successHandler->onAuthenticationSuccess($request, $token);
}

public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response
{
return $this->failureHandler->onAuthenticationFailure($request, $exception);
}
Expand Down
5 changes: 0 additions & 5 deletions Tests/Functional/Controller/ConnectControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ final class ConnectControllerTest extends WebTestCase
{
use AuthenticationHelperTrait;

protected function setUp(): void
{
static::$class = AppKernel::class;
}

public static function getKernelClass(): string
{
return AppKernel::class;
Expand Down
11 changes: 8 additions & 3 deletions Tests/Functional/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,24 @@

namespace HWI\Bundle\OAuthBundle\Tests\Functional;

use HWI\Bundle\OAuthBundle\Tests\App\AppKernel;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpFoundation\Response;

final class IntegrationTest extends WebTestCase
{
public static function getKernelClass(): string
{
return AppKernel::class;
}

public function testRequestRedirect(): void
{
$client = static::createClient();
$client->disableReboot();
$client->getContainer()->set('hwi_oauth.http_client', new MockHttpClient());
$client->request('GET', '/');

$response = $client->getResponse();
Expand All @@ -37,9 +45,6 @@ public function testRequestRedirect(): void
$this->assertInstanceOf(Response::class, $response);
$this->assertSame(200, $response->getStatusCode(), 'No landing, got redirect to '.$response->headers->get('Location'));

$client->disableReboot();
$client->getContainer()->set('hwi_oauth.http_client', new MockHttpClient());

$client->click($crawler->selectLink('Login')->link());

$response = $client->getResponse();
Expand Down
12 changes: 8 additions & 4 deletions Tests/Security/Http/Authenticator/OAuthAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public function testSupports(): void
$this->getResourceOwnerMap(),
['/a', '/b'],
$this->getAuthenticationSuccessHandlerMock(),
$this->getAuthenticationFailureHandlerMock()
$this->getAuthenticationFailureHandlerMock(),
[]
);

$this->assertTrue($authenticator->supports($request));
Expand Down Expand Up @@ -148,7 +149,8 @@ public function testAuthenticate(): void
$resourceOwnerMap,
[],
$this->getAuthenticationSuccessHandlerMock(),
$this->getAuthenticationFailureHandlerMock()
$this->getAuthenticationFailureHandlerMock(),
[]
);

$passport = $authenticator->authenticate($request);
Expand Down Expand Up @@ -187,7 +189,8 @@ public function testOnAuthenticationSuccess(): void
$this->getResourceOwnerMap(),
[],
$successHandlerMock,
$this->getAuthenticationFailureHandlerMock()
$this->getAuthenticationFailureHandlerMock(),
[]
);

$this->assertSame($response, $authenticator->onAuthenticationSuccess($request, $token, 'main'));
Expand All @@ -212,7 +215,8 @@ public function testOnAuthenticationFailure(): void
$this->getResourceOwnerMap(),
[],
$this->getAuthenticationSuccessHandlerMock(),
$failureHandlerMock
$failureHandlerMock,
[]
);

$this->assertSame($response, $authenticator->onAuthenticationFailure($request, $exception));
Expand Down

0 comments on commit 4f1675d

Please sign in to comment.