This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * EOAuth2Service 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 | * EOAuth2Service is a base class for all OAuth 2.0 providers. |
||
14 | * |
||
15 | * @package application.extensions.eauth |
||
16 | */ |
||
17 | abstract class EOAuth2Service extends EAuthServiceBase implements IAuthService { |
||
18 | |||
19 | /** |
||
20 | * @var string OAuth2 client id. |
||
21 | */ |
||
22 | protected $client_id; |
||
23 | |||
24 | /** |
||
25 | * @var string OAuth2 client secret key. |
||
26 | */ |
||
27 | protected $client_secret; |
||
28 | |||
29 | /** |
||
30 | * @var string OAuth scopes. |
||
31 | */ |
||
32 | protected $scope = ''; |
||
33 | |||
34 | /** |
||
35 | * @var array Provider options. Must contain the keys: authorize, access_token. |
||
36 | */ |
||
37 | protected $providerOptions = array( |
||
38 | 'authorize' => '', |
||
39 | 'access_token' => '', |
||
40 | ); |
||
41 | |||
42 | /** |
||
43 | * @var string current OAuth2 access token. |
||
44 | */ |
||
45 | protected $access_token = ''; |
||
46 | |||
47 | /** |
||
48 | * @var string Error key name in _GET options. |
||
49 | */ |
||
50 | protected $errorParam = 'error'; |
||
51 | |||
52 | /** |
||
53 | * @var string Error description key name in _GET options. |
||
54 | */ |
||
55 | protected $errorDescriptionParam = 'error_description'; |
||
56 | |||
57 | /** |
||
58 | * @var string Error code for access_denied response. |
||
59 | */ |
||
60 | protected $errorAccessDeniedCode = 'access_denied'; |
||
61 | |||
62 | |||
63 | public function init($component, $options = array()) { |
||
64 | parent::init($component, $options); |
||
65 | |||
66 | // Try to restore access token from session. |
||
67 | $this->restoreAccessToken(); |
||
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 | if (isset($_GET[$this->errorParam])) { |
||
78 | $error_code = $_GET[$this->errorParam]; |
||
79 | if ($error_code === $this->errorAccessDeniedCode) { |
||
80 | // access_denied error (user canceled) |
||
81 | $this->cancel(); |
||
82 | } |
||
83 | else { |
||
84 | $error = $error_code; |
||
85 | if (isset($_GET[$this->errorDescriptionParam])) { |
||
86 | $error = $_GET[$this->errorDescriptionParam].' ('.$error.')'; |
||
87 | } |
||
88 | throw new EAuthException($error); |
||
89 | } |
||
90 | return false; |
||
91 | } |
||
92 | |||
93 | // Get the access_token and save them to the session. |
||
94 | if (isset($_GET['code'])) { |
||
95 | $code = $_GET['code']; |
||
96 | $token = $this->getAccessToken($code); |
||
97 | if (isset($token)) { |
||
98 | $this->authenticated = true; |
||
99 | $this->saveAccessToken($token); |
||
100 | } |
||
101 | } |
||
102 | // Redirect to the authorization page |
||
103 | else { |
||
104 | // Use the URL of the current page as the callback URL. |
||
105 | View Code Duplication | if (isset($_GET['redirect_uri'])) { |
|
0 ignored issues
–
show
|
|||
106 | $redirect_uri = $_GET['redirect_uri']; |
||
107 | } |
||
108 | else { |
||
109 | $server = Yii::app()->request->getHostInfo(); |
||
110 | $path = Yii::app()->request->getUrl(); |
||
111 | $redirect_uri = $server . $path; |
||
112 | } |
||
113 | $url = $this->getCodeUrl($redirect_uri); |
||
114 | Yii::app()->request->redirect($url); |
||
115 | } |
||
116 | |||
117 | return $this->getIsAuthenticated(); |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * Returns the url to request to get OAuth2 code. |
||
122 | * |
||
123 | * @param string $redirect_uri url to redirect after user confirmation. |
||
124 | * @return string url to request. |
||
125 | */ |
||
126 | protected function getCodeUrl($redirect_uri) { |
||
127 | $this->setState('redirect_uri', $redirect_uri); |
||
128 | return $this->providerOptions['authorize'] . '?client_id=' . $this->client_id . '&redirect_uri=' . urlencode($redirect_uri) . '&scope=' . $this->scope . '&response_type=code'; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Returns the url to request to get OAuth2 access token. |
||
133 | * |
||
134 | * @param string $code |
||
135 | * @return string url to request. |
||
136 | */ |
||
137 | protected function getTokenUrl($code) { |
||
138 | return $this->providerOptions['access_token'] . '?client_id=' . $this->client_id . '&client_secret=' . $this->client_secret . '&code=' . $code . '&redirect_uri=' . urlencode($this->getState('redirect_uri')); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Returns the OAuth2 access token. |
||
143 | * |
||
144 | * @param string $code the OAuth2 code. See {@link getCodeUrl}. |
||
145 | * @return string the token. |
||
146 | */ |
||
147 | protected function getAccessToken($code) { |
||
148 | return $this->makeRequest($this->getTokenUrl($code)); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Save access token to the session. |
||
153 | * |
||
154 | * @param string $token access token. |
||
155 | */ |
||
156 | protected function saveAccessToken($token) { |
||
157 | $this->setState('auth_token', $token); |
||
158 | $this->access_token = $token; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Restore access token from the session. |
||
163 | * |
||
164 | * @return boolean whether the access token was successfuly restored. |
||
165 | */ |
||
166 | protected function restoreAccessToken() { |
||
167 | if (!$this->authenticated) { |
||
168 | if ($this->hasState('auth_token') && $this->getState('expires', 0) > time()) { |
||
169 | $this->access_token = $this->getState('auth_token'); |
||
170 | $this->authenticated = true; |
||
171 | } |
||
172 | else { |
||
173 | $this->access_token = null; |
||
174 | $this->authenticated = false; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | return $this->authenticated; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Returns fields required for signed request. |
||
183 | * By default returns array('access_token' => $this->access_token). |
||
184 | * Used in {@link makeSignedRequest}. |
||
185 | * |
||
186 | * @return array |
||
187 | */ |
||
188 | protected function getSignedRequestFields() |
||
189 | { |
||
190 | return array('access_token' => $this->access_token); |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Returns the protected resource. |
||
195 | * |
||
196 | * @param string $url url to request. |
||
197 | * @param array $options HTTP request options. Keys: query, data, referer. |
||
198 | * @param boolean $parseJson Whether to parse response in json format. |
||
199 | * @return stdClass the response. |
||
200 | * @see makeRequest |
||
201 | */ |
||
202 | public function makeSignedRequest($url, $options = array(), $parseJson = true) { |
||
203 | if (!$this->getIsAuthenticated()) { |
||
204 | throw new CHttpException(401, Yii::t('eauth', 'Unable to complete the request because the user was not authenticated.')); |
||
205 | } |
||
206 | |||
207 | // Merge query fields with fields required for signed request. |
||
208 | $options['query'] = |
||
209 | array_merge( |
||
210 | isset($options['query']) ? $options['query'] : array(), |
||
211 | $this->getSignedRequestFields() |
||
212 | ); |
||
213 | |||
214 | $result = $this->makeRequest($url, $options, $parseJson); |
||
215 | |||
216 | return $result; |
||
217 | } |
||
218 | } |
||
219 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.