@@ -226,18 +226,27 @@ private function _curlRequest($method, $absUrl, $headers, $params)
226
226
$ curl = curl_init ();
227
227
$ method = strtolower ($ method );
228
228
$ opts = array ();
229
+ $ requestSignature = NULL ;
229
230
if ($ method == 'get ' ) {
230
231
$ opts [CURLOPT_HTTPGET ] = 1 ;
231
232
if (count ($ params ) > 0 ) {
232
233
$ encoded = self ::encode ($ params );
233
234
$ absUrl = "$ absUrl? $ encoded " ;
234
235
}
235
- } elseif ($ method == 'post ' ) {
236
- $ opts [CURLOPT_POST ] = 1 ;
237
- $ opts [CURLOPT_POSTFIELDS ] = json_encode ($ params );
238
- } elseif ($ method == 'put ' ) {
239
- $ opts [CURLOPT_CUSTOMREQUEST ] = 'PUT ' ;
240
- $ opts [CURLOPT_POSTFIELDS ] = json_encode ($ params );
236
+ } elseif ($ method == 'post ' || $ method == 'put ' ) {
237
+ if ($ method == 'post ' ) {
238
+ $ opts [CURLOPT_POST ] = 1 ;
239
+ } else {
240
+ $ opts [CURLOPT_CUSTOMREQUEST ] = 'PUT ' ;
241
+ }
242
+ $ rawRequestBody = json_encode ($ params );
243
+ $ opts [CURLOPT_POSTFIELDS ] = $ rawRequestBody ;
244
+ if ($ this ->privateKey ()) {
245
+ $ signResult = openssl_sign ($ rawRequestBody , $ requestSignature , $ this ->privateKey (), 'sha256 ' );
246
+ if (!$ signResult ) {
247
+ throw new Error \Api ("Generate signature failed " );
248
+ }
249
+ }
241
250
} elseif ($ method == 'delete ' ) {
242
251
$ opts [CURLOPT_CUSTOMREQUEST ] = 'DELETE ' ;
243
252
if (count ($ params ) > 0 ) {
@@ -248,6 +257,10 @@ private function _curlRequest($method, $absUrl, $headers, $params)
248
257
throw new Error \Api ("Unrecognized method $ method " );
249
258
}
250
259
260
+ if ($ requestSignature ) {
261
+ $ headers [] = 'Pingplusplus-Signature: ' . base64_encode ($ requestSignature );
262
+ }
263
+
251
264
$ absUrl = Util \Util::utf8 ($ absUrl );
252
265
$ opts [CURLOPT_URL ] = $ absUrl ;
253
266
$ opts [CURLOPT_RETURNTRANSFER ] = true ;
@@ -329,4 +342,18 @@ private function caBundle()
329
342
{
330
343
return dirname (__FILE__ ) . '/../data/ca-certificates.crt ' ;
331
344
}
345
+
346
+ private function privateKey ()
347
+ {
348
+ if (!Pingpp::$ privateKey ) {
349
+ if (!Pingpp::$ privateKeyPath ) {
350
+ return NULL ;
351
+ }
352
+ if (!file_exists (Pingpp::$ privateKeyPath )) {
353
+ throw new Error \Api ('Private key file not found at: ' . Pingpp::$ privateKeyPath );
354
+ }
355
+ Pingpp::$ privateKey = file_get_contents (Pingpp::$ privateKeyPath );
356
+ }
357
+ return Pingpp::$ privateKey ;
358
+ }
332
359
}
0 commit comments