Skip to content

Commit 57c75ad

Browse files
committed
compatibility with CI 4.4
1 parent 7ffdb84 commit 57c75ad

File tree

7 files changed

+83
-52
lines changed

7 files changed

+83
-52
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ echo signedurl()->setExpiration(DAY * 2)->urlTo('namedRoute', 12);
2626
// https://example.com/route/name/12?expiration=1671980371&signature=signature-goes-here
2727
```
2828

29+
## Versions
30+
31+
Versions are not compatible - URLs generated in one version of Signed URL will not work with another version.
32+
33+
| CodeIgniter version | Signed URL version |
34+
|---------------------|--------------------|
35+
| `>= 4.4` | `2.*` |
36+
| `< 4.4` | `1.*` |
37+
2938
## Docs
3039

3140
https://michalsn.github.io/codeigniter-signed-url

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
"require-dev": {
1919
"codeigniter4/devkit": "^1.0",
20-
"codeigniter4/framework": "^4.2",
20+
"codeigniter4/framework": "^4.4",
2121
"rector/rector": "0.18.0"
2222
},
2323
"minimum-stability": "dev",

docs/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ echo signedurl()->setExpiration(DAY * 2)->urlTo('namedRoute', 12);
1616
// https://example.com/route/name/12?expiration=1671980371&signature=signature-goes-here
1717
```
1818

19+
## Versions
20+
21+
Versions are not compatible - URLs generated in one version of Signed URL will not work with another version.
22+
23+
| CodeIgniter version | Signed URL version |
24+
|---------------------|--------------------|
25+
| `>= 4.4` | `2.*` |
26+
| `< 4.4` | `1.*` |

src/Config/SignedUrl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SignedUrl extends BaseConfig
2121
* If you're not sure what you're doing
2222
* please stay with the default option.
2323
*/
24-
public string $algorithm = 'sha1';
24+
public string $algorithm = 'sha256';
2525

