4
4
5
5
use Exception ;
6
6
use Illuminate \Support \Facades \Cache ;
7
- use Illuminate \Support \Facades \Facade ;
8
7
use Illuminate \Support \Str ;
9
- use Illuminate \Foundation \Application ;
10
8
11
9
class Otp
12
10
{
13
11
private string $ format = 'numeric ' ;
12
+
14
13
private string $ customize ;
14
+
15
15
private int $ length = 6 ;
16
+
16
17
private string $ separator = '- ' ;
18
+
17
19
private bool $ sensitive = false ;
20
+
18
21
private int $ expires = 15 ;
22
+
19
23
private int $ attempts = 5 ;
24
+
20
25
private bool $ repeated = true ;
26
+
21
27
private bool $ disposable = true ;
28
+
22
29
private string $ prefix = 'OTPPX_ ' ;
30
+
23
31
private $ data ;
32
+
24
33
private bool $ skip = false ;
34
+
25
35
private bool $ demo = false ;
36
+
26
37
private array $ demo_passwords = ['1234 ' , '123456 ' , '12345678 ' ];
27
38
28
39
public function __construct ()
29
40
{
30
41
foreach (['format ' , 'customize ' , 'length ' , 'separator ' , 'sensitive ' , 'expires ' , 'attempts ' , 'repeated ' , 'disposable ' , 'prefix ' , 'data ' , 'demo ' , 'demo_passwords ' ] as $ value ) {
31
- if (!empty (config ('otp. ' . $ value ))) $ this ->{$ value } = config ('otp. ' . $ value );
42
+ if (! empty (config ('otp. ' .$ value ))) {
43
+ $ this ->{$ value } = config ('otp. ' .$ value );
44
+ }
32
45
}
33
46
}
34
47
35
48
public function __call (string $ method , $ params )
36
49
{
37
- if (!str_starts_with ($ method , 'set ' )) {
50
+ if (! str_starts_with ($ method , 'set ' )) {
38
51
return ;
39
52
}
40
53
41
54
$ property = Str::camel (substr ($ method , 3 ));
42
- if (!property_exists ($ this , $ property )) {
55
+ if (! property_exists ($ this , $ property )) {
43
56
return ;
44
57
}
45
58
46
59
$ this ->{$ property } = $ params [0 ] ?? null ;
47
60
if ($ property == 'customize ' ) {
48
61
$ this ->format = 'customize ' ;
49
62
}
63
+
50
64
return $ this ;
51
65
}
52
66
53
67
/**
54
68
* @throws Exception
55
69
*/
56
- public function generate (string $ identifier = null , array $ options = []): string
70
+ public function generate (? string $ identifier = null , array $ options = []): string
57
71
{
58
- if (!empty ($ options )) foreach (['format ' , 'customize ' , 'length ' , 'separator ' , 'sensitive ' , 'expires ' , 'repeated ' , 'prefix ' , 'data ' ] as $ value ) {
59
- if (isset ($ options [$ value ])) $ this ->{$ value } = $ options [$ value ];
72
+ if (! empty ($ options )) {
73
+ foreach (['format ' , 'customize ' , 'length ' , 'separator ' , 'sensitive ' , 'expires ' , 'repeated ' , 'prefix ' , 'data ' ] as $ value ) {
74
+ if (isset ($ options [$ value ])) {
75
+ $ this ->{$ value } = $ options [$ value ];
76
+ }
77
+ }
60
78
}
61
79
62
- if ($ identifier === null ) $ identifier = session ()->getId ();
80
+ if ($ identifier === null ) {
81
+ $ identifier = session ()->getId ();
82
+ }
63
83
$ array = $ this ->repeated ? $ this ->readData ($ identifier , []) : [];
64
84
$ password = $ this ->generateNewPassword ();
65
- if (!$ this ->sensitive ) $ password = strtoupper ($ password );
85
+ if (! $ this ->sensitive ) {
86
+ $ password = strtoupper ($ password );
87
+ }
66
88
$ array [md5 ($ password )] = [
67
89
'expires ' => time () + $ this ->expires * 60 ,
68
- 'data ' => $ this ->data
90
+ 'data ' => $ this ->data ,
69
91
];
70
92
$ this ->writeData ($ identifier , $ array );
93
+
71
94
return $ password ;
72
95
}
73
96
74
- public function validate (string $ identifier = null , string $ password = null , array $ options = []): object
97
+ public function validate (? string $ identifier = null , ? string $ password = null , array $ options = []): object
75
98
{
76
- if (!empty ($ options )) foreach (['attempts ' , 'sensitive ' , 'disposable ' , 'skip ' ] as $ value ) {
77
- if (isset ($ options [$ value ])) $ this ->{$ value } = $ options [$ value ];
99
+ if (! empty ($ options )) {
100
+ foreach (['attempts ' , 'sensitive ' , 'disposable ' , 'skip ' ] as $ value ) {
101
+ if (isset ($ options [$ value ])) {
102
+ $ this ->{$ value } = $ options [$ value ];
103
+ }
104
+ }
78
105
}
79
106
80
107
if ($ password === null ) {
81
108
if ($ identifier === null ) {
82
- throw new Exception (" Validate parameter can not be null " );
109
+ throw new Exception (' Validate parameter can not be null ' );
83
110
}
84
111
$ password = $ identifier ;
85
112
$ identifier = null ;
86
113
}
87
114
88
115
if ($ this ->demo && in_array ($ password , $ this ->demo_passwords )) {
89
- return (object )[
116
+ return (object ) [
90
117
'status ' => true ,
91
- 'demo ' => true
118
+ 'demo ' => true ,
92
119
];
93
120
}
94
121
95
- if ($ identifier === null ) $ identifier = session ()->getId ();
96
- $ attempt = $ this ->readData ('_attempt_ ' . $ identifier , 0 );
122
+ if ($ identifier === null ) {
123
+ $ identifier = session ()->getId ();
124
+ }
125
+ $ attempt = $ this ->readData ('_attempt_ ' .$ identifier , 0 );
97
126
if ($ attempt >= $ this ->attempts ) {
98
- return (object )[
127
+ return (object ) [
99
128
'status ' => false ,
100
129
'error ' => 'max_attempt ' ,
101
130
];
102
131
}
103
132
104
133
$ codes = $ this ->readData ($ identifier , []);
105
- if (!$ this ->sensitive ) $ password = strtoupper ($ password );
134
+ if (! $ this ->sensitive ) {
135
+ $ password = strtoupper ($ password );
136
+ }
137
+
138
+ if (! isset ($ codes [md5 ($ password )])) {
139
+ $ this ->writeData ('_attempt_ ' .$ identifier , $ attempt + 1 );
106
140
107
- if (!isset ($ codes [md5 ($ password )])) {
108
- $ this ->writeData ('_attempt_ ' . $ identifier , $ attempt + 1 );
109
- return (object )[
141
+ return (object ) [
110
142
'status ' => false ,
111
143
'error ' => 'invalid ' ,
112
144
];
113
145
}
114
146
115
147
if (time () > $ codes [md5 ($ password )]['expires ' ]) {
116
- $ this ->writeData ('_attempt_ ' . $ identifier , $ attempt + 1 );
117
- return (object )[
148
+ $ this ->writeData ('_attempt_ ' .$ identifier , $ attempt + 1 );
149
+
150
+ return (object ) [
118
151
'status ' => false ,
119
152
'error ' => 'expired ' ,
120
153
];
@@ -124,37 +157,46 @@ public function validate(string $identifier = null, string $password = null, arr
124
157
'status ' => true ,
125
158
];
126
159
127
- if (!empty ($ codes [md5 ($ password )]['data ' ])) {
160
+ if (! empty ($ codes [md5 ($ password )]['data ' ])) {
128
161
$ response ['data ' ] = $ codes [md5 ($ password )]['data ' ];
129
162
}
130
163
131
- if (!$ this ->skip ) $ this ->forget ($ identifier , !$ this ->disposable ? $ password : null );
164
+ if (! $ this ->skip ) {
165
+ $ this ->forget ($ identifier , ! $ this ->disposable ? $ password : null );
166
+ }
132
167
$ this ->resetAttempt ($ identifier );
133
168
134
- return (object )$ response ;
169
+ return (object ) $ response ;
135
170
}
136
171
137
- public function forget (string $ identifier = null , string $ password = null ): bool
172
+ public function forget (? string $ identifier = null , ? string $ password = null ): bool
138
173
{
139
- if ($ identifier === null ) $ identifier = session ()->getId ();
174
+ if ($ identifier === null ) {
175
+ $ identifier = session ()->getId ();
176
+ }
140
177
if ($ password !== null ) {
141
178
$ codes = $ this ->readData ($ identifier , []);
142
- if (!isset ($ codes [md5 ($ password )])) {
179
+ if (! isset ($ codes [md5 ($ password )])) {
143
180
return false ;
144
181
}
145
182
unset($ codes [md5 ($ password )]);
146
183
$ this ->writeData ($ identifier , $ codes );
184
+
147
185
return true ;
148
186
}
149
187
150
188
$ this ->deleteData ($ identifier );
189
+
151
190
return true ;
152
191
}
153
192
154
- public function resetAttempt (string $ identifier = null ): bool
193
+ public function resetAttempt (? string $ identifier = null ): bool
155
194
{
156
- if ($ identifier === null ) $ identifier = session ()->getId ();
157
- $ this ->deleteData ('_attempt_ ' . $ identifier );
195
+ if ($ identifier === null ) {
196
+ $ identifier = session ()->getId ();
197
+ }
198
+ $ this ->deleteData ('_attempt_ ' .$ identifier );
199
+
158
200
return true ;
159
201
}
160
202
@@ -168,7 +210,7 @@ private function generateNewPassword(): string
168
210
'string ' => $ this ->sensitive ? '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ ' : '23456789ABCDEFGHJKLMNPQRSTUVWXYZ ' ,
169
211
'numeric ' => '0123456789 ' ,
170
212
'numeric-no-zero ' => '123456789 ' ,
171
- 'customize ' => $ this ->customize
213
+ 'customize ' => $ this ->customize ,
172
214
];
173
215
174
216
$ lengths = is_array ($ this ->length ) ? $ this ->length : [$ this ->length ];
@@ -180,22 +222,22 @@ private function generateNewPassword(): string
180
222
181
223
return implode ($ this ->separator , $ password );
182
224
} catch (Exception ) {
183
- throw new Exception (" Fail to generate password, please check the format is correct. " );
225
+ throw new Exception (' Fail to generate password, please check the format is correct. ' );
184
226
}
185
227
}
186
228
187
229
private function writeData (string $ key , mixed $ value ): void
188
230
{
189
- Cache::put ($ this ->prefix . $ key , $ value , $ this ->expires * 60 * 3 );
231
+ Cache::put ($ this ->prefix . $ key , $ value , $ this ->expires * 60 * 3 );
190
232
}
191
233
192
234
private function readData (string $ key , mixed $ default = null ): mixed
193
235
{
194
- return Cache::get ($ this ->prefix . $ key , $ default );
236
+ return Cache::get ($ this ->prefix . $ key , $ default );
195
237
}
196
238
197
239
private function deleteData (string $ key ): void
198
240
{
199
- Cache::forget ($ this ->prefix . $ key );
241
+ Cache::forget ($ this ->prefix . $ key );
200
242
}
201
- }
243
+ }
0 commit comments