Generates a cURL handle with all of the required authentication bits set.
Access
public
Returns
Type |
Description |
|---|---|
|
A cURL handle ready for executing. |
Source
Method defined in authentication/signature_v4query.class.php | Toggle source view (117 lines) | View on GitHub
public function authenticate()
{
// Determine signing values
$current_time = time();
$timestamp = gmdate(CFUtilities::DATE_FORMAT_SIGV4, $current_time);
// Initialize
$x_amz_target = null;
$this->headers = array();
$this->signed_headers = array();
$this->canonical_headers = array();
$this->query = array('body' => is_array($this->payload) ? $this->payload : array());
// Do we have an authentication token?
if ($this->auth_token)
{
$this->headers['X-Amz-Security-Token'] = $this->auth_token;
$this->query['body']['SecurityToken'] = $this->auth_token;
}
// Manage the key-value pairs that are used in the query.
if (stripos($this->operation, 'x-amz-target') !== false)
{
$x_amz_target = trim(str_ireplace('x-amz-target:', '', $this->operation));
}
else
{
$this->query['body']['Action'] = $this->operation;
}
// Only add it if it exists.
if ($this->api_version)
{
$this->query['body']['Version'] = $this->api_version;
}
// Do a case-sensitive, natural order sort on the array keys.
uksort($this->query['body'], 'strcmp');
// Remove the default scheme from the domain.
$domain = str_replace(array('http://', 'https://'), '', $this->endpoint);
// Parse our request.
$parsed_url = parse_url('http://' . $domain);
// Set the proper host header.
if (isset($parsed_url['port']) && (integer) $parsed_url['port'] !== 80 && (integer) $parsed_url['port'] !== 443)
{
$host_header = strtolower($parsed_url['host']) . ':' . $parsed_url['port'];
}
else
{
$host_header = strtolower($parsed_url['host']);
}
// Generate the querystring from $this->query
$this->querystring = $this->util->to_query_string($this->query);
// Gather information to pass along to other classes.
$helpers = array(
'utilities' => $this->utilities_class,
'request' => $this->request_class,
'response' => $this->response_class,
);
// Compose the request.
$request_url = ($this->use_ssl ? 'https://' : 'http://') . $domain;
$request_url .= !isset($parsed_url['path']) ? '/' : '';
// Instantiate the request class
$request = new $this->request_class($request_url, $this->proxy, $helpers, $this->credentials);
$request->set_method('POST');
$request->set_body($this->canonical_querystring());
$this->querystring = $this->canonical_querystring();
$this->headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
$this->headers['X-Amz-Target'] = $x_amz_target;
// Pass along registered stream callbacks
if ($this->registered_streaming_read_callback)
{
$request->register_streaming_read_callback($this->registered_streaming_read_callback);
}
if ($this->registered_streaming_write_callback)
{
$request->register_streaming_write_callback($this->registered_streaming_write_callback);
}
// Add authentication headers
$this->headers['X-Amz-Date'] = $timestamp;
$this->headers['Content-Length'] = strlen($this->querystring);
$this->headers['Content-MD5'] = $this->util->hex_to_base64(md5($this->querystring));
$this->headers['Host'] = $host_header;
// Sort headers
uksort($this->headers, 'strnatcasecmp');
// Add headers to request and compute the string to sign
foreach ($this->headers as $header_key => $header_value)
{
// Strip line breaks and remove consecutive spaces. Services collapse whitespace in signature calculation
$header_value = preg_replace('/\s+/', ' ', trim($header_value));
$request->add_header($header_key, $header_value);
$this->canonical_headers[] = strtolower($header_key) . ':' . $header_value;
$this->signed_headers[] = strtolower($header_key);
}
$this->headers['Authorization'] = $this->authorization($timestamp);
$request->add_header('Authorization', $this->headers['Authorization']);
$request->request_headers = $this->headers;
return $request;
}

