1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright 2018 Hilmi Erdem KEREN |
5
|
|
|
* @license MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Erdemkeren\Otp; |
9
|
|
|
|
10
|
|
|
use Erdemkeren\Otp\Http\Middleware\Otp; |
11
|
|
|
use Erdemkeren\Otp\Repositories\DatabaseTokenRepository; |
12
|
|
|
use Illuminate\Routing\Router; |
13
|
|
|
use Illuminate\Support\ServiceProvider as BaseServiceProvider; |
14
|
|
|
|
15
|
|
|
class ServiceProvider extends BaseServiceProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Indicates if loading of the provider is deferred. |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected bool $defer = true; |
23
|
|
|
|
24
|
|
|
public function boot(): void |
25
|
|
|
{ |
26
|
|
|
$this->publishes([$this->configPath() => config_path('otp.php')]); |
|
|
|
|
27
|
|
|
$this->publishes([$this->migrationPath() => database_path('migrations')]); |
|
|
|
|
28
|
|
|
$this->publishes([$this->viewPath() => resource_path('views')]); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Register the otp service. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function register(): void |
37
|
|
|
{ |
38
|
|
|
$service = $this->createServiceInstance(); |
39
|
|
|
$this->registerDefaultPasswordGenerators($service); |
40
|
|
|
|
41
|
|
|
$this->app->singleton('otp', function () use ($service) { |
42
|
|
|
return $service; |
43
|
|
|
}); |
44
|
|
|
|
45
|
|
|
/** @var Router $router */ |
46
|
|
|
$router = $this->app['router']; |
47
|
|
|
$router->aliasMiddleware('otp', Otp::class); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the services provided by the provider. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function provides(): array |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'otp', |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Create a new otp service instance. |
64
|
|
|
* |
65
|
|
|
* @return OtpService |
66
|
|
|
*/ |
67
|
|
|
private function createServiceInstance(): OtpService |
68
|
|
|
{ |
69
|
|
|
return new OtpService( |
70
|
|
|
new FormatManager(), |
|
|
|
|
71
|
|
|
new Encryptor(config('app.secret')), |
|
|
|
|
72
|
|
|
new DatabaseTokenRepository() |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Register default password generators to the |
78
|
|
|
* given otp service instance. |
79
|
|
|
* |
80
|
|
|
* @param OtpService $service |
81
|
|
|
*/ |
82
|
|
|
private function registerDefaultPasswordGenerators($service): void |
83
|
|
|
{ |
84
|
|
|
$service->addPasswordGenerator('string', Generators\StringGenerator::class); |
|
|
|
|
85
|
|
|
$service->addPasswordGenerator('numeric', Generators\NumericGenerator::class); |
86
|
|
|
$service->addPasswordGenerator('numeric-no-0', Generators\NumericNo0Generator::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the project config path. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
private function configPath(): string |
95
|
|
|
{ |
96
|
|
|
return __DIR__.'/../config/otp.php'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the migration path. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function migrationPath(): string |
105
|
|
|
{ |
106
|
|
|
return __DIR__.'/../database/migrations/'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the view path. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
private function viewPath(): string |
115
|
|
|
{ |
116
|
|
|
return __DIR__.'/../views/'; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|