Robert 4 months ago
parent c9a56b7987
commit ac05c3a0a3
  1. 3
      protected/src/composer.json
  2. 16
      protected/src/configs/on_JWT.php
  3. 47
      protected/src/controllers/app/jwt_ctrl.php
  4. 17
      protected/src/controllers/app/twilio_ctrl.php
  5. 40
      protected/src/services/on_JSON_Tokens.php
  6. 29
      protected/src/services/on_twilio_setup.php

@ -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"
}
}

@ -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();
}
}
}

@ -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

@ -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"));
});

@ -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();
}
}
return \CodeHydrater\services\twilio_setup::init_sms($sid, $token);
});
}
Reg::get('di')->register('twilio_voice', function() {
return \CodeHydrater\services\twilio_setup::init_voice();
});
Loading…
Cancel
Save