1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sprocketbox\JWT\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Events\Attempting; |
6
|
|
|
use Illuminate\Auth\Events\Authenticated; |
7
|
|
|
use Illuminate\Auth\Events\Failed; |
8
|
|
|
use Illuminate\Auth\Events\Login; |
9
|
|
|
use Illuminate\Auth\Events\OtherDeviceLogout; |
10
|
|
|
use Illuminate\Auth\GuardHelpers; |
11
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
12
|
|
|
use Illuminate\Http\Request; |
13
|
|
|
|
14
|
|
|
trait DefaultCompatibility |
15
|
|
|
{ |
16
|
|
|
use GuardHelpers; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The event dispatcher instance. |
20
|
|
|
* |
21
|
|
|
* @var \Illuminate\Contracts\Events\Dispatcher |
22
|
|
|
*/ |
23
|
|
|
protected $events; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $name; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var \Illuminate\Http\Request|null |
32
|
|
|
*/ |
33
|
|
|
private $request; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \Illuminate\Contracts\Auth\Authenticatable|null |
37
|
|
|
*/ |
38
|
|
|
private $lastAttempted; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the current request instance. |
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Http\Request |
44
|
|
|
*/ |
45
|
|
|
public function getRequest(): Request |
46
|
|
|
{ |
47
|
|
|
return $this->request ?: Request::createFromGlobals(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Set the current request instance. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Http\Request $request |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function setRequest(Request $request): self |
58
|
|
|
{ |
59
|
|
|
$this->request = $request; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the event dispatcher instance. |
66
|
|
|
* |
67
|
|
|
* @return \Illuminate\Contracts\Events\Dispatcher |
68
|
|
|
*/ |
69
|
|
|
public function getDispatcher(): Dispatcher |
70
|
|
|
{ |
71
|
|
|
return $this->events; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the event dispatcher instance. |
76
|
|
|
* |
77
|
|
|
* @param \Illuminate\Contracts\Events\Dispatcher $events |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setDispatcher(Dispatcher $events): self |
82
|
|
|
{ |
83
|
|
|
$this->events = $events; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the last user we attempted to authenticate. |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable |
92
|
|
|
*/ |
93
|
|
|
public function getLastAttempted() |
94
|
|
|
{ |
95
|
|
|
return $this->lastAttempted; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Determine if the user matches the credentials. |
100
|
|
|
* |
101
|
|
|
* @param mixed $user |
102
|
|
|
* @param array $credentials |
103
|
|
|
* |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
protected function hasValidCredentials($user, $credentials): bool |
107
|
|
|
{ |
108
|
|
|
return $user !== null && $this->provider->validateCredentials($user, $credentials); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Fire the attempt event with the arguments. |
113
|
|
|
* |
114
|
|
|
* @param array $credentials |
115
|
|
|
* @param bool $remember |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
protected function fireAttemptEvent(array $credentials, $remember = false) |
120
|
|
|
{ |
121
|
|
|
if (isset($this->events)) { |
122
|
|
|
$this->events->dispatch(new Attempting( |
123
|
|
|
$this->name, $credentials, $remember |
124
|
|
|
)); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Fire the login event if the dispatcher is set. |
130
|
|
|
* |
131
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
132
|
|
|
* @param bool $remember |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
protected function fireLoginEvent($user, $remember = false) |
137
|
|
|
{ |
138
|
|
|
if (isset($this->events)) { |
139
|
|
|
$this->events->dispatch(new Login( |
140
|
|
|
$this->name, $user, $remember |
141
|
|
|
)); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Fire the authenticated event if the dispatcher is set. |
147
|
|
|
* |
148
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
149
|
|
|
* |
150
|
|
|
* @return void |
151
|
|
|
*/ |
152
|
|
|
protected function fireAuthenticatedEvent($user) |
153
|
|
|
{ |
154
|
|
|
if (isset($this->events)) { |
155
|
|
|
$this->events->dispatch(new Authenticated( |
156
|
|
|
$this->name, $user |
157
|
|
|
)); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Fire the other device logout event if the dispatcher is set. |
163
|
|
|
* |
164
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
165
|
|
|
* |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
|
|
protected function fireOtherDeviceLogoutEvent($user) |
169
|
|
|
{ |
170
|
|
|
if (isset($this->events)) { |
171
|
|
|
$this->events->dispatch(new OtherDeviceLogout( |
172
|
|
|
$this->name, $user |
173
|
|
|
)); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Fire the failed authentication attempt event with the given arguments. |
179
|
|
|
* |
180
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable|null $user |
181
|
|
|
* @param array $credentials |
182
|
|
|
* |
183
|
|
|
* @return void |
184
|
|
|
*/ |
185
|
|
|
protected function fireFailedEvent($user, array $credentials) |
186
|
|
|
{ |
187
|
|
|
if (isset($this->events)) { |
188
|
|
|
$this->events->dispatch(new Failed( |
189
|
|
|
$this->name, $user, $credentials |
190
|
|
|
)); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |