Skip to content

Commit 58c0dfe

Browse files
Pull request #9: Add some features
Merge in SDK/php_telesign from feature/91979 to developer * commit '076b4d461b1d28d2c4616184e6b3175c5ebb464f': Support send JSON
2 parents bba72a9 + 076b4d4 commit 58c0dfe

File tree

2 files changed

+67
-29
lines changed

2 files changed

+67
-29
lines changed

src/rest/RestClient.php

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class RestClient {
2323
protected $api_key;
2424
protected $user_agent;
2525
protected $client;
26+
protected $rest_endpoint;
2627

2728
/**
2829
* TeleSign RestClient instantiation function
@@ -44,9 +45,9 @@ function __construct (
4445
) {
4546
$this->customer_id = $customer_id;
4647
$this->api_key = $api_key;
48+
$this->rest_endpoint = $rest_endpoint;
4749

4850
$this->client = new Client([
49-
"base_uri" => $rest_endpoint,
5051
"timeout" => $timeout,
5152
"proxy" => $proxy,
5253
"handler" => $handler
@@ -59,6 +60,10 @@ function __construct (
5960
$this->user_agent = "TeleSignSDK/php-$sdk_version PHP/$php_version Guzzle/$guzzle_version";
6061
}
6162

63+
function setRestEndpoint($rest_endpoint) {
64+
$this->rest_endpoint = $rest_endpoint;
65+
}
66+
6267
/**
6368
* Generates the TeleSign REST API headers used to authenticate requests.
6469
*
@@ -76,6 +81,8 @@ function __construct (
7681
* @param string $date The date and time of the request
7782
* @param string $nonce A unique cryptographic nonce for the request
7883
* @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
7986
*
8087
* @return array The TeleSign authentication headers
8188
*/
@@ -88,7 +95,8 @@ static function generateTelesignHeaders (
8895
$date = null,
8996
$nonce = null,
9097
$user_agent = null,
91-
$content_type = null
98+
$content_type = null,
99+
$auth_method = "HMAC-SHA256"
92100
) {
93101
if (!$date) {
94102
$date = gmdate("D, d M Y H:i:s T");
@@ -101,30 +109,35 @@ static function generateTelesignHeaders (
101109
if (!$content_type) {
102110
$content_type = in_array($method_name, ["POST", "PUT"]) ? "application/x-www-form-urlencoded" : "";
103111
}
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-
}
118112

119-
$string_to_sign_builder[] = "\n$resource";
120113

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");
127116

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+
128141
$headers = [
129142
"Authorization" => $authorization,
130143
"Date" => $date,
@@ -203,28 +216,40 @@ function delete (...$args) {
203216
* @param array $fields Body of query params to perform the HTTP request with
204217
* @param string $date The date and time of the request
205218
* @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
206221
*
207222
* @return \telesign\sdk\rest\Response The RestClient Response object
208223
*/
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+
211233
$headers = $this->generateTelesignHeaders(
212234
$this->customer_id,
213235
$this->api_key,
214236
$method_name,
215237
$resource,
216-
$url_encoded_fields,
238+
$content_is_json ? null : $url_encoded_fields,
217239
$date,
218240
$nonce,
219-
$this->user_agent
241+
$this->user_agent,
242+
$content_type,
243+
$auth_method
220244
);
221245

222246
$option = in_array($method_name, [ "POST", "PUT" ]) ? "body" : "query";
223247

224248
return new Response($this->client->request($method_name, $resource, [
225249
"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,
228253
]));
229254
}
230255

test/Example.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,17 @@ class Example {
1212
const REFERENCE_ID = "AEBC93B5898342F790E4E19FED41A7DA";
1313
const ACCOUNT_LIFECYCLE_EVENT = "create";
1414

15+
static function objExampleVerification() {
16+
$obj = new \stdClass();
17+
18+
$info_number = new \stdClass();
19+
$info_number->phone_number = self::PHONE_NUMBER;
20+
21+
$obj->recipient = $info_number;
22+
$info_policy = new \stdClass();
23+
$info_policy->method = "sms";
24+
$obj->verification_policy[] = $info_policy;
25+
26+
return $obj;
27+
}
1528
}

0 commit comments

Comments
 (0)