Completed
Push — master ( 4c2c69...98de2e )
by Marcel
03:44 queued 58s
created

ReauthLimiter::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
3
namespace Mpociot\Reauthenticate;
4
5
use Carbon\Carbon;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Hash;
9
10
class ReauthLimiter
11
{
12
    /**
13
     * The HTTP request.
14
     *
15
     * @var \Illuminate\Http\Request
16
     */
17
    protected $request;
18
19
    /**
20
     * The Reauthentication key.
21
     *
22
     * @var string
23
     */
24
    protected $key = 'reauthenticate';
25
26
    /**
27
     * Number of minutes a successful Reauthentication is valid.
28
     *
29
     * @var int
30
     */
31
    protected $reauthTime = 30;
32
33
    /**
34
     * Create a new reauth limiter instance.
35
     *
36
     * @param \Illuminate\Http\Request $request
37
     * @param string                   $key
38
     */
39
    public function __construct(Request $request, $key = null)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
40
    {
41
        $this->request = $request;
42
        $this->key = $key ?: $this->key;
43
    }
44
45
    /**
46
     * Attempt to Reauthenticate the user.
47
     *
48
     * @param string $password
49
     *
50
     * @return bool
51
     */
52
    public function attempt($password)
53
    {
54
        if (!Hash::check($password, Auth::user()->getAuthPassword())) {
55
            return false;
56
        }
57
58
        $this->request->session()->set($this->key.'.life', Carbon::now()->timestamp);
59
        $this->request->session()->set($this->key.'.authenticated', true);
60
61
        return true;
62
    }
63
64
    /**
65
     * Validate a reauthenticated Session data.
66
     *
67
     * @return bool
68
     */
69
    public function check()
70
    {
71
        $session = $this->request->session();
72
        $validationTime = Carbon::createFromTimestamp($session->get($this->key.'.life', 0));
73
74
        return $session->get($this->key.'.authenticated', false) &&
75
            ($validationTime->diffInMinutes() <= $this->reauthTime);
76
    }
77
}
78