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