From 71bc25322ee2232b1b81269c63c7a160c8dd9774 Mon Sep 17 00:00:00 2001 From: James Edmonston Date: Sat, 14 Aug 2021 13:09:48 +0100 Subject: [PATCH] PHP 8 support (fix #65) --- composer.json | 8 ++++---- src/services/FacebookService.php | 32 ++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/composer.json b/composer.json index 2a3af31..0b08711 100644 --- a/composer.json +++ b/composer.json @@ -24,11 +24,11 @@ } ], "require": { - "abraham/twitteroauth": "^1.0", + "abraham/twitteroauth": "^3.1", "craftcms/cms": "^3.5.0", - "facebook/graph-sdk": "^5.7", - "google/apiclient": "^2.8", - "lcobucci/jwt": "^3.4" + "google/apiclient": "^2.10", + "lcobucci/jwt": "^4.1", + "league/oauth2-facebook": "^2.0" }, "autoload": { "psr-4": { diff --git a/src/services/FacebookService.php b/src/services/FacebookService.php index eab6493..c2de7eb 100644 --- a/src/services/FacebookService.php +++ b/src/services/FacebookService.php @@ -8,11 +8,11 @@ use craft\events\RegisterGqlQueriesEvent; use craft\services\Gql; use craft\services\UserGroups; -use Facebook\Facebook; use GraphQL\Error\Error; use GraphQL\Type\Definition\Type; use jamesedmonston\graphqlauthentication\gql\Auth; use jamesedmonston\graphqlauthentication\GraphqlAuthentication; +use League\OAuth2\Client\Provider\Facebook; use yii\base\Event; class FacebookService extends Component @@ -59,14 +59,15 @@ public function registerGqlQueries(RegisterGqlQueriesEvent $event) $settings = GraphqlAuthentication::$settings; $client = new Facebook([ - 'app_id' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppId), - 'app_secret' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppSecret), + 'clientId' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppId), + 'clientSecret' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppSecret), + 'redirectUri' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookRedirectUrl), + 'graphApiVersion' => 'v2.10', ]); - $url = $client->getRedirectLoginHelper()->getLoginUrl( - GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookRedirectUrl), - ['email'] - ); + $url = $client->getAuthorizationUrl([ + 'scope' => ['email'], + ]); return $url; }, @@ -170,19 +171,22 @@ protected function _getUserFromToken(string $code): array $errorService = GraphqlAuthentication::$errorService; $client = new Facebook([ - 'app_id' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppId), - 'app_secret' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppSecret), + 'clientId' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppId), + 'clientSecret' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookAppSecret), + 'redirectUri' => GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookRedirectUrl), + 'graphApiVersion' => 'v2.10', ]); - $redirectUrl = GraphqlAuthentication::getInstance()->getSettingsData($settings->facebookRedirectUrl); - $accessToken = $client->getOAuth2Client()->getAccessTokenFromCode($code, $redirectUrl); + $accessToken = $client->getAccessToken('authorization_code', [ + 'code' => $code, + ]); if (!$accessToken) { $errorService->throw($settings->invalidOauthToken, 'INVALID'); } - $user = $client->get('/me?fields=id,name,email', $accessToken->getValue())->getGraphUser(); - $email = $user['email']; + $user = $client->getResourceOwner($accessToken); + $email = $user->getEmail(); if (!$email || !isset($email)) { $errorService->throw($settings->emailNotInScope, 'INVALID'); @@ -196,7 +200,7 @@ protected function _getUserFromToken(string $code): array ); } - $name = explode(' ', $user['name'] ?? '', 1); + $name = explode(' ', $user->getName() ?? '', 1); $firstName = $name[0] ?? ''; $lastName = $name[1] ?? '';