From cf576821590fc1c4d6e14e31b1bfaba8161a39e7 Mon Sep 17 00:00:00 2001 From: Manomite Date: Fri, 10 Feb 2023 13:29:56 +0100 Subject: [PATCH 1/2] 'updated' --- src/OAuth2/AbstractProvider.php | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/OAuth2/AbstractProvider.php b/src/OAuth2/AbstractProvider.php index 50105b88c..b1e05a445 100644 --- a/src/OAuth2/AbstractProvider.php +++ b/src/OAuth2/AbstractProvider.php @@ -116,6 +116,25 @@ protected function makeAccessTokenRequest(string $code): RequestInterface ; } + /** + * @param string $refreshToken + * @return RequestInterface + */ + protected function makeRefreshAccessTokenRequest(string $refreshToken): RequestInterface + { + $parameters = [ + 'refresh_token' => $refreshToken, + 'client_id' => $this->consumer->getKey(), + 'client_secret' => $this->consumer->getSecret(), + 'grant_type' => 'refresh_token', + ]; + + return $this->httpStack->createRequest($this->requestHttpMethod, $this->getRequestTokenUri()) + ->withHeader('Content-Type', 'application/x-www-form-urlencoded') + ->withBody($this->httpStack->createStream(http_build_query($parameters, '', '&'))) + ; + } + /** * @param string $code * @return AccessToken @@ -132,6 +151,24 @@ public function getAccessToken(string $code): AccessToken return $this->parseToken($response->getBody()->getContents()); } + /** + * @param string $refreshToken + * + * @return AccessToken + * @throws InvalidAccessToken + * @throws InvalidResponse + * @throws \Psr\Http\Client\ClientExceptionInterface + */ + public function refreshAccessToken(string $refreshToken): AccessToken + { + $response = $this->executeRequest( + $this->makeRefreshAccessTokenRequest($refreshToken) + ); + + return $this->parseToken($response->getBody()->getContents()); + } + + /** * @param array $parameters * @return AccessToken From e54a45924f8910dc2c3588f52904dd8bc8cc8734 Mon Sep 17 00:00:00 2001 From: Adeyeye George Date: Fri, 15 Nov 2024 11:56:43 +0100 Subject: [PATCH 2/2] Updated twitter to oauth2 and changed the url --- src/Auth/CollectionFactory.php | 2 +- src/OAuth1/Provider/Twitter.php | 78 ------------------------------ src/OAuth2/Provider/Twitter.php | 85 +++++++++++++++++++++++++++++++++ src/OpenID/AbstractProvider.php | 2 +- 4 files changed, 87 insertions(+), 80 deletions(-) delete mode 100644 src/OAuth1/Provider/Twitter.php create mode 100644 src/OAuth2/Provider/Twitter.php diff --git a/src/Auth/CollectionFactory.php b/src/Auth/CollectionFactory.php index 94aee9a24..21601607a 100644 --- a/src/Auth/CollectionFactory.php +++ b/src/Auth/CollectionFactory.php @@ -20,7 +20,7 @@ class CollectionFactory implements FactoryInterface */ protected $providers = [ // OAuth1 - OAuth1\Provider\Twitter::NAME => OAuth1\Provider\Twitter::class, + OAuth2\Provider\Twitter::NAME => OAuth2\Provider\Twitter::class, OAuth1\Provider\Px500::NAME => OAuth1\Provider\Px500::class, OAuth1\Provider\Tumblr::NAME => OAuth1\Provider\Tumblr::class, OAuth1\Provider\Atlassian::NAME => OAuth1\Provider\Atlassian::class, diff --git a/src/OAuth1/Provider/Twitter.php b/src/OAuth1/Provider/Twitter.php deleted file mode 100644 index 00263e370..000000000 --- a/src/OAuth1/Provider/Twitter.php +++ /dev/null @@ -1,78 +0,0 @@ - - */ -declare(strict_types=1); - -namespace SocialConnect\OAuth1\Provider; - -use SocialConnect\Common\ArrayHydrator; -use SocialConnect\Provider\AccessTokenInterface; -use SocialConnect\Common\Entity\User; - -class Twitter extends \SocialConnect\OAuth1\AbstractProvider -{ - const NAME = 'twitter'; - - public function getBaseUri() - { - return 'https://api.twitter.com/1.1/'; - } - - public function getAuthorizeUri() - { - return 'https://api.twitter.com/oauth/authenticate'; - } - - public function getRequestTokenUri() - { - return 'https://api.twitter.com/oauth/request_token'; - } - - public function getRequestTokenAccessUri() - { - return 'https://api.twitter.com/oauth/access_token'; - } - - public function getName() - { - return self::NAME; - } - - /** - * {@inheritdoc} - */ - public function getIdentity(AccessTokenInterface $accessToken) - { - $this->consumerToken = $accessToken; - - $result = $this->request( - 'GET', - 'account/verify_credentials.json', - [ - // String is expected because Twitter is awful - 'include_email' => 'true' - ], - $accessToken - ); - - $hydrator = new ArrayHydrator([ - 'id' => 'id', - 'name' => 'fullname', - 'email' => 'email', - 'screen_name' => 'username', - 'profile_image_url_https' => 'pictureURL' - ]); - - /** @var User $user */ - $user = $hydrator->hydrate(new User(), $result); - - // When set to true email will be returned in the user objects as a string. - // If the user does not have an email address on their account, - // or if the email address is not verified, null will be returned. - $user->emailVerified = true; - - return $user; - } -} diff --git a/src/OAuth2/Provider/Twitter.php b/src/OAuth2/Provider/Twitter.php new file mode 100644 index 000000000..c63d6eaac --- /dev/null +++ b/src/OAuth2/Provider/Twitter.php @@ -0,0 +1,85 @@ + + */ +declare(strict_types=1); + +namespace SocialConnect\OAuth2\Provider; + +use SocialConnect\Common\ArrayHydrator; +use SocialConnect\Provider\AccessTokenInterface; +use SocialConnect\Common\Entity\User; + +class Twitter extends \SocialConnect\OAuth2\AbstractProvider +{ + const NAME = 'twitter'; + + public function getBaseUri() + { + return 'https://api.x.com/2/'; + } + + public function getAuthorizeUri() + { + return 'https://api.x.com/2/oauth2'; + } + + public function getRequestTokenUri() + { + return 'https://api.x.com/2/oauth2/token'; + } + + public function getName() + { + return self::NAME; + } + + /** + * {@inheritdoc} + */ + public function getIdentity(AccessTokenInterface $accessToken) + { + $query = []; + + $fields = $this->getArrayOption('identity.fields', []); + if ($fields) { + $query['fields'] = implode(',', $fields); + } + + $response = $this->request( + 'GET', + 'me', + $query, + $accessToken + ); + + $hydrator = new ArrayHydrator([ + 'id' => 'id', + 'first_name' => 'firstname', + 'last_name' => 'lastname', + 'email' => 'email', + 'gender' => static function ($value, User $user) { + $user->setSex($value === 1 ? User::SEX_FEMALE : User::SEX_MALE); + }, + 'birthday' => static function ($value, User $user) { + $user->setBirthday( + new \DateTime($value) + ); + }, + 'link' => 'url', + 'locale' => 'locale', + 'name' => 'fullname', + 'timezone' => 'timezone', + 'updated_time' => 'dateModified', + 'verified' => 'verified', + 'picture.data.url' => 'pictureURL' + ]); + + /** @var User $user */ + $user = $hydrator->hydrate(new User(), $response); + $user->emailVerified = true; + + return $user; + } +} diff --git a/src/OpenID/AbstractProvider.php b/src/OpenID/AbstractProvider.php index 0eef90326..25d919d87 100644 --- a/src/OpenID/AbstractProvider.php +++ b/src/OpenID/AbstractProvider.php @@ -100,7 +100,7 @@ protected function getRequiredRequestParameter(array $requestParameters, string return $requestParameters[$key]; } - throw new Unauthorized("There is no required parameter called: '${key}'"); + throw new Unauthorized("There is no required parameter called: ".$key); } /**