2626
/**
2727
* Query string key names.

src/SignedUrl.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace Michalsn\CodeIgniterSignedUrl;
44

55
use CodeIgniter\HTTP\IncomingRequest;
6+
use CodeIgniter\HTTP\SiteURI;
67
use CodeIgniter\HTTP\URI;
78
use CodeIgniter\I18n\Time;
89
use CodeIgniter\Router\Exceptions\RouterException;
10+
use Config\App;
911
use Michalsn\CodeIgniterSignedUrl\Config\SignedUrl as SignedUrlConfig;
1012
use Michalsn\CodeIgniterSignedUrl\Exceptions\SignedUrlException;
1113

@@ -72,7 +74,11 @@ public function siteUrl(array|string $relativePath): string
7274
$relativePath = implode('/', $relativePath);
7375
}
7476

75-
$uri = _get_uri($relativePath);
77+
$host = service('request')->getUri()->getHost();
78+
79+
$config = config(App::class);
80+
81+
$uri = new SiteURI($config, $relativePath, $host);
7682

7783
return $this->sign($uri);
7884
}
@@ -146,7 +152,7 @@ public function verify(IncomingRequest $request): bool
146152
$uri = $request->getUri();
147153
$uri->stripQuery($this->config->signatureKey);
148154

149-
$url = URI::createURIString('', site_url(), $uri->getPath(), $uri->getQuery(), $uri->getFragment());
155+
$url = URI::createURIString('', base_url(), $uri->getPath(), $uri->getQuery(), $uri->getFragment());
150156
$signature = hash_hmac($queryAlgorithm, $url, $this->key, true);
151157

152158
if (! hash_equals($querySignature, $signature)) {

tests/CommonTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testSignedurl(): void
3030
public function testSignedUrlSiteUrl(): void
3131
{
3232
$this->assertSame(
33-
'https://example.com/index.php/controller/method?signature=ZFCzKztQmn2yGb-ShnNyT5mF4eQ',
33+
'https://example.com/index.php/controller/method?signature=I0a1XPGLCTlRQo5c5f3LCz9R-tKP244-6pKCRV54AEk',
3434
signedurl()->siteUrl(['controller', 'method'])
3535
);
3636
}
@@ -40,7 +40,7 @@ public function testSignedUrlSiteUrlWithExpirationTime(): void
4040
Time::setTestNow('2022-12-25 14:59:11', 'UTC');
4141

4242
$this->assertSame(
43-
'https://example.com/index.php/controller/method?expires=1671980361&signature=byUOHLW6p45GrUpMsVz3AlEBMYs',
43+
'https://example.com/index.php/controller/method?expires=1671980361&signature=9ZKau6qjzGOPY6unRPozK7dtZB1k_5hHQ9j3pwaQmzU',
4444
signedurl()->setExpiration(SECOND * 10)->siteUrl('controller/method')
4545
);
4646
}
@@ -51,7 +51,7 @@ public function testSignedUrlTo(): void
5151
$routes->add('path/(:num)', 'myController::goto/$1', ['as' => 'gotoPage']);
5252

5353
$this->assertSame(
54-
'https://example.com/index.php/path/13?signature=iZd5igbJp6uYIjjLKdiiPkmON0E',
54+
'https://example.com/index.php/path/13?signature=niwm-RgYXkGSKzuEH1semjC6TU5T8WrHs7FvEEyD8uQ',
5555
signedurl()->urlTo('gotoPage', 13)
5656
);
5757
}
@@ -64,7 +64,7 @@ public function testSignedUrlToWithExpirationTime(): void
6464
Time::setTestNow('2022-12-25 14:59:11', 'UTC');
6565

6666
$this->assertSame(
67-
'https://example.com/index.php/path/13?expires=1671980361&signature=HTGY25XucRbwm9LffdsTWHzn1Eg',
67+
'https://example.com/index.php/path/13?expires=1671980361&signature=pHMHFrXI74G5JuQc1mUUETznuUNnpHkwhAOsjazxlUw',
6868
signedurl()->setExpiration(SECOND * 10)->urlTo('gotoPage', 13)
6969
);
7070
}

tests/SignedUrlTest.php

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace Tests;
44

5+
use CodeIgniter\Config\Factories;
56
use CodeIgniter\Config\Services;
67
use CodeIgniter\HTTP\IncomingRequest;
8+
use CodeIgniter\HTTP\SiteURIFactory;
79
use CodeIgniter\HTTP\URI;
810
use CodeIgniter\HTTP\UserAgent;
911
use CodeIgniter\I18n\Time;
12+
use CodeIgniter\Superglobals;
1013
use CodeIgniter\Test\CIUnitTestCase;
1114
use Config\App;
1215
use Michalsn\CodeIgniterSignedUrl\Config\SignedUrl as SignedUrlConfig;
@@ -18,15 +21,43 @@
1821
*/
1922
final class SignedUrlTest extends CIUnitTestCase
2023
{
24+
private App $config;
25+
2126
protected function setUp(): void
2227
{
2328
parent::setUp();
2429

2530
Services::reset(true);
2631

32+
$this->config = new App();
33+
$this->config->baseURL = 'http://example.com/';
34+
$this->config->indexPage = '';
35+
36+
$_SERVER['HTTP_HOST'] = 'example.com';
37+
$_SERVER['REQUEST_URI'] = '/';
38+
$_SERVER['SCRIPT_NAME'] = '';
39+
2740
config('Encryption')->key = hex2bin('6ece79d55cd04503600bd97520a0138a067690112fbfb44c704b0c626a7c62a2');
2841
}
2942

43+
private function createRequest(?App $config = null, $body = null, ?string $path = null)
44+
{
45+
$config ??= new App();
46+
47+
$factory = new SiteURIFactory($config, new Superglobals());
48+
$uri = $factory->createFromGlobals();
49+
50+
if ($path !== null) {
51+
$uri->setPath($path);
52+
}
53+
54+
$request = new IncomingRequest($config, $uri, $body, new UserAgent());
55+
56+
Factories::injectMock('config', 'App', $config);
57+
58+
return $request;
59+
}
60+
3061
public function testIncorrectAlgorithm(): void
3162
{
3263
$this->expectException(SignedUrlException::class);
@@ -98,7 +129,7 @@ public function testSignWithNoExpirationInConfig(): void
98129
$signedUrl = new SignedUrl($config);
99130
$url = $signedUrl->sign($uri);
100131

101-
$expectedUrl .= '&signature=T3Y2OoBY2KvUbkTTBpPqjXFgs0k';
132+
$expectedUrl .= '&signature=ongZW4ttfJMqN757mwNXp5kx_3snwQhaDyI6JiV-5FM';
102133

103134
$this->assertSame($expectedUrl, $url);
104135
}
@@ -114,7 +145,7 @@ public function testSignWithIncludedAlgorithm(): void
114145
$signedUrl = new SignedUrl($config);
115146
$url = $signedUrl->sign($uri);
116147

117-
$expectedUrl .= '&algorithm=sha1&signature=fBY7AIRdMqyhRwknzK3lPusRoWw';
148+
$expectedUrl .= '&algorithm=sha256&signature=IldvSUQVJqTc8Gq47i0pEvuUYNjK_oRX1PAw-ZaXyM4';
118149

119150
$this->assertSame($expectedUrl, $url);
120151
}
@@ -131,7 +162,7 @@ public function testSignWithExpirationFromConfig(): void
131162
$signedUrl = new SignedUrl($config);
132163
$url = $signedUrl->sign($uri);
133164

