diff --git a/protected/src/composer.json b/protected/src/composer.json index 76315bd..1ed14b7 100644 --- a/protected/src/composer.json +++ b/protected/src/composer.json @@ -3,6 +3,7 @@ "twig/twig": "^3.0", "liquid/liquid": "^1.4", "ezyang/htmlpurifier": "^4.18", - "twilio/sdk": "^8.7" + "twilio/sdk": "^8.7", + "firebase/php-jwt": "^6.11" } } diff --git a/protected/src/configs/on_JWT.php b/protected/src/configs/on_JWT.php new file mode 100644 index 0000000..796026c --- /dev/null +++ b/protected/src/configs/on_JWT.php @@ -0,0 +1,16 @@ + + * @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 \ No newline at end of file diff --git a/protected/src/controllers/app/jwt_ctrl.php b/protected/src/controllers/app/jwt_ctrl.php new file mode 100644 index 0000000..1ac7069 --- /dev/null +++ b/protected/src/controllers/app/jwt_ctrl.php @@ -0,0 +1,47 @@ + + * @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 . "
\n"; + echo " $access_token] ) ."\">Test Token"; + } 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(); + } + } +} diff --git a/protected/src/controllers/app/twilio_ctrl.php b/protected/src/controllers/app/twilio_ctrl.php index 46f7f73..a38acae 100644 --- a/protected/src/controllers/app/twilio_ctrl.php +++ b/protected/src/controllers/app/twilio_ctrl.php @@ -9,7 +9,7 @@ declare(strict_types=1); */ namespace Project\controllers\app; - +use CodeHydrater\bootstrap\registry as Reg; /** * Description of twilio_ctrl * @@ -18,7 +18,7 @@ namespace Project\controllers\app; * * @link https://github.com/twilio/twilio-php * @link https://www.twilio.com/docs/messaging/quickstart - * + * @link https://www.twilio.com/docs/voice/twiml * */ class twilio_ctrl { @@ -35,16 +35,19 @@ class twilio_ctrl { public function index() { header("content-type: text/xml"); - $response = \CodeHydrater\twilio_setup::init_voice(); - $the_caller_number = $this->get_caller(); + $response = Reg::get('di')->get_service('twilio_voice'); + + $caller_number = $this->get_caller(); // If the user entered digits, process their request if (array_key_exists('Digits', $_POST)) { switch ($_POST['Digits']) { case 1: - $response->say('You selected sales. Good for you!'); + $response->say('One moment and you will be connnected to Sales!'); + // Do Sales Stuff break; case 2: - $response->say('You need support. We will help!'); + $response->say('Please hold for support!'); + // Do Support Stuff break; default: $response->say('Sorry, I don\'t understand that choice.'); @@ -62,7 +65,7 @@ class twilio_ctrl { } public function send_sms(): string { - $twilio = \CodeHydrater\twilio_setup::init_sms(); + $twilio = Reg::get('di')->get_service('twilio_sms'); // Example: Send an SMS Text message to Cell Phone $message = $twilio->messages->create( '+15558675309', // To number GOES HERE diff --git a/protected/src/services/on_JSON_Tokens.php b/protected/src/services/on_JSON_Tokens.php new file mode 100644 index 0000000..5a062f8 --- /dev/null +++ b/protected/src/services/on_JSON_Tokens.php @@ -0,0 +1,40 @@ + + * @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")); +}); diff --git a/protected/src/services/on_twilio_setup.php b/protected/src/services/on_twilio_setup.php index 0fc35f9..c9d978b 100644 --- a/protected/src/services/on_twilio_setup.php +++ b/protected/src/services/on_twilio_setup.php @@ -12,29 +12,12 @@ namespace CodeHydrater; use CodeHydrater\bootstrap\registry as Reg; use CodeHydrater\bootstrap\configure as Config; -final class twilio_setup { - const VENDOR = CodeHydrater_PROJECT . 'vendor/twilio/sdk/src/Twilio/'; - - public static function init_sms() { - if (! Reg::get('loader')->is_loaded('Twilio')) { - Reg::get('loader')->add_namespace('Twilio', self::VENDOR); - } +Reg::get('di')->register('twilio_sms', function() { $sid = Config::get('twilio', 'sid') ?? false; - if ($sid === false) { - throw new \Exception("Twilio SID not defined"); - } $token = Config::get('twilio', 'token') ?? false; - if ($token === false) { - throw new \Exception("Twilio TOKEN not defined"); - } - return new \Twilio\Rest\Client($sid, $token); - } - - public static function init_voice() { - if (! Reg::get('loader')->is_loaded('Twilio')) { - Reg::get('loader')->add_namespace('Twilio', self::VENDOR); - return new \Twilio\TwiML\VoiceResponse(); - } - } - -} \ No newline at end of file + return \CodeHydrater\services\twilio_setup::init_sms($sid, $token); +}); + +Reg::get('di')->register('twilio_voice', function() { + return \CodeHydrater\services\twilio_setup::init_voice(); +}); \ No newline at end of file