Completed
Pull Request — master (#10)
by Paul
01:11
created

Mailgun::buildSignature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Clarkeash\Shield\Services;
4
5
use Illuminate\Http\Request;
6
7
class Mailgun extends BaseService
8
{
9
    public function verify(Request $request): bool
10
    {
11
        $tolerance = config('shield.services.mailgun.tolerance', 60 * 5);
12
        $timestamp = $request->input('timestamp');
13
14
        if (
15
            ! $request->isMethod('POST') ||
16
            abs(time() - $timestamp) > $tolerance
17
        ) {
18
            return false;
19
        }
20
21
        $signature = $this->buildSignature(
22
            $request->input('timestamp'),
0 ignored issues
show
Bug introduced by
It seems like $request->input('timestamp') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, Clarkeash\Shield\Service...ilgun::buildSignature() does only seem to accept string, 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...
23
            $request->input('token')
0 ignored issues
show
Bug introduced by
It seems like $request->input('token') targeting Illuminate\Http\Concerns...ractsWithInput::input() can also be of type array; however, Clarkeash\Shield\Service...ilgun::buildSignature() does only seem to accept string, 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...
24
        );
25
26
        return $signature === $request->input('signature');
27
    }
28
29
    public function headers(): array
30
    {
31
        return [];
32
    }
33
34
    /**
35
     * Build the signature for verification
36
     *
37
     * @param string $timestamp
38
     * @param string $token
39
     * @return string
40
     */
41
    protected function buildSignature(string $timestamp, string $token)
42
    {
43
        return hash_hmac(
44
            'sha256',
45
            sprintf('%s%s', $timestamp, $token),
46
            config('shield.services.mailgun.token')
47
        );
48
    }
49
}