Skip to content

Commit 928b8cf

Browse files
chore: Update signature logic to use only a single secret value
1 parent 0eb5065 commit 928b8cf

3 files changed

Lines changed: 28 additions & 56 deletions

File tree

README.md

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,16 @@ The values can either be a single value, an array of values, or an associative a
4545
```php
4646
// Create Options object with array of values
4747
$options = new Options([
48-
'width' => 300,
49-
'height' => 400,
50-
'resize' => [
51-
'width' => 300,
52-
'height' => 400,
53-
'enlarge' => false,
54-
'resize_type' => 'fill',
55-
],
56-
'gravity' => 'sm',
57-
'png_options' => [
58-
'interlaced' => true,
59-
'quantize' => false,
60-
],
61-
]);
48+
'width' => 300,
49+
'height' => 400,
50+
'fit' => 'fill',
51+
'watermarkPosition' => 'center',
52+
'border' => [
53+
'width' => 10,
54+
'color' => '000000',
55+
'borderMethod' => 'overlay',
56+
],
57+
]);
6258
```
6359

6460
### Building URLs
@@ -81,46 +77,22 @@ $imageUrl = 'https://example.com/images/image.jpg';
8177
$url = $builder->buildUrl($imageUrl, $options);
8278

8379
echo $url;
84-
// Output: https://images.my-origin.com/unsafe/preset:sharp/resize:fill:300:400:0/gravity:sm/quality:80/format:png/plain/https://example.com/images/image.jpg
85-
```
86-
87-
### Encoded URLs
88-
89-
```php
90-
use smallpics\smallpics\Options;
91-
use smallpics\smallpics\UrlBuilder;
92-
93-
// Create options object
94-
$options = new Options();
95-
$options
96-
->setWidth(300)
97-
->setHeight(400);
98-
99-
// Create URL builder
100-
$builder = new UrlBuilder('https://images.my-origin.com');
101-
102-
// Build URL with encoded source
103-
$imageUrl = 'images/image.jpg';
104-
$url = $builder->buildUrl($imageUrl, $options);
105-
106-
echo $url;
107-
// Output: https://images.my-origin.com/images/image.jpg?width=300&height=400
80+
// Output: https://images.my-origin.com/images/image.jpg?q=80&fm=png
10881
```
10982

11083
### Signed URLs
11184

11285
```php
11386
// Create URL builder with signing keys
114-
$key = '0123456789abcdef0123456789abcdef';
115-
$salt = 'fedcba9876543210fedcba9876543210';
116-
$builder = new UrlBuilder('https://images.my-origin.com', $key, $salt);
87+
$secret = '0123456789abcdef0123456789abcdef';
88+
$builder = new UrlBuilder('https://images.my-origin.com', $secret);
11789

11890
// Build signed URL
11991
$imageUrl = 'images/image.jpg';
12092
$url = $builder->buildUrl($imageUrl, $options);
12193

12294
echo $url;
123-
// Output will include a signature: https://images.my-origin.com/images/image.jpg?width=300&height=400&signature=[signature]
95+
// Output will include a signature: https://images.my-origin.com/images/image.jpg?signature=[signature]
12496
```
12597

12698
## Development

src/UrlBuilder.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ class UrlBuilder
99
*/
1010
public function __construct(
1111
protected string $host,
12-
protected ?string $key = null,
13-
protected ?string $salt = null,
12+
protected ?string $secret = null,
1413
) {
1514
}
1615

@@ -29,7 +28,7 @@ public function buildUrl(string $sourceUrl, ?Options $options = null): string
2928

3029
$unsignedUrl = "https://{$this->host}/{$sourceUrl}?{$optionsString}";
3130

32-
if ($this->key === null || $this->salt === null) {
31+
if ($this->secret === null) {
3332
return $unsignedUrl;
3433
}
3534

@@ -41,11 +40,14 @@ public function buildUrl(string $sourceUrl, ?Options $options = null): string
4140
*/
4241
protected function generateSignature(string $path): string
4342
{
44-
$keyBin = pack('H*', $this->key);
45-
$saltBin = pack('H*', $this->salt);
43+
// We always want to generate a signature for a path that exludes the signature param and value
44+
$path = preg_replace('/&?signature=[^&]*/', '', $path);
4645

47-
$signature = hash_hmac('sha256', $saltBin . $path, $keyBin, true);
46+
if ($path === null || $this->secret === null) {
47+
return '';
48+
}
4849

50+
$signature = hash_hmac('sha256', $path, $this->secret, true);
4951
$signature = base64_encode($signature);
5052
// Make it neat
5153
$signature = str_replace(['+', '/', '='], '', $signature);

tests/Unit/UrlBuilderTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,12 @@
231231
'height' => 400,
232232
]);
233233

234-
$key = '0123456789abcdef0123456789abcdef';
235-
$salt = 'fedcba9876543210fedcba9876543210';
234+
$secret = 'my-secret-value';
236235

237-
$builder = new UrlBuilder('https://images.example.com', $key, $salt);
236+
$builder = new UrlBuilder('images.example.com', $secret);
238237
$url = $builder->buildUrl('images/image.jpg', $options);
239238

240-
expect($url)->toBe('https://https://images.example.com/images/image.jpg?w=300&h=400&signature=X3DbGrEoMMGYE5xPsb0tOmEIdgvo3VbQfKFddxZwx2Q');
239+
expect($url)->toBe('https://images.example.com/images/image.jpg?w=300&h=400&signature=S0b0bvBhc0kh2L6WRhcYGaRVT1LsuzWwONsdROoBk');
241240
});
242241

243242
test('can generate complex signed URLs', function (): void {
@@ -247,11 +246,10 @@
247246
->setQuality(80)
248247
->setFormat('png');
249248

250-
$key = '0123456789abcdef0123456789abcdef';
251-
$salt = 'fedcba9876543210fedcba9876543210';
249+
$secret = 'my-secret-value';
252250

253-
$builder = new UrlBuilder('https://images.example.com', $key, $salt);
251+
$builder = new UrlBuilder('images.example.com', $secret);
254252
$url = $builder->buildUrl('images/image.jpg', $options);
255253

256-
expect($url)->toBe('https://https://images.example.com/images/image.jpg?border=5,ff0000,overlay&q=80&fm=png&signature=S0n8P4GVg2FErH3Yg3vVAC7XnRaLexs4UWfaa2r4');
254+
expect($url)->toBe('https://images.example.com/images/image.jpg?border=5,ff0000,overlay&q=80&fm=png&signature=NBzER5uyLXXVZGFMAXWjthvfYWCVaj754FoMWujdaeI');
257255
});

0 commit comments

Comments
 (0)