| 1 |
<?php |
| 2 |
namespace Photonic_Plugin\Modules; |
| 3 |
|
| 4 |
use Photonic_Plugin\Core\Photonic; |
| 5 |
|
| 6 |
require_once('Core.php'); |
| 7 |
require_once('Authenticator.php'); |
| 8 |
|
| 9 |
abstract class OAuth2 extends Core { |
| 10 |
use Authenticator; |
| 11 |
|
| 12 |
public $scope, $response_type, $client_id, $client_secret, $state, $access_token, $refresh_token_valid; |
| 13 |
|
| 14 |
protected function __construct() { |
| 15 |
parent::__construct(); |
| 16 |
} |
| 17 |
|
| 18 |
public abstract function authentication_url(); |
| 19 |
|
| 20 |
public abstract function access_token_url(); |
| 21 |
|
| 22 |
public abstract function refresh_access_token($token, $save); |
| 23 |
|
| 24 |
protected abstract function set_token_validity($validity); |
| 25 |
|
| 26 |
public function redirect_url() { |
| 27 |
return get_site_url(); |
| 28 |
} |
| 29 |
|
| 30 |
public function get_authorization_url($args = []) { |
| 31 |
$url = add_query_arg('test', 'test'); |
| 32 |
$url = remove_query_arg('test', $url); |
| 33 |
$parameters = array_merge([ |
| 34 |
'response_type' => $this->response_type, |
| 35 |
'redirect_uri' => $this->redirect_url(), |
| 36 |
'client_id' => $this->client_id, |
| 37 |
'scope' => $this->scope, |
| 38 |
'access_type' => 'offline', |
| 39 |
'state' => md5($this->client_secret.$this->provider).'::'.urlencode($url), |
| 40 |
], $args); |
| 41 |
return $this->authentication_url()."?".Authenticator::build_query($parameters); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Takes an OAuth request token and exchanges it for an access token. |
| 46 |
* |
| 47 |
* @param $request_token |
| 48 |
*/ |
| 49 |
function get_access_token($request_token) { |
| 50 |
$code = $request_token['code']; |
| 51 |
$state_args = explode('::', $request_token['state']); |
| 52 |
|
| 53 |
if ($state_args[0] == md5($this->client_secret.$this->provider)) { |
| 54 |
$url = urldecode($state_args[1]); |
| 55 |
$response = Photonic::http($this->access_token_URL(), 'POST', [ |
| 56 |
'code' => $code, |
| 57 |
'grant_type' => 'authorization_code', |
| 58 |
'client_id' => $this->client_id, |
| 59 |
'client_secret' => $this->client_secret, |
| 60 |
'redirect_uri' => $this->redirect_url(), |
| 61 |
]); |
| 62 |
|
| 63 |
if (is_wp_error($response)) { |
| 64 |
$url = add_query_arg('error', $response->get_error_code(), $url); |
| 65 |
} |
| 66 |
else if ($response == null) { |
| 67 |
$url = add_query_arg('error', 'null', $url); |
| 68 |
} |
| 69 |
} |
| 70 |
else { |
| 71 |
$url = remove_query_arg(['token', 'code', 'state']); |
| 72 |
} |
| 73 |
wp_redirect($url); |
| 74 |
exit(); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param $base_token |
| 79 |
*/ |
| 80 |
public function authenticate($base_token) { |
| 81 |
$photonic_authentication = get_option('photonic_authentication'); |
| 82 |
if (!isset($photonic_authentication)) { |
| 83 |
$photonic_authentication = []; |
| 84 |
} |
| 85 |
|
| 86 |
if (!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 to the authentication option. |
| 89 |
$token = $this->refresh_access_token($base_token, true); |
| 90 |
} |
| 91 |
else if (isset($photonic_authentication[$this->provider])) { |
| 92 |
$token = $photonic_authentication[$this->provider]; |
| 93 |
if (!empty($token)) { |
| 94 |
if ($this->is_token_expired($token)) { |
| 95 |
$token = $this->refresh_access_token($base_token, true); |
| 96 |
} |
| 97 |
else { |
| 98 |
$this->set_token_validity(true); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
if (!empty($token)) { |
| 104 |
$this->access_token = $token['oauth_token']; |
| 105 |
} |
| 106 |
else { |
| 107 |
$this->set_token_validity(false); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
function is_token_expired($token) { |
| 112 |
if (empty($token)) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
if (!isset($token['oauth_token']) || !isset($token['oauth_token_created']) || !isset($token['oauth_token_expires'])) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
if (!isset($token['client_id']) || (isset($token['client_id']) && $token['client_id'] !== $this->client_id)) { |
| 119 |
return true; |
| 120 |
} |
| 121 |
$current = time(); |
| 122 |
if ($token['oauth_token_created'] + $token['oauth_token_expires'] < $current) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* 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", |
| 130 |
* 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 |
| 131 |
* 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 |
| 132 |
* return <code>true</code>. |
| 133 |
* |
| 134 |
* For cases where the token does not exist yet, the method returns <code>null</code>. |
| 135 |
* |
| 136 |
* @param $soon_limit int Number of days to check the expiry limit for. |
| 137 |
* @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. |
| 138 |
*/ |
| 139 |
function is_token_expiring_soon($soon_limit) { |
| 140 |
$photonic_authentication = get_option('photonic_authentication'); |
| 141 |
if (empty($photonic_authentication) || empty($photonic_authentication[$this->provider]) || |
| 142 |
empty($photonic_authentication[$this->provider]['oauth_token']) || empty($photonic_authentication[$this->provider]['oauth_token_created']) || empty($photonic_authentication[$this->provider]['oauth_token_expires'])) { |
| 143 |
return null; // There is no token! |
| 144 |
} |
| 145 |
|
| 146 |
$token = $photonic_authentication[$this->provider]; |
| 147 |
$token_expiry = $token['oauth_token_created'] + $token['oauth_token_expires']; |
| 148 |
|
| 149 |
$current = time(); |
| 150 |
$test_expiry = $current + $soon_limit * 24 * 60 * 60; |
| 151 |
|
| 152 |
$time_left = $token_expiry - $test_expiry; |
| 153 |
|
| 154 |
if ($current >= $token_expiry) { |
| 155 |
return -1; // already expired |
| 156 |
} |
| 157 |
else if ($time_left <= 0) { |
| 158 |
return 1; // Expiring soon |
| 159 |
} |
| 160 |
else { |
| 161 |
return 0; // There is still time |
| 162 |
} |
| 163 |
/* if ($current + $soon_limit * 24 * 60 * 60 > $token['oauth_token_created'] + $token['oauth_token_expires']) { |
| 164 |
return true; |
| 165 |
} |
| 166 |
return false;*/ |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Takes a token response from a request token call, then puts it in an appropriate array. |
| 171 |
* |
| 172 |
* @param $response |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
public function parse_token($response) { |
| 176 |
$token = []; |
| 177 |
if (!is_wp_error($response) && is_array($response)) { |
| 178 |
$body = $response['body']; |
| 179 |
$body = json_decode($body); |
| 180 |
if (empty($body->error)) { |
| 181 |
$token['oauth_token'] = $body->access_token; |
| 182 |
$token['oauth_token_type'] = $body->token_type; |
| 183 |
$token['oauth_token_created'] = time(); |
| 184 |
$token['oauth_token_expires'] = $body->expires_in; |
| 185 |
$this->set_token_validity(true); |
| 186 |
} |
| 187 |
else { |
| 188 |
$this->set_token_validity(false); |
| 189 |
} |
| 190 |
} |
| 191 |
return $token; |
| 192 |
} |
| 193 |
} |