1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* |
5
|
|
|
* @author: Bogdan Popa https://github.com/icex <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SocialConnect\OAuth2\Provider; |
9
|
|
|
|
10
|
|
|
use SocialConnect\Common\Http\Client\Client; |
11
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
12
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
13
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
14
|
|
|
use SocialConnect\Common\Entity\User; |
15
|
|
|
use SocialConnect\Common\Hydrator\ObjectMap; |
16
|
|
|
use SocialConnect\OAuth2\AccessToken; |
17
|
|
|
|
18
|
|
|
class Yahoo extends \SocialConnect\OAuth2\AbstractProvider |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
3 |
|
public function getBaseUri() |
24
|
|
|
{ |
25
|
3 |
|
return 'https://social.yahooapis.com/v1/'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
2 |
|
public function getAuthorizeUri() |
32
|
|
|
{ |
33
|
2 |
|
return 'https://api.login.yahoo.com/oauth2/request_auth'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function getRequestTokenUri() |
40
|
|
|
{ |
41
|
2 |
|
return 'https://api.login.yahoo.com/oauth2/get_token'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
3 |
|
public function getName() |
48
|
|
|
{ |
49
|
3 |
|
return 'yahoo'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
3 |
|
public function parseToken($body) |
56
|
|
|
{ |
57
|
3 |
|
$result = json_decode($body, true); |
58
|
3 |
|
if ($result) { |
59
|
1 |
|
return new AccessToken($result); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
throw new InvalidAccessToken('AccessToken is not a valid JSON'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retreive userId |
67
|
|
|
*/ |
68
|
2 |
|
private function getCurrentUserId(AccessTokenInterface $accessToken) |
69
|
|
|
{ |
70
|
2 |
|
$parameters = []; |
71
|
2 |
|
$parameters['format'] = 'json'; |
72
|
2 |
|
$response = $this->httpClient->request( |
73
|
2 |
|
$this->getBaseUri() . 'me/guid', |
74
|
2 |
|
$parameters, |
75
|
2 |
|
Client::GET, |
76
|
|
|
[ |
77
|
2 |
|
'Authorization' => 'Bearer ' . $accessToken->getToken(), |
78
|
|
|
] |
79
|
2 |
|
); |
80
|
2 |
|
if (!$response->isSuccess()) { |
81
|
1 |
|
throw new InvalidResponse( |
82
|
1 |
|
'API (get guid) response with error code', |
83
|
|
|
$response |
84
|
1 |
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
$result = $response->json(); |
88
|
|
|
|
89
|
1 |
|
if (!$result) { |
90
|
1 |
|
throw new InvalidResponse( |
91
|
1 |
|
'API response is not a valid JSON object', |
92
|
|
|
$response |
93
|
1 |
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $result->guid->value; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
2 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
103
|
|
|
{ |
104
|
2 |
|
$uid = $this->getCurrentUserId($accessToken); |
105
|
|
|
|
106
|
|
|
$response = $this->httpClient->request( |
107
|
|
|
$this->getBaseUri() . 'user/' . $uid . '/profile', |
108
|
|
|
[ |
109
|
|
|
'format' => 'json', |
110
|
|
|
], |
111
|
|
|
Client::GET, |
112
|
|
|
[ |
113
|
|
|
'Authorization' => 'Bearer ' . $accessToken->getToken(), |
114
|
|
|
] |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
if (!$response->isSuccess()) { |
118
|
|
|
throw new InvalidResponse( |
119
|
|
|
'API response with error code', |
120
|
|
|
$response |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$result = $response->json(); |
125
|
|
|
if (!$result) { |
126
|
|
|
throw new InvalidResponse( |
127
|
|
|
'API response is not a valid JSON object', |
128
|
|
|
$response |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$result = $result->profile; |
133
|
|
|
|
134
|
|
|
if (isset($result->image)) { |
135
|
|
|
$result->image = $result->image->imageUrl; |
136
|
|
|
} |
137
|
|
|
if ($result->emails) { |
138
|
|
|
// first one should do it, should be the default one |
139
|
|
|
$result->email = reset($result->emails); |
140
|
|
|
$result->email = $result->email->handle; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($result->ims) { |
144
|
|
|
$result->username = reset($result->ims); |
145
|
|
|
$result->username = $result->username->handle; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($result->birthdate) { |
149
|
|
|
$result->birth_date = date('Y-m-d', strtotime($result->birthdate . '/' . $result->birthYear)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$hydrator = new ObjectMap( |
153
|
|
|
[ |
154
|
|
|
'guid' => 'id', |
155
|
|
|
'image' => 'picture', |
156
|
|
|
'email' => 'email', |
157
|
|
|
'givenName' => 'firstname', |
158
|
|
|
'familyName' => 'lastname', |
159
|
|
|
'username' => 'username', |
160
|
|
|
'gender' => 'gender', |
161
|
|
|
'birth_date' => 'birth_date', |
162
|
|
|
] |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
return $hydrator->hydrate(new User(), $result); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|