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 |
|
$token = new AccessToken($result); |
60
|
1 |
|
$token->setUid($result['xoauth_yahoo_guid']); |
61
|
|
|
|
62
|
1 |
|
return $token; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
throw new InvalidAccessToken('AccessToken is not a valid JSON'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
2 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
72
|
|
|
{ |
73
|
2 |
|
$response = $this->httpClient->request( |
74
|
2 |
|
$this->getBaseUri() . "user/{$accessToken->getUserId()}/profile", |
75
|
|
|
[ |
76
|
2 |
|
'format' => 'json', |
77
|
2 |
|
], |
78
|
2 |
|
Client::GET, |
79
|
|
|
[ |
80
|
2 |
|
'Authorization' => 'Bearer ' . $accessToken->getToken(), |
81
|
|
|
] |
82
|
2 |
|
); |
83
|
|
|
|
84
|
2 |
|
if (!$response->isSuccess()) { |
85
|
1 |
|
throw new InvalidResponse( |
86
|
1 |
|
'API response with error code', |
87
|
|
|
$response |
88
|
1 |
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
$result = $response->json(); |
92
|
1 |
|
if (!$result) { |
93
|
1 |
|
throw new InvalidResponse( |
94
|
1 |
|
'API response is not a valid JSON object', |
95
|
|
|
$response |
96
|
1 |
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$result = $result->profile; |
100
|
|
|
|
101
|
|
|
if (isset($result->image)) { |
102
|
|
|
$result->image = $result->image->imageUrl; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (isset($result->emails)) { |
106
|
|
|
// first one should do it, should be the default one |
107
|
|
|
$result->email = reset($result->emails); |
108
|
|
|
$result->email = $result->email->handle; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (isset($result->ims)) { |
112
|
|
|
$result->username = reset($result->ims); |
113
|
|
|
$result->username = $result->username->handle; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (isset($result->birthdate)) { |
117
|
|
|
$result->birth_date = date('Y-m-d', strtotime($result->birthdate . '/' . $result->birthYear)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$hydrator = new ObjectMap( |
121
|
|
|
[ |
122
|
|
|
'guid' => 'id', |
123
|
|
|
'image' => 'picture', |
124
|
|
|
'email' => 'email', |
125
|
|
|
'givenName' => 'firstname', |
126
|
|
|
'familyName' => 'lastname', |
127
|
|
|
'username' => 'username', |
128
|
|
|
'gender' => 'gender', |
129
|
|
|
'birth_date' => 'birth_date', |
130
|
|
|
] |
131
|
|
|
); |
132
|
|
|
|
133
|
|
|
return $hydrator->hydrate(new User(), $result); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|