| 1 |
<?php |
| 2 |
namespace Photonic_Plugin\Modules; |
| 3 |
|
| 4 |
trait Authenticator { |
| 5 |
abstract function get_access_token($token); |
| 6 |
|
| 7 |
public function oauth_signature_method() { |
| 8 |
return 'HMAC-SHA1'; |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Encodes the URL as per RFC3986 specs. This replaces some strings in addition to the ones done by a rawurlencode. |
| 13 |
* This has been adapted from the OAuth for PHP project. |
| 14 |
* |
| 15 |
* @static |
| 16 |
* @param $input |
| 17 |
* @return array|mixed|string |
| 18 |
*/ |
| 19 |
public static function urlencode_rfc3986($input) { |
| 20 |
if (is_array($input)) { |
| 21 |
return array_map(['Photonic_Plugin\Modules\Authenticator', 'urlencode_rfc3986'], $input); |
| 22 |
} |
| 23 |
else if (is_scalar($input)) { |
| 24 |
return str_replace( |
| 25 |
'+', |
| 26 |
' ', |
| 27 |
str_replace('%7E', '~', rawurlencode($input)) |
| 28 |
); |
| 29 |
} |
| 30 |
else { |
| 31 |
return ''; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Takes an array of parameters, then parses it and generates a query string. Prior to generating the query string the parameters are sorted in their natural order. |
| 37 |
* Without sorting the signatures between this application and the provider might differ. |
| 38 |
* |
| 39 |
* @static |
| 40 |
* @param $params |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public static function build_query($params) { |
| 44 |
if (!$params) { |
| 45 |
return ''; |
| 46 |
} |
| 47 |
$keys = array_map(['Photonic_Plugin\Modules\Authenticator', 'urlencode_rfc3986'], array_keys($params)); |
| 48 |
$values = array_map(['Photonic_Plugin\Modules\Authenticator', 'urlencode_rfc3986'], array_values($params)); |
| 49 |
$params = array_combine($keys, $values); |
| 50 |
|
| 51 |
// Sort by keys (natsort) |
| 52 |
uksort($params, 'strnatcmp'); |
| 53 |
$pairs = []; |
| 54 |
foreach ($params as $key => $value) { |
| 55 |
if (is_array($value)) { |
| 56 |
natsort($value); |
| 57 |
foreach ($value as $v2) { |
| 58 |
$pairs[] = ($v2 == '') ? "$key=0" : "$key=$v2"; |
| 59 |
} |
| 60 |
} |
| 61 |
else { |
| 62 |
$pairs[] = ($value == '') ? "$key=0" : "$key=$value"; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
$string = implode('&', $pairs); |
| 67 |
return $string; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Takes a token response from a request token call, then puts it in an appropriate array. |
| 72 |
* |
| 73 |
* @param $response |
| 74 |
* @return array |
| 75 |
*/ |
| 76 |
abstract public function parse_token($response); |
| 77 |
|
| 78 |
/** |
| 79 |
* Saves the token in the authentication options. |
| 80 |
* |
| 81 |
* @param $token |
| 82 |
*/ |
| 83 |
public function save_token($token) { |
| 84 |
$photonic_authentication = get_option('photonic_authentication'); |
| 85 |
if (!isset($photonic_authentication)) { |
| 86 |
$photonic_authentication = []; |
| 87 |
} |
| 88 |
$photonic_authentication[$this->provider] = $token; |
| 89 |
update_option('photonic_authentication', $photonic_authentication); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Fetches a token from authentication options |
| 94 |
* |
| 95 |
* @return array|null |
| 96 |
*/ |
| 97 |
public function get_cached_token() { |
| 98 |
$photonic_authentication = get_option('photonic_authentication'); |
| 99 |
if (!empty($photonic_authentication) && isset($photonic_authentication[$this->provider])) { |
| 100 |
return $photonic_authentication[$this->provider]; |
| 101 |
} |
| 102 |
return null; |
| 103 |
} |
| 104 |
} |