Skip to content

Commit 703dc9a

Browse files
committed
Composer Update
1 parent 0f388e7 commit 703dc9a

File tree

6 files changed

+146
-15
lines changed

6 files changed

+146
-15
lines changed

composer.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
{
2+
"name": "samirkl/authenticate-system",
3+
"description": "The PHP JWT Authorization Class provides a straightforward way to manage user authentication and authorization using JSON Web Tokens (JWT). This class is designed to handle token generation, validation, and user session management seamlessly, ensuring secure and efficient authentication for your application.",
4+
"type": "library",
25
"require": {
36
"firebase/php-jwt": "^6.10"
4-
}
7+
},
8+
"license": "MIT",
9+
"autoload": {
10+
"psr-4": {
11+
"Samirkl\\AuthenticateSystem\\": "src/"
12+
}
13+
},
14+
"authors": [
15+
{
16+
"name": "Samir",
17+
"email": "[email protected]"
18+
}
19+
]
520
}

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Authrorize.php

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

vendor/composer/autoload_psr4.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
$baseDir = dirname($vendorDir);
77

88
return array(
9+
'Samirkl\\AuthenticateSystem\\' => array($baseDir . '/src'),
910
'Firebase\\JWT\\' => array($vendorDir . '/firebase/php-jwt/src'),
1011
);

vendor/composer/autoload_static.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77
class ComposerStaticInitb432a387434ebb9247e06494b4c9e798
88
{
99
public static $prefixLengthsPsr4 = array (
10+
'S' =>
11+
array (
12+
'Samirkl\\AuthenticateSystem\\' => 27,
13+
),
1014
'F' =>
1115
array (
1216
'Firebase\\JWT\\' => 13,
1317
),
1418
);
1519

1620
public static $prefixDirsPsr4 = array (
21+
'Samirkl\\AuthenticateSystem\\' =>
22+
array (
23+
0 => __DIR__ . '/../..' . '/src',
24+
),
1725
'Firebase\\JWT\\' =>
1826
array (
1927
0 => __DIR__ . '/..' . '/firebase/php-jwt/src',

vendor/composer/installed.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
<?php return array(
22
'root' => array(
3-
'name' => '__root__',
4-
'pretty_version' => '1.0.0+no-version-set',
5-
'version' => '1.0.0.0',
6-
'reference' => NULL,
3+
'name' => 'samirkl/authenticate-system',
4+
'pretty_version' => 'dev-main',
5+
'version' => 'dev-main',
6+
'reference' => '0f388e774e0e74dd30c4cc280f0e10567e9e37e9',
77
'type' => 'library',
88
'install_path' => __DIR__ . '/../../',
99
'aliases' => array(),
1010
'dev' => true,
1111
),
1212
'versions' => array(
13-
'__root__' => array(
14-
'pretty_version' => '1.0.0+no-version-set',
15-
'version' => '1.0.0.0',
16-
'reference' => NULL,
17-
'type' => 'library',
18-
'install_path' => __DIR__ . '/../../',
19-
'aliases' => array(),
20-
'dev_requirement' => false,
21-
),
2213
'firebase/php-jwt' => array(
2314
'pretty_version' => 'v6.10.1',
2415
'version' => '6.10.1.0',
@@ -28,5 +19,14 @@
2819
'aliases' => array(),
2920
'dev_requirement' => false,
3021
),
22+
'samirkl/authenticate-system' => array(
23+
'pretty_version' => 'dev-main',
24+
'version' => 'dev-main',
25+
'reference' => '0f388e774e0e74dd30c4cc280f0e10567e9e37e9',
26+
'type' => 'library',
27+
'install_path' => __DIR__ . '/../../',
28+
'aliases' => array(),
29+
'dev_requirement' => false,
30+
),
3131
),
3232
);

0 commit comments

Comments
 (0)