@@ -23,6 +23,7 @@ class RestClient {
23
23
protected $ api_key ;
24
24
protected $ user_agent ;
25
25
protected $ client ;
26
+ protected $ rest_endpoint ;
26
27
27
28
/**
28
29
* TeleSign RestClient instantiation function
@@ -44,9 +45,9 @@ function __construct (
44
45
) {
45
46
$ this ->customer_id = $ customer_id ;
46
47
$ this ->api_key = $ api_key ;
48
+ $ this ->rest_endpoint = $ rest_endpoint ;
47
49
48
50
$ this ->client = new Client ([
49
- "base_uri " => $ rest_endpoint ,
50
51
"timeout " => $ timeout ,
51
52
"proxy " => $ proxy ,
52
53
"handler " => $ handler
@@ -59,6 +60,10 @@ function __construct (
59
60
$ this ->user_agent = "TeleSignSDK/php- $ sdk_version PHP/ $ php_version Guzzle/ $ guzzle_version " ;
60
61
}
61
62
63
+ function setRestEndpoint ($ rest_endpoint ) {
64
+ $ this ->rest_endpoint = $ rest_endpoint ;
65
+ }
66
+
62
67
/**
63
68
* Generates the TeleSign REST API headers used to authenticate requests.
64
69
*
@@ -76,6 +81,8 @@ function __construct (
76
81
* @param string $date The date and time of the request
77
82
* @param string $nonce A unique cryptographic nonce for the request
78
83
* @param string $user_agent User Agent associated with the request
84
+ * @param string $content_type Content-Type to send in header
85
+ * @param string $auth_method Authentication method
79
86
*
80
87
* @return array The TeleSign authentication headers
81
88
*/
@@ -88,7 +95,8 @@ static function generateTelesignHeaders (
88
95
$ date = null ,
89
96
$ nonce = null ,
90
97
$ user_agent = null ,
91
- $ content_type = null
98
+ $ content_type = null ,
99
+ $ auth_method = "HMAC-SHA256 "
92
100
) {
93
101
if (!$ date ) {
94
102
$ date = gmdate ("D, d M Y H:i:s T " );
@@ -101,30 +109,35 @@ static function generateTelesignHeaders (
101
109
if (!$ content_type ) {
102
110
$ content_type = in_array ($ method_name , ["POST " , "PUT " ]) ? "application/x-www-form-urlencoded " : "" ;
103
111
}
104
-
105
- $ auth_method = "HMAC-SHA256 " ;
106
-
107
- $ string_to_sign_builder = [
108
- $ method_name ,
109
- "\n$ content_type " ,
110
- "\n$ date " ,
111
- "\nx-ts-auth-method: $ auth_method " ,
112
- "\nx-ts-nonce: $ nonce "
113
- ];
114
-
115
- if ($ content_type && $ url_encoded_fields ) {
116
- $ string_to_sign_builder [] = "\n$ url_encoded_fields " ;
117
- }
118
112
119
- $ string_to_sign_builder [] = "\n$ resource " ;
120
113
121
- $ string_to_sign = join ("" , $ string_to_sign_builder );
122
-
123
- $ signature = base64_encode (
124
- hash_hmac ("sha256 " , mb_convert_encoding ($ string_to_sign , "UTF-8 " , mb_detect_encoding ($ string_to_sign )), base64_decode ($ api_key ), true )
125
- );
126
- $ authorization = "TSA $ customer_id: $ signature " ;
114
+ if ($ auth_method === "Basic " ) {
115
+ $ credentials = base64_encode ("$ customer_id: $ api_key " );
127
116
117
+ $ authorization = "Basic $ credentials " ;
118
+ } else {
119
+ $ string_to_sign_builder = [
120
+ $ method_name ,
121
+ "\n$ content_type " ,
122
+ "\n$ date " ,
123
+ "\nx-ts-auth-method: $ auth_method " ,
124
+ "\nx-ts-nonce: $ nonce "
125
+ ];
126
+
127
+ if ($ content_type && $ url_encoded_fields ) {
128
+ $ string_to_sign_builder [] = "\n$ url_encoded_fields " ;
129
+ }
130
+
131
+ $ string_to_sign_builder [] = "\n$ resource " ;
132
+
133
+ $ string_to_sign = join ("" , $ string_to_sign_builder );
134
+
135
+ $ signature = base64_encode (
136
+ hash_hmac ("sha256 " , mb_convert_encoding ($ string_to_sign , "UTF-8 " , mb_detect_encoding ($ string_to_sign )), base64_decode ($ api_key ), true )
137
+ );
138
+ $ authorization = "TSA $ customer_id: $ signature " ;
139
+ }
140
+
128
141
$ headers = [
129
142
"Authorization " => $ authorization ,
130
143
"Date " => $ date ,
@@ -203,28 +216,40 @@ function delete (...$args) {
203
216
* @param array $fields Body of query params to perform the HTTP request with
204
217
* @param string $date The date and time of the request
205
218
* @param string $nonce A unique cryptographic nonce for the request
219
+ * @param string $content_type Content-Type to send in header
220
+ * @param string $auth_method Authentication method
206
221
*
207
222
* @return \telesign\sdk\rest\Response The RestClient Response object
208
223
*/
209
- protected function execute ($ method_name , $ resource , $ fields = [], $ date = null , $ nonce = null ) {
210
- $ url_encoded_fields = http_build_query ($ fields , "" , "& " );
224
+ protected function execute ($ method_name , $ resource , $ fields = [], $ date = null , $ nonce = null , $ content_type = null , $ auth_method = "HMAC-SHA256 " ) {
225
+ $ content_is_json = $ content_type === "application/json " ;
226
+
227
+ if ($ content_is_json ) {
228
+ $ form_body = json_encode ($ fields );
229
+ } else {
230
+ $ url_encoded_fields = http_build_query ($ fields , "" , "& " );
231
+ }
232
+
211
233
$ headers = $ this ->generateTelesignHeaders (
212
234
$ this ->customer_id ,
213
235
$ this ->api_key ,
214
236
$ method_name ,
215
237
$ resource ,
216
- $ url_encoded_fields ,
238
+ $ content_is_json ? null : $ url_encoded_fields ,
217
239
$ date ,
218
240
$ nonce ,
219
- $ this ->user_agent
241
+ $ this ->user_agent ,
242
+ $ content_type ,
243
+ $ auth_method
220
244
);
221
245
222
246
$ option = in_array ($ method_name , [ "POST " , "PUT " ]) ? "body " : "query " ;
223
247
224
248
return new Response ($ this ->client ->request ($ method_name , $ resource , [
225
249
"headers " => $ headers ,
226
- $ option => $ url_encoded_fields ,
227
- "http_errors " => false
250
+ $ option => $ content_is_json ? $ form_body : $ url_encoded_fields ,
251
+ "http_errors " => false ,
252
+ "base_uri " => $ this ->rest_endpoint ,
228
253
]));
229
254
}
230
255
0 commit comments