|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Framgia\Jwt; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Auth\AuthManager; |
|
6
|
|
|
use Framgia\Jwt\Storage\CacheStorage; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Support\ServiceProvider; |
|
9
|
|
|
use Framgia\Jwt\Signers\Factory as SignerFactory; |
|
10
|
|
|
use Illuminate\Contracts\Cache\Repository as Cache; |
|
11
|
|
|
use Framgia\Jwt\Contracts\Signer as SignerContract; |
|
12
|
|
|
use Framgia\Jwt\Contracts\Storage as StorageContract; |
|
13
|
|
|
|
|
14
|
|
|
class JwtServiceProvider extends ServiceProvider |
|
15
|
|
|
{ |
|
16
|
|
|
public function boot() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->publishes([ |
|
19
|
|
|
__DIR__ . '/../config/jwt.php' => config_path('api.php') |
|
20
|
|
|
]); |
|
21
|
|
|
|
|
22
|
|
|
$this->mergeConfigFrom(__DIR__ . '/../config/jwt.php', 'jwt'); |
|
23
|
|
|
|
|
24
|
|
|
$manager = $this->app[AuthManager::class]; |
|
25
|
|
|
|
|
26
|
|
|
$manager->extend('jwt', function ($app, $name, $config) use ($manager) { |
|
27
|
|
|
$guard = new Guard($manager->provider($config['provider']), $app[Request::class], $app[Blacklist::class], $app[SignerContract::class]); |
|
28
|
|
|
|
|
29
|
|
|
$app->refresh(Request::class, $guard, 'setRequest'); |
|
30
|
|
|
|
|
31
|
|
|
return $guard; |
|
32
|
|
|
}); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Register services |
|
37
|
|
|
* |
|
38
|
|
|
* @return void |
|
39
|
|
|
*/ |
|
40
|
|
|
public function register() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->app->singleton(CacheStorage::class, function ($app) { |
|
43
|
|
|
return new CacheStorage($app[Cache::class]); |
|
44
|
|
|
}); |
|
45
|
|
|
|
|
46
|
|
|
$this->app->singleton(Blacklist::class, function ($app) { |
|
47
|
|
|
return new Blacklist($app[StorageContract::class]); |
|
48
|
|
|
}); |
|
49
|
|
|
|
|
50
|
|
|
$this->app->singleton(SignerFactory::class, function ($app) { |
|
51
|
|
|
return new SignerFactory($app); |
|
52
|
|
|
}); |
|
53
|
|
|
|
|
54
|
|
|
$this->app->bind(SignerContract::class, function ($app) { |
|
55
|
|
|
return $app[SignerFactory::class]->driver(); |
|
56
|
|
|
}); |
|
57
|
|
|
|
|
58
|
|
|
$this->app->alias(CacheStorage::class, StorageContract::class); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|