Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upated twitter provider #186

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Auth/CollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
78 changes: 0 additions & 78 deletions src/OAuth1/Provider/Twitter.php

This file was deleted.

37 changes: 37 additions & 0 deletions src/OAuth2/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
85 changes: 85 additions & 0 deletions src/OAuth2/Provider/Twitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry https://github.com/ovr <talk@dmtry.me>
*/
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;
}
}
2 changes: 1 addition & 1 deletion src/OpenID/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Loading