Merge pull request #2467 from toky-esokia/PHRAS-1752-udpate-facebook-sdk-4.1

udpate facebook SDK to 5.6
This commit is contained in:
Nicolas Maillat
2018-02-20 12:50:41 +01:00
committed by GitHub
5 changed files with 380 additions and 261 deletions

View File

@@ -67,7 +67,6 @@
"doctrine/migrations": "^1.0.0",
"doctrine/orm": "^2.4.0",
"elasticsearch/elasticsearch": "~2.0",
"facebook/php-sdk": "~3.2.3",
"firebase/php-jwt": "^3.0.0",
"gedmo/doctrine-extensions": "~2.3.0",
"goodby/csv": "^1.3.0",
@@ -119,7 +118,8 @@
"zend/gdata": "~1.12.1",
"alchemy/worker-bundle": "^0.1.6",
"alchemy/queue-bundle": "^0.1.5",
"google/recaptcha": "^1.1"
"google/recaptcha": "^1.1",
"facebook/graph-sdk": "^5.6"
},
"require-dev": {
"mikey179/vfsStream": "~1.5",

357
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -135,6 +135,7 @@ authentication:
options:
app-id: ''
secret: ''
default-graph-version: 'v2.10'
twitter:
enabled: false
options:

View File

@@ -21,13 +21,14 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface;
class Facebook extends AbstractProvider
{
/** @var \Facebook */
/** @var \Facebook\Facebook */
private $facebook;
public function __construct(\Facebook $facebook, UrlGenerator $generator)
public function __construct(\Facebook\Facebook $facebook, UrlGenerator $generator, SessionInterface $session)
{
$this->facebook = $facebook;
$this->generator = $generator;
$this->session = $session;
}
/**
@@ -51,14 +52,16 @@ class Facebook extends AbstractProvider
*/
public function authenticate()
{
return new RedirectResponse($this->facebook->getLoginUrl([
'scope' => 'email',
'redirect_uri' => $this->generator->generate(
return new RedirectResponse(
$this->facebook->getRedirectLoginHelper()->getLoginUrl(
$this->generator->generate(
'login_authentication_provider_callback',
['providerId' => $this->getId()],
UrlGenerator::ABSOLUTE_URL
),
['email']
)
]));
);
}
/**
@@ -66,15 +69,15 @@ class Facebook extends AbstractProvider
*/
public function logout()
{
$this->facebook->destroySession();
$this->session->remove('fb_access_token');
}
/**
* @param \Facebook $facebook
* @param \Facebook\Facebook $facebook
*
* @return Facebook
*/
public function setFacebook(\Facebook $facebook)
public function setFacebook(\Facebook\Facebook $facebook)
{
$this->facebook = $facebook;
@@ -82,35 +85,55 @@ class Facebook extends AbstractProvider
}
/**
* @return \Facebook
* @return \Facebook\Facebook
*/
public function getFacebook()
{
return $this->facebook;
}
/**
* @param $dataToRetrieve
* @return \Facebook\GraphNodes\GraphUser
*/
protected function getGraphUser($dataToRetrieve)
{
try {
$response = $this->facebook->get(
"/me?fields=" . implode(',', $dataToRetrieve),
$this->session->get('fb_access_token')
);
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
throw new NotAuthenticatedException('Graph returned an error: ' . $e->getMessage());
exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
throw new NotAuthenticatedException('Facebook SDK returned an error: ' . $e->getMessage());
exit;
}
if (!$response)
{
throw new NotAuthenticatedException('Not authenticated');
}
return $response->getGraphUser();
}
/**
* {@inheritdoc}
*/
public function getIdentity()
{
try {
$data = $this->facebook->api('/me');
$user = $this->getGraphUser(['id', 'name', 'email', 'picture', 'last_name', 'first_name']);
$identity = new Identity();
$identity->set(Identity::PROPERTY_ID, $data['id']);
$identity->set(Identity::PROPERTY_IMAGEURL, sprintf(
'https://graph.facebook.com/%s/picture?return_ssl_resources=1',
$data['username']
));
$identity->set(Identity::PROPERTY_EMAIL, $data['email']);
$identity->set(Identity::PROPERTY_FIRSTNAME, $data['first_name']);
$identity->set(Identity::PROPERTY_LASTNAME, $data['last_name']);
$identity->set(Identity::PROPERTY_USERNAME, $data['username']);
} catch (\FacebookApiException $e) {
throw new NotAuthenticatedException('Unable to get profile informations', $e->getCode(), $e);
}
$identity->set(Identity::PROPERTY_ID, $user['id']);
$identity->set(Identity::PROPERTY_IMAGEURL, $user['picture']);
$identity->set(Identity::PROPERTY_EMAIL, $user['email']);
$identity->set(Identity::PROPERTY_FIRSTNAME, $user['first_name']);
$identity->set(Identity::PROPERTY_LASTNAME, $user['last_name']);
$identity->set(Identity::PROPERTY_USERNAME, $user['first_name']);
return $identity;
}
@@ -120,9 +143,44 @@ class Facebook extends AbstractProvider
*/
public function onCallback(Request $request)
{
if (!$this->facebook->getUser()) {
$helper = $this->facebook->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
throw new NotAuthenticatedException('Graph returned an error: ' . $e->getMessage());
exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
throw new NotAuthenticatedException('Facebook SDK returned an error: ' . $e->getMessage());
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
$error = "Error: " . $helper->getError() . "\n";
$error .= "Error Code: " . $helper->getErrorCode() . "\n";
$error .= "Error Reason: " . $helper->getErrorReason() . "\n";
$error .= "Error Description: " . $helper->getErrorDescription() . "\n";
throw new NotAuthenticatedException($error);
} else {
throw new NotAuthenticatedException('Facebook authentication failed');
}
exit;
}
$oAuth2Client = $this->facebook->getOAuth2Client();
if (! $accessToken->isLongLived()) {
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (\Facebook\Exceptions\FacebookSDKException $e) {
throw new NotAuthenticatedException('Error getting long-lived access token: ' . $e->getMessage() );
exit;
}
}
$this->session->set('fb_access_token', (string) $accessToken);
}
/**
@@ -130,11 +188,9 @@ class Facebook extends AbstractProvider
*/
public function getToken()
{
if (0 >= $this->facebook->getUser()) {
throw new NotAuthenticatedException('Provider has not authenticated');
}
$user = $this->getGraphUser(['id']);
return new Token($this, $this->facebook->getUser());
return new Token($this, $user['id']);
}
/**
@@ -194,11 +250,12 @@ class Facebook extends AbstractProvider
*/
public static function create(UrlGenerator $generator, SessionInterface $session, array $options)
{
$config['appId'] = $options['app-id'];
$config['secret'] = $options['secret'];
$config['app_id'] = $options['app-id'];
$config['app_secret'] = $options['secret'];
$config['default_graph_version'] = $options['default-graph-version'];
$facebook = new \Facebook($config);
$facebook = new \Facebook\Facebook($config);
return new static($facebook, $generator);
return new static($facebook, $generator, $session);
}
}

View File

@@ -5,6 +5,7 @@ namespace Alchemy\Tests\Phrasea\Authentication\Provider;
use Alchemy\Phrasea\Authentication\Provider\Facebook;
use Alchemy\Phrasea\Authentication\Provider\ProviderInterface;
use Alchemy\Phrasea\Authentication\Provider\Token\Identity;
use Facebook\Authentication\AccessToken;
/**
* @group functional
@@ -12,6 +13,8 @@ use Alchemy\Phrasea\Authentication\Provider\Token\Identity;
*/
class FacebookTest extends ProviderTestCase
{
const TOKEN = 'aBcDeFgHiJkLmNoPqRsTuVwXyZ0123456789';
public function testGetSetSession()
{
$this->markTestSkipped('testGetSetSession disabled for facebook');
@@ -35,10 +38,6 @@ class FacebookTest extends ProviderTestCase
public function provideDataForFailingCallback()
{
$provider = $this->getProvider();
$provider->getFacebook()->expects($this->any())
->method('getUser')
->will($this->returnValue(null));
return [
[$provider, $this->getRequestMock()]
];
@@ -52,10 +51,8 @@ class FacebookTest extends ProviderTestCase
public function provideDataForSuccessCallback()
{
$provider = $this->getProvider();
$provider->getFacebook()->expects($this->any())
->method('getUser')
->will($this->returnValue('123456'));
$facebookMock = $this->getFacebookMock(true);
$provider->setFacebook($facebookMock);
return [
[$provider, $this->getRequestMock()]
];
@@ -63,14 +60,14 @@ class FacebookTest extends ProviderTestCase
protected function getProvider()
{
return new Facebook($this->getFacebookMock(), $this->getUrlGeneratorMock());
return new Facebook($this->getFacebookMock(), $this->getUrlGeneratorMock(), $this->getMockSession());
}
protected function authenticateProvider(ProviderInterface $provider)
{
$provider->getFacebook()->expects($this->any())
->method('getUser')
->will($this->returnValue('123456'));
$facebookMock = $this->getFacebookMock(true);
$provider->setFacebook($facebookMock);
$provider->getSession()->set('fb_access_token', self::TOKEN);
}
protected function getProviderForSuccessIdentity()
@@ -78,27 +75,6 @@ class FacebookTest extends ProviderTestCase
$provider = $this->getProvider();
$this->authenticateProvider($provider);
$facebook = $this->getMockBuilder('Facebook')
->disableOriginalConstructor()
->setMethods(['getLoginUrl', 'api', 'getUser'])
->getMock();
$facebook->expects($this->any())
->method('getLoginUrl')
->will($this->returnValue('http://www.facebook.com/'));
$facebook->expects($this->once())
->method('api')
->will($this->returnValue([
'id' => self::ID,
'username' => self::FIRSTNAME,
'first_name' => self::FIRSTNAME,
'last_name' => self::LASTNAME,
'email' => self::EMAIL,
]));
$provider->setFacebook($facebook);
return $provider;
}
@@ -123,6 +99,7 @@ class FacebookTest extends ProviderTestCase
return [
'app-id' => 'zizi',
'secret' => 's3cr3t',
'default-graph-version' => 'v2.10'
];
}
@@ -131,27 +108,100 @@ class FacebookTest extends ProviderTestCase
return $this->getProvider();
}
private function getFacebookMock()
private function getFacebookMock($ValidAccessToken = false)
{
$facebook = $this->getMockBuilder('Facebook')
$facebook = $this->getMockBuilder('Facebook\Facebook')
->disableOriginalConstructor()
->setMethods(['getLoginUrl', 'api', 'getUser'])
->setMethods(['getRedirectLoginHelper', 'get', 'getOAuth2Client'])
->getMock();
$facebook->expects($this->any())
->method('getLoginUrl')
->will($this->returnValue('http://www.facebook.com/'));
$helper = $this->getFacebookRedirectLoginHelperMock($ValidAccessToken);
$facebook->expects($this->any())
->method('api')
->will($this->returnCallback(function () use ($facebook) {
if (!$facebook->getUser()) {
throw new \FacebookApiException([
'error_msg' => 'Not authenticated'
]);
->method('getRedirectLoginHelper')
->will($this->returnValue($helper));
$OAuth2Client = $this->getOAuth2ClientMock();
$facebook->expects($this->any())
->method('getOAuth2Client')
->will($this->returnValue($OAuth2Client));
if ($ValidAccessToken)
{
$FacebookResponse = $this->getFacebookResponseMock();
$facebook->expects($this->any())
->method('get')
->will($this->returnValue($FacebookResponse));
}
}));
return $facebook;
}
private function getAccessTokenMock($valid = true)
{
$expiresAt = (time() + 3600);
return ($valid)? new AccessToken(self::TOKEN, $expiresAt) : null;
}
private function getFacebookRedirectLoginHelperMock($ValidAccessToken = true)
{
$accessToken = $this->getAccessTokenMock($ValidAccessToken);
$helper = $this->getMockBuilder('Facebook\Helpers\FacebookRedirectLoginHelper')
->disableOriginalConstructor()
->setMethods(['getLoginUrl', 'getAccessToken', 'getError'])
->getMock();
$helper->expects($this->any())
->method('getLoginUrl')
->will($this->returnValue('http://www.facebook.com/'));
$helper->expects($this->any())
->method('getAccessToken')
->will($this->returnValue($accessToken));
$helper->expects($this->any())
->method('getError')
->will($this->returnValue(null));
return $helper;
}
private function getOAuth2ClientMock()
{
$OAuth2Client = $this->getMockBuilder('Facebook\Authentication\OAuth2Client')
->disableOriginalConstructor()
->setMethods(['getLongLivedAccessToken'])
->getMock();
$OAuth2Client->expects($this->any())
->method('getLongLivedAccessToken')
->will($this->returnValue(self::TOKEN));
return $OAuth2Client;
}
private function getFacebookResponseMock()
{
$FacebookResponse = $this->getMockBuilder('Facebook\FacebookResponse')
->disableOriginalConstructor()
->setMethods(['getGraphUser'])
->getMock();
$FacebookResponse->expects($this->any())
->method('getGraphUser')
->will($this->returnValue([
'id' => self::ID,
'name' => self::FIRSTNAME,
'first_name' => self::FIRSTNAME,
'last_name' => self::LASTNAME,
'email' => self::EMAIL,
'picture' => self::IMAGEURL
]));
return $FacebookResponse;
}
}