Skip to content

Commit

Permalink
Merge pull request #38 from michalsn/fix/ci44
Browse files Browse the repository at this point in the history
Compatibility with CI 4.4
  • Loading branch information
michalsn authored Aug 26, 2023
2 parents 7ffdb84 + deb6d57 commit a3d3703
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 80 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ echo signedurl()->setExpiration(DAY * 2)->urlTo('namedRoute', 12);
// https://example.com/route/name/12?expiration=1671980371&signature=signature-goes-here
```

## Versions

Versions are not compatible - URLs generated in one version of Signed URL will not work with another version.

| CodeIgniter version | Signed URL version |
|---------------------|--------------------|
| `>= 4.4` | `2.*` |
| `< 4.4` | `1.*` |

## Docs

https://michalsn.github.io/codeigniter-signed-url
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"require-dev": {
"codeigniter4/devkit": "^1.0",
"codeigniter4/framework": "^4.2",
"codeigniter4/framework": "^4.4",
"rector/rector": "0.18.0"
},
"minimum-stability": "dev",
Expand Down
8 changes: 8 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ echo signedurl()->setExpiration(DAY * 2)->urlTo('namedRoute', 12);
// https://example.com/route/name/12?expiration=1671980371&signature=signature-goes-here
```

## Versions

Versions are not compatible - URLs generated in one version of Signed URL will not work with another version.

| CodeIgniter version | Signed URL version |
|---------------------|--------------------|
| `>= 4.4` | `2.*` |
| `< 4.4` | `1.*` |
54 changes: 26 additions & 28 deletions rector.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

use Rector\CodeQuality\Rector\BooleanAnd\SimplifyEmptyArrayCheckRector;
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector;
use Rector\CodeQuality\Rector\For_\ForToForeachRector;
use Rector\CodeQuality\Rector\Foreach_\UnusedForeachValueToArrayKeysRector;
use Rector\CodeQuality\Rector\FuncCall\AddPregQuoteDelimiterRector;
use Rector\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector;
use Rector\CodeQuality\Rector\FuncCall\SimplifyStrposLowerRector;
Expand All @@ -20,28 +19,32 @@
use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector;
use Rector\DeadCode\Rector\MethodCall\RemoveEmptyMethodCallRector;
use Rector\EarlyReturn\Rector\Foreach_\ChangeNestedForeachIfsToEarlyContinueRector;
use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector;
use Rector\EarlyReturn\Rector\If_\RemoveAlwaysElseRector;
use Rector\EarlyReturn\Rector\Return_\PreparedValueToEarlyReturnRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php73\Rector\FuncCall\StringifyStrNeedlesRector;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\PHPUnit\Set\PHPUnitLevelSetList;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\PSR4\Rector\FileWithoutNamespace\NormalizeNamespaceByPSR4ComposerAutoloadRector;
use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([SetList::DEAD_CODE, LevelSetList::UP_TO_PHP_80, PHPUnitSetList::PHPUNIT_SPECIFIC_METHOD, PHPUnitLevelSetList::UP_TO_PHPUNIT_100]);
$rectorConfig->sets([
SetList::DEAD_CODE,
LevelSetList::UP_TO_PHP_80,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PHPUnitSetList::PHPUNIT_100,
]);

$rectorConfig->parallel();

// The paths to refactor (can also be supplied with CLI arguments)
$rectorConfig->paths([
__DIR__ . '/src/',
__DIR__ . '/app/',
__DIR__ . '/tests/',
]);

Expand Down Expand Up @@ -76,30 +79,16 @@
// Note: requires php 8
RemoveUnusedPromotedPropertyRector::class,

// Ignore tests that might make calls without a result
RemoveEmptyMethodCallRector::class => [
__DIR__ . '/tests',
],

// Ignore files that should not be namespaced
NormalizeNamespaceByPSR4ComposerAutoloadRector::class => [
__DIR__ . '/src/Common.php',
__DIR__ . '/tests/_support/Views/',
],

// May load view files directly when detecting classes
StringClassNameToClassConstantRector::class,
]);

// May be uninitialized on purpose
AddDefaultValueForUndefinedVariableRector::class,
// auto import fully qualified class names
$rectorConfig->importNames();

