Completed
Push — develop ( 8cd4fc...053c5a )
by Sean
02:37
created

LumenServiceProvider::boot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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