Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for multiple accounts and service account authentication #18

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
114 changes: 97 additions & 17 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,14 @@ public function getConfigTreeBuilder()
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('happy_r_google_api');

$rootNode
->children()
->scalarNode('application_name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('oauth2_client_id')->isRequired()->cannotBeEmpty()->end()
->scalarNode('oauth2_client_secret')->isRequired()->cannotBeEmpty()->end()
->scalarNode('oauth2_redirect_uri')->isRequired()->cannotBeEmpty()->end()
->scalarNode('developer_key')->isRequired()->cannotBeEmpty()->end()
->scalarNode('site_name')->isRequired()->cannotBeEmpty()->end()

->scalarNode('authClass')->end()
->scalarNode('ioClass')->end()
->scalarNode('cacheClass')->end()
->scalarNode('basePath')->end()
->scalarNode('ioFileCache_directory')->end()
//end rootnode children
->end();
$this->configureAccountNode($rootNode);

//let use the api defaults
//$this->addServicesSection($rootNode);

return $treeBuilder;
}


/**
* Add the service section
*
Expand Down Expand Up @@ -144,4 +128,100 @@ private function addServicesSection(ArrayNodeDefinition $rootNode)

;
}

/**
* Add properties and validation for account configuration.
*
* @param ArrayNodeDefinition $node
*/
private function configureAccountNode(ArrayNodeDefinition $node)
{
$node
->beforeNormalization()
->ifTrue(function ($v) { return is_array($v) && !array_key_exists('accounts', $v) && !array_key_exists('account', $v); })
->then(function ($v) {
// Key that should not be rewritten to the account config
$excludedKeys = array('default_account' => true);
$account = array();

foreach ($v as $key => $value) {
if (isset($excludedKeys[$key])) {
continue;
}

$account[$key] = $v[$key];
unset($v[$key]);
}

$v['default_account'] = isset($v['default_account']) ? (string) $v['default_account'] : 'default';
$v['accounts'] = array($v['default_account'] => $account);

return $v;
})
->end()
->children()
->scalarNode('default_account')->cannotBeEmpty()->defaultValue('default')->end()
->end()
->fixXmlConfig('account')
->children()
->arrayNode('accounts')
->isRequired()
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype('array')
->validate()
->always(
function ($v) {
$required = array();

switch ($v['type']) {
case 'web':
$required = array('oauth2_client_secret', 'oauth2_redirect_uri', 'developer_key', 'site_name');
break;

case 'service':
if ((isset($v['getenv']) && true === $v['getenv']) || isset($v['json_file']) || isset($v['access_token'])) {
return $v;
}

$required = array('oauth2_client_email', 'oauth2_private_key', 'oauth2_scopes');
break;
}

foreach ($required as $key) {
if (!isset($v[$key]) || empty($v[$key])) {
throw new \InvalidArgumentException(sprintf('"%s" is not set or empty', $key));
}
}

return $v;
}
)
->end()
->children()
->enumNode('type')->values(array('web', 'service'))->cannotBeEmpty()->defaultValue('web')->end()
->scalarNode('application_name')->isRequired()->cannotBeEmpty()->end()
->scalarNode('oauth2_client_id')->isRequired()->cannotBeEmpty()->end()
->scalarNode('oauth2_client_secret')->end()
->scalarNode('oauth2_client_email')->end()
->variableNode('access_token')->end()
->scalarNode('oauth2_private_key')->end()
->scalarNode('oauth2_redirect_uri')->end()
->variableNode('oauth2_scopes')->end()
->scalarNode('developer_key')->end()
->scalarNode('site_name')->end()
->scalarNode('getenv')->end()
->scalarNode('json_file')->end()

->scalarNode('authClass')->end()
->scalarNode('ioClass')->end()
->scalarNode('cacheClass')->end()
->scalarNode('basePath')->end()
->scalarNode('ioFileCache_directory')->end()
->end()
->end()
->end()
->end()
;
}
}
72 changes: 58 additions & 14 deletions DependencyInjection/HappyRGoogleApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,72 @@
namespace HappyR\Google\ApiBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
use Symfony\Component\DependencyInjection\Loader;

