Build the required HMAC'd auth string.
public static build_auth_query_string ( string $auth_key, string $auth_secret, string $request_method, string $request_path, array $query_params = [], string $auth_version = '1.0', string $auth_timestamp = null ) : string | ||
$auth_key | string | |
$auth_secret | string | |
$request_method | string | |
$request_path | string | |
$query_params | array | |
$auth_version | string | [optional] |
$auth_timestamp | string | [optional] |
return | string |
/** * Utility function used to create the curl object with common settings */ private function create_curl($s_url, $request_method = 'GET', $query_params = array()) { # Create the signed signature... $signed_query = Pusher::build_auth_query_string($this->settings['auth_key'], $this->settings['secret'], $request_method, $s_url, $query_params); $full_url = $this->settings['scheme'] . '://' . $this->settings['host'] . ':' . $this->settings['port'] . $s_url . '?' . $signed_query; $this->log('curl_init( ' . $full_url . ' )'); # Set cURL opts and execute request $ch = curl_init(); if ($ch === false) { throw new PusherException('Could not initialise cURL!'); } curl_setopt($ch, CURLOPT_URL, $full_url); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $this->settings['timeout']); return $ch; }
public function get_channels() { $s_url = $this->settings['url'] . '/channels'; # Create the signed signature... $signed_query = Pusher::build_auth_query_string($this->settings['auth_key'], $this->settings['secret'], 'GET', $s_url); $full_url = $this->settings['server'] . ':' . $this->settings['port'] . $s_url . '?' . $signed_query; # Set cURL opts and execute request $ch = curl_init(); if ($ch === false) { die('Could not initialise cURL!'); } curl_setopt($ch, CURLOPT_URL, $full_url); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $this->settings['timeout']); $response = curl_exec($ch); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($http_status == 200) { $response = json_decode($response); $response = $response->channels; } else { $response = false; } curl_close($ch); return $response; }
/** * Utility function used to create the curl object with common settings */ private function create_curl($s_url, $request_method = 'GET', $query_params = array()) { # Create the signed signature... $signed_query = Pusher::build_auth_query_string($this->settings['auth_key'], $this->settings['secret'], $request_method, $s_url, $query_params); $full_url = $this->settings['scheme'] . '://' . $this->settings['host'] . ':' . $this->settings['port'] . $s_url . '?' . $signed_query; $this->log('curl_init( ' . $full_url . ' )'); // Create or reuse existing curl handle static $ch; if (null === $ch) { $ch = curl_init(); } if ($ch === false) { throw new PusherException('Could not initialise cURL!'); } // curl handle is not reusable unless reset if (function_exists('curl_reset')) { curl_reset($ch); } # Set cURL opts and execute request curl_setopt($ch, CURLOPT_URL, $full_url); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Expect:")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $this->settings['timeout']); return $ch; }
public function testGenerateSignature() { $time = time(); $auth_version = '1.0'; $method = 'POST'; $auth_key = 'thisisaauthkey'; $auth_secret = 'thisisasecret'; $request_path = '/channels/test_channel/events'; $query_params = array('name' => 'an_event'); $auth_query_string = Pusher::build_auth_query_string($auth_key, $auth_secret, $method, $request_path, $query_params, $auth_version, $time); $expected_to_sign = "POST\n{$request_path}\nauth_key={$auth_key}&auth_timestamp={$time}&auth_version={$auth_version}&name=an_event"; $expected_auth_signature = hash_hmac('sha256', $expected_to_sign, $auth_secret, false); $expected_query_string = "auth_key={$auth_key}&auth_signature={$expected_auth_signature}&auth_timestamp={$time}&auth_version={$auth_version}&name=an_event"; $this->assertEquals($auth_query_string, $expected_query_string, 'auth signature valid'); }