mirror of
https://github.com/siteboon/claudecodeui.git
synced 2025-12-09 19:09:45 +00:00
Implement comprehensive API key management functionality including generation, validation, and CRUD operations. Changes: - Add API key database schema and operations (create, validate, delete, toggle) - Generating a commit message will now work properly with claude sdk and cursor cli and return a suggested commit message - Implement crypto-based key generation with 'ck_' prefix - Add session ID tracking in claude-sdk.js and cursor-cli.js - Update database layer with API key validation and last_used tracking - Support multi-user API key management with user association This enables secure programmatic access to the agent service
179 lines
5.1 KiB
JavaScript
179 lines
5.1 KiB
JavaScript
import express from 'express';
|
|
import { apiKeysDb, credentialsDb } from '../database/db.js';
|
|
|
|
const router = express.Router();
|
|
|
|
// ===============================
|
|
// API Keys Management
|
|
// ===============================
|
|
|
|
// Get all API keys for the authenticated user
|
|
router.get('/api-keys', async (req, res) => {
|
|
try {
|
|
const apiKeys = apiKeysDb.getApiKeys(req.user.id);
|
|
// Don't send the full API key in the list for security
|
|
const sanitizedKeys = apiKeys.map(key => ({
|
|
...key,
|
|
api_key: key.api_key.substring(0, 10) + '...'
|
|
}));
|
|
res.json({ apiKeys: sanitizedKeys });
|
|
} catch (error) {
|
|
console.error('Error fetching API keys:', error);
|
|
res.status(500).json({ error: 'Failed to fetch API keys' });
|
|
}
|
|
});
|
|
|
|
// Create a new API key
|
|
router.post('/api-keys', async (req, res) => {
|
|
try {
|
|
const { keyName } = req.body;
|
|
|
|
if (!keyName || !keyName.trim()) {
|
|
return res.status(400).json({ error: 'Key name is required' });
|
|
}
|
|
|
|
const result = apiKeysDb.createApiKey(req.user.id, keyName.trim());
|
|
res.json({
|
|
success: true,
|
|
apiKey: result
|
|
});
|
|
} catch (error) {
|
|
console.error('Error creating API key:', error);
|
|
res.status(500).json({ error: 'Failed to create API key' });
|
|
}
|
|
});
|
|
|
|
// Delete an API key
|
|
router.delete('/api-keys/:keyId', async (req, res) => {
|
|
try {
|
|
const { keyId } = req.params;
|
|
const success = apiKeysDb.deleteApiKey(req.user.id, parseInt(keyId));
|
|
|
|
if (success) {
|
|
res.json({ success: true });
|
|
} else {
|
|
res.status(404).json({ error: 'API key not found' });
|
|
}
|
|
} catch (error) {
|
|
console.error('Error deleting API key:', error);
|
|
res.status(500).json({ error: 'Failed to delete API key' });
|
|
}
|
|
});
|
|
|
|
// Toggle API key active status
|
|
router.patch('/api-keys/:keyId/toggle', async (req, res) => {
|
|
try {
|
|
const { keyId } = req.params;
|
|
const { isActive } = req.body;
|
|
|
|
if (typeof isActive !== 'boolean') {
|
|
return res.status(400).json({ error: 'isActive must be a boolean' });
|
|
}
|
|
|
|
const success = apiKeysDb.toggleApiKey(req.user.id, parseInt(keyId), isActive);
|
|
|
|
if (success) {
|
|
res.json({ success: true });
|
|
} else {
|
|
res.status(404).json({ error: 'API key not found' });
|
|
}
|
|
} catch (error) {
|
|
console.error('Error toggling API key:', error);
|
|
res.status(500).json({ error: 'Failed to toggle API key' });
|
|
}
|
|
});
|
|
|
|
// ===============================
|
|
// Generic Credentials Management
|
|
// ===============================
|
|
|
|
// Get all credentials for the authenticated user (optionally filtered by type)
|
|
router.get('/credentials', async (req, res) => {
|
|
try {
|
|
const { type } = req.query;
|
|
const credentials = credentialsDb.getCredentials(req.user.id, type || null);
|
|
// Don't send the actual credential values for security
|
|
res.json({ credentials });
|
|
} catch (error) {
|
|
console.error('Error fetching credentials:', error);
|
|
res.status(500).json({ error: 'Failed to fetch credentials' });
|
|
}
|
|
});
|
|
|
|
// Create a new credential
|
|
router.post('/credentials', async (req, res) => {
|
|
try {
|
|
const { credentialName, credentialType, credentialValue, description } = req.body;
|
|
|
|
if (!credentialName || !credentialName.trim()) {
|
|
return res.status(400).json({ error: 'Credential name is required' });
|
|
}
|
|
|
|
if (!credentialType || !credentialType.trim()) {
|
|
return res.status(400).json({ error: 'Credential type is required' });
|
|
}
|
|
|
|
if (!credentialValue || !credentialValue.trim()) {
|
|
return res.status(400).json({ error: 'Credential value is required' });
|
|
}
|
|
|
|
const result = credentialsDb.createCredential(
|
|
req.user.id,
|
|
credentialName.trim(),
|
|
credentialType.trim(),
|
|
credentialValue.trim(),
|
|
description?.trim() || null
|
|
);
|
|
|
|
res.json({
|
|
success: true,
|
|
credential: result
|
|
});
|
|
} catch (error) {
|
|
console.error('Error creating credential:', error);
|
|
res.status(500).json({ error: 'Failed to create credential' });
|
|
}
|
|
});
|
|
|
|
// Delete a credential
|
|
router.delete('/credentials/:credentialId', async (req, res) => {
|
|
try {
|
|
const { credentialId } = req.params;
|
|
const success = credentialsDb.deleteCredential(req.user.id, parseInt(credentialId));
|
|
|
|
if (success) {
|
|
res.json({ success: true });
|
|
} else {
|
|
res.status(404).json({ error: 'Credential not found' });
|
|
}
|
|
} catch (error) {
|
|
console.error('Error deleting credential:', error);
|
|
res.status(500).json({ error: 'Failed to delete credential' });
|
|
}
|
|
});
|
|
|
|
// Toggle credential active status
|
|
router.patch('/credentials/:credentialId/toggle', async (req, res) => {
|
|
try {
|
|
const { credentialId } = req.params;
|
|
const { isActive } = req.body;
|
|
|
|
if (typeof isActive !== 'boolean') {
|
|
return res.status(400).json({ error: 'isActive must be a boolean' });
|
|
}
|
|
|
|
const success = credentialsDb.toggleCredential(req.user.id, parseInt(credentialId), isActive);
|
|
|
|
if (success) {
|
|
res.json({ success: true });
|
|
} else {
|
|
res.status(404).json({ error: 'Credential not found' });
|
|
}
|
|
} catch (error) {
|
|
console.error('Error toggling credential:', error);
|
|
res.status(500).json({ error: 'Failed to toggle credential' });
|
|
}
|
|
});
|
|
|
|
export default router;
|