Completed
Push — develop ( e2870e...e9d502 )
by Sean
02:25
created

LumenServiceProvider::registerJWT()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 0
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\JWT;
15
use Tymon\JWTAuth\JWTAuth;
16
use Tymon\JWTAuth\Manager;
17
use Tymon\JWTAuth\Factory;
18
use Tymon\JWTAuth\JWTGuard;
19
use Tymon\JWTAuth\Blacklist;
20
use Tymon\JWTAuth\Http\Parser;
21
use Tymon\JWTAuth\Http\AuthHeaders;
22
use Tymon\JWTAuth\Http\QueryString;
23
use Tymon\JWTAuth\Http\RouteParams;
24
use Illuminate\Support\ServiceProvider;
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
use Tymon\JWTAuth\Contracts\Providers\JWT as JWTContract;
31
32
class LumenServiceProvider extends ServiceProvider
33
{
34
    /**
35
     * Register the service provider.
36
     *
37
     * @return void
38
     */
39
    public function register()
40
    {
41
        $this->registerAliases();
42
43
        $this->registerJWTProvider();
44
        $this->registerAuthProvider();
45
        $this->registerStorageProvider();
46
        $this->registerJWTBlacklist();
47
48
        $this->registerManager();
49
        $this->registerTokenParser();
50
51
        $this->registerJWT();
52
        $this->registerJWTAuth();
53
        $this->registerPayloadValidator();
54
        $this->registerPayloadFactory();
55
        $this->registerJWTCommand();
56
57
        $this->commands('tymon.jwt.secret');
58
    }
59
60
    /**
61
     * Boot the service provider.
62
     */
63
    public function boot()
64
    {
65
        $this->app->configure('jwt');
0 ignored issues
show
Bug introduced by
The method configure() does not exist on Illuminate\Contracts\Foundation\Application. Did you maybe mean registerConfiguredProviders()?

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.

Loading history...
66
        $this->extendAuthGuard();
67
    }
68
69
    protected function extendAuthGuard()
70
    {
71
        $this->app['auth']->extend('jwt', function ($app, $name, array $config) {
72
73
            $guard = new JwtGuard(
74
                $app['tymon.jwt'],
75
                $app['auth']->createUserProvider($config['provider']),
76
                $app['request']
77
            );
78
79
            $app->refresh('request', $guard, 'setRequest');
80
81
            return $guard;
82
        });
83
    }
84
85
    /**
86
     * Bind some Interfaces and implementations
87
     */
88
    protected function registerAliases()
89
    {
90
        $this->app->alias('tymon.jwt', JWT::class);
91
        $this->app->alias('tymon.jwt.auth', JWTAuth::class);
92
        $this->app->alias('tymon.jwt.provider.jwt', JWTContract::class);
93
        $this->app->alias('tymon.jwt.provider.auth', Auth::class);
94
        $this->app->alias('tymon.jwt.provider.storage', Storage::class);
95
        $this->app->alias('tymon.jwt.manager', Manager::class);
96
        $this->app->alias('tymon.jwt.blacklist', Blacklist::class);
97
        $this->app->alias('tymon.jwt.payload.factory', Factory::class);
98
        $this->app->alias('tymon.jwt.validators.payload', PayloadValidator::class);
99
    }
100
101
    /**
102
     * Register the bindings for the JSON Web Token provider
103
     */
104
    protected function registerJWTProvider()
105
    {
106
        $this->app->singleton('tymon.jwt.provider.jwt', function ($app) {
107
            $provider = $this->config('providers.jwt');
108
109
            return $app->make($provider, [$this->config('secret'), $this->config('algo')]);
110
        });
111
    }
112
113
    /**
114
     * Register the bindings for the Auth provider
115
     */
116
    protected function registerAuthProvider()
117
    {
118
        $this->app->singleton('tymon.jwt.provider.auth', function () {
119
            return $this->getConfigInstance('providers.auth');
120
        });
121
    }
122
123
    /**
124
     * Register the bindings for the Storage provider
125
     */
126
    protected function registerStorageProvider()
127
    {
128
        $this->app->singleton('tymon.jwt.provider.storage', function () {
129
            return $this->getConfigInstance('providers.storage');
130
        });
131
    }
132
133
    /**
134
     * Register the bindings for the JWT Manager
135
     */
136
    protected function registerManager()
137
    {
138
        $this->app->singleton('tymon.jwt.manager', function ($app) {
139
140
            $instance = new Manager(
141
                $app['tymon.jwt.provider.jwt'],
142
                $app['tymon.jwt.blacklist'],
143
                $app['tymon.jwt.payload.factory']
144
            );
145
146
            return $instance->setBlacklistEnabled((bool) $this->config('blacklist_enabled'));
147
        });
148
    }
149
150
    /**
151
     * Register the bindings for the Token Parser
152
     */
153
    protected function registerTokenParser()
154
    {
155
        $this->app->singleton('tymon.jwt.parser', function ($app) {
156
            return new Parser(
157
                $app['request'],
158
                [new AuthHeaders, new QueryString, new RouteParams]
159
            );
160
        });
161
    }
162
163
    /**
164
     * Register the bindings for the main JWTAuth class
165
     */
166
    protected function registerJWT()
167
    {
168
        $this->app->singleton('tymon.jwt', function ($app) {
169
            return new JWT(
170
                $app['tymon.jwt.manager'],
171
                $app['tymon.jwt.parser']
172
            );
173
        });
174
    }
175
176
    /**
177
     * Register the bindings for the main JWTAuth class
178
     */
179
    protected function registerJWTAuth()
180
    {
181
        $this->app->singleton('tymon.jwt.auth', function ($app) {
182
            return new JWTAuth(
183
                $app['tymon.jwt.manager'],
184
                $app['tymon.jwt.provider.auth'],
185
                $app['tymon.jwt.parser']
186
            );
187
        });
188
    }
189
190
    /**
191
     * Register the bindings for the main JWTAuth class
192
     */
193
    protected function registerJWTBlacklist()
194
    {
195
        $this->app->singleton('tymon.jwt.blacklist', function ($app) {
196
            $instance = new Blacklist($app['tymon.jwt.provider.storage']);
197
198
            return $instance->setGracePeriod($this->config('blacklist_grace_period'))
199
                            ->setRefreshTTL($this->config('refresh_ttl'));
200
        });
201
    }
202
203
    /**
204
     * Register the bindings for the payload validator
205
     */
206
    protected function registerPayloadValidator()
207
    {
208
        $this->app->singleton('tymon.jwt.validators.payload', function () {
209
            return (new PayloadValidator)
210
                ->setRefreshTTL($this->config('refresh_ttl'))
211
                ->setRequiredClaims($this->config('required_claims'));
212
        });
213
    }
214
215
    /**
216
     * Register the bindings for the Payload Factory
217
     */
218
    protected function registerPayloadFactory()
219
    {
220
        $this->app->singleton('tymon.jwt.payload.factory', function ($app) {
221
            $factory = new Factory(
222
                new ClaimFactory,
223
                $app['request'],
224
                $app['tymon.jwt.validators.payload']
225
            );
226
227
            return $factory->setTTL($this->config('ttl'));
228
        });
229
    }
230
231
    /**
232
     * Register the Artisan command
233
     */
234
    protected function registerJWTCommand()
235
    {
236
        $this->app->singleton('tymon.jwt.secret', function () {
237
            return new JWTGenerateSecretCommand();
238
        });
239
    }
240
241
    /**
242
     * Helper to get the config values
243
     *
244
     * @param  string $key
245
     *
246
     * @return mixed
247
     */
248
    protected function config($key, $default = null)
249
    {
250
        return config("jwt.$key", $default);
251
    }
252
253
    /**
254
     * Get an instantiable configuration instance.
255
     *
256
     * @param  string  $key
257
     *
258
     * @return mixed
259
     */
260
    protected function getConfigInstance($key)
261
    {
262
        $instance = $this->config($key);
263
264
        if (is_string($instance)) {
265
            return $this->app->make($instance);
266
        }
267
268
        return $instance;
269
    }
270
}
271