1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Signifly\Janitor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Events\Login; |
6
|
|
|
use Illuminate\Auth\Events\Failed; |
7
|
|
|
use Illuminate\Auth\Events\Logout; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
use Illuminate\Auth\Events\Attempting; |
10
|
|
|
use Illuminate\Auth\Events\Authenticated; |
11
|
|
|
use Signifly\Janitor\Exceptions\InvalidCredentialsException; |
12
|
|
|
|
13
|
|
|
class JWTProxy extends AbstractProxy |
14
|
|
|
{ |
15
|
|
|
public function attemptLogin($username, $password) |
16
|
|
|
{ |
17
|
|
|
$credentials = [ |
18
|
|
|
$this->getUsernameField() => $username, |
19
|
|
|
'password' => $password, |
20
|
|
|
]; |
21
|
|
|
|
22
|
|
|
event(new Attempting($this->getGuard(), $credentials, false)); |
23
|
|
|
|
24
|
|
|
$user = $this->getUserInstance() |
25
|
|
|
->where($this->getUsernameField(), $username) |
26
|
|
|
->first(); |
27
|
|
|
|
28
|
|
|
$token = Auth::attempt($credentials); |
29
|
|
|
|
30
|
|
|
if (is_null($user) || is_null($token)) { |
31
|
|
|
event(new Failed($this->getGuard(), $user, $credentials)); |
32
|
|
|
throw InvalidCredentialsException::forUsername($username); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
event(new Authenticated($this->getGuard(), $user)); |
36
|
|
|
event(new Login($this->getGuard(), $user, false)); |
37
|
|
|
|
38
|
|
|
return $this->prepareToken($token); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function attemptRefresh($refreshToken = null) |
42
|
|
|
{ |
43
|
|
|
$token = Auth::refresh(); |
44
|
|
|
|
45
|
|
|
return $this->prepareToken($token); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function attemptLogout() |
49
|
|
|
{ |
50
|
|
|
$user = Auth::user(); |
51
|
|
|
|
52
|
|
|
Auth::logout(); |
53
|
|
|
|
54
|
|
|
event(new Logout($this->getGuard(), $user)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function prepareToken($token): array |
58
|
|
|
{ |
59
|
|
|
return [ |
60
|
|
|
'access_token' => $token, |
61
|
|
|
'token_type' => 'Bearer', |
62
|
|
|
'expires_in' => Auth::factory()->getTTL() * 60, |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|