| 1 |
<?php |
| 2 |
|
| 3 |
namespace Photonic_Plugin\Platforms; |
| 4 |
|
| 5 |
use Photonic_Plugin\Core\Photonic; |
| 6 |
use WP_Error; |
| 7 |
|
| 8 |
require_once 'Base.php'; |
| 9 |
require_once 'Authenticator.php'; |
| 10 |
|
| 11 |
abstract class OAuth1 extends Base { |
| 12 |
use Authenticator; |
| 13 |
|
| 14 |
public $base_url; |
| 15 |
|
| 16 |
/** |
| 17 |
* Access Token URL |
| 18 |
* |
| 19 |
* @abstract |
| 20 |
* @return string |
| 21 |
*/ |
| 22 |
abstract public function access_token_URL(): string; |
| 23 |
|
| 24 |
/** |
| 25 |
* Authenticate URL |
| 26 |
* |
| 27 |
* @abstract |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
abstract public function authenticate_URL(): string; |
| 31 |
|
| 32 |
/** |
| 33 |
* Authorize URL |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
abstract public function authorize_URL(): string; |
| 38 |
|
| 39 |
/** |
| 40 |
* Request Token URL |
| 41 |
* |
| 42 |
* @return string |
| 43 |
*/ |
| 44 |
abstract public function request_token_URL(): string; |
| 45 |
|
| 46 |
/** |
| 47 |
* Method to validate that the stored token is indeed authenticated. |
| 48 |
* |
| 49 |
* @return array|WP_Error |
| 50 |
*/ |
| 51 |
abstract public function check_access_token(); |
| 52 |
|
| 53 |
/** |
| 54 |
* Get the authorize URL |
| 55 |
* |
| 56 |
* @param $token |
| 57 |
* @param bool $sign_in |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function get_authorize_URL($token, $sign_in = true): string { |
| 61 |
if (empty($sign_in)) { |
| 62 |
return $this->authorize_URL() . "?oauth_token={$token['oauth_token']}"; |
| 63 |
} |
| 64 |
else { |
| 65 |
return $this->authenticate_URL() . "?oauth_token={$token['oauth_token']}"; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Takes a token response from a request token call, then puts it in an appropriate array. |
| 71 |
* |
| 72 |
* @param $response |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
public function parse_token($response): array { |
| 76 |
$body = $response['body']; |
| 77 |
$token = Base::parse_parameters($body); |
| 78 |
return $token; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Generates the signature for the OAuth call. |
| 83 |
* See here for the signature-generation methodology: https://www.wackylabs.net/2011/12/oauth-and-flickr-part-2/ |
| 84 |
* |
| 85 |
* @param null $api_call |
| 86 |
* @param null $api_args |
| 87 |
* @param null $method |
| 88 |
* @param array $oauth_token |
| 89 |
* @return string |
| 90 |
*/ |
| 91 |
protected function generate_signature($api_call = null, $api_args = null, $method = null, $oauth_token = []): string { |
| 92 |
$encoded_key = self::urlencode_rfc3986($this->api_secret) . '&'; |
| 93 |
if (isset($oauth_token['oauth_token_secret'])) { |
| 94 |
$encoded_key .= self::urlencode_rfc3986($oauth_token['oauth_token_secret']); |
| 95 |
} |
| 96 |
|
| 97 |
$method = is_null($method) ? 'POST' : $method; |
| 98 |
$params = [ |
| 99 |
'oauth_consumer_key' => $this->api_key, |
| 100 |
'oauth_nonce' => $this->nonce, |
| 101 |
'oauth_signature_method' => $this->oauth_signature_method(), |
| 102 |
'oauth_timestamp' => $this->oauth_timestamp, |
| 103 |
'oauth_version' => $this->oauth_version, |
| 104 |
]; |
| 105 |
|
| 106 |
if (isset($oauth_token['oauth_token'])) { |
| 107 |
$params['oauth_token'] = $oauth_token['oauth_token']; |
| 108 |
} |
| 109 |
|
| 110 |
if (isset($oauth_token['oauth_verifier'])) { |
| 111 |
$params['oauth_verifier'] = $oauth_token['oauth_verifier']; |
| 112 |
} |
| 113 |
|
| 114 |
$params = (!empty($api_args)) ? array_merge($params, $api_args) : $params; |
| 115 |
if ('smug' === $this->provider && (false !== stripos($api_call, '?_expand=') || false !== stripos($api_call, '?_config='))) { |
| 116 |
if (false !== stripos($api_call, '?_expand=')) { |
| 117 |
$params['_expand'] = substr($api_call, stripos($api_call, '?_expand=') + 9); |
| 118 |
} |
| 119 |
if (false !== stripos($api_call, '?_config=')) { |
| 120 |
$params['_config'] = substr($api_call, stripos($api_call, '?_config=') + 9); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
$end_point = 'smug' === $this->provider ? remove_query_arg(['_expand', '_config'], $api_call) : $api_call; |
| 125 |
|
| 126 |
ksort($params); |
| 127 |
$string = self::build_query($params); |
| 128 |
$base_string = $method . '&' . self::urlencode_rfc3986($end_point) . '&' . self::urlencode_rfc3986($string); |
| 129 |
$sig = base64_encode(hash_hmac('sha1', $base_string, $encoded_key, true)); |
| 130 |
|
| 131 |
$this->signature_parameters = [ |
| 132 |
'parameters' => $params, |
| 133 |
'base_string' => $base_string, |
| 134 |
'key' => $encoded_key, |
| 135 |
]; |
| 136 |
return $sig; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Gets an OAuth request token using the API Key and API Secret provided in the plugin's back-end options. |
| 141 |
* Once a token has been successfully got, the user is sent to an Authorization page where he can allow access for your site. |
| 142 |
* |
| 143 |
* @param null $oauth_callback |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
public function get_request_token($oauth_callback): array { |
| 147 |
$method = 'GET'; |
| 148 |
|
| 149 |
$signature = $this->generate_signature($this->request_token_URL(), ['oauth_callback' => $oauth_callback], $method); |
| 150 |
$parameters = [ |
| 151 |
'oauth_version' => $this->oauth_version, |
| 152 |
'oauth_nonce' => $this->nonce, |
| 153 |
'oauth_timestamp' => $this->oauth_timestamp, |
| 154 |
'oauth_callback' => $oauth_callback, |
| 155 |
'oauth_consumer_key' => $this->api_key, |
| 156 |
'oauth_signature_method' => $this->oauth_signature_method(), |
| 157 |
'oauth_signature' => $signature, |
| 158 |
]; |
| 159 |
|
| 160 |
$end_point = $this->request_token_URL(); |
| 161 |
if ('GET' === $method) { |
| 162 |
$end_point .= '?' . self::build_query($parameters); |
| 163 |
$parameters = null; |
| 164 |
} |
| 165 |
|
| 166 |
$response = Photonic::http($end_point, $method, $parameters); |
| 167 |
$token = $this->parse_token($response); |
| 168 |
|
| 169 |
return $token; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Takes an OAuth request token and exchanges it for an access token. |
| 174 |
* |
| 175 |
* @param $request_token |
| 176 |
* @return array |
| 177 |
*/ |
| 178 |
public function get_access_token($request_token): array { |
| 179 |
$method = 'GET'; |
| 180 |
$signature = $this->generate_signature($this->access_token_URL(), [], $method, $request_token); |
| 181 |
$parameters = [ |
| 182 |
'oauth_consumer_key' => $this->api_key, |
| 183 |
'oauth_nonce' => $this->nonce, |
| 184 |
'oauth_signature' => $signature, |
| 185 |
'oauth_signature_method' => $this->oauth_signature_method(), |
| 186 |
'oauth_timestamp' => $this->oauth_timestamp, |
| 187 |
'oauth_token' => $request_token['oauth_token'], |
| 188 |
'oauth_version' => $this->oauth_version, |
| 189 |
]; |
| 190 |
|
| 191 |
if (isset($request_token['oauth_verifier'])) { |
| 192 |
$parameters['oauth_verifier'] = $request_token['oauth_verifier']; |
| 193 |
} |
| 194 |
|
| 195 |
$end_point = $this->access_token_URL(); |
| 196 |
|
| 197 |
if ('GET' === $method) { |
| 198 |
$end_point .= '?' . self::build_query($parameters); |
| 199 |
$parameters = null; |
| 200 |
} |
| 201 |
|
| 202 |
$response = Photonic::http($end_point, $method, $parameters); |
| 203 |
$token = $this->parse_token($response); |
| 204 |
|
| 205 |
return $token; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Takes the response for the "Check access token", then tries to determine whether the check was successful or not. |
| 210 |
* |
| 211 |
* @param $response |
| 212 |
* @return bool |
| 213 |
*/ |
| 214 |
public function is_access_token_valid($response): bool { |
| 215 |
if (is_wp_error($response)) { |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
$body = $response['body']; |
| 220 |
$body = json_decode($body); |
| 221 |
|
| 222 |
if (!isset($body->stat) || 'fail' === $body->stat) { |
| 223 |
return false; |
| 224 |
} |
| 225 |
return true; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Checks if authentication has been enabled and the user has authenticated. If so, it signs the call, then adds the additional parameters to it. |
| 230 |
* This method also attaches the oauth_signature to the parameters. |
| 231 |
* |
| 232 |
* @param $api_method |
| 233 |
* @param $method |
| 234 |
* @param $parameters |
| 235 |
* @return mixed |
| 236 |
*/ |
| 237 |
public function sign_call($api_method, $method, $parameters) { |
| 238 |
if (isset($this->token) && isset($this->token_secret)) { |
| 239 |
$token = ['oauth_token' => $this->token, 'oauth_token_secret' => $this->token_secret]; |
| 240 |
} |
| 241 |
|
| 242 |
if (isset($token)) { |
| 243 |
$this->nonce = $this->nonce(); |
| 244 |
$this->oauth_timestamp = time(); |
| 245 |
$signature = $this->generate_signature($api_method, $parameters, $method, $token); |
| 246 |
if (isset($this->signature_parameters) && isset($this->signature_parameters['parameters'])) { |
| 247 |
$this->signature_parameters['parameters']['oauth_signature'] = $signature; |
| 248 |
return $this->signature_parameters['parameters']; |
| 249 |
} |
| 250 |
} |
| 251 |
return $parameters; |
| 252 |
} |
| 253 |
|
| 254 |
public function set_oauth_done() { |
| 255 |
if (!empty($this->token) && !empty($this->token_secret)) { |
| 256 |
$token_response = $this->check_access_token(); |
| 257 |
$this->oauth_done = $this->is_access_token_valid($token_response); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|