Skip to content

Commit

Permalink
Fix problem with twig extension after refactoring, cleanup after review
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Sep 26, 2013
1 parent 842bc1f commit 7785d1c
Show file tree
Hide file tree
Showing 56 changed files with 477 additions and 341 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ Changelog
=========

## 0.3.0-alpha3 (2013-xx-xx)
* [BC break] Removed `ResourceOwnerInterface::getOption()` method
* [BC break] `AccountConnectorInterface::connect()` method now requires the first
parameter to be instance of `Symfony\Component\Security\Core\User\UserInterface`
* [BC break] `ConnectController::authenticateUser()` method now requires the first
parameter to be instance of `Symfony\Component\HttpFoundation\Request`
* [BC break] Removed `AbstractResourceOwner::addOptions()` method
* [BC break] `OAuthUtils::getAuthorizationUrl()` & `OAuthUtils::getLoginUrl()` methods
now expect first parameter to be instance of `Symfony\Component\HttpFoundation\Request`
* [BC break] LinkedIn resource owner now uses OAuth2 approach, visit official
web page for details how to migrate: https://developer.linkedin.com/documents/authentication#migration
* [BC break] Dropbox resource owner now uses OAuth2 approach
Expand Down
9 changes: 5 additions & 4 deletions Controller/ConnectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function registrationAction(Request $request, $key)
$this->container->get('hwi_oauth.account.connector')->connect($form->getData(), $userInformation);

// Authenticate the user
$this->authenticateUser($form->getData(), $error->getResourceOwnerName(), $error->getRawToken());
$this->authenticateUser($request, $form->getData(), $error->getResourceOwnerName(), $error->getRawToken());

