# API Security Requirements ## Overview This document outlines the security requirements for all API endpoints in the AI Resource Pro application. ## Core Security Principles 1. **Dual Authentication for All Write Operations** - All POST, PUT, DELETE, and PATCH requests must be secured with: - Admin authentication (Supabase auth + is_admin check) - Valid API key (NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY) 2. **No Security Bypasses** - No endpoints should create security vulnerabilities - No endpoints to bypass authentication - No endpoints to set admin status without proper validation 3. **Admin User Management** - User management should be done via Supabase Dashboard - Admin rights assignment should be done via database administration tools - No API endpoints should ever create or modify admin status ## Authentication Methods ### 1. Admin Authentication - Based on Supabase authentication - User must be authenticated with Supabase - User must have `is_admin: true` in the database ### 2. API Key Validation - All secured endpoints must validate the `x-api-key` header - API key must match the `AI_RESOURCE_PRO_API_KEY` environment variable - API key validation is implemented in `src/middleware/auth.ts` ## Implementation ### Security Middleware We use a standardized middleware to enforce API security: ```typescript // src/middleware/protected-routes.ts export async function validateAdminApiRequest(request: NextRequest) { // 1. Validate admin authentication const { user } = await validateRequest(); if (!user || !user.isAdmin) { return NextResponse.json( { error: 'Unauthorized - Admin authentication required' }, { status: 401 } ); } // 2. Validate API key const apiKeyError = validateApiKey(request); if (apiKeyError) { return apiKeyError; } // Both checks passed return null; } ``` ### Using the Security Middleware All protected API endpoints should use the security middleware: ```typescript export async function POST(request: NextRequest) { // Validate security requirements const securityError = await validateAdminApiRequest(request); if (securityError) { return securityError; } // Proceed with the endpoint logic // ... } ``` ## Security Testing Before creating any new API endpoint, ensure that: 1. All POST, PUT, DELETE, PATCH operations are secured 2. No endpoints bypass authentication mechanisms 3. No endpoints modify admin status via API 4. API security middleware is applied correctly ## Common Issues ### Missing API Key If you receive a 401 Unauthorized error with "Missing API key header", make sure you're: - Including the `x-api-key` header in your request - Setting the header value to match the `NEXT_PUBLIC_AI_RESOURCE_PRO_API_KEY` environment variable ### Authentication Failed If you receive a 401 Unauthorized error with "Admin authentication required", ensure: - You're properly authenticated (logged in as admin) - Your user has `is_admin: true` in the database - The admin_session_active cookie is set ## Development Notes - In development, use the API key from your `.env.local` file for testing - Never create endpoints that bypass security mechanisms - Never set `BYPASS_AUTH_IN_DEV=true` in production environments