|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* EOAuthService class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Maxim Zemskov <[email protected]> |
|
6
|
|
|
* @link http://github.com/Nodge/yii-eauth/ |
|
7
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
require_once 'EAuthServiceBase.php'; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* EOAuthService is a base class for all OAuth providers. |
|
14
|
|
|
* |
|
15
|
|
|
* @package application.extensions.eauth |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class EOAuthService extends EAuthServiceBase implements IAuthService { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var EOAuthUserIdentity the OAuth library instance. |
|
21
|
|
|
*/ |
|
22
|
|
|
private $auth; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string OAuth2 client id. |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $key; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string OAuth2 client secret key. |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $secret; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string OAuth scopes. |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $scope = ''; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array Provider options. Must contain the keys: request, authorize, access. |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $providerOptions = array( |
|
44
|
|
|
'request' => '', |
|
45
|
|
|
'authorize' => '', |
|
46
|
|
|
'access' => '', |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Initialize the component. |
|
52
|
|
|
* |
|
53
|
|
|
* @param EAuth $component the component instance. |
|
54
|
|
|
* @param array $options properties initialization. |
|
55
|
|
|
*/ |
|
56
|
|
|
public function init($component, $options = array()) { |
|
57
|
|
|
parent::init($component, $options); |
|
58
|
|
|
|
|
59
|
|
|
$this->auth = new EOAuthUserIdentity(array( |
|
60
|
|
|
'scope' => $this->scope, |
|
61
|
|
|
'key' => $this->key, |
|
62
|
|
|
'secret' => $this->secret, |
|
63
|
|
|
'provider' => $this->providerOptions, |
|
64
|
|
|
)); |
|
65
|
|
|
|
|
66
|
|
|
// Try to restore access token and customer from session. |
|
67
|
|
|
$this->restoreCredentials(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Authenticate the user. |
|
72
|
|
|
* |
|
73
|
|
|
* @return boolean whether user was successfuly authenticated. |
|
74
|
|
|
* @throws EAuthException |
|
75
|
|
|
*/ |
|
76
|
|
|
public function authenticate() { |
|
77
|
|
|
$this->authenticated = $this->auth->authenticate(); |
|
78
|
|
|
$error = $this->auth->getError(); |
|
79
|
|
|
if (isset($error)) { |
|
80
|
|
|
throw new EAuthException($error); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// In case of successful authentication save access token and |
|
84
|
|
|
// customer to session. |
|
85
|
|
|
if ($this->authenticated) { |
|
86
|
|
|
$this->saveCredentials(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->getIsAuthenticated(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns the OAuth consumer. |
|
94
|
|
|
* |
|
95
|
|
|
* @return object the consumer. |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function getConsumer() { |
|
98
|
|
|
return $this->auth->getProvider()->consumer; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Returns the OAuth access token. |
|
103
|
|
|
* |
|
104
|
|
|
* @return string the token. |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function getAccessToken() { |
|
107
|
|
|
return $this->auth->getProvider()->token; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Save access credentials to the session. |
|
112
|
|
|
*/ |
|
113
|
|
|
protected function saveCredentials() { |
|
114
|
|
|
|
|
115
|
|
|
$this->setState('auth_token', $this->getAccessToken()); |
|
116
|
|
|
$this->setState('auth_consumer', $this->getConsumer()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Restore access credentials from the session. |
|
121
|
|
|
* |
|
122
|
|
|
* @return boolean whether the access credentials were successfully restored. |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function restoreCredentials() { |
|
125
|
|
|
if (!$this->authenticated) { |
|
126
|
|
|
if ($this->hasState('auth_consumer') && $this->hasState('auth_token')) { |
|
127
|
|
|
$this->auth->getProvider()->consumer = $this->getState('auth_consumer'); |
|
128
|
|
|
$this->auth->getProvider()->token = $this->getState('auth_token'); |
|
129
|
|
|
$this->authenticated = true; |
|
130
|
|
|
} |
|
131
|
|
|
else { |
|
132
|
|
|
$this->authenticated = false; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this->authenticated; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Initializes a new session and return a cURL handle. |
|
141
|
|
|
* |
|
142
|
|
|
* @param string $url url to request. |
|
143
|
|
|
* @param array $options HTTP request options. Keys: query, data, referer. |
|
144
|
|
|
* @param boolean $parseJson Whether to parse response in json format. |
|
|
|
|
|
|
145
|
|
|
* @return cURL handle. |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function initRequest($url, $options = array()) { |
|
148
|
|
|
$ch = parent::initRequest($url, $options); |
|
149
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); |
|
150
|
|
|
return $ch; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns the protected resource. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $url url to request. |
|
157
|
|
|
* @param array $options HTTP request options. Keys: query, data, referer. |
|
158
|
|
|
* @param boolean $parseJson Whether to parse response in json format. |
|
159
|
|
|
* @return string the response. |
|
160
|
|
|
* @see makeRequest |
|
161
|
|
|
*/ |
|
162
|
|
|
public function makeSignedRequest($url, $options = array(), $parseJson = true) { |
|
163
|
|
|
if (!$this->getIsAuthenticated()) { |
|
164
|
|
|
throw new CHttpException(401, Yii::t('eauth', 'Unable to complete the request because the user was not authenticated.')); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$consumer = $this->getConsumer(); |
|
168
|
|
|
$signatureMethod = new OAuthSignatureMethod_HMAC_SHA1(); |
|
169
|
|
|
$token = $this->getAccessToken(); |
|
170
|
|
|
|
|
171
|
|
|
$query = null; |
|
172
|
|
|
if (isset($options['query'])) { |
|
173
|
|
|
$query = $options['query']; |
|
174
|
|
|
unset($options['query']); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$request = OAuthRequest::from_consumer_and_token($consumer, $token, isset($options['data']) ? 'POST' : 'GET', $url, $query); |
|
178
|
|
|
$request->sign_request($signatureMethod, $consumer, $token); |
|
179
|
|
|
|
|
180
|
|
|
return $this->makeRequest($request->to_url(), $options, $parseJson); |
|
181
|
|
|
} |
|
182
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.