1 | <?php |
||
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($app['request']) |
||
125 | ->setChainOrder([ |
||
|
|||
126 | new AuthHeaders, |
||
127 | new QueryString, |
||
128 | new RouteParams |
||
129 | ]); |
||
130 | }); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Register the bindings for the main JWTAuth class |
||
135 | */ |
||
136 | protected function registerJWTAuth() |
||
137 | { |
||
138 | $this->app->singleton('tymon.jwt.auth', function ($app) { |
||
139 | return new JWTAuth( |
||
140 | $app['tymon.jwt.manager'], |
||
141 | $app['tymon.jwt.provider.auth'], |
||
142 | $app['tymon.jwt.parser'] |
||
143 | ); |
||
144 | }); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Register the bindings for the main JWTAuth class |
||
149 | */ |
||
150 | protected function registerJWTBlacklist() |
||
151 | { |
||
152 | $this->app->singleton('tymon.jwt.blacklist', function ($app) { |
||
153 | $instance = new Blacklist($app['tymon.jwt.provider.storage']); |
||
154 | |||
155 | return $instance->setGracePeriod($this->config('blacklist_grace_period')) |
||
156 | ->setRefreshTTL($this->config('refresh_ttl')); |
||
157 | }); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Register the bindings for the payload validator |
||
162 | */ |
||
163 | protected function registerPayloadValidator() |
||
164 | { |
||
165 | $this->app->singleton('tymon.jwt.validators.payload', function () { |
||
166 | return (new PayloadValidator) |
||
167 | ->setRefreshTTL($this->config('refresh_ttl')) |
||
168 | ->setRequiredClaims($this->config('required_claims')); |
||
169 | }); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Register the bindings for the Payload Factory |
||
174 | */ |
||
175 | protected function registerPayloadFactory() |
||
176 | { |
||
177 | $this->app->singleton('tymon.jwt.payload.factory', function ($app) { |
||
178 | $factory = new Factory( |
||
179 | new ClaimFactory, |
||
180 | $app['request'], |
||
181 | $app['tymon.jwt.validators.payload'] |
||
182 | ); |
||
183 | |||
184 | return $factory->setTTL($this->config('ttl')); |
||
185 | }); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Register the Artisan command |
||
190 | */ |
||
191 | protected function registerJWTCommand() |
||
192 | { |
||
193 | $this->app->singleton('tymon.jwt.secret', function () { |
||
194 | return new JWTGenerateSecretCommand(); |
||
195 | }); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Helper to get the config values |
||
200 | * |
||
201 | * @param string $key |
||
202 | * |
||
203 | * @return mixed |
||
204 | */ |
||
205 | protected function config($key, $default = null) |
||
206 | { |
||
207 | return config("jwt.$key", $default); |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Get an instantiable configuration instance. Pinched from dingo/api :) |
||
212 | * |
||
213 | * @param string $key |
||
214 | * |
||
215 | * @return mixed |
||
216 | */ |
||
217 | protected function getConfigInstance($key) |
||
218 | { |
||
219 | $instance = $this->config($key); |
||
220 | |||
221 | if (is_string($instance)) { |
||
222 | return $this->app->make($instance); |
||
223 | } |
||
228 |