From 4e87b65ba2826f1ddf4a4ec2785c5965fb6635bb Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Sun, 30 Aug 2020 00:34:03 +0200 Subject: [PATCH 1/9] Inline variable --- src/Escher/RequestHelper.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Escher/RequestHelper.php b/src/Escher/RequestHelper.php index 982b696..289fb22 100644 --- a/src/Escher/RequestHelper.php +++ b/src/Escher/RequestHelper.php @@ -68,8 +68,7 @@ public function getCurrentUrl() { $scheme = (array_key_exists('HTTPS', $this->serverVars) && $this->serverVars["HTTPS"] == "on") ? 'https' : 'http'; $host = $this->getServerHost(); - $res = "$scheme://$host" . $this->serverVars["REQUEST_URI"]; - return $res; + return "$scheme://$host" . $this->serverVars["REQUEST_URI"]; } private function process(array $serverVars) From 8daa34519f1b2404a4ec406673fbddb62dab23c6 Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Sun, 30 Aug 2020 00:35:10 +0200 Subject: [PATCH 2/9] Replace qualifier with an import --- src/Escher/AuthElements.php | 6 ++++-- src/Escher/Escher.php | 16 +++++++++------- src/Escher/Provider.php | 3 ++- src/Escher/Signer.php | 6 ++++-- src/Escher/Utils.php | 6 ++++-- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/Escher/AuthElements.php b/src/Escher/AuthElements.php index 89070b4..09f6086 100644 --- a/src/Escher/AuthElements.php +++ b/src/Escher/AuthElements.php @@ -2,6 +2,8 @@ namespace Escher; +use DateTime; +use DateTimeZone; class AuthElements { @@ -13,7 +15,7 @@ class AuthElements private $host; private $isFromHeaders; - public function __construct(array $elementParts, $accessKeyId, $shortDate, $credentialScope, \DateTime $dateTime, $host, $isFromHeaders) + public function __construct(array $elementParts, $accessKeyId, $shortDate, $credentialScope, DateTime $dateTime, $host, $isFromHeaders) { $this->elementParts = $elementParts; $this->accessKeyId = $accessKeyId; @@ -47,7 +49,7 @@ public static function parseFromHeaders(array $headerList, $authHeaderKey, $date $dateTime = Utils::parseLongDate($headerList[strtolower($dateHeaderKey)]); } else { try { - $dateTime = new \DateTime($headerList[strtolower($dateHeaderKey)], new \DateTimeZone('GMT')); + $dateTime = new DateTime($headerList[strtolower($dateHeaderKey)], new DateTimeZone('GMT')); } catch (Exception $ex) { throw new Exception('Date header is invalid, the expected format is Wed, 04 Nov 2015 09:20:22 GMT', Exception::CODE_FORMAT_INVALID_DATE_HEADER_GMT); } diff --git a/src/Escher/Escher.php b/src/Escher/Escher.php index b089853..f72e734 100644 --- a/src/Escher/Escher.php +++ b/src/Escher/Escher.php @@ -2,6 +2,8 @@ namespace Escher; +use DateTime; +use DateTimeZone; class Escher { @@ -36,11 +38,11 @@ public static function create($credentialScope) } /** - * @return \DateTime + * @return DateTime */ private static function now() { - return new \DateTime('now', new \DateTimeZone('GMT')); + return new DateTime('now', new DateTimeZone('GMT')); } /** @@ -68,7 +70,7 @@ public function authenticate($keyDB, array $serverVars = null, $requestBody = nu return $authElements->getAccessKeyId(); } - public function presignUrl($accessKeyId, $secretKey, $url, $expires = Escher::DEFAULT_EXPIRES, \DateTime $date = null) + public function presignUrl($accessKeyId, $secretKey, $url, $expires = Escher::DEFAULT_EXPIRES, DateTime $date = null) { $date = $date ? $date : self::now(); $url = $this->appendSigningParams($accessKeyId, $url, $date, $expires); @@ -90,7 +92,7 @@ public function presignUrl($accessKeyId, $secretKey, $url, $expires = Escher::DE return $url; } - public function signRequest($accessKeyId, $secretKey, $method, $url, $requestBody, $headerList = array(), $headersToSign = array(), \DateTime $date = null) + public function signRequest($accessKeyId, $secretKey, $method, $url, $requestBody, $headerList = array(), $headersToSign = array(), DateTime $date = null) { $date = $date ? $date : self::now(); list($host, $path, $query) = $this->parseUrl($url); @@ -133,7 +135,7 @@ private function generateParamName($param) return 'X-' . $this->vendorKey . '-' . $param; } - public function getSignature($secretKey, \DateTime $date, $method, $url, $requestBody, $headerList, $signedHeaders) + public function getSignature($secretKey, DateTime $date, $method, $url, $requestBody, $headerList, $signedHeaders) { list(, $path, $query) = $this->parseUrl($url); return $this->calculateSignature($secretKey, $date, $method, $path, $query, $requestBody, $headerList, $signedHeaders); @@ -149,7 +151,7 @@ private function parseUrl($url) return array($host, $path, $query); } - private function toLongDate(\DateTime $date) + private function toLongDate(DateTime $date) { return $date->format(Escher::LONG_DATE); } @@ -174,7 +176,7 @@ private function addGetParameter($url, $key, $value) * @param $date * @return string */ - private function fullCredentialScope(\DateTime $date) + private function fullCredentialScope(DateTime $date) { return $date->format(Escher::SHORT_DATE) . "/" . $this->credentialScope; } diff --git a/src/Escher/Provider.php b/src/Escher/Provider.php index 462f6ee..27568cf 100644 --- a/src/Escher/Provider.php +++ b/src/Escher/Provider.php @@ -2,6 +2,7 @@ namespace Escher; +use ArrayAccess; class Provider { @@ -49,7 +50,7 @@ public function getEscherSecret() } /** - * @return \ArrayAccess|array + * @return ArrayAccess|array */ public function getKeyDB() { diff --git a/src/Escher/Signer.php b/src/Escher/Signer.php index a62d0b0..2fe71ae 100644 --- a/src/Escher/Signer.php +++ b/src/Escher/Signer.php @@ -2,13 +2,15 @@ namespace Escher; +use DateTime; +use DateTimeZone; class Signer { - public static function createStringToSign($credentialScope, $canonicalRequestString, \DateTime $date, $hashAlgo, $algoPrefix) + public static function createStringToSign($credentialScope, $canonicalRequestString, DateTime $date, $hashAlgo, $algoPrefix) { $date = clone $date; - $date->setTimezone(new \DateTimeZone("GMT")); + $date->setTimezone(new DateTimeZone("GMT")); $formattedDate = $date->format(Escher::LONG_DATE); $scope = substr($formattedDate,0, 8) . "/" . $credentialScope; $lines = array(); diff --git a/src/Escher/Utils.php b/src/Escher/Utils.php index d4372bb..d9ef424 100644 --- a/src/Escher/Utils.php +++ b/src/Escher/Utils.php @@ -2,6 +2,8 @@ namespace Escher; +use DateTime; +use DateTimeZone; class Utils { @@ -11,9 +13,9 @@ public static function parseLongDate($dateString) throw new Exception('Date header is invalid, the expected format is 20151104T092022Z', Exception::CODE_FORMAT_INVALID_DATE_HEADER); } if (!self::advancedDateTimeFunctionsAvailable()) { - return new \DateTime($dateString, new \DateTimeZone('GMT')); + return new DateTime($dateString, new DateTimeZone('GMT')); } - return \DateTime::createFromFormat('Ymd\THisT', $dateString, new \DateTimeZone('GMT')); + return DateTime::createFromFormat('Ymd\THisT', $dateString, new DateTimeZone('GMT')); } public static function keysToLower($array) From 2ffab20d265359e6af3fd646d2baf76facd518bb Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Sun, 30 Aug 2020 00:41:01 +0200 Subject: [PATCH 3/9] Fix unhandled exception --- src/Escher/AuthElements.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Escher/AuthElements.php b/src/Escher/AuthElements.php index 09f6086..2f2a9ff 100644 --- a/src/Escher/AuthElements.php +++ b/src/Escher/AuthElements.php @@ -50,7 +50,7 @@ public static function parseFromHeaders(array $headerList, $authHeaderKey, $date } else { try { $dateTime = new DateTime($headerList[strtolower($dateHeaderKey)], new DateTimeZone('GMT')); - } catch (Exception $ex) { + } catch (\Exception $ex) { throw new Exception('Date header is invalid, the expected format is Wed, 04 Nov 2015 09:20:22 GMT', Exception::CODE_FORMAT_INVALID_DATE_HEADER_GMT); } } From 550144bc78eb5fc96d4e52e54a19c0cde2a7861f Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Sun, 30 Aug 2020 00:48:42 +0200 Subject: [PATCH 4/9] Fix code style issues --- src/Escher/AuthElements.php | 2 +- src/Escher/Escher.php | 10 +++++----- src/Escher/RequestCanonicalizer.php | 26 +++++++++++++++----------- src/Escher/RequestHelper.php | 10 +++++----- src/Escher/Signer.php | 18 +++++++++--------- 5 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/Escher/AuthElements.php b/src/Escher/AuthElements.php index 2f2a9ff..0399527 100644 --- a/src/Escher/AuthElements.php +++ b/src/Escher/AuthElements.php @@ -178,7 +178,7 @@ public function validateSignature(RequestHelper $helper, Escher $escher, $keyDB, $provided = $this->getSignature(); if ($calculated !== $provided) { - throw new Exception("The signatures do not match", Exception::CODE_SIGNATURE_NOT_MATCH); + throw new Exception('The signatures do not match', Exception::CODE_SIGNATURE_NOT_MATCH); } } diff --git a/src/Escher/Escher.php b/src/Escher/Escher.php index f72e734..95c2a6d 100644 --- a/src/Escher/Escher.php +++ b/src/Escher/Escher.php @@ -16,7 +16,7 @@ class Escher const DEFAULT_EXPIRES = 86400; const ISO8601 = 'Ymd\THis\Z'; const LONG_DATE = self::ISO8601; - const SHORT_DATE = "Ymd"; + const SHORT_DATE = 'Ymd'; const UNSIGNED_PAYLOAD = 'UNSIGNED-PAYLOAD'; private $credentialScope; @@ -83,7 +83,7 @@ public function presignUrl($accessKeyId, $secretKey, $url, $expires = Escher::DE 'GET', $path, $query, - Escher::UNSIGNED_PAYLOAD, + self::UNSIGNED_PAYLOAD, array('host' => $host), (array('host')) ); @@ -153,7 +153,7 @@ private function parseUrl($url) private function toLongDate(DateTime $date) { - return $date->format(Escher::LONG_DATE); + return $date->format(self::LONG_DATE); } private function addGetParameter($url, $key, $value) @@ -178,7 +178,7 @@ private function addGetParameter($url, $key, $value) */ private function fullCredentialScope(DateTime $date) { - return $date->format(Escher::SHORT_DATE) . "/" . $this->credentialScope; + return $date->format(self::SHORT_DATE) . '/' . $this->credentialScope; } private function generateAuthHeader($secretKey, $accessKeyId, $authHeaderKey, $date, $method, $path, $query, $requestBody, array $headerList, array $headersToSign) @@ -187,7 +187,7 @@ private function generateAuthHeader($secretKey, $accessKeyId, $authHeaderKey, $d $authHeaderValue = Signer::createAuthHeader( $signature, $this->fullCredentialScope($date), - implode(";", $headersToSign), + implode(';', $headersToSign), $this->hashAlgo, $this->algoPrefix, $accessKeyId diff --git a/src/Escher/RequestCanonicalizer.php b/src/Escher/RequestCanonicalizer.php index 4e8e493..c1b5c74 100644 --- a/src/Escher/RequestCanonicalizer.php +++ b/src/Escher/RequestCanonicalizer.php @@ -17,7 +17,7 @@ public static function canonicalize($method, $requestUri, $payload, $rawHeaders, $lines = array_merge($lines, self::canonicalizeHeaders($rawHeaders, $headersToSign)); $lines[] = ''; - $lines[] = implode(";", $headersToSign); + $lines[] = implode(';', $headersToSign); $lines[] = hash($hashAlgo, $payload); @@ -26,24 +26,26 @@ public static function canonicalize($method, $requestUri, $payload, $rawHeaders, public static function urlEncodeQueryString($query) { - if (empty($query)) return ""; - $pairs = explode("&", $query); + if (empty($query)) { + return ''; + } + $pairs = explode('&', $query); $encodedParts = array(); foreach ($pairs as $pair) { - $keyValues = array_pad(explode("=", $pair), 2, ''); - if (strpos($keyValues[0], " ") !== false) { - $keyValues[0] = substr($keyValues[0], 0, strpos($keyValues[0], " ")); - $keyValues[1] = ""; + $keyValues = array_pad(explode('=', $pair), 2, ''); + if (strpos($keyValues[0], ' ') !== false) { + $keyValues[0] = substr($keyValues[0], 0, strpos($keyValues[0], ' ')); + $keyValues[1] = ''; } $keyValues[0] = urldecode($keyValues[0]); $keyValues[1] = urldecode($keyValues[1]); - $encodedParts[] = implode("=", array( + $encodedParts[] = implode('=', array( self::rawUrlEncode(str_replace('+', ' ', $keyValues[0])), self::rawUrlEncode(str_replace('+', ' ', $keyValues[1])), )); } sort($encodedParts); - return implode("&", $encodedParts); + return implode('&', $encodedParts); } private static function normalizePath($path) @@ -59,9 +61,11 @@ private static function normalizePath($path) $path = implode('/', $path); $path = str_replace('./', '', $path); - $path = str_replace("//", "/", $path); + $path = str_replace('//', '/', $path); - if (empty($path)) return "/"; + if (empty($path)) { + return '/'; + } return $path; } diff --git a/src/Escher/RequestHelper.php b/src/Escher/RequestHelper.php index 289fb22..9a25ebb 100644 --- a/src/Escher/RequestHelper.php +++ b/src/Escher/RequestHelper.php @@ -66,9 +66,9 @@ public function getHeaderList() public function getCurrentUrl() { - $scheme = (array_key_exists('HTTPS', $this->serverVars) && $this->serverVars["HTTPS"] == "on") ? 'https' : 'http'; + $scheme = (array_key_exists('HTTPS', $this->serverVars) && $this->serverVars['HTTPS'] == 'on') ? 'https' : 'http'; $host = $this->getServerHost(); - return "$scheme://$host" . $this->serverVars["REQUEST_URI"]; + return "$scheme://$host" . $this->serverVars['REQUEST_URI']; } private function process(array $serverVars) @@ -89,7 +89,7 @@ private function getContentType() public function getServerHost() { - return $this->normalizeHost($this->serverVars['SERVER_NAME'], $this->serverVars["SERVER_PORT"]); + return $this->normalizeHost($this->serverVars['SERVER_NAME'], $this->serverVars['SERVER_PORT']); } /** @@ -114,13 +114,13 @@ private function normalizeHost($host, $port) if (is_null($port) || $this->isDefaultPort($port)) { return $host; } else { - return $host . ":" . $port; + return $host . ':' . $port; } } private function isDefaultPort($port) { - $defaultPort = isset($this->serverVars["HTTPS"]) && $this->serverVars["HTTPS"] === "on" ? '443' : '80'; + $defaultPort = isset($this->serverVars['HTTPS']) && $this->serverVars['HTTPS'] === 'on' ? '443' : '80'; return $port == $defaultPort; } } diff --git a/src/Escher/Signer.php b/src/Escher/Signer.php index 2fe71ae..dc1498a 100644 --- a/src/Escher/Signer.php +++ b/src/Escher/Signer.php @@ -10,11 +10,11 @@ class Signer public static function createStringToSign($credentialScope, $canonicalRequestString, DateTime $date, $hashAlgo, $algoPrefix) { $date = clone $date; - $date->setTimezone(new DateTimeZone("GMT")); + $date->setTimezone(new DateTimeZone('GMT')); $formattedDate = $date->format(Escher::LONG_DATE); - $scope = substr($formattedDate,0, 8) . "/" . $credentialScope; + $scope = substr($formattedDate,0, 8) . '/' . $credentialScope; $lines = array(); - $lines[] = $algoPrefix . "-HMAC-" . strtoupper($hashAlgo); + $lines[] = $algoPrefix . '-HMAC-' . strtoupper($hashAlgo); $lines[] = $formattedDate; $lines[] = $scope; $lines[] = hash($hashAlgo, $canonicalRequestString); @@ -24,7 +24,7 @@ public static function createStringToSign($credentialScope, $canonicalRequestStr public static function calculateSigningKey($secret, $credentialScope, $hashAlgo, $algoPrefix) { $key = $algoPrefix . $secret; - $credentials = explode("/", $credentialScope); + $credentials = explode('/', $credentialScope); foreach ($credentials as $data) { $key = hash_hmac($hashAlgo, $data, $key, true); } @@ -33,12 +33,12 @@ public static function calculateSigningKey($secret, $credentialScope, $hashAlgo, public static function createAuthHeader($signature, $credentialScope, $signedHeaders, $hashAlgo, $algoPrefix, $accessKey) { - return $algoPrefix . "-HMAC-" . strtoupper($hashAlgo) - . " Credential=" - . $accessKey . "/" + return $algoPrefix . '-HMAC-' . strtoupper($hashAlgo) + . ' Credential=' + . $accessKey . '/' . $credentialScope - . ", SignedHeaders=" . $signedHeaders - . ", Signature=" . $signature; + . ', SignedHeaders=' . $signedHeaders + . ', Signature=' . $signature; } public static function createSignature($stringToSign, $signerKey, $hashAlgo) From c521c883923e67e2ac694c32494e361a16bb9476 Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Sun, 30 Aug 2020 00:49:53 +0200 Subject: [PATCH 5/9] Remove redundant else keyword --- src/Escher/Escher.php | 5 ++--- src/Escher/RequestHelper.php | 7 ++++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Escher/Escher.php b/src/Escher/Escher.php index 95c2a6d..c00c437 100644 --- a/src/Escher/Escher.php +++ b/src/Escher/Escher.php @@ -167,9 +167,8 @@ private function addGetParameter($url, $key, $value) if ($fragmentPosition === false) { return $url . $glue . $key . '=' . urlencode($value); } - else { - return substr_replace($url, ($glue . $key . '=' . urlencode($value)), $fragmentPosition, 0); - } + + return substr_replace($url, ($glue . $key . '=' . urlencode($value)), $fragmentPosition, 0); } /** diff --git a/src/Escher/RequestHelper.php b/src/Escher/RequestHelper.php index 9a25ebb..ae97ada 100644 --- a/src/Escher/RequestHelper.php +++ b/src/Escher/RequestHelper.php @@ -34,7 +34,8 @@ public function getAuthElements($vendorKey, $algoPrefix) $queryParams = $this->getQueryParams(); if (isset($headerList[strtolower($this->authHeaderKey)])) { return AuthElements::parseFromHeaders($headerList, $this->authHeaderKey, $this->dateHeaderKey, $algoPrefix); - } else if($this->getRequestMethod() === 'GET' && isset($queryParams[$this->paramKey($vendorKey, 'Signature')])) { + } + if($this->getRequestMethod() === 'GET' && isset($queryParams[$this->paramKey($vendorKey, 'Signature')])) { return AuthElements::parseFromQuery($headerList, $queryParams, $vendorKey, $algoPrefix); } throw new Exception('Escher authentication is missing', Exception::CODE_MISSING_AUTH); @@ -113,9 +114,9 @@ private function normalizeHost($host, $port) { if (is_null($port) || $this->isDefaultPort($port)) { return $host; - } else { - return $host . ':' . $port; } + + return $host . ':' . $port; } private function isDefaultPort($port) From 6edcbb1b661311cbf9a80c68dd9d66f4be8944ee Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Sun, 30 Aug 2020 11:25:35 +0200 Subject: [PATCH 6/9] Replace substr to strpos --- src/Escher/RequestHelper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Escher/RequestHelper.php b/src/Escher/RequestHelper.php index ae97ada..cf84cf8 100644 --- a/src/Escher/RequestHelper.php +++ b/src/Escher/RequestHelper.php @@ -76,7 +76,7 @@ private function process(array $serverVars) { $headerList = array(); foreach ($serverVars as $key => $value) { - if (substr($key, 0, 5) === 'HTTP_') { + if (strpos($key, 'HTTP_') === 0) { $headerList[strtolower(str_replace('_', '-', substr($key, 5)))] = $value; } } From cf1c232f9bc64b26169b760bc8fab97c6176c7c8 Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Sun, 30 Aug 2020 11:27:10 +0200 Subject: [PATCH 7/9] Simplify expression with Elvis operator --- src/Escher/Escher.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Escher/Escher.php b/src/Escher/Escher.php index c00c437..d221af2 100644 --- a/src/Escher/Escher.php +++ b/src/Escher/Escher.php @@ -72,7 +72,7 @@ public function authenticate($keyDB, array $serverVars = null, $requestBody = nu public function presignUrl($accessKeyId, $secretKey, $url, $expires = Escher::DEFAULT_EXPIRES, DateTime $date = null) { - $date = $date ? $date : self::now(); + $date = $date ?: self::now(); $url = $this->appendSigningParams($accessKeyId, $url, $date, $expires); list($host, $path, $query) = $this->parseUrl($url); @@ -94,7 +94,7 @@ public function presignUrl($accessKeyId, $secretKey, $url, $expires = Escher::DE public function signRequest($accessKeyId, $secretKey, $method, $url, $requestBody, $headerList = array(), $headersToSign = array(), DateTime $date = null) { - $date = $date ? $date : self::now(); + $date = $date ?: self::now(); list($host, $path, $query) = $this->parseUrl($url); list($headerList, $headersToSign) = $this->addMandatoryHeaders( $headerList, $headersToSign, $this->dateHeaderKey, $date, $host From 40d7e4abeac45317f3c8353b214bafdb3d170168 Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Sun, 30 Aug 2020 11:28:40 +0200 Subject: [PATCH 8/9] Merge str_replace functions to improve performance --- src/Escher/RequestCanonicalizer.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Escher/RequestCanonicalizer.php b/src/Escher/RequestCanonicalizer.php index c1b5c74..1109ce9 100644 --- a/src/Escher/RequestCanonicalizer.php +++ b/src/Escher/RequestCanonicalizer.php @@ -59,9 +59,7 @@ private static function normalizePath($path) } $path = implode('/', $path); - $path = str_replace('./', '', $path); - - $path = str_replace('//', '/', $path); + $path = str_replace(array('./', '//'), array('', '/'), $path); if (empty($path)) { return '/'; From 470a758c96f8c48fe7136e8b3cc319e0146dbbfe Mon Sep 17 00:00:00 2001 From: David Szigecsan Date: Sun, 30 Aug 2020 11:33:50 +0200 Subject: [PATCH 9/9] Supplement phpDoc comment --- src/Escher/Escher.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Escher/Escher.php b/src/Escher/Escher.php index d221af2..3d8d1ce 100644 --- a/src/Escher/Escher.php +++ b/src/Escher/Escher.php @@ -39,6 +39,7 @@ public static function create($credentialScope) /** * @return DateTime + * @throws \Exception */ private static function now() {