Skip to content

Commit

Permalink
resolved conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
nixonsam committed May 28, 2020
2 parents 24be2b7 + e80809b commit d2801f4
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 6 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# Change Log

## [v4.8.0](https://github.com/plivo/plivo-php/releases/tag/v4.8.0) - 2020-05-04
## [v4.9.0](https://github.com/plivo/plivo-php/releases/tag/v4.9.0) - 2020-05-28
- Add JWT helper functions.

## [v4.8.1](https://github.com/plivo/plivo-php/releases/tag/v4.8.1) - 2020-05-28
- Fix Create Endpoint response.

## [v4.8.0](https://github.com/plivo/plivo-php/releases/tag/v4.8.0) - 2020-04-29
- Add V3 signature helper functions.

## [v4.7.1](https://github.com/plivo/plivo-php/releases/tag/v4.7.1) - 2020-04-13
- Fix MMS media_urls response.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ The Plivo PHP SDK makes it simpler to integrate communications into your PHP app

- To install a **specific release**, run the following command in the project directory:

$ composer require plivo/plivo-php:4.8.0
$ composer require plivo/plivo-php:4.9.0

- To test the features in the **beta release**, run the following command in the project directory:

Expand Down
6 changes: 3 additions & 3 deletions src/Plivo/Resources/Endpoint/EndpointCreateReponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class EndpointCreateReponse extends ResponseUpdate
/**
* @var string The username of the endpoint
*/
protected $username;
public $username;
/**
* @var string The friendly name of the endpoint
*/
protected $alias;
public $alias;
/**
* @var string The ID of the endpoint
*/
protected $endpointId;
public $endpointId;

/**
* EndpointCreateReponse constructor.
Expand Down
163 changes: 163 additions & 0 deletions src/Plivo/Util/v3SignatureValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?php


namespace Plivo\Util;

use Plivo\Exceptions\PlivoValidationException;

class v3SignatureValidation
{

private static function stringifyParams($params)
{
foreach ($params as $key => $value)
{
$params[$key] = strval($value);
}
return $params;
}

private static function getMapFromQuery($queryString)
{
$queryMap = array();
if ($queryString == null) {
return $queryMap;
}
$queryArray = explode("&", $queryString);
foreach ($queryArray as $param)
{
$keyValue = explode("=", $param);
$key = $keyValue[0];
$value = $keyValue[1];
if (array_key_exists($key, $queryMap))
{
array_push($queryMap[$key], $value);
}
else
{
$queryMap[$key] = array($value);
}
}
return $queryMap;
}

private static function getSortedQueryString($queryMap)
{
$queryString = array();
ksort($queryMap, SORT_NATURAL);
foreach ($queryMap as $key => $value)
{
if (gettype($value) === "array")
{
sort($value, SORT_NATURAL);
foreach ($value as $val)
{
array_push($queryString, strval($key)."=".strval($val));
}
}
else
{
array_push($queryString, strval($key)."=".strval($value));
}
}
return implode("&", $queryString);
}

private static function getSortedParamsString($params)
{
$keys = array_keys($params);
sort($keys, SORT_NATURAL);
$paramsString = array();

foreach ($keys as $key)
{
$value = $params[$key];
if (gettype($value) === "array")
{
$value = sort($value, SORT_NATURAL);
foreach ($value as $val)
{
array_push($paramsString, strval($key).strval($val));
}
}
else
{
array_push($paramsString, strval($key).strval($value));
}
}
return implode("", $paramsString);
}

private static function constructGetUrl($uri, $params, $emptyPostParams=true)
{
$parsedURI = parse_url($uri);
$baseURL = $parsedURI['scheme'].'://'.$parsedURI['host'];
if (isset($parsedURI['port'])) {
$baseURL .= ':'.$parsedURI['port'];
}
if (isset($parsedURI['path'])) {
$baseURL .= $parsedURI['path'];
}
$queryString = $parsedURI['query'];
$params = array_merge_recursive($params, self::getMapFromQuery($queryString));
$queryParams = self::getSortedQueryString($params);
if (strlen($queryParams) > 0 or !$emptyPostParams)
{
$baseURL .= '?'.$queryParams;
}
if (strlen($queryParams) > 0 and !$emptyPostParams)
{
$baseURL .= '.';
}
return $baseURL;
}

private static function constructPostUrl($uri, $params)
{

$baseURL = self::constructGetUrl($uri, array(),count($params) > 0 ? false : true);
return $baseURL.self::getSortedParamsString($params);
}

private static function getSignatureV3($authToken, $baseURL, $nonce)
{
$baseURL = utf8_encode($baseURL.".".$nonce);
$hmac = hash_hmac('SHA256', $baseURL, $authToken, true);
return base64_encode($hmac);
}

/**
* Return a recording instance
*
* @param string $method
* @param string $uri
* @param string $nonce
* @param string $auth_token
* @param string $v3_signature
* @param array $params
* @return boolean
* @throws PlivoValidationException
*/
public static function validateV3Signature($method, $uri, $nonce, $auth_token, $v3_signature, $params=[])
{
$auth_token = utf8_encode($auth_token);
$nonce = utf8_encode($nonce);
$v3_signature = utf8_encode($v3_signature);
$uri = utf8_encode($uri);
$params = self::stringifyParams($params);
if ($method == 'GET')
{
$base_url = self::constructGetUrl($uri, $params);
}
elseif ($method == 'POST')
{
$base_url = self::constructPostUrl($uri, $params);
}
else
{
throw new PlivoValidationException('method not allowed for signature validation');
}
$signature = self::getSignatureV3($auth_token, $base_url, $nonce);
return in_array($signature, explode(',', $v3_signature));
}
}
2 changes: 1 addition & 1 deletion src/Plivo/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Version
/**
* @const int PHP helper library minor version number
*/
const MINOR = 8;
const MINOR = 9;
/**
* @const int PHP helper library patch number
*/
Expand Down

0 comments on commit d2801f4

Please sign in to comment.