1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of jwt-auth |
5
|
|
|
* |
6
|
|
|
* (c) Sean Tymon <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tymon\JWTAuth\Providers; |
13
|
|
|
|
14
|
|
|
use Tymon\JWTAuth\JWTAuth; |
15
|
|
|
use Tymon\JWTAuth\Manager; |
16
|
|
|
use Tymon\JWTAuth\Factory; |
17
|
|
|
use Tymon\JWTAuth\JWTGuard; |
18
|
|
|
use Tymon\JWTAuth\Blacklist; |
19
|
|
|
use Tymon\JWTAuth\Http\Parser; |
20
|
|
|
use Tymon\JWTAuth\Http\AuthHeaders; |
21
|
|
|
use Tymon\JWTAuth\Http\QueryString; |
22
|
|
|
use Tymon\JWTAuth\Http\RouteParams; |
23
|
|
|
use Illuminate\Support\ServiceProvider; |
24
|
|
|
use Tymon\JWTAuth\Contracts\Providers\JWT; |
25
|
|
|
use Tymon\JWTAuth\Contracts\Providers\Auth; |
26
|
|
|
use Tymon\JWTAuth\Contracts\Providers\Storage; |
27
|
|
|
use Tymon\JWTAuth\Validators\PayloadValidator; |
28
|
|
|
use Tymon\JWTAuth\Claims\Factory as ClaimFactory; |
29
|
|
|
use Tymon\JWTAuth\Commands\JWTGenerateSecretCommand; |
30
|
|
|
|
31
|
|
|
class LumenServiceProvider extends ServiceProvider |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Register the service provider. |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function register() |
39
|
|
|
{ |
40
|
|
|
$this->registerAliases(); |
41
|
|
|
|
42
|
|
|
$this->registerJWTProvider(); |
43
|
|
|
$this->registerAuthProvider(); |
44
|
|
|
$this->registerStorageProvider(); |
45
|
|
|
$this->registerJWTBlacklist(); |
46
|
|
|
|
47
|
|
|
$this->registerManager(); |
48
|
|
|
$this->registerTokenParser(); |
49
|
|
|
|
50
|
|
|
$this->registerJWTAuth(); |
51
|
|
|
$this->registerPayloadValidator(); |
52
|
|
|
$this->registerPayloadFactory(); |
53
|
|
|
$this->registerJWTCommand(); |
54
|
|
|
|
55
|
|
|
$this->commands('tymon.jwt.secret'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Boot the service provider. |
60
|
|
|
*/ |
61
|
|
|
public function boot() |
62
|
|
|
{ |
63
|
|
|
$this->app->configure('jwt'); |
|
|
|
|
64
|
|
|
$this->extendAuthGuard(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function extendAuthGuard() |
68
|
|
|
{ |
69
|
|
|
$this->app['auth']->extend('jwt', function ($app, $name, array $config) { |
70
|
|
|
|
71
|
|
|
$guard = new JwtGuard( |
72
|
|
|
$app['tymon.jwt.auth'], |
73
|
|
|
$app['auth']->createUserProvider($config['provider']), |
74
|
|
|
$app['request'] |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$app->refresh('request', $guard, 'setRequest'); |
78
|
|
|
|
79
|
|
|
return $guard; |
80
|
|
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Bind some Interfaces and implementations |
85
|
|
|
*/ |
86
|
|
|
protected function registerAliases() |
87
|
|
|
{ |
88
|
|
|
$this->app->alias('tymon.jwt.auth', JWTAuth::class); |
89
|
|
|
$this->app->alias('tymon.jwt.provider.jwt', JWT::class); |
90
|
|
|
$this->app->alias('tymon.jwt.provider.auth', Auth::class); |
91
|
|
|
$this->app->alias('tymon.jwt.provider.storage', Storage::class); |
92
|
|
|
$this->app->alias('tymon.jwt.manager', Manager::class); |
93
|
|
|
$this->app->alias('tymon.jwt.blacklist', Blacklist::class); |
94
|
|
|
$this->app->alias('tymon.jwt.payload.factory', Factory::class); |
95
|
|
|
$this->app->alias('tymon.jwt.validators.payload', PayloadValidator::class); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Register the bindings for the JSON Web Token provider |
100
|
|
|
*/ |
101
|
|
|
protected function registerJWTProvider() |
102
|
|
|
{ |
103
|
|
|
$this->app->singleton('tymon.jwt.provider.jwt', function ($app) { |
104
|
|
|
$provider = $this->config('providers.jwt'); |
105
|
|
|
|
106
|
|
|
return $app->make($provider, [$this->config('secret'), $this->config('algo')]); |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Register the bindings for the Auth provider |
112
|
|
|
*/ |
113
|
|
|
protected function registerAuthProvider() |
114
|
|
|
{ |
115
|
|
|
$this->app->singleton('tymon.jwt.provider.auth', function () { |
116
|
|
|
return $this->getConfigInstance('providers.auth'); |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Register the bindings for the Storage provider |
122
|
|
|
*/ |
123
|
|
|
protected function registerStorageProvider() |
124
|
|
|
{ |
125
|
|
|
$this->app->singleton('tymon.jwt.provider.storage', function () { |
126
|
|
|
return $this->getConfigInstance('providers.storage'); |
127
|
|
|
}); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Register the bindings for the JWT Manager |
132
|
|
|
*/ |
133
|
|
|
protected function registerManager() |
134
|
|
|
{ |
135
|
|
|
$this->app->singleton('tymon.jwt.manager', function ($app) { |
136
|
|
|
|
137
|
|
|
$instance = new Manager( |
138
|
|
|
$app['tymon.jwt.provider.jwt'], |
139
|
|
|
$app['tymon.jwt.blacklist'], |
140
|
|
|
$app['tymon.jwt.payload.factory'] |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
return $instance->setBlacklistEnabled((bool) $this->config('blacklist_enabled')); |
144
|
|
|
}); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Register the bindings for the Token Parser |
149
|
|
|
*/ |
150
|
|
|
protected function registerTokenParser() |
151
|
|
|
{ |
152
|
|
|
$this->app->singleton('tymon.jwt.parser', function ($app) { |
153
|
|
|
return new Parser( |
154
|
|
|
$app['request'], |
155
|
|
|
[new AuthHeaders, new QueryString, new RouteParams] |
156
|
|
|
); |
157
|
|
|
}); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Register the bindings for the main JWTAuth class |
162
|
|
|
*/ |
163
|
|
|
protected function registerJWTAuth() |
164
|
|
|
{ |
165
|
|
|
$this->app->singleton('tymon.jwt.auth', function ($app) { |
166
|
|
|
return new JWTAuth( |
167
|
|
|
$app['tymon.jwt.manager'], |
168
|
|
|
$app['tymon.jwt.provider.auth'], |
169
|
|
|
$app['tymon.jwt.parser'] |
170
|
|
|
); |
171
|
|
|
}); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Register the bindings for the main JWTAuth class |
176
|
|
|
*/ |
177
|
|
|
protected function registerJWTBlacklist() |
178
|
|
|
{ |
179
|
|
|
$this->app->singleton('tymon.jwt.blacklist', function ($app) { |
180
|
|
|
$instance = new Blacklist($app['tymon.jwt.provider.storage']); |
181
|
|
|
|
182
|
|
|
return $instance->setGracePeriod($this->config('blacklist_grace_period')) |
183
|
|
|
->setRefreshTTL($this->config('refresh_ttl')); |
184
|
|
|
}); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Register the bindings for the payload validator |
189
|
|
|
*/ |
190
|
|
|
protected function registerPayloadValidator() |
191
|
|
|
{ |
192
|
|
|
$this->app->singleton('tymon.jwt.validators.payload', function () { |
193
|
|
|
return (new PayloadValidator) |
194
|
|
|
->setRefreshTTL($this->config('refresh_ttl')) |
195
|
|
|
->setRequiredClaims($this->config('required_claims')); |
196
|
|
|
}); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Register the bindings for the Payload Factory |
201
|
|
|
*/ |
202
|
|
|
protected function registerPayloadFactory() |
203
|
|
|
{ |
204
|
|
|
$this->app->singleton('tymon.jwt.payload.factory', function ($app) { |
205
|
|
|
$factory = new Factory( |
206
|
|
|
new ClaimFactory, |
207
|
|
|
$app['request'], |
208
|
|
|
$app['tymon.jwt.validators.payload'] |
209
|
|
|
); |
210
|
|
|
|
211
|
|
|
return $factory->setTTL($this->config('ttl')); |
212
|
|
|
}); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Register the Artisan command |
217
|
|
|
*/ |
218
|
|
|
protected function registerJWTCommand() |
219
|
|
|
{ |
220
|
|
|
$this->app->singleton('tymon.jwt.secret', function () { |
221
|
|
|
return new JWTGenerateSecretCommand(); |
222
|
|
|
}); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Helper to get the config values |
227
|
|
|
* |
228
|
|
|
* @param string $key |
229
|
|
|
* |
230
|
|
|
* @return mixed |
231
|
|
|
*/ |
232
|
|
|
protected function config($key, $default = null) |
233
|
|
|
{ |
234
|
|
|
return config("jwt.$key", $default); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Get an instantiable configuration instance. |
239
|
|
|
* |
240
|
|
|
* @param string $key |
241
|
|
|
* |
242
|
|
|
* @return mixed |
243
|
|
|
*/ |
244
|
|
|
protected function getConfigInstance($key) |
245
|
|
|
{ |
246
|
|
|
$instance = $this->config($key); |
247
|
|
|
|
248
|
|
|
if (is_string($instance)) { |
249
|
|
|
return $this->app->make($instance); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $instance; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.