Permalink
Browse files

It's now possible to easily add mutiple GrantExtension

  • Loading branch information...
adrienbrault committed Sep 19, 2012
1 parent 10f3757 commit 47061c6bb483ca2c87c05d5aefc501c1f355b4ad
@@ -0,0 +1,33 @@
+<?php
+
+namespace FOS\OAuthServerBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
+
+/**
+ * @author Adrien Brault <adrien.brault@gmail.com>
+ */
+class GrantExtensionsCompilerPass implements CompilerPassInterface
+{
+ public function process(ContainerBuilder $container)
+ {
+ $storageDefinition = $container->findDefinition('fos_oauth_server.storage');
+ $storageClass = new \ReflectionClass($storageDefinition->getClass());
+ if (!$storageClass->implementsInterface('FOS\OAuthServerBundle\Storage\GrantExtensionDispatcherInterface')) {
+ return;
+ }
+
+ foreach ($container->findTaggedServiceIds('fos_oauth_server.grant_extension') as $id => $tags) {
+ foreach ($tags as $tag) {
+ if (empty($tag['uri'])) {
+ throw new InvalidArgumentException(sprintf('Service "%s" must define the "uri" attribute on "fos_oauth_server.grant_extension" tags.', $id));
+ }
+
+ $storageDefinition->addMethodCall('setGrantExtension', array($tag['uri'], new Reference($id)));
+ }
+ }
+ }
+}
View
@@ -13,6 +13,7 @@
use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
use FOS\OAuthServerBundle\DependencyInjection\Security\Factory\OAuthFactory;
+use FOS\OAuthServerBundle\DependencyInjection\Compiler\GrantExtensionsCompilerPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
@@ -32,5 +33,7 @@ public function build(ContainerBuilder $container)
$extension = $container->getExtension('security');
$extension->addSecurityListenerFactory(new OAuthFactory());
}
+
+ $container->addCompilerPass(new GrantExtensionsCompilerPass());
}
}
@@ -0,0 +1,13 @@
+<?php
+
+namespace FOS\OAuthServerBundle\Storage;
+
+use OAuth2\IOAuth2GrantExtension;
+
+/**
+ * @author Adrien Brault <adrien.brault@gmail.com>
+ */
+interface GrantExtensionDispatcherInterface
+{
+ public function setGrantExtension($uri, GrantExtensionInterface $grantExtension);
+}
@@ -0,0 +1,16 @@
+<?php
+
+namespace FOS\OAuthServerBundle\Storage;
+
+use OAuth2\Model\IOAuth2Client;
+
+/**
+ * @author Adrien Brault <adrien.brault@gmail.com>
+ */
+interface GrantExtensionInterface
+{
+ /**
+ * @see OAuth2\IOAuth2GrantExtension::checkGrantExtension
+ */
+ public function checkGrantExtension(IOAuth2Client $client, array $inputData, array $authHeaders);
+}
View
@@ -19,14 +19,18 @@
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
+use OAuth2\OAuth2;
+use OAuth2\OAuth2ServerException;
use OAuth2\IOAuth2RefreshTokens;
use OAuth2\IOAuth2GrantUser;
use OAuth2\IOAuth2GrantCode;
use OAuth2\IOAuth2GrantImplicit;
use OAuth2\IOAuth2GrantClient;
+use OAuth2\IOAuth2GrantExtension;
use OAuth2\Model\IOAuth2Client;
-class OAuthStorage implements IOAuth2RefreshTokens, IOAuth2GrantUser, IOAuth2GrantCode, IOAuth2GrantImplicit, IOAuth2GrantClient
+class OAuthStorage implements IOAuth2RefreshTokens, IOAuth2GrantUser, IOAuth2GrantCode, IOAuth2GrantImplicit,
+ IOAuth2GrantClient, IOAuth2GrantExtension, GrantExtensionDispatcherInterface
{
/**
* @var \FOS\OAuthServerBundle\Model\ClientManagerInterface
@@ -58,6 +62,11 @@ class OAuthStorage implements IOAuth2RefreshTokens, IOAuth2GrantUser, IOAuth2Gra
*/
protected $encoderFactory;
+ /**
+ * @var array [uri] => GrantExtensionInterface
+ */
+ protected $grantExtensions;
+
/**
* @param \FOS\OAuthServerBundle\Model\ClientManagerInterface $clientManager
* @param \FOS\OAuthServerBundle\Model\AccessTokenManagerInterface $accessTokenManager
@@ -76,6 +85,16 @@ public function __construct(ClientManagerInterface $clientManager, AccessTokenMa
$this->authCodeManager = $authCodeManager;
$this->userProvider = $userProvider;
$this->encoderFactory = $encoderFactory;
+
+ $this->grantExtensions = array();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setGrantExtension($uri, GrantExtensionInterface $grantExtension)
+ {
+ $this->grantExtensions[$uri] = $grantExtension;
}
public function getClient($clientId)
@@ -229,4 +248,18 @@ public function unsetRefreshToken($tokenString)
$this->refreshTokenManager->deleteToken($token);
}
}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function checkGrantExtension(IOAuth2Client $client, $uri, array $inputData, array $authHeaders)
+ {
+ if (!isset($this->grantExtensions[$uri])) {
+ throw new OAuth2ServerException(OAuth2::HTTP_BAD_REQUEST, OAuth2::ERROR_UNSUPPORTED_GRANT_TYPE);
+ }
+
+ $grantExtension = $this->grantExtensions[$uri];
+
+ return $grantExtension->checkGrantExtension($client, $inputData, $authHeaders);
+ }
}
@@ -423,6 +423,44 @@ public function testGetAuthCodeReturnsNullIfNotExists()
$this->assertNull($this->storage->getAuthCode('123_abc'));
}
+
+ public function testValidGrantExtension()
+ {
+ $grantExtension = $this->getMock('FOS\OAuthServerBundle\Storage\GrantExtensionInterface');
+ $grantExtension
+ ->expects($this->once())
+ ->method('checkGrantExtension')
+ ->will($this->returnValue(true))
+ ;
+ $this->storage->setGrantExtension('https://friendsofsymfony.com/grants/foo', $grantExtension);
+
+ $client = $this->getMock('OAuth2\Model\IOAuth2Client');
+ $this->assertTrue($this->storage->checkGrantExtension($client, 'https://friendsofsymfony.com/grants/foo', array(), array()));
+ }
+
+ /**
+ * @expectedException \OAuth2\OAuth2ServerException
+ */
+ public function testInvalidGrantExtension()
+ {
+ $client = $this->getMock('OAuth2\Model\IOAuth2Client');
+ $this->storage->checkGrantExtension($client, 'https://friendsofsymfony.com/grants/bar', array(), array());
+ }
+
+ public function testDoubleSetGrantExtension()
+ {
+ $grantExtension = $this->getMock('FOS\OAuthServerBundle\Storage\GrantExtensionInterface');
+ $grantExtension2 = $this->getMock('FOS\OAuthServerBundle\Storage\GrantExtensionInterface');
+ $this->storage->setGrantExtension($uri = 'https://friendsofsymfony.com/grants/foo', $grantExtension);
+ $this->storage->setGrantExtension($uri, $grantExtension2);
+
+ $storageClass = new \ReflectionClass(get_class($this->storage));
+ $grantExtensionsProperty = $storageClass->getProperty('grantExtensions');
+ $grantExtensionsProperty->setAccessible(true);
+ $grantExtensions = $grantExtensionsProperty->getValue($this->storage);
+
+ $this->assertEquals($grantExtension2, $grantExtensions[$uri]);
+ }
}
class User implements UserInterface

0 comments on commit 47061c6

Please sign in to comment.