|
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) |
|
|
|
|
|
|
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
|
|
|
|