Skip to content

Commit

Permalink
All resource owners are collected automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
gassan authored and stloyd committed Jul 22, 2022
1 parent 63489a1 commit 9a12b9a
Show file tree
Hide file tree
Showing 79 changed files with 473 additions and 285 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Changelog
## 2.0.0-BETA3 (2022-xx-xx)
* BC Break: Dropped support for Symfony: ~6.0,
* BC Break: Class `Templating\Helper\OAuthHelper` was merged into `Twig\Extension\OAuthRuntime`,
* BC Break: When resource owner class doesn't define `TYPE` constant or is `null`, then key will be calculated by converting its class name without `ResourceOwner` suffix to `snake_case`, if neither is felt, then `\LogicException` will be thrown,
* Deprecated: method `UserResponseInterface::getUsername()` was deprecated in favour of `UserResponseInterface::getUserIdentifier()` to match changes in Symfony Security component,
* Enhancement: `@internal` resourceOwner oauth types in Configuration are calculated automatically by scandir. All classes extended from `GenericOAuth[X]ResourceOwner` get `oauth[X]` type. If class only implements ResourceOwnerInterface then its oauth type is `unknown`. ResourceOwner key (parameter `type` in configs) should have defined ResourceOwner::TYPE constant. Each user defined custom ResourceOwner class that implemented `ResourceOwnerInterface` will be registered automatically. If `autoconfigure` option is disabled user have to add the tag `hwi_oauth.resource_owner` to the service definition,
* Enhancement: Class `ConnectController` was split into two smaller ones, `Connect\ConnectController` & `Connect\RegisterController`,
* Bugfix: Added `OAuth1ResourceOwner` & `OAuth2ResourceOwner` to cover case of implementing custom oauth resource owners,
* Bugfix: Fixed Authorization Header in `CleverResourceOwner::doGetRequest`,
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@

"require": {
"php": "^7.4 || ^8.0",
"symfony/deprecation-contracts": "^2.5 || ^3.0",
"symfony/framework-bundle": "^4.4.20 || ^5.4 || ^6.1",
"symfony/security-bundle": "^4.4 || ^5.4 || ^6.1",
"symfony/options-resolver": "^4.4 || ^5.4 || ^6.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

final class EnableRefreshOAuthTokenListenerCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
/** @var HWIOAuthExtension $extension */
$extension = $container->getExtension('hwi_oauth');
if (!$extension->isRefreshTokenListenerEnabled()) {
return;
}

if ($extension->isRefreshTokenListenerEnabled()) {
foreach ($extension->getFirewallNames() as $firewallName => $_) {
$container->getDefinition('hwi_oauth.context_listener.token_refresher.'.$firewallName)
->addMethodCall('enable');
}
foreach ($extension->getFirewallNames() as $firewallName => $_) {
$container->findDefinition('hwi_oauth.context_listener.token_refresher.'.$firewallName)
->addMethodCall('enable');
}
}
}
101 changes: 101 additions & 0 deletions src/DependencyInjection/CompilerPass/ResourceOwnerCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/*
* This file is part of the HWIOAuthBundle package.
*
* (c) Hardware Info <[email protected]>
*
* 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\Configuration;
use HWI\Bundle\OAuthBundle\DependencyInjection\HWIOAuthExtension;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;

/**
* Registers "hwi_oauth.resource_owner.$type.class" Parameters and checks resource owner configurations, whether given
* type exists (Apps can add own ResourceOwners).
*
* Adds resource owner maps to the locator and utils.
*/
final class ResourceOwnerCompilerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
$this->registerResourceOwnerTypeClassParameters($container);
$this->addResourceOwnerMapToLocatorAndUtils($container);
}

private function registerResourceOwnerTypeClassParameters(ContainerBuilder $container): void
{
foreach ($container->findTaggedServiceIds('hwi_oauth.resource_owner') as $serviceId => $_) {
$definition = $container->findDefinition($serviceId);
Configuration::registerResourceOwner($definition->getClass());
}

foreach (Configuration::getResourceOwnerTypesClassMap() as $type => $resourceOwnerClass) {
$parameterName = "hwi_oauth.resource_owner.$type.class";
if (!$container->hasParameter($parameterName)) {
$container->setParameter($parameterName, $resourceOwnerClass);
}
}

// Check whether resource owner set with parameter '%hwi_oauth.resource_owner.[type].class%' type exists
/** @var ServiceLocator $locator */
$locator = $container->get('hwi_oauth.resource_owners.locator');

foreach ($locator->getProvidedServices() as $resourceOwnerName => $_) {
try {
$definition = $container->findDefinition('hwi_oauth.resource_owner.'.$resourceOwnerName);
} catch (ServiceNotFoundException $e) {
// Resource owner defined with "options.service"
continue;
}

$resourceOwnerClass = $definition->getClass();

// Check whether a ResourceOwner class exists only if resource owner was set by its "options.type"
if (false === preg_match('~^%(?P<parameter>hwi_oauth.resource_owner.(?P<type>.+).class)%$~', $resourceOwnerClass, $match)) {
return;
}

if (!Configuration::isResourceOwnerSupported($match['type'])) {
$e = new \InvalidArgumentException(sprintf('Unknown resource owner type "%s"', $match['type']));

throw new InvalidConfigurationException(sprintf('Invalid configuration for path "hwi_oauth.resource_owners.%s.type": %s', $resourceOwnerName, $e->getMessage()), $e->getCode(), $e);
}
}
}

private function addResourceOwnerMapToLocatorAndUtils(ContainerBuilder $container): void
{
/** @var HWIOAuthExtension $extension */
$extension = $container->getExtension('hwi_oauth');

$locatorDef = $container->findDefinition('hwi_oauth.resource_ownermap_locator');
$oauthUtilsDef = $container->findDefinition('hwi_oauth.security.oauth_utils');

foreach ($extension->getFirewallNames() as $firewallName => $_) {
$resourceOwnerMapId = 'hwi_oauth.resource_ownermap.'.$firewallName;

$container->findDefinition($resourceOwnerMapId)
->setArgument('$locator', new Reference('hwi_oauth.resource_owners.locator'));

$resourceOwnerMapRef = new Reference($resourceOwnerMapId);

$locatorDef->addMethodCall('set', [$firewallName, $resourceOwnerMapRef]);
$oauthUtilsDef->addMethodCall('addResourceOwnerMap', [$firewallName, $resourceOwnerMapRef]);
}
}
}

This file was deleted.

Loading

0 comments on commit 9a12b9a

Please sign in to comment.