feat: Implement write authentication for API routes and add .env loading to init-db script.

This commit is contained in:
Yusuf İpek
2025-11-24 03:01:45 +03:00
parent 02bb1a1e72
commit 0e4716db54
5 changed files with 89 additions and 2 deletions
+20
View File
@@ -4,6 +4,26 @@ const { Pool } = require('pg')
const fs = require('fs') const fs = require('fs')
const path = require('path') const path = require('path')
// Load .env manually
try {
const envPath = path.join(__dirname, '..', '.env')
if (fs.existsSync(envPath)) {
const envConfig = fs.readFileSync(envPath, 'utf8')
envConfig.split('\n').forEach(line => {
const match = line.match(/^([^=]+)=(.*)$/)
if (match) {
const key = match[1].trim()
const value = match[2].trim().replace(/^["']|["']$/g, '') // remove quotes
if (!process.env[key]) {
process.env[key] = value
}
}
})
}
} catch (e) {
console.error('Error loading .env', e)
}
const DB_HOST = process.env.DB_HOST || 'localhost' const DB_HOST = process.env.DB_HOST || 'localhost'
const DB_PORT = parseInt(process.env.DB_PORT || '5432', 10) const DB_PORT = parseInt(process.env.DB_PORT || '5432', 10)
const DB_USER = process.env.DB_USER || 'postgres' const DB_USER = process.env.DB_USER || 'postgres'
+12 -2
View File
@@ -1,7 +1,17 @@
import { NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { PlatformInitializer } from '@/services/platformInitializer' import { PlatformInitializer } from '@/services/platformInitializer'
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 }
)
}
export async function POST() {
try { try {
await PlatformInitializer.initializePlatforms() await PlatformInitializer.initializePlatforms()
return NextResponse.json({ return NextResponse.json({
+10
View File
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { PackageService } from '@/services/packageService' import { PackageService } from '@/services/packageService'
import { SyncAuth } from '@/lib/sync/auth'
export async function GET( export async function GET(
request: NextRequest, request: NextRequest,
@@ -54,6 +55,15 @@ export async function DELETE(
request: NextRequest, request: NextRequest,
{ params }: { params: { id: string } } { 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 { try {
const success = await PackageService.delete(params.id) const success = await PackageService.delete(params.id)
+10
View File
@@ -1,5 +1,6 @@
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
import { PackageService } from '@/services/packageService' import { PackageService } from '@/services/packageService'
import { SyncAuth } from '@/lib/sync/auth'
export async function GET(request: NextRequest) { export async function GET(request: NextRequest) {
try { try {
@@ -40,6 +41,15 @@ export async function GET(request: NextRequest) {
} }
export async function POST(request: NextRequest) { 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 { try {
const body = await request.json() const body = await request.json()
const packageData = await PackageService.create(body) const packageData = await PackageService.create(body)
+37
View File
@@ -55,6 +55,43 @@ export class SyncAuth {
return { allowed: true } return { allowed: true }
} }
/**
* Check if write operations (create/update/delete) are allowed
* ALWAYS requires authentication (secret key or localhost), ignoring SYNC_SERVER_ONLY
*/
static async isWriteAllowed(request: NextRequest): Promise<{ allowed: boolean; reason?: string }> {
// Always enforce auth for writes
const secretKey = request.headers.get('x-sync-secret')
// Check localhost
const clientIP = request.headers.get('x-forwarded-for') ||
request.headers.get('x-real-ip') ||
'unknown'
const allowedIPs = ['127.0.0.1', 'localhost', '::1']
const isLocalRequest = allowedIPs.includes(clientIP.split(',')[0].trim())
if (isLocalRequest) {
return { allowed: true }
}
if (!this.SYNC_SECRET) {
return {
allowed: false,
reason: 'Secret key not configured on server'
}
}
if (secretKey === this.SYNC_SECRET) {
return { allowed: true }
}
return {
allowed: false,
reason: 'Write operations require authentication'
}
}
/** /**
* Get automatic sync frequency in days * Get automatic sync frequency in days
*/ */