10 defined(
'JPATH_PLATFORM') or die;
57 $this->options = isset($options) ? $options :
new JRegistry;
58 $this->http = isset($http) ? $http :
new JHttp($this->options);
60 $this->application = isset($application) ? $application :
new JApplicationWeb;
71 public function authenticate()
73 if ($data[
'code'] = $this->input->get(
'code',
false,
'raw'))
75 $data[
'grant_type'] =
'authorization_code';
76 $data[
'redirect_uri'] = $this->getOption(
'redirecturi');
77 $data[
'client_id'] = $this->getOption(
'clientid');
78 $data[
'client_secret'] = $this->getOption(
'clientsecret');
79 $response = $this->http->post($this->getOption(
'tokenurl'), $data);
81 if ($response->code >= 200 && $response->code < 400)
84 if ($response->headers[
'Content-Type'] ==
'application/json')
86 $token = array_merge(json_decode($response->body,
true), array(
'created' => time()));
90 parse_str($response->body, $token);
91 $token = array_merge($token, array(
'created' => time()));
94 $this->setToken($token);
100 throw new RuntimeException(
'Error code ' . $response->code .
' received requesting access token: ' . $response->body .
'.');
104 if ($this->getOption(
'sendheaders'))
106 $this->application->redirect($this->createUrl());
118 public function isAuthenticated()
120 $token = $this->getToken();
122 if (!$token || !array_key_exists(
'access_token', $token))
126 elseif (array_key_exists(
'expires_in', $token) && $token[
'created'] + $token[
'expires_in'] < time() + 20)
144 public function createUrl()
146 if (!$this->getOption(
'authurl') || !$this->getOption(
'clientid'))
148 throw new InvalidArgumentException(
'Authorization URL and client_id are required');
151 $url = $this->getOption(
'authurl');
153 if (strpos($url,
'?'))
162 $url .=
'response_type=code';
163 $url .=
'&client_id=' . urlencode($this->getOption(
'clientid'));
165 if ($this->getOption(
'redirecturi'))
167 $url .=
'&redirect_uri=' . urlencode($this->getOption(
'redirecturi'));
170 if ($this->getOption(
'scope'))
172 $scope = is_array($this->getOption(
'scope')) ? implode(
' ', $this->getOption(
'scope')) : $this->getOption(
'scope');
173 $url .=
'&scope=' . urlencode($scope);
176 if ($this->getOption(
'state'))
178 $url .=
'&state=' . urlencode($this->getOption(
'state'));
181 if (is_array($this->getOption(
'requestparams')))
183 foreach ($this->getOption(
'requestparams') as $key => $value)
185 $url .=
'&' . $key .
'=' . urlencode($value);
207 public function query($url, $data = null, $headers = array(), $method =
'get', $timeout = null)
209 $token = $this->getToken();
211 if (array_key_exists(
'expires_in', $token) && $token[
'created'] + $token[
'expires_in'] < time() + 20)
213 if (!$this->getOption(
'userefresh'))
217 $token = $this->refreshToken($token[
'refresh_token']);
220 if (!$this->getOption(
'authmethod') || $this->getOption(
'authmethod') ==
'bearer')
222 $headers[
'Authorization'] =
'Bearer ' . $token[
'access_token'];
224 elseif ($this->getOption(
'authmethod') ==
'get')
226 if (strpos($url,
'?'))
234 $url .= $this->getOption(
'getparam') ? $this->getOption(
'getparam') :
'access_token';
235 $url .=
'=' . $token[
'access_token'];
244 $response = $this->http->$method($url, $headers, $timeout);
249 $response = $this->http->$method($url, $data, $headers, $timeout);
252 throw new InvalidArgumentException(
'Unknown HTTP request method: ' . $method .
'.');
255 if ($response->code < 200 || $response->code >= 400)
257 throw new RuntimeException(
'Error code ' . $response->code .
' received requesting data: ' . $response->body .
'.');
271 public function getOption($key)
273 return $this->options->get($key);
286 public function setOption($key, $value)
288 $this->options->set($key, $value);
300 public function getToken()
302 return $this->getOption(
'accesstoken');
314 public function setToken($value)
316 if (is_array($value) && !array_key_exists(
'expires_in', $value) && array_key_exists(
'expires', $value))
318 $value[
'expires_in'] = $value[
'expires'];
319 unset($value[
'expires']);
321 $this->setOption(
'accesstoken', $value);
337 public function refreshToken($token = null)
339 if (!$this->getOption(
'userefresh'))
341 throw new RuntimeException(
'Refresh token is not supported for this OAuth instance.');
346 $token = $this->getToken();
348 if (!array_key_exists(
'refresh_token', $token))
350 throw new RuntimeException(
'No refresh token is available.');
352 $token = $token[
'refresh_token'];
354 $data[
'grant_type'] =
'refresh_token';
355 $data[
'refresh_token'] = $token;
356 $data[
'client_id'] = $this->getOption(
'clientid');
357 $data[
'client_secret'] = $this->getOption(
'clientsecret');
358 $response = $this->http->post($this->getOption(
'tokenurl'), $data);
360 if ($response->code >= 200 || $response->code < 400)
362 if ($response->headers[
'Content-Type'] ==
'application/json')
364 $token = array_merge(json_decode($response->body,
true), array(
'created' => time()));
368 parse_str($response->body, $token);
369 $token = array_merge($token, array(
'created' => time()));
372 $this->setToken($token);
378 throw new Exception(
'Error code ' . $response->code .
' received refreshing token: ' . $response->body .
'.');