Refactoring of Google Authentication (new callback url)
This commit is contained in:
@@ -31,6 +31,20 @@ class HttpClient extends Base
|
||||
*/
|
||||
const HTTP_USER_AGENT = 'Kanboard';
|
||||
|
||||
/**
|
||||
* Send a GET HTTP request and parse JSON response
|
||||
*
|
||||
* @access public
|
||||
* @param string $url
|
||||
* @param string[] $headers
|
||||
* @return array
|
||||
*/
|
||||
public function getJson($url, array $headers = array())
|
||||
{
|
||||
$response = $this->doRequest('GET', $url, '', array_merge(array('Accept: application/json'), $headers));
|
||||
return json_decode($response, true) ?: array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a POST HTTP request encoded in JSON
|
||||
*
|
||||
@@ -43,6 +57,7 @@ class HttpClient extends Base
|
||||
public function postJson($url, array $data, array $headers = array())
|
||||
{
|
||||
return $this->doRequest(
|
||||
'POST',
|
||||
$url,
|
||||
json_encode($data),
|
||||
array_merge(array('Content-type: application/json'), $headers)
|
||||
@@ -61,6 +76,7 @@ class HttpClient extends Base
|
||||
public function postForm($url, array $data, array $headers = array())
|
||||
{
|
||||
return $this->doRequest(
|
||||
'POST',
|
||||
$url,
|
||||
http_build_query($data),
|
||||
array_merge(array('Content-type: application/x-www-form-urlencoded'), $headers)
|
||||
@@ -71,12 +87,13 @@ class HttpClient extends Base
|
||||
* Make the HTTP request
|
||||
*
|
||||
* @access private
|
||||
* @param string $method
|
||||
* @param string $url
|
||||
* @param string $content
|
||||
* @param string[] $headers
|
||||
* @return string
|
||||
*/
|
||||
private function doRequest($url, $content, array $headers)
|
||||
private function doRequest($method, $url, $content, array $headers)
|
||||
{
|
||||
if (empty($url)) {
|
||||
return '';
|
||||
@@ -86,7 +103,7 @@ class HttpClient extends Base
|
||||
|
||||
$context = stream_context_create(array(
|
||||
'http' => array(
|
||||
'method' => 'POST',
|
||||
'method' => $method,
|
||||
'protocol_version' => 1.1,
|
||||
'timeout' => self::HTTP_TIMEOUT,
|
||||
'max_redirects' => self::HTTP_MAX_REDIRECTS,
|
||||
|
||||
75
app/Core/OAuth2.php
Normal file
75
app/Core/OAuth2.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace Core;
|
||||
|
||||
/**
|
||||
* OAuth2 client
|
||||
*
|
||||
* @package core
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class OAuth2 extends Base
|
||||
{
|
||||
private $clientId;
|
||||
private $secret;
|
||||
private $callbackUrl;
|
||||
private $authUrl;
|
||||
private $tokenUrl;
|
||||
private $scopes;
|
||||
private $tokenType;
|
||||
private $accessToken;
|
||||
|
||||
public function createService($clientId, $secret, $callbackUrl, $authUrl, $tokenUrl, array $scopes)
|
||||
{
|
||||
$this->clientId = $clientId;
|
||||
$this->secret = $secret;
|
||||
$this->callbackUrl = $callbackUrl;
|
||||
$this->authUrl = $authUrl;
|
||||
$this->tokenUrl = $tokenUrl;
|
||||
$this->scopes = $scopes;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAuthorizationUrl()
|
||||
{
|
||||
$params = array(
|
||||
'response_type' => 'code',
|
||||
'client_id' => $this->clientId,
|
||||
'redirect_uri' => $this->callbackUrl,
|
||||
'scope' => implode(' ', $this->scopes),
|
||||
);
|
||||
|
||||
return $this->authUrl.'?'.http_build_query($params);
|
||||
}
|
||||
|
||||
public function getAuthorizationHeader()
|
||||
{
|
||||
if ($this->tokenType === 'Bearer') {
|
||||
return 'Authorization: Bearer '.$this->accessToken;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
public function getAccessToken($code)
|
||||
{
|
||||
if (empty($this->accessToken) && ! empty($code)) {
|
||||
|
||||
$params = array(
|
||||
'code' => $code,
|
||||
'client_id' => $this->clientId,
|
||||
'client_secret' => $this->secret,
|
||||
'redirect_uri' => $this->callbackUrl,
|
||||
'grant_type' => 'authorization_code',
|
||||
);
|
||||
|
||||
$response = json_decode($this->httpClient->postForm($this->tokenUrl, $params), true);
|
||||
|
||||
$this->tokenType = isset($response['token_type']) ? $response['token_type'] : '';
|
||||
$this->accessToken = isset($response['access_token']) ? $response['access_token'] : '';
|
||||
}
|
||||
|
||||
return $this->accessToken;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user