/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class HappyRGoogleApiExtension extends Extension
class HappyRGoogleApiExtension extends ConfigurableExtension
{
/**
* {@inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
public function loadInternal(array $config, ContainerBuilder $container)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ConfigurableExtension is just a shortcut with helper methods.

{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
foreach ($config['accounts'] as $name => $account) {
$this->loadAccount($name, $account, $container);
}

$container->setParameter('happy_r_google_api', $config);
// Backwards compatibility
$default = $config['default_account'];
$container->setParameter('happy_r_google_api', array_merge($config['accounts'][$default], $config));
$container->setAlias('happyr.google.api.client', sprintf('happyr.google.api.%s_client', $default));
$container->setAlias('happyr.google.api.analytics', sprintf('happyr.google.api.%s_analytics', $default));
$container->setAlias('happyr.google.api.youtube', sprintf('happyr.google.api.%s_youtube', $default));
}

/**
* Define services for each account configuration.
*
* @param string $name The account name
* @param array $config The account configuration
* @param ContainerBuilder $container The container builder
*/
public function loadAccount($name, array $config, ContainerBuilder $container)
{
$clientId = sprintf('happyr.google.api.%s_client', $name);
$client = new Definition(
'HappyR\Google\ApiBundle\Services\GoogleClient',
array($config, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE))
);

$client->addTag('monolog.logger', array('channel' => 'google_client'));

$container->setDefinition($clientId, $client);

$container->setDefinition(
sprintf('happyr.google.api.%s_analytics', $name),
new Definition(
'HappyR\Google\ApiBundle\Services\AnalyticsService',
array(new Reference($clientId))
)
);

$container->setDefinition(
sprintf('happyr.google.api.%s_youtube', $name),
new Definition(
'HappyR\Google\ApiBundle\Services\YoutubeService',
array(new Reference($clientId))
)
);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$container->setDefinition(
sprintf('happyr.google.api.%s_groups_migration', $name),
new Definition(
'HappyR\Google\ApiBundle\Services\GroupsMigrationService',
array(new Reference($clientId))
)
);
}
}
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,45 @@ happy_r_google_api:
site_name: mysite.com
```

#### Advanced configuration

You can set up multiple accounts, including service accounts.

``` yaml
# app/config/config.yml
happy_r_google_api:
accounts:

# regular web authentication
default:
type: web
application_name: MySite
oauth2_client_id:
oauth2_client_secret:
oauth2_redirect_uri:
developer_key:
site_name: mysite.com

# Credentials from GOOGLE_APPLICATION_CREDENTIALS environment variable (recommended)
service_env:
type: service
getenv: true

# Credentials from service-account.json
service_file:
type: service
json_file: /path/to/your/service-account.json

# with service credentials, example to access Google Analytics
service_account:
type: service
application_name: MySite
oauth2_client_id:
oauth2_client_email:
oauth2_private_key:
oauth2_scopes:
- https://www.googleapis.com/auth/analytics.readonly
```


[1]: https://github.com/google/google-api-php-client
18 changes: 0 additions & 18 deletions Resources/config/services.yml

This file was deleted.

56 changes: 50 additions & 6 deletions Services/GoogleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,57 @@ public function __construct(array $config, LoggerInterface $symfonyLogger = null
}
}

$client -> setApplicationName($config['application_name']);
$client -> setClientId($config['oauth2_client_id']);
$client -> setClientSecret($config['oauth2_client_secret']);
$client -> setRedirectUri($config['oauth2_redirect_uri']);
$client -> setDeveloperKey($config['developer_key']);
$client->setApplicationName($config['application_name']);
$client->setClientId($config['oauth2_client_id']);

$this -> client = $client;
switch ($config['type']) {
case 'web':
$client->setClientSecret($config['oauth2_client_secret']);
$client->setRedirectUri($config['oauth2_redirect_uri']);
$client->setDeveloperKey($config['developer_key']);
break;

case 'service':
$client->setAccessType('offline');
$client->setClientSecret($config['oauth2_client_secret']);
$client->setRedirectUri($config['oauth2_redirect_uri']);

if (isset($config['oauth2_scopes'])) {
$client->setScopes($config['oauth2_scopes']);
}

if (isset($config['getenv']) && true === $config['getenv']) {
$client->useApplicationDefaultCredentials();

} else if (isset($config['json_file'])) {
$client->setAuthConfigFile($config['json_file']);

} else if (isset($config['access_token'])) {
$client->setAccessToken($config['access_token']);

} else if (class_exists('\Google_Auth_AssertionCredentials')) {
//BC for Google API 1.0
$client->setAssertionCredentials(
new \Google_Auth_AssertionCredentials(
$config['oauth2_client_email'],
$config['oauth2_scopes'],
$config['oauth2_private_key']
)
);
} else {
$client->setAuthConfig(
array(
'type' => 'service_account',
'client_id' => $config['oauth2_client_id'],
'client_email' => $config['oauth2_client_email'],
'private_key' => $config['oauth2_private_key'],
)
);
}
break;
}

$this->client = $client;
}

/**
Expand Down