UrlSigner   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 88
c 0
b 0
f 0
wmc 7
lcom 1
cbo 4
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A sign() 0 19 2
A temporarySign() 0 3 1
A validate() 0 14 3
A setKeyResolver() 0 5 1
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)];
0 ignored issues
show
Bug introduced by
It seems like $expiration defined by parameter $expiration on line 31 can also be of type object<DateInterval> or object<DateTimeInterface>; however, Illuminate\Support\Inter...WithTime::availableAt() does only seem to accept integer, maybe add an additional type check?

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.

Loading history...
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')
0 ignored issues
show
Bug introduced by
It seems like $url->query() targeting AnthonySan95\UrlSigner\Url\Url::query() can also be of type null or string; however, Illuminate\Support\Arr::except() does only seem to accept array, maybe add an additional type check?

This check looks at variables that 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.

Loading history...
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