mirror of
https://github.com/awslabs/aws-crt-php.git
synced 2026-08-18 18:13:09 +00:00
Implemented async callbacks to PHP regardless of thread model, Sigv4 signing (#24)
* Updated to get latest aws-crt-ffi * Added implementation for signing_result * Added main thread queue and callback queueing to signing process * Updated to get support for date on signing_config_aws * added date support to SigningConfigAWS, fixed up reference issues on http message signing * Broke C code into separate files, moved common stuff to php_aws_crt.h * Use a unity build to make PHP do the right thing with our lib * Updated aws-crt-ffi to v0.1.3 to get sigv4 changes * Added CRT::isAvailable for safe runtime detection of CRT * Fixed up test resource safety, added tests to prove it * Added CI jobs for every version of PHP on linux (#26)
This commit is contained in:
@@ -9,6 +9,10 @@ use AWS\CRT\NativeResource;
|
||||
|
||||
abstract class Signing extends NativeResource {
|
||||
static function signRequestAws($signable, $signing_config, $on_complete) {
|
||||
return self::$crt->sign_request_aws($signable, $signing_config, $on_complete);
|
||||
return self::$crt->sign_request_aws($signable->native, $signing_config->native,
|
||||
function($result, $error_code) use ($on_complete) {
|
||||
$signing_result = SigningResult::fromNative($result);
|
||||
$on_complete($signing_result, $error_code);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ class SigningConfigAWS extends NativeResource {
|
||||
'signed_body_value' => null,
|
||||
'signed_body_header_type' => SignedBodyHeaderType::NONE,
|
||||
'expiration_in_seconds' => 0,
|
||||
'date' => time()
|
||||
];
|
||||
}
|
||||
|
||||
@@ -34,15 +35,28 @@ class SigningConfigAWS extends NativeResource {
|
||||
$sc = $this->acquire(self::$crt->signing_config_aws_new());
|
||||
self::$crt->signing_config_aws_set_algorithm($sc, $options->algorithm->asInt());
|
||||
self::$crt->signing_config_aws_set_signature_type($sc, $options->signature_type->asInt());
|
||||
self::$crt->signing_config_aws_set_credentials_provider($sc, $options->credentials_provider->asObject());
|
||||
self::$crt->signing_config_aws_set_region($sc, $options->region->asString());
|
||||
self::$crt->signing_config_aws_set_service($sc, $options->service->asString());
|
||||
self::$crt->signing_config_aws_set_use_double_uri_encode($sc, $options->use_double_uri_encode->asBool());
|
||||
self::$crt->signing_config_aws_set_should_normalize_uri_path($sc, $options->should_normalize_uri_path->asBool());
|
||||
self::$crt->signing_config_aws_set_omit_session_token($sc, $options->omit_session_token->asBool());
|
||||
self::$crt->signing_config_aws_set_signed_body_value($sc, $options->signed_body_value->asString());
|
||||
self::$crt->signing_config_aws_set_signed_body_header_type($sc, $options->signed_body_header_type->asInt());
|
||||
self::$crt->signing_config_aws_set_expiration_in_seconds($sc, $options->expiration_in_seconds->asInt());
|
||||
if ($credentials_provider = $options->credentials_provider->asObject()) {
|
||||
self::$crt->signing_config_aws_set_credentials_provider(
|
||||
$sc,
|
||||
$credentials_provider->native);
|
||||
}
|
||||
self::$crt->signing_config_aws_set_region(
|
||||
$sc, $options->region->asString());
|
||||
self::$crt->signing_config_aws_set_service(
|
||||
$sc, $options->service->asString());
|
||||
self::$crt->signing_config_aws_set_use_double_uri_encode(
|
||||
$sc, $options->use_double_uri_encode->asBool());
|
||||
self::$crt->signing_config_aws_set_should_normalize_uri_path(
|
||||
$sc, $options->should_normalize_uri_path->asBool());
|
||||
self::$crt->signing_config_aws_set_omit_session_token(
|
||||
$sc, $options->omit_session_token->asBool());
|
||||
self::$crt->signing_config_aws_set_signed_body_value(
|
||||
$sc, $options->signed_body_value->asString());
|
||||
self::$crt->signing_config_aws_set_signed_body_header_type(
|
||||
$sc, $options->signed_body_header_type->asInt());
|
||||
self::$crt->signing_config_aws_set_expiration_in_seconds(
|
||||
$sc, $options->expiration_in_seconds->asInt());
|
||||
self::$crt->signing_config_aws_set_date($sc, $options->date->asInt());
|
||||
}
|
||||
|
||||
function __destruct()
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0.
|
||||
*/
|
||||
namespace AWS\CRT\Auth;
|
||||
|
||||
use AWS\CRT\NativeResource;
|
||||
use AWS\CRT\HTTP\Request;
|
||||
|
||||
class SigningResult extends NativeResource {
|
||||
protected function __construct($native) {
|
||||
parent::__construct();
|
||||
|
||||
$this->acquire($native);
|
||||
}
|
||||
|
||||
function __destruct() {
|
||||
// No destruction necessary, SigningResults are transient, just release
|
||||
$this->release();
|
||||
parent::__destruct();
|
||||
}
|
||||
|
||||
public static function fromNative($ptr) {
|
||||
return new SigningResult($ptr);
|
||||
}
|
||||
|
||||
public function applyToHttpRequest(&$http_request) {
|
||||
self::$crt->signing_result_apply_to_http_request($this->native, $http_request->native);
|
||||
// Update http_request from native
|
||||
$http_request = Request::unmarshall($http_request->toBlob());
|
||||
}
|
||||
}
|
||||
@@ -48,6 +48,25 @@ final class CRT {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool whether or not the CRT is currently loaded with an active backend
|
||||
*/
|
||||
public static function isLoaded() {
|
||||
return !is_null(self::$impl);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool whether or not the CRT is available via one of the possible backends
|
||||
*/
|
||||
public static function isAvailable() {
|
||||
try {
|
||||
new CRT();
|
||||
return true;
|
||||
} catch (RuntimeException $ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return integer last error code reported within the CRT
|
||||
*/
|
||||
@@ -211,6 +230,10 @@ final class CRT {
|
||||
return self::$impl->aws_crt_http_message_new_from_blob($blob);
|
||||
}
|
||||
|
||||
function http_message_to_blob($message) {
|
||||
return self::$impl->aws_crt_http_message_to_blob($message);
|
||||
}
|
||||
|
||||
function http_message_release($message) {
|
||||
self::$impl->aws_crt_http_message_release($message);
|
||||
}
|
||||
@@ -267,6 +290,10 @@ final class CRT {
|
||||
self::$impl->aws_crt_signing_config_aws_set_expiration_in_seconds($signing_config, $expiration_in_seconds);
|
||||
}
|
||||
|
||||
function signing_config_aws_set_date($signing_config, $timestamp) {
|
||||
self::$impl->aws_crt_signing_config_aws_set_date($signing_config, $timestamp);
|
||||
}
|
||||
|
||||
function signable_new_from_http_request($http_message) {
|
||||
return self::$impl->aws_crt_signable_new_from_http_request($http_message);
|
||||
}
|
||||
@@ -283,6 +310,15 @@ final class CRT {
|
||||
self::$impl->aws_crt_signable_release($signable);
|
||||
}
|
||||
|
||||
function signing_result_release($signing_result) {
|
||||
self::$impl->aws_crt_signing_result_release($signing_result);
|
||||
}
|
||||
|
||||
function signing_result_apply_to_http_request($signing_result, $http_message) {
|
||||
return self::$impl->aws_crt_signing_result_apply_to_http_request(
|
||||
$signing_result, $http_message);
|
||||
}
|
||||
|
||||
function sign_request_aws($signable, $signing_config, $on_complete) {
|
||||
return self::$impl->aws_crt_sign_request_aws($signable, $signing_config, $on_complete);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,10 @@ abstract class Message extends NativeResource {
|
||||
parent::__destruct();
|
||||
}
|
||||
|
||||
public function toBlob() {
|
||||
return self::$crt->http_message_to_blob($this->native);
|
||||
}
|
||||
|
||||
protected static function marshall($msg) {
|
||||
$buf = "";
|
||||
$buf .= Encoding::encodeString($msg->method);
|
||||
|
||||
Reference in New Issue
Block a user