// PHPUnit attributes
AnnotationToAttributeRector::class,
]);
$rectorConfig->rule(SimplifyUselessVariableRector::class);
$rectorConfig->rule(RemoveAlwaysElseRector::class);
$rectorConfig->rule(CountArrayToEmptyArrayComparisonRector::class);
$rectorConfig->rule(ForToForeachRector::class);
$rectorConfig->rule(ChangeNestedForeachIfsToEarlyContinueRector::class);
$rectorConfig->rule(ChangeIfElseValueAssignToEarlyReturnRector::class);
$rectorConfig->rule(SimplifyStrposLowerRector::class);
Expand All @@ -112,10 +101,19 @@
$rectorConfig->rule(UnusedForeachValueToArrayKeysRector::class);
$rectorConfig->rule(ChangeArrayPushToArrayAssignRector::class);
$rectorConfig->rule(UnnecessaryTernaryExpressionRector::class);
$rectorConfig->rule(AddPregQuoteDelimiterRector::class);
$rectorConfig->rule(SimplifyRegexPatternRector::class);
$rectorConfig->rule(FuncGetArgsToVariadicParamRector::class);
$rectorConfig->rule(MakeInheritedMethodVisibilitySameAsParentRector::class);
$rectorConfig->rule(SimplifyEmptyArrayCheckRector::class);
$rectorConfig->rule(NormalizeNamespaceByPSR4ComposerAutoloadRector::class);
$rectorConfig
->ruleWithConfiguration(TypedPropertyFromAssignsRector::class, [
/**
* The INLINE_PUBLIC value is default to false to avoid BC break, if you use for libraries and want to preserve BC break, you don't need to configure it, as it included in LevelSetList::UP_TO_PHP_74
* Set to true for projects that allow BC break
*/
TypedPropertyFromAssignsRector::INLINE_PUBLIC => true,
]);
$rectorConfig->rule(StringClassNameToClassConstantRector::class);
$rectorConfig->rule(PrivatizeFinalClassPropertyRector::class);
$rectorConfig->rule(CompleteDynamicPropertiesRector::class);
};
2 changes: 1 addition & 1 deletion src/Config/SignedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SignedUrl extends BaseConfig
* If you're not sure what you're doing
* please stay with the default option.
*/
public string $algorithm = 'sha1';
public string $algorithm = 'sha256';

/**
* Query string key names.
Expand Down
10 changes: 8 additions & 2 deletions src/SignedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Michalsn\CodeIgniterSignedUrl;

use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\HTTP\URI;
use CodeIgniter\I18n\Time;
use CodeIgniter\Router\Exceptions\RouterException;
use Config\App;
use Michalsn\CodeIgniterSignedUrl\Config\SignedUrl as SignedUrlConfig;
use Michalsn\CodeIgniterSignedUrl\Exceptions\SignedUrlException;

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

$uri = _get_uri($relativePath);
$host = service('request')->getUri()->getHost();

$config = config(App::class);

$uri = new SiteURI($config, $relativePath, $host);

return $this->sign($uri);
}
Expand Down Expand Up @@ -146,7 +152,7 @@ public function verify(IncomingRequest $request): bool
$uri = $request->getUri();
$uri->stripQuery($this->config->signatureKey);

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

if (! hash_equals($querySignature, $signature)) {
Expand Down
8 changes: 4 additions & 4 deletions tests/CommonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testSignedurl(): void
public function testSignedUrlSiteUrl(): void
{
$this->assertSame(
'https://example.com/index.php/controller/method?signature=ZFCzKztQmn2yGb-ShnNyT5mF4eQ',
'https://example.com/index.php/controller/method?signature=I0a1XPGLCTlRQo5c5f3LCz9R-tKP244-6pKCRV54AEk',
signedurl()->siteUrl(['controller', 'method'])
);
}
Expand All @@ -40,7 +40,7 @@ public function testSignedUrlSiteUrlWithExpirationTime(): void
Time::setTestNow('2022-12-25 14:59:11', 'UTC');

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

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

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

0 comments on commit a3d3703

Please sign in to comment.