Skip to content

Commit

Permalink
Merge pull request aws#569 from aws/presign-requests
Browse files Browse the repository at this point in the history
[v3] Signature::presign and S3Client::getPresignedRequest
  • Loading branch information
mtdowling committed May 9, 2015
2 parents 93654ff + 53471d2 commit 50e71d5
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/Ec2/CopySnapshotMiddleware.php
Expand Up @@ -69,10 +69,10 @@ private function createPresignedUrl(
// Create a presigned URL for our generated request.
$signer = new SignatureV4('ec2', $cmd['SourceRegion']);

return $signer->createPresignedUrl(
return (string) $signer->presign(
SignatureV4::convertPostToGet($request),
$client->getCredentials(),
'+1 hour'
);
)->getUri();
}
}
14 changes: 9 additions & 5 deletions src/S3/S3Client.php
Expand Up @@ -14,6 +14,7 @@
use Aws\ResultInterface;
use Aws\CommandInterface;
use GuzzleHttp\Psr7;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;

/**
Expand Down Expand Up @@ -176,19 +177,21 @@ public static function isBucketDnsCompatible($bucket)
* expire. This can be a Unix
* timestamp, a PHP DateTime object,
* or a string that can be evaluated
* by strtotime
* @return string
* by strtotime().
*
* @return RequestInterface
*/
public function createPresignedUrl(CommandInterface $command, $expires)
public function createPresignedRequest(CommandInterface $command, $expires)
{
/** @var \Aws\Signature\SignatureInterface $signer */
$signer = call_user_func(
$this->getSignatureProvider(),
$this->getConfig('signature_version'),
$this->getApi()->getSigningName(),
$this->getRegion()
);

return $signer->createPresignedUrl(
return $signer->presign(
$this->serialize($command),
$this->getCredentials(),
$expires
Expand All @@ -200,7 +203,8 @@ public function createPresignedUrl(CommandInterface $command, $expires)
*
* The URL returned by this method is not signed nor does it ensure the the
* bucket and key given to the method exist. If you need a signed URL, then
* use the {@see \Aws\S3\S3Client::createPresignedUrl} method.
* use the {@see \Aws\S3\S3Client::createPresignedRequest} method and get
* the URI of the signed request.
*
* @param string $bucket The name of the bucket where the object is located
* @param string $key The key of the object
Expand Down
4 changes: 2 additions & 2 deletions src/Signature/AnonymousSignature.php
Expand Up @@ -16,11 +16,11 @@ public function signRequest(
return $request;
}

public function createPresignedUrl(
public function presign(
RequestInterface $request,
CredentialsInterface $credentials,
$expires
) {
return '';
return $request;
}
}
4 changes: 2 additions & 2 deletions src/Signature/S3Signature.php
Expand Up @@ -50,7 +50,7 @@ public function signRequest(
return $request->withHeader('Authorization', $auth);
}

public function createPresignedUrl(
public function presign(
RequestInterface $request,
CredentialsInterface $credentials,
$expires
Expand Down Expand Up @@ -92,7 +92,7 @@ public function createPresignedUrl(

$queryString = http_build_query($query, null, '&', PHP_QUERY_RFC3986);

return (string) $request->getUri()->withQuery($queryString);
return $request->withUri($request->getUri()->withQuery($queryString));
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Signature/SignatureInterface.php
Expand Up @@ -26,17 +26,17 @@ public function signRequest(
);

/**
* Create a pre-signed URL
* Create a pre-signed request.
*
* @param RequestInterface $request Request to sign
* @param RequestInterface $request Request to sign
* @param CredentialsInterface $credentials Credentials used to sign
* @param int|string|\DateTime $expires The time at which the URL should
* expire. This can be a Unix timestamp, a PHP DateTime object, or a
* string that can be evaluated by strtotime.
*
* @return string
* @return RequestInterface
*/
public function createPresignedUrl(
public function presign(
RequestInterface $request,
CredentialsInterface $credentials,
$expires
Expand Down
2 changes: 1 addition & 1 deletion src/Signature/SignatureV2.php
Expand Up @@ -43,7 +43,7 @@ public function signRequest(
return $request->withBody(Psr7\stream_for(http_build_query($params)));
}

public function createPresignedUrl(
public function presign(
RequestInterface $request,
CredentialsInterface $credentials,
$expires
Expand Down
4 changes: 2 additions & 2 deletions src/Signature/SignatureV4.php
Expand Up @@ -69,7 +69,7 @@ public function signRequest(
return $this->buildRequest($parsed);
}

public function createPresignedUrl(
public function presign(
RequestInterface $request,
CredentialsInterface $credentials,
$expires
Expand All @@ -95,7 +95,7 @@ public function createPresignedUrl(
);
$parsed['query']['X-Amz-Signature'] = hash_hmac('sha256', $stringToSign, $key);

return (string) $this->buildRequest($parsed)->getUri();
return $this->buildRequest($parsed);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/S3/S3ClientTest.php
Expand Up @@ -89,15 +89,15 @@ public function testValidatesDnsBucketNames($bucket, $valid)
$this->assertEquals($valid, S3Client::isBucketDnsCompatible($bucket));
}

public function testCreatesPresignedUrls()
public function testCreatesPresignedRequests()
{
/** @var S3Client $client */
$client = $this->getTestClient('S3', [
'region' => 'us-east-1',
'credentials' => ['key' => 'foo', 'secret' => 'bar']
]);
$command = $client->getCommand('GetObject', ['Bucket' => 'foo', 'Key' => 'bar']);
$url = $client->createPresignedUrl($command, 1342138769);
$url = (string) $client->createPresignedRequest($command, 1342138769)->getUri();
$this->assertContains(
'https://foo.s3.amazonaws.com/bar?AWSAccessKeyId=',
$url
Expand All @@ -117,7 +117,7 @@ public function testCreatesPresignedUrlsWithSpecialCharacters()
'Bucket' => 'foobar test: abc',
'Key' => '+%.a'
]);
$url = $client->createPresignedUrl($command, 1342138769);
$url = (string) $client->createPresignedRequest($command, 1342138769)->getUri();
$this->assertContains(
'https://s3.amazonaws.com/foobar%20test%3A%20abc/%2B%25.a?AWSAccessKeyId=',
$url
Expand Down
4 changes: 2 additions & 2 deletions tests/Signature/AnonymousSignatureTest.php
Expand Up @@ -25,9 +25,9 @@ public function testDoesNotSignsRequests()
);

$result = $signature->signRequest($request, $creds);
$this->assertSame($result, $request);
$this->assertSame($request, $result);

$this->assertEquals('', $signature->createPresignedUrl(
$this->assertEquals($request, $signature->presign(
$request,
$creds,
'+1 minute'
Expand Down
12 changes: 6 additions & 6 deletions tests/Signature/S3SignatureTest.php
Expand Up @@ -61,11 +61,11 @@ public function testCreatesPresignedUrls($message, $url)
$signature = new S3Signature();
$req = Psr7\parse_request($message);
// Try with string
$res = $signature->createPresignedUrl($req, $creds, $dt);
$this->assertSame($url, $res);
$res = $signature->presign($req, $creds, $dt);
$this->assertSame($url, (string) $res->getUri());
// Try with timestamp
$res = $signature->createPresignedUrl($req, $creds, new \DateTime($dt));
$this->assertSame($url, $res);
$res = $signature->presign($req, $creds, new \DateTime($dt));
$this->assertSame($url, (string) $res->getUri());
}

public function signatureDataProvider()
Expand Down Expand Up @@ -304,11 +304,11 @@ public function testCreatesPreSignedUrlWithXAmzHeaders()
$meth->invoke($signature, $request, time())
);

$result = $signature->createPresignedUrl(
$result = (string) $signature->presign(
$request,
new Credentials('foo', 'bar', 'baz'),
time()
);
)->getUri();

$this->assertContains('&x-amz-acl=public-read', $result);
$this->assertContains('x-amz-foo=bar', $result);
Expand Down
20 changes: 10 additions & 10 deletions tests/Signature/S3SignatureV4Test.php
Expand Up @@ -68,33 +68,33 @@ public function testDoesNotRemoveDotSegments()
public function testCreatesPresignedDatesFromDateTime()
{
list($request, $credentials, $signature) = $this->getFixtures();
$url = $signature->createPresignedUrl(
$url = (string) $signature->presign(
$request,
$credentials,
new \DateTime('December 11, 2013 00:00:00 UTC')
);
)->getUri();
$this->assertContains('X-Amz-Expires=518400', $url);
}

public function testCreatesPresignedDatesFromUnixTimestamp()
{
list($request, $credentials, $signature) = $this->getFixtures();
$url = $signature->createPresignedUrl(
$url = (string) $signature->presign(
$request,
$credentials,
1386720000
);
)->getUri();
$this->assertContains('X-Amz-Expires=518400', $url);
}

public function testCreatesPresignedDateFromStrtotime()
{
list($request, $credentials, $signature) = $this->getFixtures();
$url = $signature->createPresignedUrl(
$url = (string) $signature->presign(
$request,
$credentials,
'December 11, 2013 00:00:00 UTC'
);
)->getUri();
$this->assertContains('X-Amz-Expires=518400', $url);
}

Expand All @@ -106,11 +106,11 @@ public function testAddsSecurityTokenIfPresent()
$credentials->getSecretKey(),
'123'
);
$url = $signature->createPresignedUrl(
$url = (string) $signature->presign(
$request,
$credentials,
1386720000
);
)->getUri();
$this->assertContains('X-Amz-Security-Token=123', $url);
$this->assertContains('X-Amz-Expires=518400', $url);
}
Expand All @@ -121,10 +121,10 @@ public function testAddsSecurityTokenIfPresent()
public function testEnsuresSigV4DurationIsLessThanOneWeek()
{
list($request, $credentials, $signature) = $this->getFixtures();
$signature->createPresignedUrl(
$signature->presign(
$request,
$credentials,
'December 31, 2026 00:00:00 UTC'
);
)->getUri();
}
}
2 changes: 1 addition & 1 deletion tests/Signature/SignatureV2Test.php
Expand Up @@ -44,6 +44,6 @@ public function testThrowsWhenNotImplemented()
$mock = $sig = new SignatureV2();
$request = new Request('GET', 'http://foo.com');
$credentials = new Credentials('foo', 'bar');
$mock->createPresignedUrl($request, $credentials, '+10 minutes');
$mock->presign($request, $credentials, '+10 minutes');
}
}
4 changes: 2 additions & 2 deletions tests/Signature/SignatureV4Test.php
Expand Up @@ -127,7 +127,7 @@ public function testAddsSecurityTokenIfPresentInPresigned()
$_SERVER['override_v4_time'] = true;
list($request, $credentials, $signature) = $this->getFixtures();
$credentials = new Credentials('foo', 'bar', '123');
$url = $signature->createPresignedUrl($request, $credentials, 1386720000);
$url = (string) $signature->presign($request, $credentials, 1386720000)->getUri();
$this->assertContains('X-Amz-Security-Token=123', $url);
$this->assertContains('X-Amz-Expires=518400', $url);
}
Expand All @@ -139,7 +139,7 @@ public function testEnsuresSigV4DurationIsLessThanOneWeek()
{
$_SERVER['override_v4_time'] = true;
list($request, $credentials, $signature) = $this->getFixtures();
$signature->createPresignedUrl($request, $credentials, 'December 31, 2013 00:00:00 UTC');
$signature->presign($request, $credentials, 'December 31, 2013 00:00:00 UTC');
}

public function testConvertsPostToGet()
Expand Down

0 comments on commit 50e71d5

Please sign in to comment.