Files
nextcloud-mcp-server/third_party/astrolabe/src/adminSettings.js
T
Chris Coutinho 65c3f099fa feat(astrolabe): implement app password provisioning for multi-user background sync
Adds complete app password provisioning workflow for multi-user BasicAuth
deployments, allowing users to independently enable background sync by
generating and storing Nextcloud app passwords.

**New Components:**

Backend (PHP):
- CredentialsController: Validates and stores app passwords
  * Validates app password format and authenticity via OCS API
  * Stores encrypted passwords in oc_preferences
  * Provides status and credential management endpoints
- AstrolabeAdminSettings: Admin configuration page for MCP server URL
- AstrolabeAdminSettingsListener: Event listener for admin section
- Updated McpTokenStorage: Added background sync credential methods

Frontend:
- personalSettings.js: Form handling for app password entry
  * AJAX submission with error handling
  * Shows success/error notifications
  * Triggers page reload after successful save
- settings.css: Styling for settings pages
- Updated personal.php template: Two-option UI
  * Option 1: OAuth refresh token (future, not yet available)
  * Option 2: App password (works today, recommended)
  * Shows "Active" badge when provisioned
  * Displays credential type and provisioned timestamp

Routes:
- POST /api/v1/background-sync/credentials - Store app password
- GET /api/v1/background-sync/status - Get provisioning status
- DELETE /api/v1/background-sync/credentials - Revoke credentials
- GET /api/v1/background-sync/credentials/{userId} - Admin only

**Testing:**
- test_astrolabe_settings_buttons.py: Integration test for UI buttons

**Workflow:**
1. User generates app password in Nextcloud Security settings
2. User navigates to Astrolabe personal settings
3. User enters app password in "Option 2: App Password" form
4. Backend validates password via OCS API call
5. Password stored encrypted in oc_preferences
6. Page reloads showing "Active" badge with credential details
7. MCP server can now use stored password for background operations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-22 19:39:13 +01:00

246 lines
6.7 KiB
JavaScript

