1+ <?php
2+ namespace Laxo \AuthecticateSystem ;
3+
4+ use Firebase \JWT \JWT ;
5+ use Firebase \JWT \Key ;
6+
7+
8+ class Authorize {
9+ private static string $ JWTKey = 'your-key ' ;
10+
11+ public static function hash ( string |array $ value ): string {
12+ return bin2hex ( JWT ::encode ( $ value , self ::$ JWTKey , 'HS256 ' ) );
13+ }
14+
15+ /**
16+ * @throws JsonException
17+ */
18+ public static function unHash ( string $ value ): false |string {
19+ return json_encode ( JWT ::decode ( hex2bin ( $ value ), new Key ( self ::$ JWTKey , 'HS256 ' ) ), JSON_THROW_ON_ERROR );
20+ }
21+
22+ /**
23+ * @param array|bool $protectedData Data of use must be correct like username, password, name, phone number, ...
24+ *
25+ * @throws Exception
26+ */
27+ public static function auth ( array |bool $ protectedData = false ): void {
28+
29+ $ _SESSION ['userinfo ' ] ??= [];
30+ $ _SESSION ['userinfo ' ]['last_request ' ] = time ();
31+ $ _SESSION ['userinfo ' ]['ip ' ] = self ::getIPAddress ();
32+ $ _SESSION ['lastToken ' ] = $ _COOKIE ['token ' ] ?? '' ;
33+
34+ if ( $ protectedData ) {
35+ $ _SESSION ['userinfo ' ]['protectedData ' ] = $ protectedData ;
36+ $ current_token = self ::hash ( $ _SESSION ['userinfo ' ] );
37+ $ _SESSION ['current_token ' ] = $ current_token ;
38+ setcookie ( 'token ' , $ current_token , time () + 28800 , "/ " );
39+ }
40+
41+ }
42+
43+ /**
44+ * verify identity of user
45+ * @param bool $isApi if set to true, token will be updated after authentication
46+ *
47+ * @throws Exception
48+ */
49+ public static function verifyIdentity ( bool $ isApi = false ): bool {
50+
51+ $ tokenData = self ::validateToken ( $ _COOKIE ['token ' ] ?? '' );
52+
53+ if ( $ tokenData && self ::isValidToken ( $ tokenData ) ) {
54+ if ( $ isApi ) {
55+ self ::auth ( $ tokenData ['protectedData ' ] );
56+ }
57+
58+ return true ;
59+ }
60+
61+ self ::auth ();
62+ self ::logOut ();
63+
64+ return false ;
65+
66+ }
67+
68+ public static function getIPAddress () {
69+ return $ _SERVER ['HTTP_CLIENT_IP ' ] ?? $ _SERVER ['HTTP_X_FORWARDED_FOR ' ] ?? $ _SERVER ['REMOTE_ADDR ' ];
70+ }
71+
72+ public static function logOut (): bool {
73+ if ( isset ( $ _COOKIE ['token ' ] ) ) {
74+ unset( $ _COOKIE ['token ' ] );
75+ unset( $ _SESSION ['userinfo ' ] );
76+ setcookie ( 'token ' , '' , - 1 , '/ ' );
77+
78+ } else {
79+ return 0 ;
80+ }
81+
82+ return 1 ;
83+ }
84+
85+ /**
86+ * @throws JsonException
87+ */
88+ private static function validateToken ( string $ token ): ?array {
89+ if ( ! $ token ) {
90+ return null ;
91+ }
92+
93+ return json_decode ( self ::unHash ( $ token ), true , 512 , JSON_THROW_ON_ERROR );
94+ }
95+
96+ private static function isValidToken ( array $ tokenData ): bool {
97+ return isset ( $ tokenData ['protectedData ' ], $ tokenData ['last_request ' ], $ tokenData ['ip ' ] ) &&
98+ ( time () - $ tokenData ['last_request ' ] >= 1 ) &&
99+ ( $ tokenData ['ip ' ] === self ::getIPAddress () ) &&
100+ ( $ _SESSION ['lastToken ' ] !== $ _COOKIE ['token ' ] ) &&
101+ ( $ tokenData ['protectedData ' ] === $ _SESSION ['userinfo ' ]['protectedData ' ] ) &&
102+ ( $ _SESSION ['current_token ' ] === $ _COOKIE ['token ' ] );
103+ }
104+
105+
106+ }
0 commit comments