|
1
|
|
|
<?php namespace Tymon\JWTAuth; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
|
4
|
|
|
use Illuminate\Contracts\Auth\Guard; |
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use Koodzo\Models\Db\User; |
|
7
|
|
|
use Tymon\JWTAuth\JWTAuth; |
|
8
|
|
|
|
|
9
|
|
|
class Auth implements Guard |
|
10
|
|
|
{ |
|
11
|
|
|
protected $auth; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct(JWTAuth $auth) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->auth = $auth; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Determine if the current user is authenticated. |
|
20
|
|
|
* |
|
21
|
|
|
* @return bool |
|
22
|
|
|
*/ |
|
23
|
|
|
public function check() |
|
24
|
|
|
{ |
|
25
|
|
|
try |
|
26
|
|
|
{ |
|
27
|
|
|
return ($token = $this->auth->parseToken()) && ($user = $this->auth->authenticate($token->getToken())); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
catch (\Exception $e) |
|
30
|
|
|
{ |
|
31
|
|
|
return false; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Determine if the current user is a guest. |
|
37
|
|
|
* |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
public function guest() |
|
41
|
|
|
{ |
|
42
|
|
|
return !$this->check(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the currently authenticated user's ID. |
|
47
|
|
|
* |
|
48
|
|
|
* @return int|null |
|
49
|
|
|
*/ |
|
50
|
|
|
public function id() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->auth->parseToken()->getPayload()->get('sub'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the currently authenticated user. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $attr Attributes to retrieve. |
|
59
|
|
|
* |
|
60
|
|
|
* @return \Koodzo\Models\Db\User|null |
|
61
|
|
|
*/ |
|
62
|
|
|
public function user($attr = ['*']) |
|
63
|
|
|
{ |
|
64
|
|
|
$id = $this->auth->parseToken()->getPayload()->get('sub'); |
|
65
|
|
|
|
|
66
|
|
|
return User::whereId($id)->first($attr); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Log a user into the application without sessions or cookies. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $credentials |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public function once(array $credentials = []) |
|
76
|
|
|
{ |
|
77
|
|
|
// Skip |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Attempt to authenticate a user using the given credentials. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $credentials |
|
84
|
|
|
* @param bool $remember |
|
85
|
|
|
* @param bool $login |
|
86
|
|
|
* @return false|string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function attempt(array $credentials = [], $remember = false, $login = true) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->auth->attempt($credentials); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Attempt to authenticate using HTTP Basic Auth. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $field |
|
97
|
|
|
* @return \Symfony\Component\HttpFoundation\Response|null |
|
98
|
|
|
*/ |
|
99
|
|
|
public function basic($field = 'email') |
|
100
|
|
|
{ |
|
101
|
|
|
// Skip |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Perform a stateless HTTP Basic login attempt. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $field |
|
108
|
|
|
* @return \Symfony\Component\HttpFoundation\Response|null |
|
109
|
|
|
*/ |
|
110
|
|
|
public function onceBasic($field = 'email') |
|
111
|
|
|
{ |
|
112
|
|
|
// Skip |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Validate a user's credentials. |
|
117
|
|
|
* |
|
118
|
|
|
* @param array $credentials |
|
119
|
|
|
* @return bool |
|
120
|
|
|
*/ |
|
121
|
|
|
public function validate(array $credentials = []) |
|
122
|
|
|
{ |
|
123
|
|
|
return !!$this->auth->attempt($credentials); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Log a user into the application. |
|
128
|
|
|
* |
|
129
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
|
130
|
|
|
* @param bool $remember |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function login(Authenticatable $user, $remember = false) |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->auth->fromUser($user); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Log the given user ID into the application. |
|
140
|
|
|
* |
|
141
|
|
|
* @param mixed $id |
|
142
|
|
|
* @param bool $remember |
|
143
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable |
|
144
|
|
|
*/ |
|
145
|
|
|
public function loginUsingId($id, $remember = false) |
|
146
|
|
|
{ |
|
147
|
|
|
$user = User::whereId($id)->first(); |
|
148
|
|
|
|
|
149
|
|
|
return $this->auth->fromUser($user); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Determine if the user was authenticated via "remember me" cookie. |
|
154
|
|
|
* |
|
155
|
|
|
* @return bool |
|
156
|
|
|
*/ |
|
157
|
|
|
public function viaRemember() |
|
158
|
|
|
{ |
|
159
|
|
|
// Skip |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Log the user out of the application. |
|
164
|
|
|
* |
|
165
|
|
|
* @return void |
|
166
|
|
|
*/ |
|
167
|
|
|
public function logout() |
|
168
|
|
|
{ |
|
169
|
|
|
$this->auth->invalidate($this->auth->getToken()); |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.