Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace league/url with league/uri & league/uri-components #25

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: php

php:
- 7.1
- 7.2
- 7.3
- 7.4

before_script:
- travis_retry composer install --no-interaction --prefer-source
Expand Down
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
79 changes: 45 additions & 34 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 @@ -55,12 +56,13 @@ public function __construct($signatureKey, $expiresParameter = 'expires', $signa
* @param string $url
* @param \DateTime|int $expiration
*
* @return string
* @throws InvalidExpiration
*
* @return string
*/
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 +73,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->modify([
$this->expiresParameter => $expiration,
$this->signatureParameter => $signature,
]);
$query = QueryString::extract($url->getQuery());

$urlSigner = $url->setQuery($query);
$query[$this->expiresParameter] = $expiration;
$query[$this->signatureParameter] = $signature;

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

/**
Expand All @@ -100,9 +98,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 +122,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 +154,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 +199,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 +216,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);
}
}