-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicJwtAuthDriver.php
More file actions
231 lines (201 loc) · 5.39 KB
/
Copy pathDynamicJwtAuthDriver.php
File metadata and controls
231 lines (201 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
namespace Supaapps\Guard\Auth;
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Guard;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use stdClass;
use Throwable;
class DynamicJwtAuthDriver implements Guard
{
use GuardHelpers;
/**
* @var Request|array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application|mixed|string|null
*/
private Request $request;
/**
* @var stdClass
*/
private stdClass $jwtPayload;
/**
* @var string
*/
private string $firstName;
/**
* @var string
*/
private string $lastName;
/**
* @var string
*/
private string $email;
/**
* @var array
*/
private array $scopesArray;
/**
* @var string
*/
private string $scopes;
/**
* @var bool
*/
private bool $admin;
/**
* @var string
*/
private string $publicKey;
/**
* @param EloquentUserProvider|null $provider
*/
public function __construct(
?EloquentUserProvider $provider,
) {
$this->provider = $provider;
$this->request = request();
}
/**
* Get the currently authenticated user.
*
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function user()
{
if (!$this->validate()) {
return null;
}
if (!is_null($this->user)) {
return $this->user;
}
// retrieve user from db or create a new user by id
if (is_null($user = $this->provider->retrieveById($this->jwtPayload->sub))) {
$model = $this->provider->createModel();
$model->forceCreate([
'id' => $this->jwtPayload->sub
]);
$user = $model->query()->where('id', $this->jwtPayload->sub)->first();
}
$this->setUser($user);
$this->afterRetrieveUserValidation();
return $user;
}
/**
* @return false|string
*/
public function fetchPublicKey()
{
$url = rtrim(config('sguard.auth_server_url'), '/') . '/public/public_key';
return file_get_contents($url);
}
/**
* @return false|string
*/
public function fetchAlgo()
{
$url = rtrim(config('sguard.auth_server_url'), '/') . '/public/algo';
return file_get_contents($url);
}
/**
* @return array
*/
public function fetchRevokedTokens(): array
{
try {
$url = rtrim(config('sguard.auth_server_url'), '/') . '/public/revoked_ids';
return json_decode(file_get_contents($url));
} catch (Throwable $e) {
return [];
}
}
/**
* Validate a token signature
*
* @param array $credentials
* @return bool
* @throws AuthenticationException
*/
public function validate(array $credentials = []): bool
{
if (is_null($bearerToken = $this->request->bearerToken())) {
return false;
}
try {
$publicKey = Cache::remember('supaapps_jwt/public_key', 600, function () {
return $this->fetchPublicKey();
});
$algorithm = Cache::remember('supaapps_jwt/algo', 600, function () {
return $this->fetchAlgo();
});
$this->jwtPayload = JWT::decode($bearerToken, new Key(trim($publicKey), trim($algorithm)));
$revokedIds = Cache::remember('supaapps_jwt/revoked_ids', 15, function () {
return $this->fetchRevokedTokens();
});
if (in_array($this->jwtPayload->id, $revokedIds)) {
throw new AuthenticationException('access token has been revoked');
}
$this->firstName = $this->jwtPayload->first_name;
$this->lastName = $this->jwtPayload->last_name;
$this->email = $this->jwtPayload->email;
$this->scopesArray = explode(' ', $this->jwtPayload->scopes);
$this->scopes = $this->jwtPayload->scopes;
} catch (Throwable $ex) {
throw new AuthenticationException('Auth error - ' . $ex->getMessage());
}
return true;
}
public function afterRetrieveUserValidation()
{
if ($this->user->realm->name !== $this->jwtPayload->aud) {
throw new AuthenticationException('Auth error - realm mismatch');
}
if (strpos($this->scopes, '/' . $this->jwtPayload->aud . '/*') !== false) {
$this->admin = true;
} else {
$this->admin = false;
}
}
public function setRequest(Request $request)
{
$this->request = $request;
return $this;
}
/**
* @return string
*/
public function firstName(): string
{
return $this->firstName;
}
/**
* @return string
*/
public function lastName(): string
{
return $this->lastName;
}
/**
* @return string
*/
public function email(): string
{
return $this->email;
}
/**
* @return string
*/
public function scopes(): string
{
return $this->scopes;
}
/**
* @return array
*/
public function scopesArray(): array
{
return $this->scopesArray;
}
}