-
Notifications
You must be signed in to change notification settings - Fork 102
/
Yahoo.php
126 lines (105 loc) · 3.21 KB
/
Yahoo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry https://github.com/ovr <[email protected]>
* @author: Bogdan Popa https://github.com/icex <[email protected]>
*/
declare(strict_types=1);
namespace SocialConnect\OAuth2\Provider;
use SocialConnect\Common\ArrayHydrator;
use SocialConnect\Provider\AccessTokenInterface;
use SocialConnect\Provider\Exception\InvalidAccessToken;
use SocialConnect\Common\Entity\User;
use SocialConnect\OAuth2\AccessToken;
class Yahoo extends \SocialConnect\OAuth2\AbstractProvider
{
const NAME = 'yahoo';
/**
* {@inheritdoc}
*/
public function getBaseUri()
{
return 'https://social.yahooapis.com/v1/';
}
/**
* {@inheritdoc}
*/
public function getAuthorizeUri()
{
return 'https://api.login.yahoo.com/oauth2/request_auth';
}
/**
* {@inheritdoc}
*/
public function getRequestTokenUri()
{
return 'https://api.login.yahoo.com/oauth2/get_token';
}
/**
* {@inheritdoc}
*/
public function getName()
{
return self::NAME;
}
/**
* {@inheritdoc}
*/
public function parseToken(string $body)
{
if (empty($body)) {
throw new InvalidAccessToken('Provider response with empty body');
}
$result = json_decode($body, true);
if ($result) {
$token = new AccessToken($result);
$token->setUserId((string) $result['xoauth_yahoo_guid']);
return $token;
}
throw new InvalidAccessToken('AccessToken is not a valid JSON');
}
/**
* {@inheritDoc}
*/
public function prepareRequest(string $method, string $uri, array &$headers, array &$query, AccessTokenInterface $accessToken = null): void
{
$query['format'] = 'json';
if ($accessToken) {
$headers['Authorization'] = "Bearer {$accessToken->getToken()}";
}
}
/**
* {@inheritdoc}
*/
public function getIdentity(AccessTokenInterface $accessToken)
{
$response = $this->request('GET', "user/{$accessToken->getUserId()}/profile", [], $accessToken);
$result = $response['profile'];
if (isset($result['image'])) {
$result->image = $result['image']['imageUrl'];
}
if (isset($result['emails'])) {
// first one should do it, should be the default one
$email = reset($result['emails']);
$result['email'] = $email['handle'];
}
if (isset($result['ims'])) {
$username = reset($result['ims']);
$result->username = $username['handle'];
}
if (isset($result['birthdate'])) {
$result['birthdate'] = date('Y-m-d', strtotime($result['birthdate'] . '/' . $result['birthdate']));
}
$hydrator = new ArrayHydrator([
'guid' => 'id',
'image' => 'picture',
'email' => 'email',
'givenName' => 'firstname',
'familyName' => 'lastname',
'username' => 'username',
'gender' => 'gender',
'birth_date' => 'birth_date',
]);
return $hydrator->hydrate(new User(), $result);
}
}