/**
* Admin settings page JavaScript for Astrolabe.
*
* Handles:
* - Loading webhook presets
* - Enabling/disabling webhook presets
* - Search settings form submission
*/
import { generateUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import './styles/settings.css'
document.addEventListener('DOMContentLoaded', () => {
// Initialize search settings form
initSearchSettingsForm()
// Initialize webhook management (only if webhook section exists)
if (document.getElementById('webhook-presets')) {
initWebhookManagement()
}
})
/**
* Initialize search settings form handling.
*/
function initSearchSettingsForm() {
const form = document.getElementById('astrolabe-search-settings-form')
if (!form) return
const scoreThresholdInput = document.getElementById('search-score-threshold')
const scoreThresholdValue = document.getElementById('score-threshold-value')
// Update score threshold display when slider changes
if (scoreThresholdInput && scoreThresholdValue) {
scoreThresholdInput.addEventListener('input', (e) => {
scoreThresholdValue.textContent = e.target.value + '%'
})
}
// Handle form submission
form.addEventListener('submit', async (e) => {
e.preventDefault()
const formData = new FormData(form)
const data = {
algorithm: formData.get('algorithm'),
fusion: formData.get('fusion'),
scoreThreshold: parseInt(formData.get('scoreThreshold')),
limit: parseInt(formData.get('limit')),
}
const statusEl = document.getElementById('search-settings-status')
if (statusEl) {
statusEl.textContent = 'Saving...'
statusEl.className = 'mcp-status-message'
}
try {
const response = await axios.post(
generateUrl('/apps/astrolabe/api/admin/search-settings'),
data,
{ headers: { 'Content-Type': 'application/json' } },
)
if (response.data.success) {
if (statusEl) {
statusEl.textContent = '✓ Settings saved'
statusEl.className = 'mcp-status-message success'
setTimeout(() => {
statusEl.textContent = ''
}, 3000)
}
}
} catch (error) {
console.error('Failed to save search settings:', error)
if (statusEl) {
statusEl.textContent = '✗ Failed to save'
statusEl.className = 'mcp-status-message error'
}
}
})
}
/**
* Initialize webhook management UI.
*/
async function initWebhookManagement() {
const container = document.getElementById('webhook-presets-container')
if (!container) return
try {
// Load webhook presets from API
const response = await axios.get(
generateUrl('/apps/astrolabe/api/admin/webhooks/presets'),
)
if (!response.data.success) {
throw new Error(response.data.error || 'Failed to load presets')
}
const presets = response.data.presets
renderWebhookPresets(container, presets)
} catch (error) {
console.error('Failed to load webhook presets:', error)
container.innerHTML = `
<div class="notecard notecard-error">
<p><strong>Error loading webhook presets:</strong></p>
<p>${error.message || 'Unknown error'}</p>
</div>
`
}
}
/**
* Render webhook preset cards.
*
* @param {HTMLElement} container Container element
* @param {object} presets Preset configurations
*/
function renderWebhookPresets(container, presets) {
const presetIds = Object.keys(presets)
if (presetIds.length === 0) {
container.innerHTML = `
<div class="notecard notecard-info">
<p>No webhook presets available. Install supported apps (Notes, Calendar, Tables, Forms) to enable webhooks.</p>
</div>
`
return
}
// Create preset cards grid
const grid = document.createElement('div')
grid.className = 'mcp-preset-grid'
presetIds.forEach(presetId => {
const preset = presets[presetId]
const card = createPresetCard(presetId, preset)
grid.appendChild(card)
})
container.innerHTML = ''
container.appendChild(grid)
}
/**
* Create a webhook preset card.
*
* @param {string} presetId Preset ID
* @param {object} preset Preset configuration
* @return {HTMLElement} Card element
*/
function createPresetCard(presetId, preset) {
const card = document.createElement('div')
card.className = 'mcp-preset-card'
card.dataset.presetId = presetId
const statusClass = preset.enabled ? 'enabled' : 'disabled'
const statusText = preset.enabled ? 'Enabled' : 'Disabled'
const buttonText = preset.enabled ? 'Disable' : 'Enable'
const buttonClass = preset.enabled ? 'secondary' : 'primary'
card.innerHTML = `
<div class="mcp-preset-header">
<h4>${escapeHtml(preset.name)}</h4>
<span class="mcp-preset-status mcp-status-${statusClass}">${statusText}</span>
</div>
<p class="mcp-preset-description">${escapeHtml(preset.description)}</p>
<div class="mcp-preset-meta">
<span class="mcp-preset-app">App: ${escapeHtml(preset.app)}</span>
<span class="mcp-preset-events">${preset.events.length} events</span>
</div>
<div class="mcp-preset-actions">
<button class="mcp-preset-toggle ${buttonClass}" data-preset-id="${presetId}">
${buttonText}
</button>
</div>
`
// Attach event listener to toggle button
const toggleBtn = card.querySelector('.mcp-preset-toggle')
toggleBtn.addEventListener('click', () => togglePreset(presetId, preset.enabled))
return card
}
/**
* Toggle a webhook preset (enable/disable).
*
* @param {string} presetId Preset ID
* @param {boolean} currentlyEnabled Current enabled state
*/
async function togglePreset(presetId, currentlyEnabled) {
const card = document.querySelector(`[data-preset-id="${presetId}"]`)
if (!card) return
const toggleBtn = card.querySelector('.mcp-preset-toggle')
const originalText = toggleBtn.textContent
// Disable button during request
toggleBtn.disabled = true
toggleBtn.textContent = currentlyEnabled ? 'Disabling...' : 'Enabling...'
try {
const action = currentlyEnabled ? 'disable' : 'enable'
const url = generateUrl(`/apps/astrolabe/api/admin/webhooks/presets/${presetId}/${action}`)
const response = await axios.post(url)
if (!response.data.success) {
throw new Error(response.data.error || `Failed to ${action} preset`)
}
// Reload presets to update UI
await initWebhookManagement()
// Show success notification
OC.Notification.showTemporary(response.data.message || `Preset ${action}d successfully`)
} catch (error) {
console.error(`Failed to toggle preset ${presetId}:`, error)
// Restore button state
toggleBtn.disabled = false
toggleBtn.textContent = originalText
// Show error notification
OC.Notification.showTemporary(
error.message || 'Failed to toggle webhook preset',
{ type: 'error' },
)
}
}
/**
* Escape HTML to prevent XSS.
*
* @param {string} text Text to escape
* @return {string} Escaped text
*/
function escapeHtml(text) {
const div = document.createElement('div')
div.textContent = text
return div.innerHTML
}