From 2f42283a54a2f302400cbaa447285cd7096618a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yusuf=20=C4=B0pek?= Date: Mon, 24 Nov 2025 03:17:48 +0300 Subject: [PATCH] feat: Add authentication to platform and auto-sync APIs and remove debug endpoints. --- src/app/api/auto-sync/route.ts | 31 ++++++++++++------- src/app/api/debug-arch/route.ts | 36 ---------------------- src/app/api/platforms/[id]/route.ts | 26 ++++++++++++++-- src/app/api/platforms/route.ts | 11 +++++++ src/app/api/test-db/route.ts | 46 ----------------------------- 5 files changed, 54 insertions(+), 96 deletions(-) delete mode 100644 src/app/api/debug-arch/route.ts delete mode 100644 src/app/api/test-db/route.ts diff --git a/src/app/api/auto-sync/route.ts b/src/app/api/auto-sync/route.ts index 614d297..17ea53e 100644 --- a/src/app/api/auto-sync/route.ts +++ b/src/app/api/auto-sync/route.ts @@ -14,10 +14,19 @@ export const maxDuration = 1800 // 30 minutes timeout for auto sync let lastAutoSync: Date | null = null export async function POST(request: NextRequest) { + // Check auth - require sync secret or localhost + const authResult = await SyncAuth.isSyncAllowed(request) + if (!authResult.allowed) { + return NextResponse.json( + { error: 'Sync operation not allowed', reason: authResult.reason }, + { status: 403 } + ) + } + try { // Check if auto sync is enabled if (!SyncAuth.isAutoSyncEnabled()) { - return NextResponse.json({ + return NextResponse.json({ message: 'Auto sync is disabled', next_sync: null }) @@ -26,9 +35,9 @@ export async function POST(request: NextRequest) { // Check if enough time has passed since last sync const now = new Date() const nextSyncTime = SyncAuth.getNextSyncTime(lastAutoSync || undefined) - + if (now < nextSyncTime) { - return NextResponse.json({ + return NextResponse.json({ message: 'Auto sync not due yet', last_sync: lastAutoSync?.toISOString(), next_sync: nextSyncTime.toISOString(), @@ -37,10 +46,10 @@ export async function POST(request: NextRequest) { } console.log('🔄 Starting automatic package sync...') - + // Sync all platforms in sequence const syncResults = [] - + try { // Sync Debian/Ubuntu packages console.log('Syncing Debian/Ubuntu packages...') @@ -102,12 +111,12 @@ export async function POST(request: NextRequest) { // Update last sync time lastAutoSync = now const nextSync = SyncAuth.getNextSyncTime(lastAutoSync) - + const successCount = syncResults.filter(r => r.status === 'success').length const totalCount = syncResults.length - + console.log(`✅ Auto sync completed: ${successCount}/${totalCount} platforms synced successfully`) - + return NextResponse.json({ message: 'Auto sync completed', timestamp: now.toISOString(), @@ -124,8 +133,8 @@ export async function POST(request: NextRequest) { } catch (error) { console.error('Auto sync failed:', error) return NextResponse.json( - { - error: 'Auto sync failed', + { + error: 'Auto sync failed', details: error instanceof Error ? error.message : 'Unknown error', timestamp: new Date().toISOString() }, @@ -137,7 +146,7 @@ export async function POST(request: NextRequest) { export async function GET() { const now = new Date() const nextSyncTime = SyncAuth.getNextSyncTime(lastAutoSync || undefined) - + return NextResponse.json({ auto_sync_enabled: SyncAuth.isAutoSyncEnabled(), auto_sync_days: SyncAuth.getAutoSyncDays(), diff --git a/src/app/api/debug-arch/route.ts b/src/app/api/debug-arch/route.ts deleted file mode 100644 index a4a1692..0000000 --- a/src/app/api/debug-arch/route.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { NextResponse } from 'next/server' -import { query } from '@/lib/database/config' - -export async function GET() { - try { - // Check if arch platform exists - const platformResult = await query( - 'SELECT * FROM platforms WHERE id = $1', - ['arch'] - ) - - // Count arch packages - const countResult = await query( - 'SELECT COUNT(*) FROM packages WHERE platform_id = $1', - ['arch'] - ) - - // Get sample packages - const sampleResult = await query( - 'SELECT name, version, description FROM packages WHERE platform_id = $1 LIMIT 5', - ['arch'] - ) - - return NextResponse.json({ - platform: platformResult.rows[0] || null, - totalPackages: parseInt(countResult.rows[0].count), - samplePackages: sampleResult.rows - }) - } catch (error) { - console.error('Debug error:', error) - return NextResponse.json( - { error: error instanceof Error ? error.message : 'Unknown error' }, - { status: 500 } - ) - } -} diff --git a/src/app/api/platforms/[id]/route.ts b/src/app/api/platforms/[id]/route.ts index e8df68a..4995a96 100644 --- a/src/app/api/platforms/[id]/route.ts +++ b/src/app/api/platforms/[id]/route.ts @@ -7,7 +7,7 @@ export async function GET( ) { try { const platform = await PlatformService.getById(params.id) - + if (!platform) { return NextResponse.json( { error: 'Platform not found' }, @@ -25,14 +25,25 @@ export async function GET( } } +import { SyncAuth } from '@/lib/sync/auth' + export async function PUT( request: NextRequest, { params }: { params: { id: string } } ) { + // Check auth + const auth = await SyncAuth.isWriteAllowed(request) + if (!auth.allowed) { + return NextResponse.json( + { error: auth.reason || 'Unauthorized' }, + { status: 403 } + ) + } + try { const body = await request.json() const platform = await PlatformService.update(params.id, body) - + if (!platform) { return NextResponse.json( { error: 'Platform not found' }, @@ -54,9 +65,18 @@ export async function DELETE( request: NextRequest, { params }: { params: { id: string } } ) { + // Check auth + const auth = await SyncAuth.isWriteAllowed(request) + if (!auth.allowed) { + return NextResponse.json( + { error: auth.reason || 'Unauthorized' }, + { status: 403 } + ) + } + try { const success = await PlatformService.delete(params.id) - + if (!success) { return NextResponse.json( { error: 'Platform not found' }, diff --git a/src/app/api/platforms/route.ts b/src/app/api/platforms/route.ts index a7cf53f..c7b64da 100644 --- a/src/app/api/platforms/route.ts +++ b/src/app/api/platforms/route.ts @@ -14,7 +14,18 @@ export async function GET() { } } +import { SyncAuth } from '@/lib/sync/auth' + export async function POST(request: NextRequest) { + // Check auth + const auth = await SyncAuth.isWriteAllowed(request) + if (!auth.allowed) { + return NextResponse.json( + { error: auth.reason || 'Unauthorized' }, + { status: 403 } + ) + } + try { const body = await request.json() const platform = await PlatformService.create(body) diff --git a/src/app/api/test-db/route.ts b/src/app/api/test-db/route.ts deleted file mode 100644 index ce5572d..0000000 --- a/src/app/api/test-db/route.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { NextResponse } from 'next/server' -import { query } from '@/lib/database/config' - -export async function GET() { - try { - // Test 1: Database connection - const dbTest = await query('SELECT NOW() as current_time', []) - - // Test 2: Packages table count - const packageCount = await query('SELECT COUNT(*) as count FROM packages', []) - - // Test 3: Debian packages count - const debianCount = await query('SELECT COUNT(*) as count FROM packages WHERE platform_id = $1', ['debian']) - - // Test 4: Active packages count - const activeCount = await query('SELECT COUNT(*) as count FROM packages WHERE is_active = true', []) - - // Test 5: Sample packages - const samplePackages = await query(` - SELECT id, name, version, platform_id - FROM packages - WHERE platform_id = $1 AND is_active = true - LIMIT 5 - `, ['debian']) - - return NextResponse.json({ - database: { - connected: true, - currentTime: dbTest.rows[0].current_time - }, - packages: { - total: packageCount.rows[0].count, - debian: debianCount.rows[0].count, - active: activeCount.rows[0].count, - sample: samplePackages.rows - } - }) - - } catch (error) { - console.error('Database test error:', error) - return NextResponse.json({ - error: error instanceof Error ? error.message : 'Unknown error', - database: { connected: false } - }, { status: 500 }) - } -}