1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tymon\JWTAuth\Providers; |
4
|
|
|
|
5
|
|
|
use Tymon\JWTAuth\JWTAuth; |
6
|
|
|
use Tymon\JWTAuth\Blacklist; |
7
|
|
|
use Tymon\JWTAuth\JWTManager; |
8
|
|
|
use Tymon\JWTAuth\PayloadFactory; |
9
|
|
|
use Tymon\JWTAuth\Claims\Factory; |
10
|
|
|
use Illuminate\Support\ServiceProvider; |
11
|
|
|
use Tymon\JWTAuth\Commands\JWTGenerateCommand; |
12
|
|
|
use Tymon\JWTAuth\Validators\PayloadValidator; |
13
|
|
|
|
14
|
|
|
class JWTAuthServiceProvider extends ServiceProvider |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Indicates if loading of the provider is deferred. |
18
|
|
|
* |
19
|
|
|
* @var bool |
20
|
|
|
*/ |
21
|
|
|
protected $defer = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Boot the service provider. |
25
|
|
|
*/ |
26
|
|
|
public function boot() |
27
|
|
|
{ |
28
|
|
|
$this->publishes([ |
29
|
|
|
__DIR__.'/../config/config.php' => config_path('jwt.php') |
30
|
|
|
], 'config'); |
31
|
|
|
|
32
|
|
|
$this->bootBindings(); |
33
|
|
|
|
34
|
|
|
$this->commands('tymon.jwt.generate'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Bind some Interfaces and implementations |
39
|
|
|
*/ |
40
|
|
|
protected function bootBindings() |
41
|
|
|
{ |
42
|
|
|
$this->app['Tymon\JWTAuth\JWTAuth'] = function ($app) { |
43
|
|
|
return $app['tymon.jwt.auth']; |
44
|
|
|
}; |
45
|
|
|
|
46
|
|
|
$this->app['Tymon\JWTAuth\Providers\User\UserInterface'] = function ($app) { |
47
|
|
|
return $app['tymon.jwt.provider.user']; |
48
|
|
|
}; |
49
|
|
|
|
50
|
|
|
$this->app['Tymon\JWTAuth\Providers\JWT\JWTInterface'] = function ($app) { |
51
|
|
|
return $app['tymon.jwt.provider.jwt']; |
52
|
|
|
}; |
53
|
|
|
|
54
|
|
|
$this->app['Tymon\JWTAuth\Providers\Auth\AuthInterface'] = function ($app) { |
55
|
|
|
return $app['tymon.jwt.provider.auth']; |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
$this->app['Tymon\JWTAuth\Providers\Storage\StorageInterface'] = function ($app) { |
59
|
|
|
return $app['tymon.jwt.provider.storage']; |
60
|
|
|
}; |
61
|
|
|
|
62
|
|
|
$this->app['Tymon\JWTAuth\JWTManager'] = function ($app) { |
63
|
|
|
return $app['tymon.jwt.manager']; |
64
|
|
|
}; |
65
|
|
|
|
66
|
|
|
$this->app['Tymon\JWTAuth\Blacklist'] = function ($app) { |
67
|
|
|
return $app['tymon.jwt.blacklist']; |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
$this->app['Tymon\JWTAuth\PayloadFactory'] = function ($app) { |
71
|
|
|
return $app['tymon.jwt.payload.factory']; |
72
|
|
|
}; |
73
|
|
|
|
74
|
|
|
$this->app['Tymon\JWTAuth\Claims\Factory'] = function ($app) { |
75
|
|
|
return $app['tymon.jwt.claim.factory']; |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
$this->app['Tymon\JWTAuth\Validators\PayloadValidator'] = function ($app) { |
79
|
|
|
return $app['tymon.jwt.validators.payload']; |
80
|
|
|
}; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Register the service provider. |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function register() |
89
|
|
|
{ |
90
|
|
|
// register providers |
91
|
|
|
$this->registerUserProvider(); |
92
|
|
|
$this->registerJWTProvider(); |
93
|
|
|
$this->registerAuthProvider(); |
94
|
|
|
$this->registerStorageProvider(); |
95
|
|
|
$this->registerJWTBlacklist(); |
96
|
|
|
|
97
|
|
|
$this->registerClaimFactory(); |
98
|
|
|
$this->registerJWTManager(); |
99
|
|
|
|
100
|
|
|
$this->registerJWTAuth(); |
101
|
|
|
$this->registerPayloadValidator(); |
102
|
|
|
$this->registerPayloadFactory(); |
103
|
|
|
$this->registerJWTCommand(); |
104
|
|
|
|
105
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../config/config.php', 'jwt'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Register the bindings for the User provider |
110
|
|
|
*/ |
111
|
|
|
protected function registerUserProvider() |
112
|
|
|
{ |
113
|
|
|
$this->app['tymon.jwt.provider.user'] = $this->app->share(function ($app) { |
|
|
|
|
114
|
|
|
return $app->make($this->config('providers.user'), [$app->make($this->config('user'))]); |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Register the bindings for the JSON Web Token provider |
120
|
|
|
*/ |
121
|
|
|
protected function registerJWTProvider() |
122
|
|
|
{ |
123
|
|
|
$this->app['tymon.jwt.provider.jwt'] = $this->app->share(function ($app) { |
|
|
|
|
124
|
|
|
|
125
|
|
|
$secret = $this->config('secret'); |
126
|
|
|
$algo = $this->config('algo'); |
127
|
|
|
$provider = $this->config('providers.jwt'); |
128
|
|
|
|
129
|
|
|
return $app->make($provider, [$secret, $algo]); |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Register the bindings for the Auth provider |
135
|
|
|
*/ |
136
|
|
|
protected function registerAuthProvider() |
137
|
|
|
{ |
138
|
|
|
$this->app['tymon.jwt.provider.auth'] = $this->app->share(function ($app) { |
|
|
|
|
139
|
|
|
return $this->getConfigInstance($this->config('providers.auth')); |
140
|
|
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Register the bindings for the Storage provider |
145
|
|
|
*/ |
146
|
|
|
protected function registerStorageProvider() |
147
|
|
|
{ |
148
|
|
|
$this->app['tymon.jwt.provider.storage'] = $this->app->share(function ($app) { |
|
|
|
|
149
|
|
|
return $this->getConfigInstance($this->config('providers.storage')); |
150
|
|
|
}); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Register the bindings for the Payload Factory |
155
|
|
|
*/ |
156
|
|
|
protected function registerClaimFactory() |
157
|
|
|
{ |
158
|
|
|
$this->app->singleton('tymon.jwt.claim.factory', function () { |
159
|
|
|
return new Factory(); |
160
|
|
|
}); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Register the bindings for the JWT Manager |
165
|
|
|
*/ |
166
|
|
|
protected function registerJWTManager() |
167
|
|
|
{ |
168
|
|
|
$this->app['tymon.jwt.manager'] = $this->app->share(function ($app) { |
|
|
|
|
169
|
|
|
|
170
|
|
|
$instance = new JWTManager( |
171
|
|
|
$app['tymon.jwt.provider.jwt'], |
172
|
|
|
$app['tymon.jwt.blacklist'], |
173
|
|
|
$app['tymon.jwt.payload.factory'] |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
return $instance->setBlacklistEnabled((bool) $this->config('blacklist_enabled')); |
177
|
|
|
}); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Register the bindings for the main JWTAuth class |
182
|
|
|
*/ |
183
|
|
|
protected function registerJWTAuth() |
184
|
|
|
{ |
185
|
|
|
$this->app['tymon.jwt.auth'] = $this->app->share(function ($app) { |
|
|
|
|
186
|
|
|
|
187
|
|
|
$auth = new JWTAuth( |
188
|
|
|
$app['tymon.jwt.manager'], |
189
|
|
|
$app['tymon.jwt.provider.user'], |
190
|
|
|
$app['tymon.jwt.provider.auth'], |
191
|
|
|
$app['request'] |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
return $auth->setIdentifier($this->config('identifier')); |
195
|
|
|
}); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Register the bindings for the main JWTAuth class |
200
|
|
|
*/ |
201
|
|
|
protected function registerJWTBlacklist() |
202
|
|
|
{ |
203
|
|
|
$this->app['tymon.jwt.blacklist'] = $this->app->share(function ($app) { |
|
|
|
|
204
|
|
|
return new Blacklist($app['tymon.jwt.provider.storage']); |
205
|
|
|
}); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Register the bindings for the payload validator |
210
|
|
|
*/ |
211
|
|
|
protected function registerPayloadValidator() |
212
|
|
|
{ |
213
|
|
|
$this->app['tymon.jwt.validators.payload'] = $this->app->share(function () { |
|
|
|
|
214
|
|
|
return with(new PayloadValidator())->setRefreshTTL($this->config('refresh_ttl'))->setRequiredClaims($this->config('required_claims')); |
215
|
|
|
}); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Register the bindings for the Payload Factory |
220
|
|
|
*/ |
221
|
|
|
protected function registerPayloadFactory() |
222
|
|
|
{ |
223
|
|
|
$this->app['tymon.jwt.payload.factory'] = $this->app->share(function ($app) { |
|
|
|
|
224
|
|
|
$factory = new PayloadFactory($app['tymon.jwt.claim.factory'], $app['request'], $app['tymon.jwt.validators.payload']); |
225
|
|
|
|
226
|
|
|
return $factory->setTTL($this->config('ttl')); |
227
|
|
|
}); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Register the Artisan command |
232
|
|
|
*/ |
233
|
|
|
protected function registerJWTCommand() |
234
|
|
|
{ |
235
|
|
|
$this->app['tymon.jwt.generate'] = $this->app->share(function () { |
|
|
|
|
236
|
|
|
return new JWTGenerateCommand(); |
237
|
|
|
}); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Helper to get the config values |
242
|
|
|
* |
243
|
|
|
* @param string $key |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
protected function config($key, $default = null) |
247
|
|
|
{ |
248
|
|
|
return config("jwt.$key", $default); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Get an instantiable configuration instance. Pinched from dingo/api :) |
253
|
|
|
* |
254
|
|
|
* @param mixed $instance |
255
|
|
|
* @return object |
256
|
|
|
*/ |
257
|
|
|
protected function getConfigInstance($instance) |
258
|
|
|
{ |
259
|
|
|
if (is_callable($instance)) { |
260
|
|
|
return call_user_func($instance, $this->app); |
261
|
|
|
} elseif (is_string($instance)) { |
262
|
|
|
return $this->app->make($instance); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return $instance; |
|
|
|
|
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.