Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Compat/LegacySecurityFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace FOS\OAuthServerBundle\Compat;

/**
* Empty stub that replaces Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface
* when it doesn't exist (Symfony 6.x+). Allows OAuthFactory to implement it on all Symfony versions.
*/
interface LegacySecurityFactoryInterface
{
}
15 changes: 15 additions & 0 deletions Compat/passport_interface_polyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

/*
* Polyfill for PassportInterface, which was removed in Symfony 6.0.
* On Symfony 6.x, it is aliased to Passport so that type hints using
* PassportInterface remain valid on both Symfony versions.
*/
if (!interface_exists(\Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface::class)) {
class_alias(
\Symfony\Component\Security\Http\Authenticator\Passport\Passport::class,
\Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface::class
);
}
15 changes: 15 additions & 0 deletions Compat/security_factory_interface_polyfill.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

/*
* Polyfill for SecurityFactoryInterface, which was removed in Symfony 6.0.
* Defining it as an empty interface allows OAuthFactory to safely implement it
* on both Symfony 5.4 and 6.x without a fatal error.
*/
if (!interface_exists(\Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface::class)) {
class_alias(
\FOS\OAuthServerBundle\Compat\LegacySecurityFactoryInterface::class,
\Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface::class
);
}
29 changes: 29 additions & 0 deletions DependencyInjection/Compiler/PasswordHasherCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace FOS\OAuthServerBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* On Symfony 6.x, security.encoder_factory no longer exists.
* This compiler pass rewires the OAuthStorage to use security.user_password_hasher instead.
*/
class PasswordHasherCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('fos_oauth_server.storage.default')) {
return;
}

$storage = $container->getDefinition('fos_oauth_server.storage.default');

if ($container->has('security.user_password_hasher')) {
$storage->replaceArgument(5, new Reference('security.user_password_hasher'));
}
}
}
7 changes: 5 additions & 2 deletions DependencyInjection/FOSOAuthServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load(sprintf('%s.xml', $config['db_driver']));
}

foreach (['oauth', 'security'] as $basename) {
$loader->load(sprintf('%s.xml', $basename));
$loader->load('oauth.xml');
$loader->load('security.xml');

if (interface_exists(\Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface::class)) {
$loader->load('security_legacy.xml');
}

$container->setAlias('fos_oauth_server.storage', $config['service']['storage']);
Expand Down
8 changes: 8 additions & 0 deletions DependencyInjection/Security/Factory/OAuthFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ public function getPosition()
return 'pre_auth';
}

/**
* Required by AuthenticatorFactoryInterface in Symfony 6.x (replaces getPosition()).
*/
public function getPriority(): int
{
return -10;
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 7 additions & 1 deletion FOSOAuthServerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace FOS\OAuthServerBundle;

use FOS\OAuthServerBundle\DependencyInjection\Compiler\GrantExtensionsCompilerPass;
use FOS\OAuthServerBundle\DependencyInjection\Compiler\PasswordHasherCompilerPass;
use FOS\OAuthServerBundle\DependencyInjection\Compiler\RequestStackCompilerPass;
use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
use FOS\OAuthServerBundle\DependencyInjection\Security\Factory\OAuthFactory;
Expand All @@ -34,9 +35,14 @@ public function build(ContainerBuilder $container)

/** @var SecurityExtension $extension */
$extension = $container->getExtension('security');
$extension->addSecurityListenerFactory(new OAuthFactory());
if (method_exists($extension, 'addAuthenticatorFactory')) {
$extension->addAuthenticatorFactory(new OAuthFactory());
} else {
$extension->addSecurityListenerFactory(new OAuthFactory());
}

$container->addCompilerPass(new GrantExtensionsCompilerPass());
$container->addCompilerPass(new RequestStackCompilerPass());
$container->addCompilerPass(new PasswordHasherCompilerPass());
}
}
14 changes: 0 additions & 14 deletions Resources/config/security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,16 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="fos_oauth_server.security.authentication.provider.class">FOS\OAuthServerBundle\Security\Authentication\Provider\OAuthProvider</parameter>
<parameter key="fos_oauth_server.security.authentication.listener.class">FOS\OAuthServerBundle\Security\Firewall\OAuthListener</parameter>
<parameter key="fos_oauth_server.security.entry_point.class">FOS\OAuthServerBundle\Security\EntryPoint\OAuthEntryPoint</parameter>
</parameters>

<services>
<service id="fos_oauth_server.security.authentication.provider" class="%fos_oauth_server.security.authentication.provider.class%" public="false">
<argument /> <!-- user provider -->
<argument type="service" id="fos_oauth_server.server" />
<argument type="service" id="security.user_checker" />
</service>

<service id="fos_oauth_server.security.authentication.authenticator" class="FOS\OAuthServerBundle\Security\Authentication\Authenticator\OAuthAuthenticator" public="false">
<argument type="service" id="fos_oauth_server.server" />
<argument type="service" id="security.user_checker" />
<argument /> <!-- user provider -->
</service>

<service id="fos_oauth_server.security.authentication.listener" class="%fos_oauth_server.security.authentication.listener.class%" public="false">
<argument type="service" id="security.token_storage"/>
<argument type="service" id="security.authentication.manager" />
<argument type="service" id="fos_oauth_server.server" />
</service>

<service id="fos_oauth_server.security.entry_point" class="%fos_oauth_server.security.entry_point.class%" public="false">
<argument type="service" id="fos_oauth_server.server" />
</service>
Expand Down
26 changes: 26 additions & 0 deletions Resources/config/security_legacy.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<!-- Legacy Symfony 5.x authentication system (provider + listener). Loaded only on Symfony < 6.0. -->
<parameters>
<parameter key="fos_oauth_server.security.authentication.provider.class">FOS\OAuthServerBundle\Security\Authentication\Provider\OAuthProvider</parameter>
<parameter key="fos_oauth_server.security.authentication.listener.class">FOS\OAuthServerBundle\Security\Firewall\OAuthListener</parameter>
</parameters>

