Fixes the calls to agent with JWT

main
Yasen Pramatarov 2024-10-02 14:04:45 +03:00
parent 8bb2e8c838
commit 4e827e62f0
2 changed files with 9 additions and 8 deletions

View File

@ -21,13 +21,12 @@
$jwt = $agentObject->generateAgentToken($payload, $agent['secret_key']); $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']) ?>', '<?= htmlspecialchars($jwt) ?>')">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']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', true)">force refresh</button>
<?php } else { ?> <?php } else { ?>
<button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get data from the agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>')">fetch data</button> <button class="btn btn-primary" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="get data from the agent" onclick="fetchData('<?= htmlspecialchars($agent['id']) ?>', '<?= htmlspecialchars($agent['url']) ?>', '<?= htmlspecialchars($agent['agent_endpoint']) ?>', '<?= htmlspecialchars($jwt) ?>')">fetch data</button>
<button class="btn btn-light" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="no cache to refresh">force refresh</button> <button class="btn btn-light" data-toggle="tooltip" data-trigger="hover" data-placement="bottom" title="no cache to refresh">force refresh</button>
<?php } ?> <?php } ?>
</p> </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 } ?> <?php } ?>

View File

@ -16,8 +16,8 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
const agentUrl = url + endpoint; const agentUrl = url + endpoint;
// FIXME for debugging purpose // DEBUG show the requested URL for debugging purpose
console.log("Requesting URL:", agentUrl); //console.log("Requesting URL:", agentUrl);
// Handle invalid URL error // Handle invalid URL error
try { try {
@ -29,9 +29,8 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
} }
// send the token // 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");
xhr.setRequestHeader("Authorization", "Bearer " + jwtToken);
// Set a timeout in milliseconds (10 seconds = 10000 ms) // Set a timeout in milliseconds (10 seconds = 10000 ms)
xhr.timeout = 10000; xhr.timeout = 10000;
@ -46,6 +45,9 @@ function fetchData(agent_id, url, endpoint, jwtToken, force = false) {
try { try {
// Parse and display the result // Parse and display the result
let result = JSON.parse(xhr.responseText); let result = JSON.parse(xhr.responseText);
// DEBUG display the result
//console.log(xhr.responseText);
if (result.error) { if (result.error) {
resultElement.innerHTML = "Error: " + result.error; resultElement.innerHTML = "Error: " + result.error;
} else { } else {