Passed
Push — ft/package ( 5ee474...180175 )
by Philippe
05:16 queued 12s
created

HoneyPot::timer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Chief\App\Http\Middleware;
4
5
use Closure;
6
use Illuminate\Http\Request;
7
8
class HoneyPot
9
{
10
    /**
11
     * Handle an incoming request.
12
     *
13
     * @param  \Illuminate\Http\Request  $request
14
     * @param  \Closure  $next
15
     * @return mixed
16
     */
17
    public function handle($request, Closure $next)
18
    {
19
        $this->honeypot($request);
20
        $this->timer($request);
21
22
        return $next($request);
23
    }
24
25
    /**
26
     * Honeypot field protection
27
     *
28
     * A hidden field in the comment form is tagged as honeypot.
29
     * Should this field be filled with data or if this field is removed
30
     * from the input, We can assume the submit is forged.
31
     *
32
     * A field with the attribute key of your_name is assumed
33
     */
34
    private function honeypot(Request $request)
35
    {
36
        if (!$request->exists('your_name') or $request->has('your_name')) {
37
            abort('403', 'Request blocked due to assumed spam attempt. Honeypot field was filled in.');
38
        }
39
    }
40
41
    /**
42
     * Timer lock
43
     *
44
     * Should the request be performed in less then 3 seconds
45
     * A automatic spam submit is assumed.
46
     * Validation is performed by setting a timestamp
47
     * at the time of the comment form creation
48
     */
49
    private function timer(Request $request)
50
    {
51
        if (!$request->exists('_timer') or (time()-2) <= $request->get('_timer')) {
52
            abort('403', 'Request blocked due to assumed spam attempt. Submission happened too fast.');
53
        }
54
    }
55
}
56