2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Auth;
|
|
|
|
|
|
|
|
abstract class OAuth
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $appID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $appSecret;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $callback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $state;
|
|
|
|
|
2020-01-12 08:09:22 +13:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-01-13 00:51:30 +13:00
|
|
|
protected $userScopes;
|
2020-01-12 08:09:22 +13:00
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
/**
|
|
|
|
* OAuth constructor.
|
|
|
|
*
|
|
|
|
* @param string $appId
|
|
|
|
* @param string $appSecret
|
|
|
|
* @param string $callback
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param array $state
|
2020-01-13 00:51:30 +13:00
|
|
|
* @param array $userScopes
|
2019-05-09 18:54:39 +12:00
|
|
|
*/
|
2020-01-13 00:51:30 +13:00
|
|
|
public function __construct(string $appId, string $appSecret, string $callback, $state = [], $userScopes = [])
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2019-09-07 05:04:26 +12:00
|
|
|
$this->appID = $appId;
|
|
|
|
$this->appSecret = $appSecret;
|
|
|
|
$this->callback = $callback;
|
|
|
|
$this->state = $state;
|
2020-01-13 00:51:30 +13:00
|
|
|
$this->userScopes = $userScopes;
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getName():string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getLoginURL():string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $code
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getAccessToken(string $code):string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $accessToken
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getUserID(string $accessToken):string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $accessToken
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getUserEmail(string $accessToken):string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $accessToken
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getUserName(string $accessToken):string;
|
|
|
|
|
2020-01-12 08:29:22 +13:00
|
|
|
/**
|
|
|
|
* @param $scope
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function addScope(string $scope){
|
|
|
|
// Add a scope to the scopes array if it isn't already present
|
2020-01-13 00:51:30 +13:00
|
|
|
if (!in_array($scope, $this->userScopes)){
|
|
|
|
$this->userScopes[] = $scope;
|
2020-01-12 08:29:22 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getScopes(){
|
2020-01-13 00:51:30 +13:00
|
|
|
return $this->userScopes;
|
2020-01-12 08:29:22 +13:00
|
|
|
}
|
2019-10-08 08:39:01 +13:00
|
|
|
|
2020-01-13 00:51:30 +13:00
|
|
|
|
2019-10-07 01:56:44 +13:00
|
|
|
// The parseState function was designed specifically for Amazon OAuth Adapter to override.
|
2019-10-07 01:58:01 +13:00
|
|
|
// The response from Amazon is html encoded and hence it needs to be html_decoded before
|
2019-10-07 01:56:44 +13:00
|
|
|
// json_decoding
|
|
|
|
/**
|
|
|
|
* @param $state
|
|
|
|
*
|
2019-10-08 08:39:01 +13:00
|
|
|
* @return string
|
2019-10-07 01:56:44 +13:00
|
|
|
*/
|
2019-10-14 07:25:39 +13:00
|
|
|
public function parseState(string $state)
|
2019-10-07 01:58:01 +13:00
|
|
|
{
|
2019-10-07 01:56:44 +13:00
|
|
|
return json_decode($state, true);
|
|
|
|
}
|
|
|
|
|
2019-05-09 18:54:39 +12:00
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param string $url
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param array $headers
|
2019-05-09 18:54:39 +12:00
|
|
|
* @param string $payload
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function request(string $method, string $url = '', array $headers = [], string $payload = ''):string
|
|
|
|
{
|
2019-09-07 05:04:26 +12:00
|
|
|
$ch = curl_init($url);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Console_OAuth_Agent');
|
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
if (!empty($payload)) {
|
2019-05-09 18:54:39 +12:00
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
|
|
|
}
|
|
|
|
|
2019-10-01 17:34:01 +13:00
|
|
|
$headers[] = 'Content-length: '.strlen($payload);
|
2019-09-30 11:03:22 +13:00
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
|
|
|
|
// Send the request & save response to $response
|
2019-05-09 18:54:39 +12:00
|
|
|
$response = curl_exec($ch);
|
|
|
|
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
2019-09-07 05:04:26 +12:00
|
|
|
}
|