44
55class Auth
66{
7- static $ usersTable = 'users ' ;
8- static $ usernameField = 'username ' ;
9- static $ passwordField = 'password ' ;
10- static $ createdField = 'created ' ;
11- static $ totpSecretField = 'totp_secret ' ;
7+ public static string $ usersTable = 'users ' ;
8+ public static string $ usernameField = 'username ' ;
9+ public static string $ passwordField = 'password ' ;
10+ public static string $ createdField = 'created ' ;
11+ public static string $ totpSecretField = 'totp_secret ' ;
1212
13- public static function login (string $ username , string $ password , string $ totp = null )
13+ /** @return array<string,array<string|mixed>> */
14+ public static function login (string $ username , string $ password , string $ totp = null ): array
1415 {
1516 $ query = sprintf (
1617 'select * from `%s` where `%s` = ? limit 1 ' ,
17- static ::$ usersTable ,
18- static ::$ usernameField
18+ self ::$ usersTable ,
19+ self ::$ usernameField
1920 );
2021 $ user = DB ::selectOne ($ query , $ username );
2122 if ($ user ) {
22- $ table = static ::$ usersTable ;
23- if (password_verify ($ password , $ user [$ table ][static ::$ passwordField ])) {
24- if (!Totp::verify ($ user [$ table ][static ::$ totpSecretField ] ?? '' , $ totp ?: '' )) {
25- throw new TotpError ($ user [$ table ][static ::$ usernameField ]);
23+ $ table = self ::$ usersTable ;
24+ if (password_verify ($ password , $ user [$ table ][self ::$ passwordField ])) {
25+ if (!Totp::verify ($ user [$ table ][self ::$ totpSecretField ] ?? '' , $ totp ?: '' )) {
26+ throw new TotpError ($ user [$ table ][self ::$ usernameField ]);
2627 }
2728 Session::regenerate ();
2829 $ _SESSION ['user ' ] = $ user [$ table ];
2930 } else {
30- $ user = array () ;
31+ $ user = [] ;
3132 }
3233 }
3334 return $ user ;
@@ -40,49 +41,49 @@ public static function logout(): bool
4041 return true ;
4142 }
4243
43- public static function register (string $ username , string $ password )
44+ public static function register (string $ username , string $ password ): int
4445 {
4546 $ query = sprintf (
4647 'insert into `%s` (`%s`,`%s`,`%s`) values (?,?,NOW()) ' ,
47- static ::$ usersTable ,
48- static ::$ usernameField ,
49- static ::$ passwordField ,
50- static ::$ createdField
48+ self ::$ usersTable ,
49+ self ::$ usernameField ,
50+ self ::$ passwordField ,
51+ self ::$ createdField
5152 );
5253 $ password = password_hash ($ password , PASSWORD_DEFAULT );
5354 return DB ::insert ($ query , $ username , $ password );
5455 }
5556
56- public static function update (string $ username , string $ password )
57+ public static function update (string $ username , string $ password ): int
5758 {
5859 $ query = sprintf (
5960 'update `%s` set `%s`=? where `%s`=? ' ,
60- static ::$ usersTable ,
61- static ::$ passwordField ,
62- static ::$ usernameField
61+ self ::$ usersTable ,
62+ self ::$ passwordField ,
63+ self ::$ usernameField
6364 );
6465 $ password = password_hash ($ password , PASSWORD_DEFAULT );
6566 return DB ::update ($ query , $ password , $ username );
6667 }
6768
68- public static function updateTotpSecret (string $ username , string $ secret )
69+ public static function updateTotpSecret (string $ username , string $ secret ): int
6970 {
7071 $ query = sprintf (
7172 'update `%s` set `%s`=? where `%s`=? ' ,
72- static ::$ usersTable ,
73- static ::$ totpSecretField ,
74- static ::$ usernameField
73+ self ::$ usersTable ,
74+ self ::$ totpSecretField ,
75+ self ::$ usernameField
7576 );
7677 return DB ::update ($ query , $ secret , $ username );
7778 }
7879
79- public static function exists (string $ username )
80+ public static function exists (string $ username ): bool
8081 {
8182 $ query = sprintf (
8283 'select 1 from `%s` where `%s`=? ' ,
83- static ::$ usersTable ,
84- static ::$ usernameField
84+ self ::$ usersTable ,
85+ self ::$ usernameField
8586 );
86- return DB ::selectValue ($ query , $ username );
87+ return boolval ( DB ::selectValue ($ query , $ username) );
8788 }
8889}
0 commit comments