Prepares for agents' JWT

main
Yasen Pramatarov 2024-10-01 10:18:53 +03:00
parent ff2bc61bda
commit 8bb2e8c838
3 changed files with 47 additions and 4 deletions

View File

@ -110,6 +110,7 @@ class Agent {
} }
} }
// check for agent cache // check for agent cache
public function checkAgentCache($agent_id) { public function checkAgentCache($agent_id) {
$agent_cache_name = $agent_id . '_cache'; $agent_cache_name = $agent_id . '_cache';
@ -117,6 +118,38 @@ class Agent {
return isset($_SESSION[$agent_cache_name]) && isset($_SESSION[$agent_cache_time]) && (time() - $_SESSION[$agent_cache_time] < 600); return isset($_SESSION[$agent_cache_name]) && isset($_SESSION[$agent_cache_time]) && (time() - $_SESSION[$agent_cache_time] < 600);
} }
// method for base64 URL encoding for JWT tokens
private function base64UrlEncode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
// generate a JWT token for jilo agent
public function generateAgentToken($payload, $secret_key) {
// header
$header = json_encode([
'typ' => 'JWT',
'alg' => 'HS256'
]);
$base64Url_header = $this->base64UrlEncode($header);
// payload
$payload = json_encode($payload);
$base64Url_payload = $this->base64UrlEncode($payload);
// signature
$signature = hash_hmac('sha256', $base64Url_header . "." . $base64Url_payload, $secret_key, true);
$base64Url_signature = $this->base64UrlEncode($signature);
// build the JWT
$jwt = $base64Url_header . "." . $base64Url_payload . "." . $base64Url_signature;
return $jwt;
}
// fetch result from jilo agent API // fetch result from jilo agent API
public function fetchAgent($agent_id, $force = false) { public function fetchAgent($agent_id, $force = false) {

View File

@ -10,6 +10,16 @@
<br /> <br />
endpoint: <strong><?= htmlspecialchars($agent['url']) ?><?= htmlspecialchars($agent['agent_endpoint']) ?></strong> endpoint: <strong><?= htmlspecialchars($agent['url']) ?><?= htmlspecialchars($agent['agent_endpoint']) ?></strong>
<br /> <br />
<?php
$payload = [
'iss' => 'Jilo Web',
'aud' => $config['domain'],
'iat' => time(),
'exp' => time() + 3600,
'agent_id' => $agent['id']
];
$jwt = $agentObject->generateAgentToken($payload, $agent['secret_key']);
?>
<?php if (isset($_SESSION["{$agent['id']}_cache"])) { ?> <?php if (isset($_SESSION["{$agent['id']}_cache"])) { ?>
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="load recently cached data" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>')">load cache</button> <button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="load recently cached data" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>')">load cache</button>
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get fresh data from agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', true)">force refresh</button> <button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get fresh data from agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', true)">force refresh</button>
@ -20,7 +30,4 @@
</p> </p>
<p>Result:</p> <p>Result:</p>
<pre id="result<?= htmlspecialchars($agent['id']) ?>">click a button to fetch data from the agent.</pre> <pre id="result<?= htmlspecialchars($agent['id']) ?>">click a button to fetch data from the agent.</pre>
<?php
print_r($_SESSION);
?>
<?php } ?> <?php } ?>

View File

@ -1,4 +1,4 @@
function fetchData(agent_id, url, endpoint, force = false) { function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
let counter = 0; let counter = 0;
const resultElement = document.getElementById("result" + agent_id); const resultElement = document.getElementById("result" + agent_id);
@ -28,6 +28,9 @@ function fetchData(agent_id, url, endpoint, force = false) {
return; // Exit the function early return; // Exit the function early
} }
// send the token
xhr.setRequestHeader("Authorization", "Bearer " + jwtToken);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// Set a timeout in milliseconds (10 seconds = 10000 ms) // Set a timeout in milliseconds (10 seconds = 10000 ms)