Skip to content

Commit

Permalink
[OAuth2] Provider\Yahoo - speedup (get uid from xoauth_yahoo_guid)
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jan 30, 2018
1 parent 6216aad commit d08c69f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 40 deletions.
45 changes: 5 additions & 40 deletions src/OAuth2/Provider/Yahoo.php
Expand Up @@ -56,57 +56,22 @@ public function parseToken($body)
{
$result = json_decode($body, true);
if ($result) {
return new AccessToken($result);
}

throw new InvalidAccessToken('AccessToken is not a valid JSON');
}

/**
* Retreive userId
* @throws InvalidResponse
*/
private function getCurrentUserId(AccessTokenInterface $accessToken)
{
$response = $this->httpClient->request(
$this->getBaseUri() . 'me/guid',
[
'format' => 'json'
],
Client::GET,
[
'Authorization' => 'Bearer ' . $accessToken->getToken(),
]
);
$token = new AccessToken($result);
$token->setUid($result['xoauth_yahoo_guid']);

if (!$response->isSuccess()) {
throw new InvalidResponse(
'API (get guid) response with error code',
$response
);
return $token;
}

$result = $response->json();

if (!$result) {
throw new InvalidResponse(
'API response is not a valid JSON object',
$response
);
}

return $result->guid->value;
throw new InvalidAccessToken('AccessToken is not a valid JSON');
}

/**
* {@inheritdoc}
*/
public function getIdentity(AccessTokenInterface $accessToken)
{
$uid = $this->getCurrentUserId($accessToken);

$response = $this->httpClient->request(
$this->getBaseUri() . 'user/' . $uid . '/profile',
$this->getBaseUri() . "user/{$accessToken->getUserId()}/profile",
[
'format' => 'json',
],
Expand Down
25 changes: 25 additions & 0 deletions tests/Test/OAuth2/Provider/YahooTest.php
Expand Up @@ -8,6 +8,8 @@

namespace Test\OAuth2\Provider;

use SocialConnect\OAuth2\AccessToken;

class YahooTest extends AbstractProviderTestCase
{
/**
Expand All @@ -17,4 +19,27 @@ protected function getProviderClassName()
{
return \SocialConnect\OAuth2\Provider\Yahoo::class;
}

/**
* @throws \SocialConnect\Provider\Exception\InvalidAccessToken
*/
public function testParseTokenSuccess()
{
$expectedToken = 'XXXXXXXX';
$expectedUserId = 'ETAPBOZJBRRBKZE6LLUMJYD3JA';

$accessToken = $this->getProvider()->parseToken(
json_encode(
[
'access_token' => $expectedToken,
// Yahoo uses xoauth_yahoo_guid instead of user_id
'xoauth_yahoo_guid' => $expectedUserId
]
)
);

parent::assertInstanceOf(AccessToken::class, $accessToken);
parent::assertSame($expectedToken, $accessToken->getToken());
parent::assertSame($expectedUserId, $accessToken->getUserId());
}
}

2 comments on commit d08c69f

@ovr
Copy link
Member Author

@ovr ovr commented on d08c69f Jan 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ping @icex I found a better way to get getCurrentUserId :)

@icex
Copy link
Contributor

@icex icex commented on d08c69f Jan 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh.. very nice :)

Please sign in to comment.