|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniSharp\JWT; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\ServiceProvider; |
|
6
|
|
|
use UniSharp\JWT\Auth\Guards\JWTAuthGuard; |
|
7
|
|
|
use UniSharp\JWT\Http\Middleware\JWTRefresh; |
|
8
|
|
|
|
|
9
|
|
|
class JWTServiceProvider extends ServiceProvider |
|
10
|
|
|
{ |
|
11
|
|
|
protected $configs; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The middleware aliases. |
|
15
|
|
|
* |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $middlewareAliases = [ |
|
19
|
|
|
'laravel.jwt' => JWTRefresh::class, |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Boot the services for the application. |
|
24
|
|
|
* |
|
25
|
|
|
* @return void |
|
26
|
|
|
*/ |
|
27
|
|
|
public function boot() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->bootConfig(); |
|
30
|
|
|
$this->loadConfig(); |
|
31
|
|
|
$this->extendAuthGuard(); |
|
32
|
|
|
$this->registerMiddleware(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Register any application services. |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
public function register() |
|
41
|
|
|
{ |
|
42
|
|
|
// |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Boot configure. |
|
47
|
|
|
* |
|
48
|
|
|
* @return void |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function bootConfig() |
|
51
|
|
|
{ |
|
52
|
|
|
$path = __DIR__ . '/config/laravel_jwt.php'; |
|
53
|
|
|
$this->mergeConfigFrom($path, 'laravel_jwt'); |
|
54
|
|
|
if (function_exists('config_path')) { |
|
55
|
|
|
$this->publishes([$path => config_path('laravel_jwt.php')]); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Register middleware. |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function registerMiddleware() |
|
65
|
|
|
{ |
|
66
|
|
|
if ($this->isLumen()) { |
|
67
|
|
|
$this->app->routeMiddleware($this->middlewareAliases); |
|
|
|
|
|
|
68
|
|
|
} else { |
|
69
|
|
|
$this->aliasMiddleware(); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Load configure. |
|
75
|
|
|
* |
|
76
|
|
|
* @return void |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function loadConfig($configs = []) |
|
79
|
|
|
{ |
|
80
|
|
|
$this->configs = $configs ?: config('laravel_jwt'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Extend auth guard. |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function extendAuthGuard() |
|
89
|
|
|
{ |
|
90
|
|
|
$this->app['auth']->extend('jwt-auth', function ($app, $name, array $config) { |
|
91
|
|
|
$guard = new JWTAuthGuard( |
|
92
|
|
|
$app['tymon.jwt'], |
|
93
|
|
|
$app['auth']->createUserProvider($config['provider']), |
|
94
|
|
|
$app['request'] |
|
95
|
|
|
); |
|
96
|
|
|
|
|
97
|
|
|
$app->refresh('request', $guard, 'setRequest'); |
|
98
|
|
|
|
|
99
|
|
|
return $guard; |
|
100
|
|
|
}); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Return isLumen. |
|
105
|
|
|
* |
|
106
|
|
|
* @return boolean |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function isLumen() |
|
109
|
|
|
{ |
|
110
|
|
|
return str_contains($this->app->version(), 'Lumen'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Alias the middleware. |
|
115
|
|
|
* |
|
116
|
|
|
* @return void |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function aliasMiddleware() |
|
119
|
|
|
{ |
|
120
|
|
|
$router = $this->app['router']; |
|
121
|
|
|
$method = method_exists($router, 'aliasMiddleware') ? 'aliasMiddleware' : 'middleware'; |
|
122
|
|
|
foreach ($this->middlewareAliases as $alias => $middleware) { |
|
123
|
|
|
$router->$method($alias, $middleware); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
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.