parent
c9a56b7987
commit
ac05c3a0a3
@ -0,0 +1,16 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Robert Strutts <Bob_586@Yahoo.com> |
||||||
|
* @copyright (c) 2025, Robert Strutts |
||||||
|
* @license MIT |
||||||
|
*/ |
||||||
|
|
||||||
|
use CodeHydrater\bootstrap\configure as Config; |
||||||
|
|
||||||
|
// Your secret key, keep it secure |
||||||
|
Config::set('JWT', [ |
||||||
|
'key' => '0ad1f6d270949857a0d4893ffe02a3d329aa77183ee13183d85c3006e69b204d', |
||||||
|
]); // Use a string, randome key in production |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types = 1); |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Robert Strutts <Bob_586@Yahoo.com> |
||||||
|
* @copyright (c) 2025, Robert Strutts |
||||||
|
* @license MIT |
||||||
|
*/ |
||||||
|
namespace Project\controllers\app; |
||||||
|
use CodeHydrater\bootstrap\registry as Reg; |
||||||
|
|
||||||
|
class jwt_ctrl { |
||||||
|
public function index() { |
||||||
|
|
||||||
|
// Payload data (customize as needed) |
||||||
|
$payload = [ |
||||||
|
'iss' => 'your-issuer', // Issuer |
||||||
|
'aud' => 'your-audience', // Audience |
||||||
|
'iat' => time(), // Issued at (timestamp) |
||||||
|
'exp' => time() + 3600, // Expiration time (1 hour) |
||||||
|
'sub' => 'user123', // Subject (user ID) |
||||||
|
'role' => 'admin' // Custom claim (e.g., user role) |
||||||
|
]; |
||||||
|
try { |
||||||
|
$access_token = Reg::get('di')->get_service('JWT_Encode', $payload); |
||||||
|
echo "Access Token: ". $access_token . "<br>\n"; |
||||||
|
echo "<a href=\"". \CodeHydrater\misc::get_url("app/jwt", "read", ['token' => $access_token] ) ."\">Test Token</a>"; |
||||||
|
} catch (\Exception $e) { |
||||||
|
echo "Error generating token: " . $e->getMessage(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function read(array $p) { |
||||||
|
$token = $p['token'] ?? false; |
||||||
|
if ($token === false) { |
||||||
|
echo "Sorry, no token in URL"; |
||||||
|
exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
\CodeHydrater\bootstrap\common::dump(Reg::get('di')->get_service('JWT_Decode', $token)); |
||||||
|
} catch (\Exception $e) { |
||||||
|
echo "Invalid token: " . $e->getMessage(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,40 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
declare(strict_types=1); |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Robert Strutts <Bob_586@Yahoo.com> |
||||||
|
* @copyright (c) 2025, Robert Strutts |
||||||
|
* @license MIT |
||||||
|
*/ |
||||||
|
namespace CodeHydrater; |
||||||
|
|
||||||
|
use CodeHydrater\bootstrap\registry as Reg; |
||||||
|
use CodeHydrater\bootstrap\configure as Config; |
||||||
|
|
||||||
|
const VENDOR_JWT = CodeHydrater_PROJECT . "vendor/firebase/php-jwt/src/"; |
||||||
|
|
||||||
|
Reg::get('di')->register('JWT_Encode', function(array $payload) { |
||||||
|
$key = Config::get('JWT', 'key') ?? false; |
||||||
|
if ($key === false) { |
||||||
|
throw new \Exception("Invalid JWT KEY"); |
||||||
|
} |
||||||
|
if (! Reg::get('loader')->is_loaded('Firebase\JWT')) { |
||||||
|
Reg::get('loader')->add_namespace('Firebase\JWT', VENDOR_JWT); |
||||||
|
} |
||||||
|
|
||||||
|
return \Firebase\JWT\JWT::encode($payload, $key, "HS256"); |
||||||
|
}); |
||||||
|
|
||||||
|
Reg::get('di')->register('JWT_Decode', function(string $token) { |
||||||
|
$key = Config::get('JWT', 'key') ?? false; |
||||||
|
if ($key === false) { |
||||||
|
throw new \Exception("Invalid JWT KEY"); |
||||||
|
} |
||||||
|
|
||||||
|
if (! Reg::get('loader')->is_loaded('Firebase\JWT')) { |
||||||
|
Reg::get('loader')->add_namespace('Firebase\JWT', VENDOR_JWT); |
||||||
|
} |
||||||
|
|
||||||
|
return \Firebase\JWT\JWT::decode($token, new \Firebase\JWT\Key($key, "HS256")); |
||||||
|
}); |
||||||
Loading…
Reference in new issue