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 Illuminate\Support\ServiceProvider; |
15
|
|
|
use Tymon\JWTAuth\Blacklist; |
16
|
|
|
use Tymon\JWTAuth\Claims\Factory as ClaimFactory; |
17
|
|
|
use Tymon\JWTAuth\Commands\JWTGenerateSecretCommand; |
18
|
|
|
use Tymon\JWTAuth\Contracts\Providers\Auth; |
19
|
|
|
use Tymon\JWTAuth\Contracts\Providers\JWT; |
20
|
|
|
use Tymon\JWTAuth\Contracts\Providers\Storage; |
21
|
|
|
use Tymon\JWTAuth\Http\TokenParser; |
22
|
|
|
use Tymon\JWTAuth\JWTAuth; |
23
|
|
|
use Tymon\JWTAuth\Manager; |
24
|
|
|
use Tymon\JWTAuth\Factory; |
25
|
|
|
use Tymon\JWTAuth\Validators\PayloadValidator; |
26
|
|
|
|
27
|
|
|
class LumenServiceProvider extends ServiceProvider |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Register the service provider. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
|
|
public function register() |
35
|
|
|
{ |
36
|
|
|
$this->registerAliases(); |
37
|
|
|
|
38
|
|
|
$this->registerJWTProvider(); |
39
|
|
|
$this->registerAuthProvider(); |
40
|
|
|
$this->registerStorageProvider(); |
41
|
|
|
$this->registerJWTBlacklist(); |
42
|
|
|
|
43
|
|
|
$this->registerManager(); |
44
|
|
|
$this->registerTokenParser(); |
45
|
|
|
|
46
|
|
|
$this->registerJWTAuth(); |
47
|
|
|
$this->registerPayloadValidator(); |
48
|
|
|
$this->registerPayloadFactory(); |
49
|
|
|
$this->registerJWTCommand(); |
50
|
|
|
|
51
|
|
|
$this->commands('tymon.jwt.secret'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Bind some Interfaces and implementations |
56
|
|
|
*/ |
57
|
|
|
protected function registerAliases() |
58
|
|
|
{ |
59
|
|
|
$this->app->alias('tymon.jwt.auth', JWTAuth::class); |
60
|
|
|
$this->app->alias('tymon.jwt.provider.jwt', JWT::class); |
61
|
|
|
$this->app->alias('tymon.jwt.provider.auth', Auth::class); |
62
|
|
|
$this->app->alias('tymon.jwt.provider.storage', Storage::class); |
63
|
|
|
$this->app->alias('tymon.jwt.manager', Manager::class); |
64
|
|
|
$this->app->alias('tymon.jwt.blacklist', Blacklist::class); |
65
|
|
|
$this->app->alias('tymon.jwt.payload.factory', Factory::class); |
66
|
|
|
$this->app->alias('tymon.jwt.validators.payload', PayloadValidator::class); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Register the bindings for the JSON Web Token provider |
71
|
|
|
*/ |
72
|
|
|
protected function registerJWTProvider() |
73
|
|
|
{ |
74
|
|
|
$this->app->singleton('tymon.jwt.provider.jwt', function ($app) { |
75
|
|
|
$provider = $this->config('providers.jwt'); |
76
|
|
|
|
77
|
|
|
return $app->make($provider, [$this->config('secret'), $this->config('algo')]); |
78
|
|
|
}); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Register the bindings for the Auth provider |
83
|
|
|
*/ |
84
|
|
|
protected function registerAuthProvider() |
85
|
|
|
{ |
86
|
|
|
$this->app->singleton('tymon.jwt.provider.auth', function () { |
87
|
|
|
return $this->getConfigInstance('providers.auth'); |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Register the bindings for the Storage provider |
93
|
|
|
*/ |
94
|
|
|
protected function registerStorageProvider() |
95
|
|
|
{ |
96
|
|
|
$this->app->singleton('tymon.jwt.provider.storage', function () { |
97
|
|
|
return $this->getConfigInstance('providers.storage'); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Register the bindings for the JWT Manager |
103
|
|
|
*/ |
104
|
|
|
protected function registerManager() |
105
|
|
|
{ |
106
|
|
|
$this->app->singleton('tymon.jwt.manager', function ($app) { |
107
|
|
|
|
108
|
|
|
$instance = new Manager( |
109
|
|
|
$app['tymon.jwt.provider.jwt'], |
110
|
|
|
$app['tymon.jwt.blacklist'], |
111
|
|
|
$app['tymon.jwt.payload.factory'] |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
return $instance->setBlacklistEnabled((bool) $this->config('blacklist_enabled')); |
115
|
|
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Register the bindings for the Token Parser |
120
|
|
|
*/ |
121
|
|
|
protected function registerTokenParser() |
122
|
|
|
{ |
123
|
|
|
$this->app->singleton('tymon.jwt.parser', function ($app) { |
124
|
|
|
return new TokenParser( |
125
|
|
|
$app['request'], |
126
|
|
|
[new AuthHeaders, new QueryString, new RouteParams] |
127
|
|
|
); |
128
|
|
|
}); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Register the bindings for the main JWTAuth class |
133
|
|
|
*/ |
134
|
|
|
protected function registerJWTAuth() |
135
|
|
|
{ |
136
|
|
|
$this->app->singleton('tymon.jwt.auth', function ($app) { |
137
|
|
|
return new JWTAuth( |
138
|
|
|
$app['tymon.jwt.manager'], |
139
|
|
|
$app['tymon.jwt.provider.auth'], |
140
|
|
|
$app['tymon.jwt.parser'] |
141
|
|
|
); |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Register the bindings for the main JWTAuth class |
147
|
|
|
*/ |
148
|
|
|
protected function registerJWTBlacklist() |
149
|
|
|
{ |
150
|
|
|
$this->app->singleton('tymon.jwt.blacklist', function ($app) { |
151
|
|
|
$instance = new Blacklist($app['tymon.jwt.provider.storage']); |
152
|
|
|
|
153
|
|
|
return $instance->setGracePeriod($this->config('blacklist_grace_period')) |
154
|
|
|
->setRefreshTTL($this->config('refresh_ttl')); |
155
|
|
|
}); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Register the bindings for the payload validator |
160
|
|
|
*/ |
161
|
|
|
protected function registerPayloadValidator() |
162
|
|
|
{ |
163
|
|
|
$this->app->singleton('tymon.jwt.validators.payload', function () { |
164
|
|
|
return (new PayloadValidator) |
165
|
|
|
->setRefreshTTL($this->config('refresh_ttl')) |
166
|
|
|
->setRequiredClaims($this->config('required_claims')); |
167
|
|
|
}); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Register the bindings for the Payload Factory |
172
|
|
|
*/ |
173
|
|
|
protected function registerPayloadFactory() |
174
|
|
|
{ |
175
|
|
|
$this->app->singleton('tymon.jwt.payload.factory', function ($app) { |
176
|
|
|
$factory = new Factory( |
177
|
|
|
new ClaimFactory, |
178
|
|
|
$app['request'], |
179
|
|
|
$app['tymon.jwt.validators.payload'] |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
return $factory->setTTL($this->config('ttl')); |
183
|
|
|
}); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Register the Artisan command |
188
|
|
|
*/ |
189
|
|
|
protected function registerJWTCommand() |
190
|
|
|
{ |
191
|
|
|
$this->app->singleton('tymon.jwt.secret', function () { |
192
|
|
|
return new JWTGenerateSecretCommand(); |
193
|
|
|
}); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Helper to get the config values |
198
|
|
|
* |
199
|
|
|
* @param string $key |
200
|
|
|
* |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
protected function config($key, $default = null) |
204
|
|
|
{ |
205
|
|
|
return config("jwt.$key", $default); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get an instantiable configuration instance. Pinched from dingo/api :) |
210
|
|
|
* |
211
|
|
|
* @param string $key |
212
|
|
|
* |
213
|
|
|
* @return mixed |
214
|
|
|
*/ |
215
|
|
|
protected function getConfigInstance($key) |
216
|
|
|
{ |
217
|
|
|
$instance = $this->config($key); |
218
|
|
|
|
219
|
|
|
if (is_string($instance)) { |
220
|
|
|
return $this->app->make($instance); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $instance; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|