<services>
<service id="fos_oauth_server.security.authentication.provider" class="%fos_oauth_server.security.authentication.provider.class%" public="false">
<argument /> <!-- user provider -->
<argument type="service" id="fos_oauth_server.server" />
<argument type="service" id="security.user_checker" />
</service>

<service id="fos_oauth_server.security.authentication.listener" class="%fos_oauth_server.security.authentication.listener.class%" public="false">
<argument type="service" id="security.token_storage"/>
<argument type="service" id="security.authentication.manager" />
<argument type="service" id="fos_oauth_server.server" />
</service>
</services>
</container>
4 changes: 0 additions & 4 deletions Security/Authentication/Authenticator/OAuthAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ public function authenticate(Request $request): Passport
*/
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
{
if (!$passport instanceof Passport) {
throw new \RuntimeException(sprintf('Cannot create a OAuth2 authenticated token. $passport should be a %s', Passport::class));
}

$token = $this->createToken($passport, $firewallName);
$token->setAuthenticated(true);

Expand Down
25 changes: 17 additions & 8 deletions Storage/OAuthStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use OAuth2\OAuth2;
use OAuth2\OAuth2ServerException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserProviderInterface;

Expand Down Expand Up @@ -60,9 +59,9 @@ class OAuthStorage implements IOAuth2RefreshTokens, IOAuth2GrantUser, IOAuth2Gra
protected $userProvider;

/**
* @var EncoderFactoryInterface
* @var object|null EncoderFactoryInterface (Symfony 5.x) or UserPasswordHasherInterface (Symfony 6.x)
*/
protected $encoderFactory;
protected $passwordHandler;

/**
* @var array [uri] => GrantExtensionInterface
Expand All @@ -71,14 +70,14 @@ class OAuthStorage implements IOAuth2RefreshTokens, IOAuth2GrantUser, IOAuth2Gra

public function __construct(ClientManagerInterface $clientManager, AccessTokenManagerInterface $accessTokenManager,
RefreshTokenManagerInterface $refreshTokenManager, AuthCodeManagerInterface $authCodeManager,
UserProviderInterface $userProvider = null, EncoderFactoryInterface $encoderFactory = null)
UserProviderInterface $userProvider = null, object $passwordHandler = null)
{
$this->clientManager = $clientManager;
$this->accessTokenManager = $accessTokenManager;
$this->refreshTokenManager = $refreshTokenManager;
$this->authCodeManager = $authCodeManager;
$this->userProvider = $userProvider;
$this->encoderFactory = $encoderFactory;
$this->passwordHandler = $passwordHandler;

$this->grantExtensions = [];
}
Expand Down Expand Up @@ -152,13 +151,23 @@ public function checkUserCredentials(IOAuth2Client $client, $username, $password
}

try {
$user = $this->userProvider->loadUserByUsername($username);
if (method_exists($this->userProvider, 'loadUserByIdentifier')) {
$user = $this->userProvider->loadUserByIdentifier($username);
} else {
$user = $this->userProvider->loadUserByUsername($username);
}
} catch (AuthenticationException $e) {
return false;
}

$encoder = $this->encoderFactory->getEncoder($user);
if ($encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt())) {
if ($this->passwordHandler instanceof \Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface) {
$valid = $this->passwordHandler->isPasswordValid($user, $password);
} else {
$encoder = $this->passwordHandler->getEncoder($user);
$valid = $encoder->isPasswordValid($user->getPassword(), $password, $user->getSalt());
}

if ($valid) {
return [
'data' => $user,
];
Expand Down
23 changes: 14 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"friendsofsymfony/oauth2-php": "~1.1",
"symfony/dependency-injection": "^4.4 || ^5.1",
"symfony/framework-bundle": "^4.4 || ^5.1",
"symfony/security-bundle": "^4.4 || ^5.1",
"symfony/twig-bundle": "^4.4 || ^5.1"
"symfony/dependency-injection": "^5.4 || ^6.4",
"symfony/framework-bundle": "^5.4 || ^6.4",
"symfony/password-hasher": "^5.4 || ^6.4",
"symfony/security-bundle": "^5.4 || ^6.4",
"symfony/twig-bundle": "^5.4 || ^6.4"
},
"conflict": {
"twig/twig": "<1.40 || >=2.0,<2.9"
Expand All @@ -40,10 +41,10 @@
"phpstan/phpstan-phpunit": "~0.9",
"phpunit/phpunit": "^9.0",
"propel/propel1": "~1.6",
"symfony/console": "^4.4 || ^5.1",
"symfony/form": "^4.4 || ^5.1",
"symfony/phpunit-bridge": "^4.4 || ^5.1",
"symfony/yaml": "^4.4 || ^5.1",
"symfony/console": "^5.4 || ^6.4",
"symfony/form": "^5.4 || ^6.4",
"symfony/phpunit-bridge": "^5.4 || ^6.4",
"symfony/yaml": "^5.4 || ^6.4",
"willdurand/propel-typehintable-behavior": "~1.0"
},
"suggest": {
Expand All @@ -66,6 +67,10 @@
"psr-4": {
"FOS\\OAuthServerBundle\\": ""
},
"files": [
"Compat/security_factory_interface_polyfill.php",
"Compat/passport_interface_polyfill.php"
],
"exclude-from-classmap": [
"/Tests/"
]
Expand Down