|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Tharanga Kothalawala <[email protected]> |
|
4
|
|
|
* @date 22-01-2019 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace TSK\SSO\ThirdParty\Yahoo; |
|
8
|
|
|
|
|
9
|
|
|
use TSK\SSO\Http\CurlRequest; |
|
10
|
|
|
use TSK\SSO\ThirdParty; |
|
11
|
|
|
use TSK\SSO\ThirdParty\CommonAccessToken; |
|
12
|
|
|
use TSK\SSO\ThirdParty\Exception\NoThirdPartyEmailFoundException; |
|
13
|
|
|
use TSK\SSO\ThirdParty\Exception\ThirdPartyConnectionFailedException; |
|
14
|
|
|
use TSK\SSO\ThirdParty\ThirdPartyUser; |
|
15
|
|
|
use TSK\SSO\ThirdParty\VendorConnection; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @codeCoverageIgnore |
|
19
|
|
|
* @package TSK\SSO\ThirdParty\Yahoo |
|
20
|
|
|
* @see https://developer.yahoo.com/oauth2/guide/flows_authcode |
|
21
|
|
|
*/ |
|
22
|
|
|
class YahooConnection implements VendorConnection |
|
23
|
|
|
{ |
|
24
|
|
|
const API_BASE = 'https://api.login.yahoo.com/oauth2'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var YahooApiConfiguration |
|
28
|
|
|
*/ |
|
29
|
|
|
private $apiConfiguration; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var CurlRequest |
|
33
|
|
|
*/ |
|
34
|
|
|
private $curlClient; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* YahooConnection constructor. |
|
38
|
|
|
* @param YahooApiConfiguration $apiConfiguration |
|
39
|
|
|
* @param CurlRequest $curlClient |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(YahooApiConfiguration $apiConfiguration, CurlRequest $curlClient) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->apiConfiguration = $apiConfiguration; |
|
44
|
|
|
$this->curlClient = $curlClient; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Use this to get a link to redirect a user to the third party login |
|
49
|
|
|
* |
|
50
|
|
|
* @return string|null |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getGrantUrl() |
|
53
|
|
|
{ |
|
54
|
|
|
return sprintf( |
|
55
|
|
|
'%s/request_auth?client_id=%s&redirect_uri=%s&response_type=code&language=en-us&state=%s', |
|
56
|
|
|
self::API_BASE, |
|
57
|
|
|
$this->apiConfiguration->clientId(), |
|
58
|
|
|
urlencode($this->apiConfiguration->redirectUrl()), |
|
59
|
|
|
$this->apiConfiguration->ourSecretState() |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Grants a new access token |
|
65
|
|
|
* |
|
66
|
|
|
* @return CommonAccessToken |
|
67
|
|
|
* @throws ThirdPartyConnectionFailedException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function grantNewAccessToken() |
|
70
|
|
|
{ |
|
71
|
|
|
if (empty($_GET['code']) |
|
72
|
|
|
|| empty($_GET['state']) |
|
73
|
|
|
|| $_GET['state'] !== $this->apiConfiguration->ourSecretState() |
|
74
|
|
|
) { |
|
75
|
|
|
throw new ThirdPartyConnectionFailedException('Invalid request!'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$accessTokenJsonInfo = $this->curlClient->postRaw( |
|
79
|
|
|
sprintf("%s/get_token", self::API_BASE), |
|
80
|
|
|
sprintf( |
|
81
|
|
|
'client_id=%s&client_secret=%s&grant_type=authorization_code&redirect_uri=%s&code=%s', |
|
82
|
|
|
$this->apiConfiguration->clientId(), |
|
83
|
|
|
$this->apiConfiguration->clientSecret(), |
|
84
|
|
|
urlencode($this->apiConfiguration->redirectUrl()), |
|
85
|
|
|
$_GET['code'] |
|
86
|
|
|
), |
|
87
|
|
|
array( |
|
88
|
|
|
'Authorization' => sprintf( |
|
89
|
|
|
'Basic %s', |
|
90
|
|
|
base64_encode( |
|
91
|
|
|
sprintf('%s:%s', $this->apiConfiguration->clientId(), $this->apiConfiguration->clientSecret()) |
|
92
|
|
|
) |
|
93
|
|
|
), |
|
94
|
|
|
'Content-Type' => 'application/x-www-form-urlencoded', |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
$accessTokenJson = json_decode($accessTokenJsonInfo, true); |
|
99
|
|
|
if (empty($accessTokenJson['access_token'])) { |
|
100
|
|
|
throw new ThirdPartyConnectionFailedException('Failed to establish a new third party vendor connection.'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return new CommonAccessToken( |
|
104
|
|
|
$accessTokenJson['access_token'], |
|
105
|
|
|
ThirdParty::YAHOO |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Use this to retrieve the current user's third party user data using there existing granted access token |
|
111
|
|
|
* |
|
112
|
|
|
* @param CommonAccessToken $accessToken |
|
113
|
|
|
* @return ThirdPartyUser |
|
114
|
|
|
* @throws NoThirdPartyEmailFoundException |
|
115
|
|
|
* @throws ThirdPartyConnectionFailedException |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getSelf(CommonAccessToken $accessToken) |
|
118
|
|
|
{ |
|
119
|
|
|
$userJsonInfo = $this->curlClient->yahooGet( |
|
120
|
|
|
'https://social.yahooapis.com/v1/user/me/profile?format=json', |
|
121
|
|
|
array( |
|
122
|
|
|
sprintf('Authorization: Bearer %s', $accessToken->token()), |
|
123
|
|
|
'Accept: application/json', |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$userInfo = json_decode($userJsonInfo, true); |
|
128
|
|
|
if (empty($userInfo['profile']['emails'][0]['handle'])) { |
|
129
|
|
|
throw new NoThirdPartyEmailFoundException('An email address cannot be found from vendor'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return new ThirdPartyUser( |
|
133
|
|
|
$userInfo['profile']['guid'], |
|
134
|
|
|
sprintf('%s %s', $userInfo['profile']['givenName'], $userInfo['profile']['familyName']), |
|
135
|
|
|
$userInfo['profile']['emails'][0]['handle'], |
|
136
|
|
|
!empty($userInfo['profile']['image']['imageUrl']) ? $userInfo['profile']['image']['imageUrl'] : '', |
|
137
|
|
|
!empty($userInfo['profile']['gender']) ? $userInfo['profile']['gender'] : '' |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Use this to revoke the access to the third party data. |
|
143
|
|
|
* This will completely remove the access from the vendor side. |
|
144
|
|
|
* |
|
145
|
|
|
* @param CommonAccessToken $accessToken |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
|
|
public function revokeAccess(CommonAccessToken $accessToken) |
|
149
|
|
|
{ |
|
150
|
|
|
// noop : cannot find documentation on how to revoke the app's access. |
|
151
|
|
|
return true; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|