1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lab404\AuthChecker\Services; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Config\Repository as Config; |
7
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
use Illuminate\Foundation\Application; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
use Jenssegers\Agent\Agent; |
12
|
|
|
use Lab404\AuthChecker\Events\DeviceCreated; |
13
|
|
|
use Lab404\AuthChecker\Events\LoginCreated; |
14
|
|
|
use Lab404\AuthChecker\Models\Device; |
15
|
|
|
use Lab404\AuthChecker\Models\Login; |
16
|
|
|
|
17
|
|
|
class AuthChecker |
18
|
|
|
{ |
19
|
|
|
/** @var Application */ |
20
|
|
|
private $app; |
21
|
|
|
|
22
|
|
|
/** @var Request */ |
23
|
|
|
private $request; |
24
|
|
|
|
25
|
|
|
/** @var Config */ |
26
|
|
|
private $config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* AuthChecker |
30
|
|
|
* |
31
|
|
|
* @param Application $app |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Application $app, Request $request) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$this->app = $app; |
36
|
|
|
$this->request = $request; |
37
|
|
|
$this->config = $app['config']; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Authenticatable $user |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
public function handleLogin(Authenticatable $user) |
45
|
|
|
{ |
46
|
|
|
$agent = $this->app['agent']; |
47
|
|
|
$device = $this->findOrCreateUserDeviceByAgent($user, $agent); |
48
|
|
|
|
49
|
|
|
if ($this->shouldLogDeviceLogin($device)) { |
|
|
|
|
50
|
|
|
$this->createUserLoginForDevice($user, $device); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($device->wasRecentlyCreated) { |
54
|
|
|
event(DeviceCreated::class); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Authenticatable $user |
60
|
|
|
* @param Agent $agent |
61
|
|
|
* @return Device|null |
62
|
|
|
*/ |
63
|
|
|
public function findOrCreateUserDeviceByAgent(Authenticatable $user, Agent $agent) |
64
|
|
|
{ |
65
|
|
|
$device = $this->findUserDeviceByAgent($user, $agent); |
66
|
|
|
|
67
|
|
|
if (is_null($device)) { |
68
|
|
|
$device = $this->createDeviceForUser($user); |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $device; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Authenticatable $user |
76
|
|
|
* @param Agent $agent |
77
|
|
|
* @return Device|null |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function findUserDeviceByAgent(Authenticatable $user, Agent $agent) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
if (!$user->hasDevices()) { |
|
|
|
|
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$matching = $user->devices->filter(function ($item) use ($agent) { |
|
|
|
|
86
|
|
|
return $this->deviceMatch($item, $agent); |
87
|
|
|
})->first(); |
88
|
|
|
|
89
|
|
|
return $matching ? $matching : null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Model $user |
94
|
|
|
* @param Agent $agent |
95
|
|
|
* @return Device |
96
|
|
|
*/ |
97
|
|
|
public function createUserDeviceByAgent(Model $user, Agent $agent) |
98
|
|
|
{ |
99
|
|
|
$device = new Device(); |
100
|
|
|
|
101
|
|
|
$device->platform = $agent->platform(); |
|
|
|
|
102
|
|
|
$device->platform_version = $agent->version($device->platform); |
|
|
|
|
103
|
|
|
$device->browser = $agent->browser(); |
|
|
|
|
104
|
|
|
$device->browser_version = $agent->version($device->browser); |
|
|
|
|
105
|
|
|
$device->is_desktop = $agent->isDesktop() ? true : false; |
|
|
|
|
106
|
|
|
$device->is_mobile = $agent->isMobile() ? true : false; |
|
|
|
|
107
|
|
|
$device->language = count($agent->languages()) ? $agent->languages()[0] : null; |
|
|
|
|
108
|
|
|
$device->user_id = $user->getKey(); |
|
|
|
|
109
|
|
|
|
110
|
|
|
$device->save(); |
111
|
|
|
|
112
|
|
|
return $device; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Model $user |
117
|
|
|
* @param Device $device |
118
|
|
|
* @return Login |
119
|
|
|
*/ |
120
|
|
|
public function createUserLoginForDevice(Model $user, Device $device) |
121
|
|
|
{ |
122
|
|
|
$ip = $this->request->ip(); |
123
|
|
|
|
124
|
|
|
$login = new Login(['user_id' => $user->getKey(), 'ip_address' => $ip, 'device_id' => $device->id]); |
|
|
|
|
125
|
|
|
event(new LoginCreated($login)); |
126
|
|
|
|
127
|
|
|
return $login; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param Model $user |
132
|
|
|
* @param Agent $agent |
133
|
|
|
* @return false|Device |
134
|
|
|
*/ |
135
|
|
View Code Duplication |
public function findDeviceForUser(Model $user, Agent $agent) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
if (!$user->hasDevices()) { |
138
|
|
|
return false; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$device = $user->devices->filter(function ($item) use ($agent) { |
142
|
|
|
return $this->deviceMatch($item, $agent); |
143
|
|
|
})->first(); |
144
|
|
|
|
145
|
|
|
return is_null($device) ? false : $device; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param Device $device |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function shouldLogDeviceLogin(Device $device) |
153
|
|
|
{ |
154
|
|
|
$throttle = $this->getLoginThrottle(); |
155
|
|
|
|
156
|
|
|
if (!$device->relationLoaded('login')) { |
157
|
|
|
$device->load('login'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($throttle === 0 || is_null($device->login)) { |
|
|
|
|
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$limit = Carbon::now()->subMinutes($throttle); |
165
|
|
|
$login = $device->login; |
|
|
|
|
166
|
|
|
|
167
|
|
|
if (isset($login->created_at) && $login->created_at->gt($limit)) { |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param Device $device |
176
|
|
|
* @param Agent $agent |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function deviceMatch(Device $device, Agent $agent, array $attributes = null) |
180
|
|
|
{ |
181
|
|
|
$attributes = is_null($attributes) ? $this->getDeviceMatchingAttributes() : $attributes; |
182
|
|
|
$matches = count($attributes) > 0 ? false : true; |
183
|
|
|
|
184
|
|
|
if (in_array('platform', $attributes)) { |
185
|
|
|
$matches = $device->platform === $agent->platform(); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
if (in_array('platform_version', $attributes)) { |
189
|
|
|
$matches = $device->platform_version === $agent->version($device->platform); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if (in_array('browser', $attributes)) { |
193
|
|
|
$matches = $device->browser === $agent->browser(); |
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
if (in_array('browser_version', $attributes)) { |
197
|
|
|
$matches = $device->browser_version === $agent->version($device->browser); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if (in_array('language', $attributes)) { |
201
|
|
|
$matches = $device->language === $agent->version($device->language); |
|
|
|
|
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $matches; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param void |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
|
public function getDeviceMatchingAttributes() |
212
|
|
|
{ |
213
|
|
|
return $this->config->get('laravel-auth-checker.device_matching_attributes', [ |
214
|
|
|
'ip', |
215
|
|
|
'platform', |
216
|
|
|
'platform_version', |
217
|
|
|
'browser', |
218
|
|
|
]); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param void |
223
|
|
|
* @return int |
224
|
|
|
*/ |
225
|
|
|
public function getLoginThrottle() |
226
|
|
|
{ |
227
|
|
|
return (int)$this->config->get('laravel-auth-checker.throttle', 0); |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|