Skip to content
Browse files

Facebook OAuth provider

Had to give oauth provider a way to compute the redirect_uri themselves.
  • Loading branch information...
1 parent 5997b95 commit b5e6bdab62de59c26f34e861804d2176eecaad1f @ubermuda ubermuda committed Jan 13, 2012
View
1 DependencyInjection/Security/Factory/OAuthFactory.php
@@ -45,6 +45,7 @@ protected function createOAuthProvider(ContainerBuilder $container, $id, $config
$container
->setDefinition($oauthProviderId, new DefinitionDecorator($baseOAuthProviderId))
->addArgument(new Reference('buzz.client'))
+ ->addArgument(new Reference('security.http_utils'))
->addArgument($config);
return $oauthProviderId;
View
2 Resources/config/oauth.xml
@@ -12,6 +12,7 @@
<parameter key="knp_oauth.user.provider.entity.class">Knp\Bundle\OAuthBundle\Security\Core\UserProvider\EntityUserProvider</parameter>
<parameter key="knp_oauth.security.oauth.oauth_provider.class">Knp\Bundle\OAuthBundle\Security\Http\OAuth\OAuthProvider</parameter>
<parameter key="knp_oauth.security.oauth.github_provider.class">Knp\Bundle\OAuthBundle\Security\Http\OAuth\GithubProvider</parameter>
+ <parameter key="knp_oauth.security.oauth.facebook_provider.class">Knp\Bundle\OAuthBundle\Security\Http\OAuth\FacebookProvider</parameter>
</parameters>
<services>
@@ -34,5 +35,6 @@
<service id="knp_oauth.security.oauth.oauth_provider" class="%knp_oauth.security.oauth.oauth_provider.class%" />
<service id="knp_oauth.security.oauth.github_provider" class="%knp_oauth.security.oauth.github_provider.class%" />
+ <service id="knp_oauth.security.oauth.facebook_provider" class="%knp_oauth.security.oauth.facebook_provider.class%" />
</services>
</container>
View
6 Security/Http/EntryPoint/OAuthEntryPoint.php
@@ -63,11 +63,7 @@ public function start(Request $request, AuthenticationException $authException =
$request->getSession()->remove('_security.target_path');
}
- $loginCheckUrl = $this->httpUtils
- ->createRequest($request, $this->checkPath)
- ->getUri();
-
- $authorizationUrl = $this->oauthProvider->getAuthorizationUrl($loginCheckUrl);
+ $authorizationUrl = $this->oauthProvider->getAuthorizationUrl($request);
return $this->httpUtils->createRedirectResponse($request, $authorizationUrl);
}
View
2 Security/Http/Firewall/OAuthListener.php
@@ -43,7 +43,7 @@ public function setOAuthProvider(OAuthProviderInterface $oauthProvider)
*/
protected function attemptAuthentication(Request $request)
{
- $accessToken = $this->oauthProvider->getAccessToken($request->get('code'));
+ $accessToken = $this->oauthProvider->getAccessToken($request);
$token = new OAuthToken($accessToken);
View
41 Security/Http/OAuth/FacebookProvider.php
@@ -0,0 +1,41 @@
+<?php
+
+/*
+ * This file is part of the KnpOAuthBundle package.
+ *
+ * (c) KnpLabs <hello@knplabs.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Knp\Bundle\OAuthBundle\Security\Http\OAuth;
+
+use Knp\Bundle\OAuthBundle\Security\Http\OAuth\OAuthProvider;
+
+/**
+ * FacebookProvider
+ *
+ * @author Geoffrey Bachelet <geoffrey.bachelet@gmail.com>
+ */
+class FacebookProvider extends OAuthProvider
+{
+ /**
+ * {@inheritDoc}
+ */
+ protected $options = array(
+ 'authorization_url' => 'https://www.facebook.com/dialog/oauth',
+ 'access_token_url' => 'https://graph.facebook.com/oauth/access_token',
+ 'infos_url' => 'https://graph.facebook.com/me',
+ 'username_path' => 'name',
+ );
+
+ /**
+ * Github unfortunately breaks the spec by using commas instead of spaces
+ * to separate scopes
+ */
+ public function configure()
+ {
+ $this->options['scope'] = str_replace(',', ' ', $this->options['scope']);
+ }
+}
View
28 Security/Http/OAuth/OAuthProvider.php
@@ -15,7 +15,9 @@
Buzz\Message\Request as HttpRequest,
Buzz\Message\Response as HttpResponse;
-use Symfony\Component\Security\Core\Exception\AuthenticationException;
+use Symfony\Component\Security\Core\Exception\AuthenticationException,
+ Symfony\Component\Security\Http\HttpUtils,
+ Symfony\Component\HttpFoundation\Request;
use Knp\Bundle\OAuthBundle\Security\Http\OAuth\OAuthProviderInterface;
@@ -40,7 +42,7 @@ class OAuthProvider implements OAuthProviderInterface
* @param Buzz\Client\ClientInterface
* @param array $options
*/
- public function __construct(HttpClientInterface $httpClient, array $options)
+ public function __construct(HttpClientInterface $httpClient, HttpUtils $httpUtils, array $options)
{
if (null !== $options['infos_url'] && null === $options['username_path']) {
throw new \InvalidArgumentException('You must set an "username_path" to use an "infos_url"');
@@ -62,6 +64,7 @@ public function __construct(HttpClientInterface $httpClient, array $options)
$this->options = array_merge($this->options, $options);
$this->httpClient = $httpClient;
+ $this->httpUtils = $httpUtils;
$this->configure();
}
@@ -75,6 +78,15 @@ public function configure()
}
/**
+ * @param Symfony\Component\HttpFoundation\Request $request
+ * @return string
+ */
+ public function getRedirectUri(Request $request)
+ {
+ return $this->httpUtils->createRequest($request, $this->getOption('check_path'))->getUri();
+ }
+
+ /**
* Retrieve an option by name
*
* @throws InvalidArgumentException When the option does not exist
@@ -127,7 +139,7 @@ public function getUsername($accessToken)
foreach ($usernamePath as $path) {
if (!array_key_exists($path, $username)) {
- throw new AuthenticationException(sprintf('Could not follow username path "%s" in OAuth provider response: %s', $this->getOption('username_path')), var_export($userInfos, true));
+ throw new AuthenticationException(sprintf('Could not follow username path "%s" in OAuth provider response: %s', $this->getOption('username_path'), var_export($userInfos, true)));
}
$username = $username[$path];
}
@@ -138,13 +150,13 @@ public function getUsername($accessToken)
/**
* {@inheritDoc}
*/
- public function getAuthorizationUrl($loginCheckUrl, array $extraParameters = array())
+ public function getAuthorizationUrl(Request $request, array $extraParameters = array())
{
$parameters = array_merge($extraParameters, array(
'response_type' => 'code',
'client_id' => $this->getOption('client_id'),
'scope' => $this->getOption('scope'),
- 'redirect_uri' => $loginCheckUrl,
+ 'redirect_uri' => $this->getRedirectUri($request),
));
return $this->getOption('authorization_url').'?'.http_build_query($parameters);
@@ -153,16 +165,18 @@ public function getAuthorizationUrl($loginCheckUrl, array $extraParameters = arr
/**
* {@inheritDoc}
*/
- public function getAccessToken($code, array $extraParameters = array())
+ public function getAccessToken(Request $request, array $extraParameters = array())
{
$parameters = array_merge($extraParameters, array(
- 'code' => $code,
+ 'code' => $request->get('code'),
'grant_type' => 'authorization_code',
'client_id' => $this->getOption('client_id'),
'client_secret' => $this->getOption('secret'),
+ 'redirect_uri' => $this->getRedirectUri($request),
));
$url = $this->getOption('access_token_url').'?'.http_build_query($parameters);
+
$response = array();
parse_str($this->httpRequest($url), $response);
View
6 Security/Http/OAuth/OAuthProviderInterface.php
@@ -11,6 +11,8 @@
namespace Knp\Bundle\OAuthBundle\Security\Http\OAuth;
+use Symfony\Component\HttpFoundation\Request;
+
/**
* OAuthProviderInterface
*
@@ -33,7 +35,7 @@ function getUsername($accessToken);
* @param array $extraParameters An array of parameters to add to the url
* @return string The authorization url
*/
- function getAuthorizationUrl($loginCheckUrl, array $extraParameters = array());
+ function getAuthorizationUrl(Request $request, array $extraParameters = array());
/**
* Retrieve an access token for a given code
@@ -42,5 +44,5 @@ function getAuthorizationUrl($loginCheckUrl, array $extraParameters = array());
* @param array $extraParameters An array of parameters to add to the url
* @return string The access token
*/
- function getAccessToken($code, array $extraParameters = array());
+ function getAccessToken(Request $request, array $extraParameters = array());
}

0 comments on commit b5e6bda

Please sign in to comment.
Something went wrong with that request. Please try again.