134-
$expectedUrl .= '&expires=1671980361&signature=ILQnUh4hW3O9qEM541lZFgexlB4';
165+
$expectedUrl .= '&expires=1671980361&signature=qohLh7fvypmDF9vktdJ6DBXH6fiKyBezNQblosN2sbA';
135166

136167
$this->assertSame($expectedUrl, $url);
137168
}
@@ -148,20 +179,20 @@ public function testSignWithOverwritenExpirationFromConfig(): void
148179
$signedUrl = new SignedUrl($config);
149180
$url = $signedUrl->setExpiration(SECOND * 20)->sign($uri);
150181

151-
$expectedUrl .= '&expires=1671980371&signature=GSU95yKkJm3DqU5t3ZyYxUpgmBI';
182+
$expectedUrl .= '&expires=1671980371&signature=IzHjHhkTOOBPTayZnk8f_ut0H4-3q0YrDb11slKPWWE';
152183

153184
$this->assertSame($expectedUrl, $url);
154185
}
155186

156187
public function testVerifyWithIndexPage(): void
157188
{
158-
$path = '/index.php/path?query=string&signature=q0qKGOtgw3F153F1W3HZ0hUwxGc';
159-
$url = 'https://example.com' . $path;
189+
$this->config->indexPage = 'index.php';
190+
$_SERVER['SCRIPT_NAME'] = '/index.php';
160191

161-
$_SERVER['REQUEST_URI'] = $path;
192+
$_SERVER['REQUEST_URI'] = '/path?query=string&signature=joVnKjlHYIeuLtyUW5SnQ-US2FPkWkykZnSmf2D_RZY';
193+
194+
$request = $this->createRequest($this->config);
162195

163-
$uri = new URI($url);
164-
$request = new IncomingRequest(new App(), $uri, null, new UserAgent());
165196
$config = new SignedUrlConfig();
166197
$signedUrl = new SignedUrl($config);
167198

@@ -172,13 +203,10 @@ public function testVerifyWithIndexPage(): void
172203

173204
public function testVerifyWithoutExpiration(): void
174205
{
175-
$path = '/path?query=string&signature=9IOk6sKK9VmpboZXQCFa-Xv2BEE';
176-
$url = 'https://example.com' . $path;
206+
$_SERVER['REQUEST_URI'] = '/path?query=string&signature=iBEmAoQ9cPafZ3N05b9jEMj906Nd5nmSsJV7rKzFZSY';
177207

178-
$_SERVER['REQUEST_URI'] = $path;
208+
$request = $this->createRequest($this->config);
179209

180-
$uri = new URI($url);
181-
$request = new IncomingRequest(new App(), $uri, null, new UserAgent());
182210
$config = new SignedUrlConfig();
183211
$signedUrl = new SignedUrl($config);
184212

@@ -189,13 +217,9 @@ public function testVerifyWithoutExpiration(): void
189217

190218
public function testVerifyWithExpiration(): void
191219
{
192-
$path = '/path?query=string&expires=1671980371&signature=VQ1Nu3FAYcKKO3FrdmjFLk6PxNQ';
193-
$url = 'https://example.com' . $path;
194-
195-
$_SERVER['REQUEST_URI'] = $path;
220+
$_SERVER['REQUEST_URI'] = '/path?query=string&expires=1671980371&signature=9GNwvgcsK7jJUPpXe3MK5xFbE0rb5ZBHIjKc1qqWSgU';
196221

197-
$uri = new URI($url);
198-
$request = new IncomingRequest(new App(), $uri, null, new UserAgent());
222+
$request = $this->createRequest($this->config);
199223

200224
$config = new SignedUrlConfig();
201225
$signedUrl = new SignedUrl($config);
@@ -210,13 +234,9 @@ public function testVerifyThrowExceptionForMissingSignature(): void
210234
$this->expectException(SignedUrlException::class);
211235
$this->expectExceptionMessage('This URL have to be signed.');
212236

213-
$path = '/path?query=string';
214-
$url = 'https://example.com' . $path;
237+
$_SERVER['REQUEST_URI'] = '/path?query=string';
215238

216-
$_SERVER['REQUEST_URI'] = $path;
217-
218-
$uri = new URI($url);
219-
$request = new IncomingRequest(new App(), $uri, null, new UserAgent());
239+
$request = $this->createRequest($this->config);
220240

221241
$config = new SignedUrlConfig();
222242
$signedUrl = new SignedUrl($config);
@@ -228,13 +248,9 @@ public function testVerifyThrowExceptionForInvalidAlgorithm(): void
228248
$this->expectException(SignedUrlException::class);
229249
$this->expectExceptionMessage('Algorithm is invalid or not supported.');
230250

231-
$path = '/path?query=string&algorithm=fake&signature=fake';
232-
$url = 'https://example.com' . $path;
233-
234-
$_SERVER['REQUEST_URI'] = $path;
251+
$_SERVER['REQUEST_URI'] = '/path?query=string&algorithm=fake&signature=fake';
235252

236-
$uri = new URI($url);
237-
$request = new IncomingRequest(new App(), $uri, null, new UserAgent());
253+
$request = $this->createRequest($this->config);
238254

239255
$config = new SignedUrlConfig();
240256
$signedUrl = new SignedUrl($config);
@@ -246,13 +262,9 @@ public function testVerifyThrowExceptionForUrlNotValid(): void
246262
$this->expectException(SignedUrlException::class);
247263
$this->expectExceptionMessage('URL is not valid.');
248264

249-
$path = '/path?query=string123&expires=1671980371&signature=GSU95yKkJm3DqU5t3ZyYxUpgmBI';
250-
$url = 'https://example.com' . $path;
265+
$_SERVER['REQUEST_URI'] = '/path?query=string123&expires=1671980371&signature=GSU95yKkJm3DqU5t3ZyYxUpgmBI';
251266

252-
$_SERVER['REQUEST_URI'] = $path;
253-
254-
$uri = new URI($url);
255-
$request = new IncomingRequest(new App(), $uri, null, new UserAgent());
267+
$request = $this->createRequest($this->config);
256268

257269
$config = new SignedUrlConfig();
258270
$signedUrl = new SignedUrl($config);
@@ -264,13 +276,9 @@ public function testVerifyThrowExceptionForExpiredUrl(): void
264276
$this->expectException(SignedUrlException::class);
265277
$this->expectExceptionMessage('This URL has expired.');
266278

267-
$path = '/path?query=string&expires=1671980371&signature=VQ1Nu3FAYcKKO3FrdmjFLk6PxNQ';
268-
$url = 'https://example.com' . $path;
269-
270-
$_SERVER['REQUEST_URI'] = $path;
279+
$_SERVER['REQUEST_URI'] = '/path?query=string&expires=1671980371&signature=9GNwvgcsK7jJUPpXe3MK5xFbE0rb5ZBHIjKc1qqWSgU';
271280

272-
$uri = new URI($url);
273-
$request = new IncomingRequest(new App(), $uri, null, new UserAgent());
281+
$request = $this->createRequest($this->config);
274282

275283
Time::setTestNow('2022-12-25 15:59:11', 'UTC');
276284

0 commit comments

Comments
 (0)