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\PasswordGenerators as Generators; |
12
|
|
|
use Illuminate\Routing\Router; |
13
|
|
|
use Illuminate\Support\ServiceProvider; |
14
|
|
|
|
15
|
|
|
class OtpServiceProvider extends ServiceProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Indicates if loading of the provider is deferred. |
19
|
|
|
* |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $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
|
|
|
public function register(): void |
35
|
|
|
{ |
36
|
|
|
$service = $this->createServiceInstance(); |
37
|
|
|
$this->registerDefaultPasswordGenerators($service); |
38
|
|
|
|
39
|
|
|
$this->app->singleton('otp', function () use ($service) { |
40
|
|
|
return $service; |
41
|
|
|
}); |
42
|
|
|
|
43
|
|
|
/** @var Router $router */ |
44
|
|
|
$router = $this->app['router']; |
45
|
|
|
$router->aliasMiddleware('otp', Otp::class); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the services provided by the provider. |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function provides(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'otp', |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create a new otp service instance. |
62
|
|
|
* |
63
|
|
|
* @return OtpService |
64
|
|
|
*/ |
65
|
|
|
private function createServiceInstance(): OtpService |
66
|
|
|
{ |
67
|
|
|
return new OtpService( |
68
|
|
|
new PasswordGeneratorManager(), |
69
|
|
|
new Encryptor(config('app.secret')), |
70
|
|
|
config('otp.password_generator', 'string'), |
|
|
|
|
71
|
|
|
6, |
72
|
|
|
Token::class |
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\StringPasswordGenerator::class); |
85
|
|
|
$service->addPasswordGenerator('numeric', Generators\NumericPasswordGenerator::class); |
86
|
|
|
$service->addPasswordGenerator('numeric-no-0', Generators\NumericNo0PasswordGenerator::class); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the project config path. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
private function configPath() |
95
|
|
|
{ |
96
|
|
|
return __DIR__.'/../config/otp.php'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the migration path. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function migrationPath() |
105
|
|
|
{ |
106
|
|
|
return __DIR__.'/../database/migrations/'; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the view path. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
private function viewPath() |
115
|
|
|
{ |
116
|
|
|
return __DIR__.'/../views/'; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|