/
Client.php
258 lines (223 loc) · 8.26 KB
/
Client.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* @project SocialLogin
* @package LitExtension_SocialLogin
* @author LitExtension
* @email litextension@gmail.com
*/
class LitExtension_SocialLogin_Model_Yahoo_Client
{
const REDIRECT_URI_ROUTE = 'le_sociallogin/yahoo/connect';
const REQUEST_TOKEN_URI_ROUTE = 'le_sociallogin/yahoo/request';
const OAUTH_URI = 'https://api.login.yahoo.com/oauth/v2';
const OAUTH2_SERVICE_URI = 'https://social.yahooapis.com/v1/user/';
const XML_PATH_ENABLED = 'le_sociallogin/yahoo/enabled';
const XML_PATH_CLIENT_ID = 'le_sociallogin/yahoo/api_key';
const XML_PATH_CLIENT_SECRET = 'le_sociallogin/yahoo/secret';
protected $clientId = null;
protected $clientSecret = null;
protected $redirectUri = null;
protected $client = null;
protected $token = null;
protected $protocol = "http";
public function __construct()
{
if(($this->isEnabled = $this->_isEnabled())) {
$this->clientId = $this->_getClientId();
$this->clientSecret = $this->_getClientSecret();
$isSecure = Mage::app()->getStore()->isCurrentlySecure();
if($isSecure){
$this->protocol = "https";
}
$this->redirectUri = Mage::getModel('core/url')->sessionUrlVar(
Mage::getUrl(self::REDIRECT_URI_ROUTE, array('_secure'=>true))
);
$this->client = new Zend_Oauth_Consumer(
array(
'callbackUrl' => $this->redirectUri,
'siteUrl' => self::OAUTH_URI,
'authorizeUrl' => self::OAUTH_URI.'/request_auth',
'consumerKey' => $this->clientId,
'consumerSecret' => $this->clientSecret,
'requestTokenUrl' => 'https://api.login.yahoo.com/oauth/v2/get_request_token',
'userAuthorisationUrl' => 'https://api.login.yahoo.com/oauth/v2/request_auth',
'accessTokenUrl' => 'https://api.login.yahoo.com/oauth/v2/get_token',
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_signature' => $this->clientId . '%26' . $this->clientSecret,
)
);
}
}
public function isEnabled()
{
return (bool) $this->isEnabled;
}
public function getClient()
{
return $this->client;
}
public function getClientId()
{
return $this->clientId;
}
public function getClientSecret()
{
return $this->clientSecret;
}
public function getRedirectUri()
{
return $this->redirectUri;
}
public function setAccessToken($token)
{
$this->token = unserialize($token);
}
public function getAccessToken()
{
if(empty($this->token)) {
$this->fetchAccessToken();
}
return unserialize($this->token);
}
public function getXoauthYahooGuid(){
if(empty($this->token)) {
$this->fetchAccessToken();
}
return $this->token->xoauth_yahoo_guid;
}
public function createAuthUrl()
{
return Mage::getUrl('le_sociallogin/yahoo/request', array("mainw_protocol" => $this->protocol));
}
public function fetchRequestToken()
{
try{
$requestToken = $this->client->getRequestToken();
if($requestToken == FALSE){
echo "Connect to 'https://api.login.yahoo.com' is false ! <br />Error code :".$this->client->getLastResponse() ;
return ;
}
}catch (Zend_Oauth_Exception $e){
echo 'Connect to \'https://api.login.yahoo.com\' is false ! <br />Error code : '.$e->getMessage();
return ;
}
Mage::getSingleton('core/session')
->setYahooRequestToken(serialize($requestToken));
$this->client->redirect();
}
protected function fetchAccessToken()
{
if (!($params = Mage::app()->getFrontController()->getRequest()->getParams())
||
!($requestToken = Mage::getSingleton('core/session')
->getYahooRequestToken())
) {
throw new Exception(
Mage::helper('le_sociallogin')
->__('Unable to retrieve access code.')
);
}
if(!($token = $this->client->getAccessToken(
$params,
unserialize($requestToken)
)
)
) {
throw new Exception(
Mage::helper('le_sociallogin')
->__('Unable to retrieve access token.')
);
}
Mage::getSingleton('core/session')->unsYahooRequestToken();
return $this->token = $token;
}
public function api($endpoint, $method = 'GET', $params = array())
{
if(empty($this->token)) {
throw new Exception(
Mage::helper('le_sociallogin')
->__('Unable to proceeed without an access token.')
);
}
$url = self::OAUTH2_SERVICE_URI.$this->token->xoauth_yahoo_guid.'/profile';
$response = $this->_httpRequest($url, strtoupper($method), $params);
return $response;
}
protected function _httpRequest($url, $method = 'GET', $params = array())
{
$client = $this->token->getHttpClient(
array(
'callbackUrl' => $this->redirectUri,
'siteUrl'=>self::OAUTH_URI,
'authorizeUrl' => self::OAUTH_URI.'/request_auth',
'consumerKey' => $this->clientId,
'consumerSecret' => $this->clientSecret,
'oauth_signature_method' => 'HMAC-SHA1',
'requestTokenUrl' => 'https://api.login.yahoo.com/oauth/v2/get_request_token',
'userAuthorisationUrl' => 'https://api.login.yahoo.com/oauth/v2/request_auth',
'accessTokenUrl' => 'https://api.login.yahoo.com/oauth/v2/get_token',
'oauth_signature' => $this->clientSecret,
)
);
$client->setUri($url);
switch ($method) {
case 'GET':
$client->setMethod(Zend_Http_Client::GET);
$client->setParameterGet(array('format' => 'json'));
break;
case 'POST':
$client->setMethod(Zend_Http_Client::POST);
$client->setParameterPost($params);
break;
case 'DELETE':
$client->setMethod(Zend_Http_Client::DELETE);
break;
default:
throw new Exception(
Mage::helper('le_sociallogin')
->__('Required HTTP method is not supported.')
);
}
$response = $client->request();
Mage::log($response->getStatus().' - '. $response->getBody());
$decoded_response = json_decode($response->getBody());
if($response->isError()) {
$status = $response->getStatus();
if(($status == 400 || $status == 401 || $status == 429)) {
if(isset($decoded_response->error->message)) {
$message = $decoded_response->error->message;
} else {
$message = Mage::helper('le_sociallogin')
->__('Unspecified OAuth error occurred.');
}
throw new LitExtension_SocialLogin_YahooOAuthException($message);
} else {
$message = sprintf(
Mage::helper('le_sociallogin')
->__('HTTP error %d occurred while issuing request.'),
$status
);
throw new Exception($message);
}
}
return $decoded_response;
}
protected function _isEnabled()
{
return $this->_getStoreConfig(self::XML_PATH_ENABLED);
}
protected function _getClientId()
{
return $this->_getStoreConfig(self::XML_PATH_CLIENT_ID);
}
protected function _getClientSecret()
{
return $this->_getStoreConfig(self::XML_PATH_CLIENT_SECRET);
}
protected function _getStoreConfig($xmlPath)
{
return Mage::getStoreConfig($xmlPath, Mage::app()->getStore()->getId());
}
}
class LitExtension_SocialLogin_YahooOAuthException extends Exception
{}