Skip to content

Commit

Permalink
Replace league/url with league/uri & league/uri-components
Browse files Browse the repository at this point in the history
league/url has been abandoned since 2015-09-23 and this PR replaces it with its successor: league/uri and its components package.

The package change requires the minimum PHP version to be set to 7.2. PHPUnit was also updated to the last version that supports PHP7.2.

I know there was concern about requiring new extensions (#1) by making the switch, however these are not hard dependencies.
If you don't use the `league/uri` i18n URI processing feature (`ext-intl`) or don't want to create a data URI from a filepath (`ext-fileinfo`) then there is no issues (an exception is thrown should you use one of these features and don't have the required extension).
  • Loading branch information
jakejackson1 committed Jul 20, 2020
1 parent 83af228 commit 62f1ecf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
}
],
"require": {
"php": "^7.1",
"league/url": "^3.3"
"php": "^7.2",
"league/uri": "^6.0",
"league/uri-components": "^2.2"
},
"require-dev": {
"phpunit/phpunit": "^6.5"
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
Expand Down
76 changes: 43 additions & 33 deletions src/BaseUrlSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Spatie\UrlSigner;

use DateTime;
use League\Url\Components\QueryInterface;
use League\Url\UrlImmutable;
use League\Uri\Http;
use League\Uri\QueryString;
use Psr\Http\Message\UriInterface;
use Spatie\UrlSigner\Exceptions\InvalidExpiration;
use Spatie\UrlSigner\Exceptions\InvalidSignatureKey;

Expand Down Expand Up @@ -60,7 +61,7 @@ public function __construct($signatureKey, $expiresParameter = 'expires', $signa
*/
public function sign($url, $expiration)
{
$url = UrlImmutable::createFromUrl($url);
$url = Http::createFromString($url);

$expiration = $this->getExpirationTimestamp($expiration);
$signature = $this->createSignature((string) $url, $expiration);
Expand All @@ -71,24 +72,20 @@ public function sign($url, $expiration)
/**
* Add expiration and signature query parameters to an url.
*
* @param \League\Url\UrlImmutable $url
* @param string $expiration
* @param string $signature
* @param UriInterface $url
* @param string $expiration
* @param string $signature
*
* @return \League\Url\UrlImmutable
*/
protected function signUrl(UrlImmutable $url, $expiration, $signature)
protected function signUrl(UriInterface $url, $expiration, $signature)
{
$query = $url->getQuery();
$query = QueryString::extract($url->getQuery());

$query->modify([
$this->expiresParameter => $expiration,
$this->signatureParameter => $signature,
]);
$query[$this->expiresParameter] = $expiration;
$query[$this->signatureParameter] = $signature;

$urlSigner = $url->setQuery($query);

return $urlSigner;
return $url->withQuery($this->buildQueryStringFromArray($query));
}

/**
Expand All @@ -100,9 +97,9 @@ protected function signUrl(UrlImmutable $url, $expiration, $signature)
*/
public function validate($url)
{
$url = UrlImmutable::createFromUrl($url);
$url = Http::createFromString($url);

$query = $url->getQuery();
$query = QueryString::extract($url->getQuery());

if ($this->isMissingAQueryParameter($query)) {
return false;
Expand All @@ -124,11 +121,11 @@ public function validate($url)
/**
* Check if a query is missing a necessary parameter.
*
* @param \League\Url\Components\QueryInterface $query
* @param array $query
*
* @return bool
*/
protected function isMissingAQueryParameter(QueryInterface $query)
protected function isMissingAQueryParameter(array $query)
{
if (!isset($query[$this->expiresParameter])) {
return true;
Expand Down Expand Up @@ -156,22 +153,18 @@ protected function isFuture($timestamp)
/**
* Retrieve the intended URL by stripping off the UrlSigner specific parameters.
*
* @param \League\Url\UrlImmutable $url
* @param UriInterface $url
*
* @return \League\Url\UrlImmutable
* @return UriInterface
*/
protected function getIntendedUrl(UrlImmutable $url)
protected function getIntendedUrl(UriInterface $url)
{
$intendedQuery = $url->getQuery();

$intendedQuery->modify([
$this->expiresParameter => null,
$this->signatureParameter => null,
]);
$intendedQuery = QueryString::extract($url->getQuery());

$intendedUrl = $url->setQuery($intendedQuery);
unset($intendedQuery[$this->expiresParameter]);
unset($intendedQuery[$this->signatureParameter]);

return $intendedUrl;
return $url->withQuery($this->buildQueryStringFromArray($intendedQuery));
}

/**
Expand Down Expand Up @@ -205,13 +198,13 @@ protected function getExpirationTimestamp($expiration)
/**
* Determine if the url has a forged signature.
*
* @param \League\Url\UrlImmutable $url
* @param UriInterface $url
*
* @return bool
*/
protected function hasValidSignature(UrlImmutable $url)
protected function hasValidSignature(UriInterface $url)
{
$query = $url->getQuery();
$query = QueryString::extract($url->getQuery());

$expiration = $query[$this->expiresParameter];
$providedSignature = $query[$this->signatureParameter];
Expand All @@ -222,4 +215,21 @@ protected function hasValidSignature(UrlImmutable $url)

return hash_equals($validSignature, $providedSignature);
}

/**
* Turn a key => value associate array into a query string
*
* @param array $query
*
* @return string|null
*/
protected function buildQueryStringFromArray(array $query)
{
$buildQuery = [];
foreach ($query as $key => $value) {
$buildQuery[] = [$key, $value];
}

return QueryString::build($buildQuery);
}
}

0 comments on commit 62f1ecf

Please sign in to comment.