From b07ded44d34780203f0d1c7eef5997b703e5a71b Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 2 Dec 2023 11:05:34 +0100 Subject: [PATCH 1/2] Allow supplying user agent via headers --- src/main/php/com/amazon/aws/ServiceEndpoint.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/php/com/amazon/aws/ServiceEndpoint.class.php b/src/main/php/com/amazon/aws/ServiceEndpoint.class.php index 669bd0b..7b84f48 100755 --- a/src/main/php/com/amazon/aws/ServiceEndpoint.class.php +++ b/src/main/php/com/amazon/aws/ServiceEndpoint.class.php @@ -183,7 +183,7 @@ public function request(string $method, string $target, array $headers= [], stri $signed= [ 'Host' => $host, 'X-Amz-Date' => $this->signature->datetime($time), - 'X-Amz-User-Agent' => $this->userAgent, + 'X-Amz-User-Agent' => $headers['User-Agent'] ?? $this->userAgent, ]; // Automatically include security token if available From a6ecdffcb3a212968a882261811a1fb84e6f115b Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sat, 2 Dec 2023 11:08:10 +0100 Subject: [PATCH 2/2] Allow overwriting user agent via headers --- .../aws/unittest/RequestSigningTest.class.php | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 src/test/php/com/amazon/aws/unittest/RequestSigningTest.class.php diff --git a/src/test/php/com/amazon/aws/unittest/RequestSigningTest.class.php b/src/test/php/com/amazon/aws/unittest/RequestSigningTest.class.php new file mode 100755 index 0000000..e0ac784 --- /dev/null +++ b/src/test/php/com/amazon/aws/unittest/RequestSigningTest.class.php @@ -0,0 +1,74 @@ +connecting(function($uri) use($handler) { return new TestConnection(['/' => $handler]); }) + ->request('GET', '/', ['User-Agent' => 'xp-aws/1.0.0 OS/Test/1.0 lang/php/8.3.0'], null, self::TEST_TIME) + ; + } + + #[Test] + public function host() { + $handler= function($request) { + return ['HTTP/1.1 200 OK', '', $request->headers['Host'][0]]; + }; + + Assert::equals('test.amazonaws.com', $this->execute($handler)->content()); + } + + #[Test] + public function amz_date() { + $handler= function($request) { + return ['HTTP/1.1 200 OK', '', $request->headers['X-Amz-Date'][0]]; + }; + + Assert::equals('20230314T231444Z', $this->execute($handler)->content()); + } + + #[Test] + public function amz_user_agent() { + $handler= function($request) { + return ['HTTP/1.1 200 OK', '', $request->headers['X-Amz-User-Agent'][0]]; + }; + + Assert::matches( + '/xp-aws\/[0-9.]+ OS\/.+\/.+ lang\/php\/[0-9.]+/', + $this->execute($handler)->content() + ); + } + + #[Test] + public function authorization() { + $handler= function($request) { + return ['HTTP/1.1 200 OK', '', $request->headers['Authorization'][0]]; + }; + + Assert::equals( + 'AWS4-HMAC-SHA256 Credential=key/20230314/*/test/aws4_request, '. + 'SignedHeaders=host;x-amz-date;x-amz-user-agent, '. + 'Signature=252720f9823080fb461b7a311b52ce4dea9ed7e28c16cfa288054737e388786f', + $this->execute($handler)->content() + ); + } + + #[Test] + public function authorization_with_session() { + $handler= function($request) { + return ['HTTP/1.1 200 OK', '', $request->headers['Authorization'][0]]; + }; + + Assert::equals( + 'AWS4-HMAC-SHA256 Credential=key/20230314/*/test/aws4_request, '. + 'SignedHeaders=host;x-amz-date;x-amz-security-token;x-amz-user-agent, '. + 'Signature=c026606276b2854bcf04371f086c1e6339dbd01ce3ac04da51566521e7afc87f', + $this->execute($handler, 'session')->content() + ); + } +} \ No newline at end of file