attstore / templates /webhooks.html
dn-scribe's picture
feat: support offline (remove cdn and other web-dependencies)
8dd8135
Raw History Blame Contribute Delete
19.6 kB
{% extends "base.html" %}
{% block title %}Webhooks - Attestation Store{% endblock %}
{% block content %}
<div class="container my-4">
<div class="row mb-4">
<div class="col">
<h1 class="h2">
<i data-feather="zap" class="me-2"></i>
Webhooks
</h1>
<p class="text-muted">Manage and test outgoing webhooks</p>
</div>
<div class="col-auto">
<div class="mb-3 d-flex flex-wrap gap-2">
<button type="button" class="btn btn-primary add-webhook-btn" id="addWebhookBtn" data-bs-toggle="modal" data-bs-target="#webhookModal">
<i data-feather="plus" class="me-1"></i>
Add Webhook
</button>
<button id="download-webhooks-json" class="btn btn-outline-success">Download JSON</button>
<button id="upload-webhooks-json" class="btn btn-outline-info" data-bs-toggle="modal" data-bs-target="#uploadWebhooksModal">Upload JSON</button>
</div>
</div>
</div>
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Badge</th>
<th>Condition</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for webhook in webhooks %}
<tr>
<td>{{ webhook.name }}</td>
<td>{{ webhook.badge }}</td>
<td>
{% if webhook.trigger_non_zero %}
non-zero
{% else %}
zero
{% endif %}
</td>
<td>
<div class="d-flex gap-2">
<form action="{{ url_for('web.test_webhook_ui', webhook_id=webhook.id) }}" method="post" style="display:inline;">
<button type="submit" class="btn btn-outline-info">
<i data-feather="play" class="me-1"></i>
Test
</button>
</form>
<button type="button" class="btn btn-outline-warning edit-webhook-btn" data-webhook-id="{{ webhook.id }}">
<i data-feather="edit" class="me-1"></i>
Edit
</button>
<form action="{{ url_for('web.delete_webhook_ui', webhook_id=webhook.id) }}" method="post" style="display:inline;" onsubmit="return confirm('Delete this webhook?');">
<button type="submit" class="btn btn-outline-danger">
<i data-feather="trash" class="me-1"></i>
Delete
</button>
</form>
</div>
</td>
</tr>
{% else %}
<tr>
<td colspan="8" class="text-center py-5">
<i data-feather="zap" style="width: 3rem; height: 3rem;" class="mb-3 text-muted"></i>
<h5>No Webhooks Found</h5>
<p class="text-muted">No webhooks have been configured yet.</p>
<button type="button" class="btn btn-primary add-webhook-btn" data-bs-toggle="modal" data-bs-target="#webhookModal">
<i data-feather="plus" class="me-1"></i>
Add First Webhook
</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Upload Modal -->
<div class="modal fade" id="uploadWebhooksModal" tabindex="-1" aria-labelledby="uploadWebhooksModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="uploadWebhooksModalLabel">Upload Webhooks Settings JSON</h5>
<button type="button" class="btn-close"
data-bs-dismiss="modal"
aria-label="Close"
onclick="this.blur();document.getElementById('addWebhookBtn').focus()"></button>
</div>
<div class="modal-body">
<form id="upload-webhooks-form">
<input type="file" id="webhooks-json-file" accept="application/json" class="form-control mb-2" required />
<div id="upload-webhooks-feedback" class="text-danger small"></div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" form="upload-webhooks-form" class="btn btn-primary">Upload</button>
</div>
</div>
</div>
</div>
<!-- Webhook Modal -->
<div class="modal fade" id="webhookModal" tabindex="-1" aria-labelledby="webhookModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="webhookModalLabel">Add Webhook</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form id="webhookFormModal" method="post" action="{{ url_for('web.add_webhook') }}">
<div class="modal-body">
<div class="mb-3">
<label for="webhookName" class="form-label">Name</label>
<input type="text" class="form-control" id="webhookName" name="name" required>
</div>
<div class="mb-3">
<label for="webhookTriggerType" class="form-label">Trigger Type</label>
<select class="form-select" id="webhookTriggerType" name="trigger_type" required onchange="updateTriggerTypeUI()">
<option value="badge" selected>Badge</option>
<option value="attestation-condition">Attestation Condition</option>
</select>
</div>
<div id="badge-trigger-section">
<div class="mb-3">
<label for="webhookBadge" class="form-label">Badge</label>
<select class="form-select" id="webhookBadge" name="badge">
{% for badge in badges %}
<option value="{{ badge.name }}">{{ badge.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" id="webhookTriggerNonZero" name="trigger_non_zero" checked>
<label class="form-check-label" for="webhookTriggerNonZero">
Trigger on non-zero badge count
</label>
</div>
</div>
<div id="attestation-condition-section" style="display:none;">
<label class="form-label">Attestation Conditions</label>
<div id="conditions-list-modal"></div>
<button type="button" class="btn btn-secondary btn-sm" onclick="addConditionRowModal()">Add Condition</button>
<small class="form-text text-muted">
Each condition is ANDed. For a list-typed context field, the value must be in the list.
</small>
</div>
<div class="mb-3">
<label for="webhookUrl" class="form-label">Webhook URL</label>
<input type="url" class="form-control" id="webhookUrl" name="url" required>
</div>
<div class="mb-3">
<label for="webhookMethod" class="form-label">HTTP Method</label>
<select class="form-select" id="webhookMethod" name="method">
<option value="POST">POST</option>
<option value="GET">GET</option>
<option value="PUT">PUT</option>
<option value="DELETE">DELETE</option>
</select>
</div>
<div class="mb-3">
<label for="webhookHeaders" class="form-label">Headers (JSON)</label>
<textarea class="form-control" id="webhookHeaders" name="headers" rows="3" placeholder='{"Authorization": "Bearer ..."}'></textarea>
</div>
<div class="mb-3">
<label for="webhookContent" class="form-label">Content Template (JSON)</label>
<textarea class="form-control" id="webhookContent" name="content" rows="5" placeholder='{"attestation":{attestation_details}}'></textarea>
<small class="form-text text-muted">
<b>Templating:</b> Use variables in the form <code>--varname--</code> (two dashes, name, two dashes) anywhere in the URL, headers, or content.<br>
<b>Supported variables:</b>
<ul>
<li><code>product-key</code> (product key string), <code>product-version</code> (product version string)</li>
<li><code>attestation_raw</code> (raw file content, as bytes), <code>attestation_predicate</code> (decoded predicate), <code>attestation_predicate_base64</code> (base64-encoded predicate)</li>
<li>Any context field: <code>--contextkey--</code> (empty string if missing)</li>
<li>Labels: <code>--labels.labelname--</code> (gets value for label <code>labelname</code>, empty if not found)</li>
</ul>
<b>Examples:</b>
<ul>
<li><code>{"Authorization": "Bearer --secret--"}</code> → <code>{"Authorization": "Bearer ghpat-1232455"}</code></li>
<li><code>{"predicate": --attestation_predicate--}</code> → <code>{"predicate": { ... }}</code></li>
</ul>
If a variable is not found, it is replaced with an empty string.
</small>
</div>
<div class="mb-3">
<label for="webhookSecret" class="form-label">Secret (optional)</label>
<input type="text" class="form-control" id="webhookSecret" name="secret">
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" id="webhookIsActive" name="is_active" checked>
<label class="form-check-label" for="webhookIsActive">Active</label>
</div>
<input type="hidden" id="webhookId" name="webhook_id" value="">
<input type="hidden" id="webhookActionType" name="actionType" value="save">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary"
data-bs-dismiss="modal"
onclick="this.blur();document.getElementById('addWebhookBtn').focus()">Cancel</button>
<button type="submit" class="btn btn-success">Save</button>
</div>
</form>
</div>
</div>
</div>
{% block scripts %}
<script>
// Download webhooks JSON
document.getElementById('download-webhooks-json').onclick = function() {
fetch('/api/webhooks/settings/download', {
headers: { 'Authorization': 'Bearer ' + (window.authToken || '') }
})
.then(resp => { if (!resp.ok) throw new Error('Failed to download'); return resp.blob(); })
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'webhooks_settings.json';
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
})
.catch(err => showAlert('Download failed: ' + err, 'danger'));
};
// Upload webhooks JSON
document.getElementById('upload-webhooks-form').onsubmit = function(e) {
e.preventDefault();
const fileInput = document.getElementById('webhooks-json-file');
const feedback = document.getElementById('upload-webhooks-feedback');
feedback.textContent = '';
if (!fileInput.files.length) {
feedback.textContent = 'Please select a JSON file.';
return;
}
const formData = new FormData();
formData.append('file', fileInput.files[0]);
fetch('/api/webhooks/settings/upload', {
method: 'POST',
headers: { 'Authorization': 'Bearer ' + (window.authToken || '') },
body: formData
})
.then(resp => resp.json())
.then(data => {
if (data.accepted.length) showAlert('Uploaded: ' + data.accepted.map(x => x.name + ' (' + x.action + ')').join(', '), 'success');
if (data.rejected.length) showAlert('Rejected: ' + data.rejected.map(x => x.name + ' (' + x.reason + ')').join(', '), 'warning');
if (!data.accepted.length && !data.rejected.length) showAlert('No valid entries found in JSON.', 'info');
window.location.reload();
})
.catch(err => showAlert('Upload failed: ' + err, 'danger'));
};
var webhookModal = new bootstrap.Modal(document.getElementById('webhookModal'), { backdrop: 'static', keyboard: true });
// --- Modal Dynamic UI Logic ---
function updateTriggerTypeUI() {
var triggerType = document.getElementById('webhookTriggerType').value;
document.getElementById('badge-trigger-section').style.display = (triggerType === 'badge') ? '' : 'none';
document.getElementById('attestation-condition-section').style.display = (triggerType === 'attestation-condition') ? '' : 'none';
// If you add an attestation-finalized section, handle here
}
function addConditionRowModal(field = '', value = '') {
var list = document.getElementById('conditions-list-modal');
var row = document.createElement('div');
row.className = 'row mb-2 condition-row';
row.innerHTML = `
<div class="col">
<input type="text" class="form-control" name="condition_field" placeholder="Context Field" value="${field}">
</div>
<div class="col">
<input type="text" class="form-control" name="condition_value" placeholder="Value (comma for list)" value="${value}">
</div>
<div class="col-auto">
<button type="button" class="btn btn-danger btn-sm" onclick="removeConditionRowModal(this)">Remove</button>
</div>
`;
list.appendChild(row);
}
function removeConditionRowModal(btn) {
btn.closest('.condition-row').remove();
}
// Open modal for Add Webhook
document.getElementById('addWebhookBtn').addEventListener('click', function() {
clearWebhookModal();
document.getElementById('webhookModalLabel').textContent = 'Add Webhook';
document.getElementById('webhookFormModal').setAttribute('action', '{{ url_for("web.add_webhook") }}');
webhookModal.show();
document.getElementById('webhookName').focus();
updateTriggerTypeUI();
});
// Open modal for Edit Webhook
document.querySelectorAll('.edit-webhook-btn').forEach(function(btn) {
btn.addEventListener('click', function() {
var id = this.getAttribute('data-webhook-id');
axios.get('/api/webhooks/' + id + '/json', { headers: { 'Authorization': 'Bearer ' + (window.authToken || '') } })
.then(function(resp) {
fillWebhookModal(resp.data, id);
document.getElementById('webhookModalLabel').textContent = 'Edit Webhook';
document.getElementById('webhookFormModal').setAttribute('action', '{{ url_for("web.update_webhook_ui", webhook_id=0) }}'.replace('0', id));
webhookModal.show();
document.getElementById('webhookName').focus();
updateTriggerTypeUI();
})
.catch(function(err) {
var msg = (err.response && err.response.data && err.response.data.error) || err.message;
showAlert('Failed to load webhook data: ' + msg, 'danger');
});
});
});
function clearWebhookModal() {
document.getElementById('webhookName').value = '';
document.getElementById('webhookTriggerType').value = 'badge';
updateTriggerTypeUI();
// Reset badge selection and trigger condition
var badgeSelect = document.getElementById('webhookBadge');
if (badgeSelect && badgeSelect.options.length) {
badgeSelect.selectedIndex = 0;
}
document.getElementById('webhookTriggerNonZero').checked = true;
document.getElementById('webhookUrl').value = '';
document.getElementById('webhookMethod').value = 'POST';
document.getElementById('webhookHeaders').value = '';
document.getElementById('webhookContent').value = '';
document.getElementById('webhookSecret').value = '';
document.getElementById('webhookIsActive').checked = true;
document.getElementById('webhookId').value = '';
// Clear attestation conditions
document.getElementById('conditions-list-modal').innerHTML = '';
}
function fillWebhookModal(data, id) {
document.getElementById('webhookName').value = data.name;
document.getElementById('webhookTriggerType').value = data.trigger_type || 'badge';
updateTriggerTypeUI();
document.getElementById('webhookBadge').value = data.badge || '';
document.getElementById('webhookTriggerNonZero').checked = data.trigger_non_zero;
document.getElementById('webhookUrl').value = data.url;
document.getElementById('webhookMethod').value = data.method;
document.getElementById('webhookHeaders').value = data.headers;
document.getElementById('webhookContent').value = data.content;
document.getElementById('webhookSecret').value = data.secret;
document.getElementById('webhookIsActive').checked = data.is_active;
document.getElementById('webhookId').value = id;
// Fill attestation conditions
var condList = document.getElementById('conditions-list-modal');
condList.innerHTML = '';
if (data.attestation_conditions && Array.isArray(data.attestation_conditions)) {
data.attestation_conditions.forEach(function(cond) {
addConditionRowModal(cond.field, cond.value);
});
}
}
// Accessibility fix: Move focus out of modal before it is hidden to prevent aria-hidden focus warning
document.getElementById('webhookModal').addEventListener('hide.bs.modal', function () {
var addBtn = document.getElementById('addWebhookBtn');
if (addBtn) {
addBtn.focus();
}
});
// Ensure trigger type UI is correct on load
document.addEventListener('DOMContentLoaded', function() {
updateTriggerTypeUI();
});
</script>
{% endblock %}
{% endblock %}