1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AnthonySan95\UrlSigner; |
4
|
|
|
|
5
|
|
|
use AnthonySan95\UrlSigner\Contracts\UrlSigner as UrlSignerContract; |
6
|
|
|
use AnthonySan95\UrlSigner\Url\Url; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Illuminate\Support\Carbon; |
9
|
|
|
use Illuminate\Support\InteractsWithTime; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class UrlSigner implements UrlSignerContract { |
13
|
|
|
use InteractsWithTime; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The encryption key resolver callable. |
17
|
|
|
* |
18
|
|
|
* @var callable |
19
|
|
|
*/ |
20
|
|
|
protected $keyResolver; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Create a signed URL. |
24
|
|
|
* |
25
|
|
|
* @param string $url |
26
|
|
|
* @param array $parameters |
27
|
|
|
* @param \DateTimeInterface|\DateInterval|int $expiration |
28
|
|
|
* @return string |
29
|
|
|
* @throws \Throwable |
30
|
|
|
*/ |
31
|
|
|
public function sign($url, $parameters = [], $expiration = null): string { |
32
|
|
|
$parameters = Arr::wrap($parameters); |
33
|
|
|
|
34
|
|
|
if ($expiration) { |
35
|
|
|
$parameters = $parameters + ['expires' => $this->availableAt($expiration)]; |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
ksort($parameters); |
39
|
|
|
|
40
|
|
|
$url = new Url($url, $parameters); |
41
|
|
|
|
42
|
|
|
$key = call_user_func($this->keyResolver); |
43
|
|
|
|
44
|
|
|
$url = new Url($url, $parameters + [ |
45
|
|
|
'signature' => hash_hmac('sha256', $url, $key), |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
return $url; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a temporary signed URL. |
53
|
|
|
* |
54
|
|
|
* @param string $url |
55
|
|
|
* @param \DateTimeInterface|\DateInterval|int $expiration |
56
|
|
|
* @param array $parameters |
57
|
|
|
* @return string |
58
|
|
|
* @throws \Throwable |
59
|
|
|
*/ |
60
|
|
|
public function temporarySign($url, $expiration, $parameters = []): string { |
61
|
|
|
return $this->sign($url, $parameters, $expiration); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Validate a signed url. |
66
|
|
|
* |
67
|
|
|
* @param string $url |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
* @throws \Throwable |
71
|
|
|
*/ |
72
|
|
|
public function validate($url) { |
73
|
|
|
$url = new Url($url); |
74
|
|
|
|
75
|
|
|
$original = rtrim($url->getWithoutQuery() . '?' . Arr::query( |
76
|
|
|
Arr::except($url->query(), 'signature') |
|
|
|
|
77
|
|
|
), '?'); |
78
|
|
|
|
79
|
|
|
$expires = $url->query('expires'); |
80
|
|
|
|
81
|
|
|
$signature = hash_hmac('sha256', $original, call_user_func($this->keyResolver)); |
82
|
|
|
|
83
|
|
|
return hash_equals($signature, $url->query('signature', '')) && |
84
|
|
|
!($expires && Carbon::now()->getTimestamp() > $expires); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the encryption key resolver. |
90
|
|
|
* |
91
|
|
|
* @param callable $keyResolver |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function setKeyResolver(callable $keyResolver) { |
95
|
|
|
$this->keyResolver = $keyResolver; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.