forked from Nodge/yii-eauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EOAuthService.php
130 lines (110 loc) · 3.33 KB
/
EOAuthService.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
<?php
/**
* EOAuthService class file.
*
* @author Maxim Zemskov <[email protected]>
* @link http://github.com/Nodge/yii-eauth/
* @license http://www.opensource.org/licenses/bsd-license.php
*/
require_once 'EAuthServiceBase.php';
/**
* EOAuthService is a base class for all OAuth providers.
* @package application.extensions.eauth
*/
abstract class EOAuthService extends EAuthServiceBase implements IAuthService {
/**
* @var EOAuthUserIdentity the OAuth library instance.
*/
private $auth;
/**
* @var string OAuth2 client id.
*/
protected $key;
/**
* @var string OAuth2 client secret key.
*/
protected $secret;
/**
* @var string OAuth scopes.
*/
protected $scope = '';
/**
* @var array Provider options. Must contain the keys: request, authorize, access.
*/
protected $providerOptions = array(
'request' => '',
'authorize' => '',
'access' => '',
);
/**
* Initialize the component.
* @param EAuth $component the component instance.
* @param array $options properties initialization.
*/
public function init($component, $options = array()) {
parent::init($component, $options);
$this->auth = new EOAuthUserIdentity(array(
'scope' => $this->scope,
'key' => $this->key,
'secret' => $this->secret,
'provider' => $this->providerOptions,
));
}
/**
* Authenticate the user.
* @return boolean whether user was successfuly authenticated.
*/
public function authenticate() {
$this->authenticated = $this->auth->authenticate();
return $this->getIsAuthenticated();
}
/**
* Returns the OAuth consumer.
* @return object the consumer.
*/
protected function getConsumer() {
return $this->auth->getProvider()->consumer;
}
/**
* Returns the OAuth access token.
* @return string the token.
*/
protected function getAccessToken() {
return $this->auth->getProvider()->token;
}
/**
* Initializes a new session and return a cURL handle.
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, referer.
* @param boolean $parseJson Whether to parse response in json format.
* @return cURL handle.
*/
protected function initRequest($url, $options = array()) {
$ch = parent::initRequest($url, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
return $ch;
}
/**
* Returns the protected resource.
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, referer.
* @param boolean $parseJson Whether to parse response in json format.
* @return string the response.
* @see makeRequest
*/
public function makeSignedRequest($url, $options = array(), $parseJson = true) {
if (!$this->getIsAuthenticated())
throw new CHttpException(401, Yii::t('eauth', 'Unable to complete the request because the user was not authenticated.'));
$consumer = $this->getConsumer();
$signatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
$token = $this->getAccessToken();
$query = null;
if (isset($options['query'])) {
$query = $options['query'];
unset($options['query']);
}
$request = OAuthRequest::from_consumer_and_token($consumer, $token, isset($options['data']) ? 'POST' : 'GET', $url, $query);
$request->sign_request($signatureMethod, $consumer, $token);
return $this->makeRequest($request->to_url(), $options, $parseJson);
}
}