1
1
<?php
2
2
3
- # WORK IN PROGRESS. EVERYTHING WILL CHANGE.
4
- # Derived from https://github.com/avi-wish/aws4-signature-php
3
+ # This was originally derived from https://github.com/avi-wish/aws4-signature-php
5
4
6
5
# If you need to verify / sanity check things against a known-working implementation:
7
6
# https://github.com/aaronland/go-aws-auth?tab=readme-ov-file#aws-sign-request
8
7
9
- function aws_signer_v4_execute_request ($ http_method , $ uri , $ region , $ service , $ access_key , $ secret_key , $ security_token = "" , $ data ){
8
+ function aws_signer_v4_execute_request ($ http_method , $ uri , $ region , $ service , $ creds , $ data ){
10
9
11
- $ headers = aws_signer_v4_headers ($ http_method , $ uri , $ region , $ service , $ access_key , $ secret_key , $ security_token );
10
+ $ headers = aws_signer_v4_headers ($ http_method , $ uri , $ region , $ service , $ creds , $ data );
12
11
13
- switch (strtouppper ($ http_method )){
12
+ // START OF for reasons I do not understand
13
+ // there is something about using the lib_http methods (below) that makes AWS sad
14
+ // and return "403 Forbidden" errors. For the sake of expediency we are just going
15
+ // to call the native curl functions until I can figure out what is going on.
16
+
17
+ $ ch = curl_init ();
18
+
19
+ curl_setopt_array ($ ch , array (
20
+ CURLOPT_URL => $ uri ,
21
+ CURLOPT_RETURNTRANSFER => true ,
22
+ CURLOPT_FOLLOWLOCATION => true ,
23
+ CURLOPT_TIMEOUT => 30 ,
24
+ CURLOPT_POST => true ,
25
+ CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1 ,
26
+ CURLOPT_CUSTOMREQUEST => $ http_method ,
27
+ CURLOPT_POSTFIELDS => $ data ,
28
+ CURLOPT_VERBOSE => 0 ,
29
+ CURLOPT_SSL_VERIFYHOST => 1 ,
30
+ CURLOPT_SSL_VERIFYPEER => 1 ,
31
+ CURLOPT_HEADER => false ,
32
+ CURLINFO_HEADER_OUT =>true ,
33
+ CURLOPT_HTTPHEADER => $ headers ,
34
+ ));
35
+
36
+ $ rsp = curl_exec ($ ch );
37
+ $ rsp_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
38
+
39
+ curl_close ($ ch );
40
+
41
+ if (($ rsp_code >= 200 ) && ($ rsp_code <= 299 )){
42
+ return array ("ok " => 1 , "body " => $ rsp );
43
+ }
44
+
45
+ $ err = curl_error ($ ch );
46
+
47
+ if ($ err != "" ){
48
+ $ rsp = $ err ;
49
+ }
50
+
51
+ return array ("ok " => 0 , "error " => $ rsp );
52
+
53
+ // END OF for reasons I do not understand
54
+
55
+ /*
56
+ switch (strtoupper($http_method)){
14
57
case "GET":
15
58
return http_get($uri, $headers);
16
59
break;
@@ -27,15 +70,18 @@ function aws_signer_v4_execute_request($http_method, $uri, $region, $service, $a
27
70
return array("ok" => 0, "error" => "Unsupported method");
28
71
break;
29
72
}
73
+ */
30
74
}
31
75
32
- // function aws_signer_v4_headers($host, $uri, $requestUrl, $accessKey, $secretKey, $securityToken, $region, $service, $httpRequestMethod, $data, $debug = FALSE){
33
-
34
- function aws_signer_v4_headers ($ http_method , $ uri , $ region , $ service , $ access_key , $ secret_key , $ security_token , $ debug =FALSE ){
76
+ function aws_signer_v4_headers ($ http_method , $ uri , $ region , $ service , $ creds , $ data , $ debug =FALSE ){
35
77
36
78
$ host = parse_url ($ uri , PHP_URL_HOST );
37
79
$ path = parse_url ($ uri , PHP_URL_PATH );
38
80
$ query = parse_url ($ uri , PHP_URL_QUERY );
81
+
82
+ $ access_key = $ creds ["access_key " ];
83
+ $ secret_key = $ creds ["secret_key " ];
84
+ $ security_token = $ creds ["security_token " ];
39
85
40
86
$ headers_to_sign = array (
41
87
"content-length " ,
@@ -58,7 +104,7 @@ function aws_signer_v4_headers($http_method, $uri, $region, $service, $access_ke
58
104
$ dt = new DateTime ('UTC ' );
59
105
$ req_date = $ dt ->format ('Ymd ' );
60
106
$ req_datetime = $ dt ->format ('Ymd\THis\Z ' );
61
-
107
+
62
108
// Create signing key
63
109
$ k_secret = $ secret_key ;
64
110
$ k_date = hash_hmac ($ php_algorithm , $ req_date , "AWS4 {$ k_secret }" , true );
@@ -100,20 +146,20 @@ function aws_signer_v4_headers($http_method, $uri, $region, $service, $access_ke
100
146
}
101
147
102
148
$ canonical_request_hashed = strtolower (bin2hex (hash ($ php_algorithm , $ canonical_request_str , true )));
103
-
149
+
104
150
// Create scope
105
151
$ credential_scope = array ();
106
152
$ credential_scope [] = $ req_date ;
107
153
$ credential_scope [] = $ region ;
108
154
$ credential_scope [] = $ service ;
109
155
$ credential_scope [] = $ termination_string ;
110
- $ credential_scopeStr = implode ('/ ' , $ credential_scope );
156
+ $ credential_scope_str = implode ('/ ' , $ credential_scope );
111
157
112
158
// Create string to signing
113
159
$ to_sign = array ();
114
160
$ to_sign [] = $ algorithm ;
115
161
$ to_sign [] = $ req_datetime ;
116
- $ to_sign [] = $ credential_scopeStr ;
162
+ $ to_sign [] = $ credential_scope_str ;
117
163
$ to_sign [] = $ canonical_request_hashed ;
118
164
$ to_sign_str = implode ("\n" , $ to_sign );
119
165
@@ -130,7 +176,7 @@ function aws_signer_v4_headers($http_method, $uri, $region, $service, $access_ke
130
176
131
177
// Create authorization header
132
178
$ auth_header = array ();
133
- $ auth_header [] = 'Credential= ' . $ access_key . '/ ' . $ credential_scopeStr ;
179
+ $ auth_header [] = 'Credential= ' . $ access_key . '/ ' . $ credential_scope_str ;
134
180
$ auth_header [] = 'SignedHeaders= ' . $ signed_headers ;
135
181
$ auth_header [] = 'Signature= ' . ($ signature );
136
182
$ auth_header_str = $ algorithm . ' ' . implode (', ' , $ auth_header );
0 commit comments