| 1 |
<?php |
| 2 |
|
| 3 |
namespace Photonic_Plugin\Platforms; |
| 4 |
|
| 5 |
use Photonic_Plugin\Core\Photonic; |
| 6 |
|
| 7 |
require_once 'Base.php'; |
| 8 |
require_once 'Authenticator.php'; |
| 9 |
|
| 10 |
abstract class OAuth2 extends Base { |
| 11 |
use Authenticator; |
| 12 |
|
| 13 |
public $scope; |
| 14 |
public $response_type; |
| 15 |
public $client_id; |
| 16 |
public $client_secret; |
| 17 |
public $state; |
| 18 |
public $access_token; |
| 19 |
public $auth_error; |
| 20 |
public $soon_limit; |
| 21 |
|
| 22 |
protected function __construct() { |
| 23 |
$this->soon_limit = 30; |
| 24 |
parent::__construct(); |
| 25 |
} |
| 26 |
|
| 27 |
abstract public function authentication_URL(); |
| 28 |
|
| 29 |
abstract public function access_token_URL(); |
| 30 |
|
| 31 |
abstract public function renew_token($token): array; |
| 32 |
|
| 33 |
abstract protected function set_token_validity($validity); |
| 34 |
|
| 35 |
public function redirect_url() { |
| 36 |
return get_site_url(); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Takes an OAuth request token and exchanges it for an access token. |
| 41 |
* |
| 42 |
* @param $request_token |
| 43 |
*/ |
| 44 |
public function get_access_token($request_token) { |
| 45 |
$code = $request_token['code']; |
| 46 |
$state_args = explode('::', $request_token['state']); |
| 47 |
|
| 48 |
if (md5($this->client_secret . $this->provider) === $state_args[0]) { |
| 49 |
$url = urldecode($state_args[1]); |
| 50 |
$response = Photonic::http( |
| 51 |
$this->access_token_URL(), |
| 52 |
'POST', |
| 53 |
[ |
| 54 |
'code' => $code, |
| 55 |
'grant_type' => 'authorization_code', |
| 56 |
'client_id' => $this->client_id, |
| 57 |
'client_secret' => $this->client_secret, |
| 58 |
'redirect_uri' => $this->redirect_url(), |
| 59 |
] |
| 60 |
); |
| 61 |
|
| 62 |
if (is_wp_error($response)) { |
| 63 |
$url = add_query_arg('error', $response->get_error_code(), $url); |
| 64 |
} |
| 65 |
elseif (empty($response)) { |
| 66 |
$url = add_query_arg('error', 'null', $url); |
| 67 |
} |
| 68 |
} |
| 69 |
else { |
| 70 |
$url = remove_query_arg(['token', 'code', 'state']); |
| 71 |
} |
| 72 |
wp_safe_redirect($url); |
| 73 |
exit(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @param $base_token |
| 78 |
*/ |
| 79 |
public function authenticate($base_token) { |
| 80 |
$photonic_authentication = get_option('photonic_authentication'); |
| 81 |
if (!isset($photonic_authentication)) { |
| 82 |
$photonic_authentication = []; |
| 83 |
} |
| 84 |
|
| 85 |
$transient_token = get_transient('photonic_' . $this->provider . '_token'); |
| 86 |
if (empty($transient_token) && !isset($photonic_authentication[$this->provider]) && !empty($base_token)) { |
| 87 |
// Nothing is in the authentication option, but there is a token in overall Photonic Options. |
| 88 |
// Refresh it if required, and save it as a transient via renew_token. |
| 89 |
list($token, $error) = $this->renew_token($base_token); |
| 90 |
} |
| 91 |
elseif ($transient_token || isset($photonic_authentication[$this->provider])) { |
| 92 |
$token = empty($transient_token) ? $photonic_authentication[$this->provider] : $transient_token; |
| 93 |
if (!empty($token)) { |
| 94 |
if (empty($transient_token) && !($this->is_token_expired($token) || $this->is_token_expiring_soon($this->soon_limit) > 0)) { |
| 95 |
set_transient('photonic_' . $this->provider . '_token', $token, $token['oauth_token_expires']); |
| 96 |
$this->set_token_validity(true); |
| 97 |
} |
| 98 |
elseif ($this->is_token_expired($token) || $this->is_token_expiring_soon($this->soon_limit) > 0) { |
| 99 |
list($token, $error) = $this->renew_token($base_token); |
| 100 |
} |
| 101 |
else { |
| 102 |
$this->set_token_validity(true); |
| 103 |
} |
| 104 |
} |
| 105 |
else { |
| 106 |
list($token, $error) = $this->renew_token($base_token); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
if (!empty($token)) { |
| 111 |
$this->access_token = $token['oauth_token']; |
| 112 |
$this->auth_error = ''; |
| 113 |
} |
| 114 |
else { |
| 115 |
$this->set_token_validity(false); |
| 116 |
if (!empty($error)) { |
| 117 |
$this->auth_error = $error; |
| 118 |
} |
| 119 |
else { |
| 120 |
$this->auth_error = ''; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
public function is_token_expired($token) { |
| 126 |
if (empty($token)) { |
| 127 |
return true; |
| 128 |
} |
| 129 |
if (!isset($token['oauth_token']) || !isset($token['oauth_token_created']) || !isset($token['oauth_token_expires'])) { |
| 130 |
return true; |
| 131 |
} |
| 132 |
if (!isset($token['client_id']) || (isset($token['client_id']) && $token['client_id'] !== $this->client_id)) { |
| 133 |
return true; |
| 134 |
} |
| 135 |
$current = time(); |
| 136 |
if ($token['oauth_token_created'] + $token['oauth_token_expires'] < $current) { |
| 137 |
return true; |
| 138 |
} |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Checks if a token will expire soon. This is used to trigger a refresh for sources such as Instagram. Google uses a separate "Refresh Token", |
| 144 |
* so this is not applicable to it. The <code>soon_limit</code> defines how many days is "soon", and a refresh is triggered if the current date |
| 145 |
* is in the "soon" range. E.g. If you have a soon limit of 30 days, and your token expires in 15 days when you load the page, this method will |
| 146 |
* return <code>true</code>. |
| 147 |
* |
| 148 |
* For cases where the token does not exist yet, the method returns <code>null</code>. |
| 149 |
* |
| 150 |
* @param $soon_limit int Number of days to check the expiry limit for. |
| 151 |
* @return int|null If there is no token, return null. Otherwise, if there are < $soon_limit days left, return 1, if token is expired return -1, and if there is time return 0. |
| 152 |
*/ |
| 153 |
abstract public function is_token_expiring_soon($soon_limit); |
| 154 |
|
| 155 |
/** |
| 156 |
* Takes a token response from a request token call, then puts it in an appropriate array. |
| 157 |
* |
| 158 |
* @param $response |
| 159 |
* @return array |
| 160 |
*/ |
| 161 |
public function parse_token($response): array { |
| 162 |
$token = []; |
| 163 |
if (!is_wp_error($response) && is_array($response)) { |
| 164 |
$body = $response['body']; |
| 165 |
$body = json_decode($body); |
| 166 |
if (empty($body->error)) { |
| 167 |
$token['oauth_token'] = $body->access_token; |
| 168 |
$token['oauth_token_type'] = $body->token_type; |
| 169 |
$token['oauth_token_created'] = time(); |
| 170 |
$token['oauth_token_expires'] = $body->expires_in; |
| 171 |
if (!empty($body->refresh_token)) { |
| 172 |
$token['oauth_refresh_token'] = $body->refresh_token; |
| 173 |
} |
| 174 |
$this->set_token_validity(true); |
| 175 |
} |
| 176 |
else { |
| 177 |
$this->set_token_validity(false); |
| 178 |
} |
| 179 |
} |
| 180 |
return $token; |
| 181 |
} |
| 182 |
} |
| 183 |
|