return $this->container->get('templating')->renderResponse('HWIOAuthBundle:Connect:registration_success.html.' . $this->getTemplatingEngine(), array(
'userInformation' => $userInformation,
Expand Down Expand Up @@ -209,7 +209,7 @@ public function connectServiceAction(Request $request, $service)
$this->container->get('hwi_oauth.account.connector')->connect($currentUser, $userInformation);

// Update user token with new details
$this->authenticateUser($currentUser, $service, $currentToken->getRawToken(), false);
$this->authenticateUser($request, $currentUser, $service, $currentToken->getRawToken(), false);

return $this->container->get('templating')->renderResponse('HWIOAuthBundle:Connect:connect_success.html.' . $this->getTemplatingEngine(), array(
'userInformation' => $userInformation,
Expand Down Expand Up @@ -303,12 +303,13 @@ protected function generate($route, $params = array(), $absolute = false)
/**
* Authenticate a user with Symfony Security
*
* @param Request $request
* @param UserInterface $user
* @param string $resourceOwnerName
* @param string $accessToken
* @param boolean $fakeLogin
*/
protected function authenticateUser(UserInterface $user, $resourceOwnerName, $accessToken, $fakeLogin = true)
protected function authenticateUser(Request $request, UserInterface $user, $resourceOwnerName, $accessToken, $fakeLogin = true)
{
try {
$this->container->get('hwi_oauth.user_checker')->checkPostAuth($user);
Expand All @@ -328,7 +329,7 @@ protected function authenticateUser(UserInterface $user, $resourceOwnerName, $ac
// Since we're "faking" normal login, we need to throw our INTERACTIVE_LOGIN event manually
$this->container->get('event_dispatcher')->dispatch(
SecurityEvents::INTERACTIVE_LOGIN,
new InteractiveLoginEvent($this->container->get('request'), $token)
new InteractiveLoginEvent($request, $token)
);
}
}
Expand Down
60 changes: 14 additions & 46 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,9 @@ public function getConfigTreeBuilder()
$rootNode = $builder->root('hwi_oauth');
$rootNode
->children()
->scalarNode('firewall_name')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('target_path_parameter')
->defaultNull()
->end()
->scalarNode('templating_engine')
->defaultValue('twig')
->end()
->scalarNode('firewall_name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('target_path_parameter')->defaultNull()->end()
->scalarNode('templating_engine')->defaultValue('twig')->end()
->end()
;

Expand Down Expand Up @@ -151,12 +144,8 @@ private function addResourceOwnersConfiguration(ArrayNodeDefinition $node)
->thenUnset()
->end()
->end()
->scalarNode('client_id')
->cannotBeEmpty()
->end()
->scalarNode('client_secret')
->cannotBeEmpty()
->end()
->scalarNode('client_id')->cannotBeEmpty()->end()
->scalarNode('client_secret')->cannotBeEmpty()->end()
->scalarNode('realm')
->validate()
->ifTrue(function($v) {
Expand Down Expand Up @@ -326,20 +315,10 @@ private function addHttpClientConfiguration(ArrayNodeDefinition $node)
->arrayNode('http_client')
->addDefaultsIfNotSet()
->children()
->scalarNode('timeout')
->defaultValue(5)
->cannotBeEmpty()
->end()
->booleanNode('verify_peer')
->defaultTrue()
->end()
->scalarNode('max_redirects')
->defaultValue(5)
->cannotBeEmpty()
->end()
->booleanNode('ignore_errors')
->defaultTrue()
->end()
->scalarNode('timeout')->defaultValue(5)->cannotBeEmpty()->end()
->booleanNode('verify_peer')->defaultTrue()->end()
->scalarNode('max_redirects')->defaultValue(5)->cannotBeEmpty()->end()
->booleanNode('ignore_errors')->defaultTrue()->end()
->end()
->end()
->end()
Expand All @@ -352,18 +331,10 @@ private function addConnectConfiguration(ArrayNodeDefinition $node)
->children()
->arrayNode('connect')
->children()
->booleanNode('confirmation')
->defaultTrue()
->end()
->scalarNode('account_connector')
->cannotBeEmpty()
->end()
->scalarNode('registration_form_handler')
->cannotBeEmpty()
->end()
->scalarNode('registration_form')
->cannotBeEmpty()
->end()
->booleanNode('confirmation')->defaultTrue()->end()
->scalarNode('account_connector')->cannotBeEmpty()->end()
->scalarNode('registration_form_handler')->cannotBeEmpty()->end()
->scalarNode('registration_form')->cannotBeEmpty()->end()
->end()
->end()
->end()
Expand All @@ -376,10 +347,7 @@ private function addFosubConfiguration(ArrayNodeDefinition $node)
->children()
->arrayNode('fosub')
->children()
->scalarNode('username_iterations')
->defaultValue(5)
->cannotBeEmpty()
->end()
->scalarNode('username_iterations')->defaultValue(5)->cannotBeEmpty()->end()
->arrayNode('properties')
->isRequired()
->useAttributeAsKey('name')
Expand Down
18 changes: 0 additions & 18 deletions DependencyInjection/HWIOAuthExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,12 @@ public function createResourceOwnerService(ContainerBuilder $container, $name, a
$type = $options['type'];
unset($options['type']);

if (!empty($options['paths'])) {
$paths = $options['paths'];
unset($options['paths']);
}

if (!empty($options['options'])) {
$customOptions = $options['options'];
unset($options['options']);
}

$definition = new DefinitionDecorator('hwi_oauth.abstract_resource_owner.'.$type);
$container->setDefinition('hwi_oauth.resource_owner.'.$name, $definition);
$definition
->replaceArgument(2, $options)
->replaceArgument(3, $name)
;

if (isset($paths)) {
$definition->addMethodCall('addPaths', array($paths));
}

if (isset($customOptions)) {
$definition->addMethodCall('addOptions', array($customOptions));
}
}
}

Expand Down
124 changes: 55 additions & 69 deletions DependencyInjection/Security/Factory/OAuthFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace HWI\Bundle\OAuthBundle\DependencyInjection\Security\Factory;

use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AbstractFactory;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
Expand All @@ -36,14 +35,11 @@ public function addConfiguration(NodeDefinition $node)

$builder = $node->children();
$builder
->scalarNode('login_path')
->cannotBeEmpty()
->isRequired()
->end()
->scalarNode('login_path')->cannotBeEmpty()->isRequired()->end()
;

$this->addOAuthProviderConfiguration($builder);
$this->addResourceOwnersConfiguration($builder);
$this->addOAuthProviderConfiguration($node);
$this->addResourceOwnersConfiguration($node);
}

/**
Expand Down Expand Up @@ -79,7 +75,7 @@ protected function createResourceOwnerMap(ContainerBuilder $container, $id, arra

$container
->setDefinition($this->getResourceOwnerMapReference($id), new DefinitionDecorator('hwi_oauth.abstract_resource_ownermap'))
->replaceArgument(3, new Parameter('hwi_oauth.resource_ownermap.configured.'.$id))
->replaceArgument(2, new Parameter('hwi_oauth.resource_ownermap.configured.'.$id))
;
}

Expand Down Expand Up @@ -145,7 +141,7 @@ protected function createOAuthAwareUserProvider(ContainerBuilder $container, $id
/**
* {@inheritDoc}
*/
protected function createEntryPoint(ContainerBuilder $container, $id, $config, $defaultEntryPoint)
protected function createEntryPoint($container, $id, $config, $defaultEntryPoint)
{
$entryPointId = 'hwi_oauth.authentication.entry_point.oauth.'.$id;

Expand All @@ -160,7 +156,7 @@ protected function createEntryPoint(ContainerBuilder $container, $id, $config, $
/**
* {@inheritDoc}
*/
protected function createListener(ContainerBuilder $container, $id, $config, $userProvider)
protected function createListener($container, $id, $config, $userProvider)
{
$listenerId = parent::createListener($container, $id, $config, $userProvider);

Expand All @@ -186,81 +182,71 @@ protected function getListenerId()
return 'hwi_oauth.authentication.listener.oauth';
}

private function addOAuthProviderConfiguration(ArrayNodeDefinition $node)
private function addOAuthProviderConfiguration(NodeDefinition $node)
{
$node
->children()
->arrayNode('oauth_user_provider')
->isRequired()
->children()
->arrayNode('orm')
->children()
->scalarNode('class')
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('manager_name')
->defaultNull()
->end()
->arrayNode('properties')
->isRequired()
->useAttributeAsKey('name')
->prototype('scalar')
->end()
$builder = $node->children();
$builder
->arrayNode('oauth_user_provider')
->isRequired()
->children()
->arrayNode('orm')
->children()
->scalarNode('class')->isRequired()->cannotBeEmpty()->end()
->scalarNode('manager_name')->defaultNull()->end()
->arrayNode('properties')
->isRequired()
->useAttributeAsKey('name')
->prototype('scalar')
->end()
->end()
->end()
->scalarNode('service')
->cannotBeEmpty()
->end()
->scalarNode('oauth')
->end()
->arrayNode('fosub')
->children()
->arrayNode('properties')
->isRequired()
->useAttributeAsKey('name')
->prototype('scalar')
->end()
->end()
->scalarNode('service')->cannotBeEmpty()->end()
->scalarNode('oauth')->end()
->arrayNode('fosub')
->children()
->arrayNode('properties')
->isRequired()
->useAttributeAsKey('name')
->prototype('scalar')
->end()
->end()
->end()
->end()
->validate()
->ifTrue(function($c) {
return 1 !== count($c) || !in_array(key($c), array('fosub', 'oauth', 'orm', 'service'));
})
->thenInvalid("You should configure (only) one of: 'fosub', 'oauth', 'orm', 'service'.")
->end()
->end()
->validate()
->ifTrue(function($c) {
return 1 !== count($c) || !in_array(key($c), array('fosub', 'oauth', 'orm', 'service'));
})
->thenInvalid("You should configure (only) one of: 'fosub', 'oauth', 'orm', 'service'.")
->end()
->end()
;
}

private function addResourceOwnersConfiguration(ArrayNodeDefinition $node)
private function addResourceOwnersConfiguration(NodeDefinition $node)
{
$node
->children()
->arrayNode('resource_owners')
->isRequired()
->useAttributeAsKey('name')
->prototype('scalar')
->end()
->validate()
->ifTrue(function($c) {
$checkPaths = array();
foreach ($c as $checkPath) {
if (in_array($checkPath, $checkPaths)) {
return true;
}

$checkPaths[] = $checkPath;
$builder = $node->children();
$builder
->arrayNode('resource_owners')
->isRequired()
->useAttributeAsKey('name')
->prototype('scalar')
->end()
->validate()
->ifTrue(function($c) {
$checkPaths = array();
foreach ($c as $checkPath) {
if (in_array($checkPath, $checkPaths)) {
return true;
}

return false;
})
->thenInvalid('Each resource owner should have a unique "check_path".')
->end()
$checkPaths[] = $checkPath;
}

return false;
})
->thenInvalid('Each resource owner should have a unique "check_path".')
->end()
->end()
;
Expand Down
Loading

0 comments on commit 7785d1c

Please sign in to comment.