Skip to content

Commit 6983012

Browse files
committed
Added tempUrl generating capability
1 parent 5230354 commit 6983012

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/ObjectStore/v1/Service.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,42 @@ public function containerExists(string $name): bool
8686
throw $e;
8787
}
8888
}
89+
90+
/**
91+
* Creates a temporary URL to access object in private containers.
92+
* This method loosely follows swift command's way to generate temporary url: `swift tempurl $METHOD $EXPIRE $PATH $KEY`.
93+
*
94+
* @param string $method An HTTP method to allow for this temporary URL. Any of GET, POST, HEAD, PUT, POST, DELETE.
95+
* @param int $expires Unix timestamp
96+
* @param string $path The full path or storage URL to the Swift object. Example: '/v1/AUTH_account/c/o' or: 'http://saio:8080/v1/AUTH_account/c/o'
97+
* For prefix based signature, set path to 'prefix:/v1/AUTH_account/container/pre'
98+
* @param string $key The secret temporary URL key set on the Swift cluster*
99+
* @param string $ipRange [OPTIONAL] If present, the temporary URL will be restricted to the given ip or ip range
100+
* @param string $digest [OPTIONAL] The digest algorithm to be used may be configured by the operator. Default to sha1.
101+
* Check the tempurl.allowed_digests entry in the cluster's capabilities response to see which algorithms are supported by your
102+
* deployment;
103+
*
104+
* @return string
105+
*
106+
* @throws \RuntimeException
107+
*/
108+
public function tempUrl(string $method, int $expires, string $path, string $key, string $ipRange = null, string $digest = 'sha1'): string
109+
{
110+
if (!function_exists('hash_hmac')) {
111+
throw new \RuntimeException(sprintf('tempUrl requires hash extension enabled.'));
112+
}
113+
114+
if ($ipRange) {
115+
$message = sprintf("ip=%s\n%s\n%s\n%s", $ipRange, $method, $expires, $path);
116+
} else {
117+
$message = sprintf("%s\n%s\n%s", $method, $expires, $path);
118+
}
119+
120+
$signature = hash_hmac($digest, $message, $key);
121+
122+
// sha512 requires prefixing signature
123+
$signature = 'sha512' === $digest ? 'sha512:'.$signature : $signature;
124+
125+
return sprintf('%s?temp_url_sig=%s&temp_url_expires=%s', $path, $signature, $expires);
126+
}
89127
}

tests/unit/ObjectStore/v1/ServiceTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class ServiceTest extends TestCase
1515
{
16+
/** @var Service */
1617
private $service;
1718

1819
public function setUp()
@@ -84,4 +85,31 @@ public function test_it_throws_exception_when_error()
8485

8586
$this->assertFalse($this->service->containerExists('foo'));
8687
}
88+
89+
public function test_it_generates_temp_url_sha1()
90+
{
91+
$cases = [
92+
[
93+
['GET', '1516741234', '/v1/AUTH_account/container/object', 'mykey'],
94+
'/v1/AUTH_account/container/object?temp_url_sig=712dcef48d391e39bd2e3b63fc0a07146a36055e&temp_url_expires=1516741234'
95+
],
96+
[
97+
['HEAD', '1516741234', '/v1/AUTH_account/container/object', 'somekey'],
98+
'/v1/AUTH_account/container/object?temp_url_sig=a4516e93f2023652641fec44c82163dc298620e8&temp_url_expires=1516741234'
99+
],
100+
[
101+
['GET', '1323479485', 'prefix:/v1/AUTH_account/container/pre', 'mykey'],
102+
'/v1/AUTH_account/container/object?temp_url_sig=a4516e93f2023652641fec44c82163dc298620e8&temp_url_expires=1516741234'
103+
]
104+
];
105+
106+
foreach ($cases as $case)
107+
{
108+
$params = $case[0];
109+
$expected = $case[1];
110+
111+
$actual = call_user_func_array([$this->service, 'tempUrl'], $params);
112+
$this->assertEquals($expected, $actual);
113+
}
114+
}
87115
}

0 commit comments

Comments
 (0)