From 7ff4d64153cf432bacfa3ffd70393bea3a930f36 Mon Sep 17 00:00:00 2001 From: Davey Shafik Date: Thu, 31 Aug 2017 23:50:27 -0700 Subject: [PATCH] Trim whitespace on credentials/max-body size Fixes #3 --- CHANGELOG.md | 5 +++++ src/Authentication.php | 12 ++++++++---- tests/AuthenticationTest.php | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fc990f..2c6ce31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +1.0.1 +--- +[01 Sep, 2017] +* Fixed an issue with handling trailing whitespace in credentials (see: #3) + 1.0.0 --- [19 May, 2017] diff --git a/src/Authentication.php b/src/Authentication.php index 9e8fdf2..bf16717 100644 --- a/src/Authentication.php +++ b/src/Authentication.php @@ -305,7 +305,7 @@ public function getPath() /** * Set signing timestamp * - * @param mixed $timestamp + * @param Timestamp|string $timestamp * @return $this */ public function setTimestamp($timestamp = null) @@ -320,7 +320,7 @@ public function setTimestamp($timestamp = null) /** * Set signing nonce * - * @param Nonce $nonce + * @param Nonce|string $nonce * @return $this */ public function setNonce($nonce = null) @@ -352,7 +352,7 @@ public function setHeadersToSign($headers_to_sign) */ public function setMaxBodySize($max_body_size) { - $this->max_body_size = $max_body_size; + $this->max_body_size = trim($max_body_size); return $this; } @@ -366,7 +366,11 @@ public function setMaxBodySize($max_body_size) */ public function setAuth($client_token, $client_secret, $access_token) { - $this->auth = compact('client_token', 'client_secret', 'access_token'); + $this->auth = array( + 'client_token' => trim($client_token), + 'client_secret' => trim($client_secret), + 'access_token' => trim($access_token), + ); return $this; } diff --git a/tests/AuthenticationTest.php b/tests/AuthenticationTest.php index fcbaf85..e5d916f 100644 --- a/tests/AuthenticationTest.php +++ b/tests/AuthenticationTest.php @@ -57,6 +57,41 @@ public function testCreateAuthHeader( $this->assertEquals($expected, $result); } + public function testCreateAuthHeaderTrailingSpaces() { + $mockTimestamp = $this->prophesize('\Akamai\Open\EdgeGrid\Authentication\Timestamp'); + $mockTimestamp->__toString()->willReturn("20170831T19:34:21+0000"); + $mockTimestamp->isValid()->willReturn(true); + $mockNonce = $this->prophesize('\Akamai\Open\EdgeGrid\Authentication\Nonce'); + $mockNonce->__toString()->willReturn("nonce-xx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"); + + $authentication = new \Akamai\Open\EdgeGrid\Authentication(); + $authentication->setHttpMethod("POST"); + $authentication->setPath("/ccu/v3/invalidate/url/production"); + $authentication->setHost("akaa-baseurl-xxxxxxxxxxx-xxxxxxxxxxxxx.luna.akamaiapis.net"); + $authentication->setBody("{\"objects\":[\"https:\/\/example.org\/\",\"https:\/\/example.org\/test.html\"]}"); + $authentication->setTimestamp($mockTimestamp->reveal()); + $authentication->setNonce($mockNonce->reveal()); + + $authentication->setAuth( + 'akab-client-token-xxx-xxxxxxxxxxxxxxxx', + 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=', + 'akab-access-token-xxx-xxxxxxxxxxxxxxxx' + ); + $authentication->setMaxBodySize("15"); + $noSpacesResult = $authentication->createAuthHeader(); + + + $authentication->setAuth( + 'akab-client-token-xxx-xxxxxxxxxxxxxxxx ', + ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx= ', + ' akab-access-token-xxx-xxxxxxxxxxxxxxxx' + ); + $authentication->setMaxBodySize(" 15 "); + $spacesResult = $authentication->createAuthHeader(); + + $this->assertEquals($noSpacesResult, $spacesResult); + } + public function testDefaultTimestamp() { $authentication = new \Akamai\Open\EdgeGrid\Authentication();