44
55use Exception ;
66use Illuminate \Support \Facades \Cache ;
7- use Illuminate \Support \Facades \Facade ;
87use Illuminate \Support \Str ;
9- use Illuminate \Foundation \Application ;
108
119class Otp
1210{
1311 private string $ format = 'numeric ' ;
12+
1413 private string $ customize ;
14+
1515 private int $ length = 6 ;
16+
1617 private string $ separator = '- ' ;
18+
1719 private bool $ sensitive = false ;
20+
1821 private int $ expires = 15 ;
22+
1923 private int $ attempts = 5 ;
24+
2025 private bool $ repeated = true ;
26+
2127 private bool $ disposable = true ;
28+
2229 private string $ prefix = 'OTPPX_ ' ;
30+
2331 private $ data ;
32+
2433 private bool $ skip = false ;
34+
2535 private bool $ demo = false ;
36+
2637 private array $ demo_passwords = ['1234 ' , '123456 ' , '12345678 ' ];
2738
2839 public function __construct ()
2940 {
3041 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+ }
3245 }
3346 }
3447
3548 public function __call (string $ method , $ params )
3649 {
37- if (!str_starts_with ($ method , 'set ' )) {
50+ if (! str_starts_with ($ method , 'set ' )) {
3851 return ;
3952 }
4053
4154 $ property = Str::camel (substr ($ method , 3 ));
42- if (!property_exists ($ this , $ property )) {
55+ if (! property_exists ($ this , $ property )) {
4356 return ;
4457 }
4558
4659 $ this ->{$ property } = $ params [0 ] ?? null ;
4760 if ($ property == 'customize ' ) {
4861 $ this ->format = 'customize ' ;
4962 }
63+
5064 return $ this ;
5165 }
5266
5367 /**
5468 * @throws Exception
5569 */
56- public function generate (string $ identifier = null , array $ options = []): string
70+ public function generate (? string $ identifier = null , array $ options = []): string
5771 {
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+ }
6078 }
6179
62- if ($ identifier === null ) $ identifier = session ()->getId ();
80+ if ($ identifier === null ) {
81+ $ identifier = session ()->getId ();
82+ }
6383 $ array = $ this ->repeated ? $ this ->readData ($ identifier , []) : [];
6484 $ password = $ this ->generateNewPassword ();
65- if (!$ this ->sensitive ) $ password = strtoupper ($ password );
85+ if (! $ this ->sensitive ) {
86+ $ password = strtoupper ($ password );
87+ }
6688 $ array [md5 ($ password )] = [
6789 'expires ' => time () + $ this ->expires * 60 ,
68- 'data ' => $ this ->data
90+ 'data ' => $ this ->data ,
6991 ];
7092 $ this ->writeData ($ identifier , $ array );
93+
7194 return $ password ;
7295 }
7396
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
7598 {
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+ }
78105 }
79106
80107 if ($ password === null ) {
81108 if ($ identifier === null ) {
82- throw new Exception (" Validate parameter can not be null " );
109+ throw new Exception (' Validate parameter can not be null ' );
83110 }
84111 $ password = $ identifier ;
85112 $ identifier = null ;
86113 }
87114
88115 if ($ this ->demo && in_array ($ password , $ this ->demo_passwords )) {
89- return (object )[
116+ return (object ) [
90117 'status ' => true ,
91- 'demo ' => true
118+ 'demo ' => true ,
92119 ];
93120 }
94121
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 );
97126 if ($ attempt >= $ this ->attempts ) {
98- return (object )[
127+ return (object ) [
99128 'status ' => false ,
100129 'error ' => 'max_attempt ' ,
101130 ];
102131 }
103132
104133 $ 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 );
106140
107- if (!isset ($ codes [md5 ($ password )])) {
108- $ this ->writeData ('_attempt_ ' . $ identifier , $ attempt + 1 );
109- return (object )[
141+ return (object ) [
110142 'status ' => false ,
111143 'error ' => 'invalid ' ,
112144 ];
113145 }
114146
115147 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 ) [
118151 'status ' => false ,
119152 'error ' => 'expired ' ,
120153 ];
@@ -124,37 +157,46 @@ public function validate(string $identifier = null, string $password = null, arr
124157 'status ' => true ,
125158 ];
126159
127- if (!empty ($ codes [md5 ($ password )]['data ' ])) {
160+ if (! empty ($ codes [md5 ($ password )]['data ' ])) {
128161 $ response ['data ' ] = $ codes [md5 ($ password )]['data ' ];
129162 }
130163
131- if (!$ this ->skip ) $ this ->forget ($ identifier , !$ this ->disposable ? $ password : null );
164+ if (! $ this ->skip ) {
165+ $ this ->forget ($ identifier , ! $ this ->disposable ? $ password : null );
166+ }
132167 $ this ->resetAttempt ($ identifier );
133168
134- return (object )$ response ;
169+ return (object ) $ response ;
135170 }
136171
137- public function forget (string $ identifier = null , string $ password = null ): bool
172+ public function forget (? string $ identifier = null , ? string $ password = null ): bool
138173 {
139- if ($ identifier === null ) $ identifier = session ()->getId ();
174+ if ($ identifier === null ) {
175+ $ identifier = session ()->getId ();
176+ }
140177 if ($ password !== null ) {
141178 $ codes = $ this ->readData ($ identifier , []);
142- if (!isset ($ codes [md5 ($ password )])) {
179+ if (! isset ($ codes [md5 ($ password )])) {
143180 return false ;
144181 }
145182 unset($ codes [md5 ($ password )]);
146183 $ this ->writeData ($ identifier , $ codes );
184+
147185 return true ;
148186 }
149187
150188 $ this ->deleteData ($ identifier );
189+
151190 return true ;
152191 }
153192
154- public function resetAttempt (string $ identifier = null ): bool
193+ public function resetAttempt (? string $ identifier = null ): bool
155194 {
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+
158200 return true ;
159201 }
160202
@@ -168,7 +210,7 @@ private function generateNewPassword(): string
168210 'string ' => $ this ->sensitive ? '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ ' : '23456789ABCDEFGHJKLMNPQRSTUVWXYZ ' ,
169211 'numeric ' => '0123456789 ' ,
170212 'numeric-no-zero ' => '123456789 ' ,
171- 'customize ' => $ this ->customize
213+ 'customize ' => $ this ->customize ,
172214 ];
173215
174216 $ lengths = is_array ($ this ->length ) ? $ this ->length : [$ this ->length ];
@@ -180,22 +222,22 @@ private function generateNewPassword(): string
180222
181223 return implode ($ this ->separator , $ password );
182224 } 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. ' );
184226 }
185227 }
186228
187229 private function writeData (string $ key , mixed $ value ): void
188230 {
189- Cache::put ($ this ->prefix . $ key , $ value , $ this ->expires * 60 * 3 );
231+ Cache::put ($ this ->prefix . $ key , $ value , $ this ->expires * 60 * 3 );
190232 }
191233
192234 private function readData (string $ key , mixed $ default = null ): mixed
193235 {
194- return Cache::get ($ this ->prefix . $ key , $ default );
236+ return Cache::get ($ this ->prefix . $ key , $ default );
195237 }
196238
197239 private function deleteData (string $ key ): void
198240 {
199- Cache::forget ($ this ->prefix . $ key );
241+ Cache::forget ($ this ->prefix . $ key );
200242 }
201- }
243+ }
0 commit comments