Files
laravel-llm-gateway/laravel-app/app/Http/Controllers/ApiKeyController.php
wtrinkl cb495e18e3 Fix API controllers to use correct database column names
- Fix model_pricing table references (model_id -> model, display_name -> model)
- Fix price columns (output_price_per_1k -> output_price_per_million)
- Add price conversion (per_million / 1000 = per_1k) in all API responses
- Add whereNotNull('model') filters to exclude invalid entries
- Add getModelDisplayName() helper method to all controllers
- Fix AccountController to use gateway_users budget fields directly
- Remove Budget model dependencies from AccountController
- Add custom Scramble server URL configuration for API docs
- Create ScrambleServiceProvider to set correct /api prefix
- Add migration to rename user_id to gateway_user_id in llm_requests
- Add custom ApiGuard for gateway_users authentication
- Update all API controllers: AccountController, ModelController, PricingController, ProviderController

All API endpoints now working correctly:
- GET /api/account
- GET /api/models
- GET /api/pricing
- GET /api/providers/{provider}
2025-11-19 19:36:58 +01:00

171 lines
5.3 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\ApiKey;
use App\Models\GatewayUser;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class ApiKeyController extends Controller
{
/**
* Display a listing of the API keys.
*/
public function index(Request $request)
{
$query = ApiKey::with('gatewayUser');
// Filter by status
if ($request->has('status')) {
switch ($request->status) {
case 'active':
$query->active();
break;
case 'expired':
$query->expired();
break;
case 'inactive':
$query->where('is_active', false);
break;
}
}
// Filter by user
if ($request->has('user_id') && $request->user_id) {
$query->where('user_id', $request->user_id);
}
// Search by key name
if ($request->has('search') && $request->search) {
$query->where('key_name', 'like', '%' . $request->search . '%');
}
// Sort
$sortBy = $request->get('sort_by', 'created_at');
$sortOrder = $request->get('sort_order', 'desc');
$query->orderBy($sortBy, $sortOrder);
$apiKeys = $query->paginate(20)->withQueryString();
$gatewayUsers = GatewayUser::orderBy('alias')->get();
return view('keys.index', compact('apiKeys', 'gatewayUsers'));
}
/**
* Show the form for creating a new API key.
*/
public function create()
{
$gatewayUsers = GatewayUser::orderBy('alias')->get();
return view('keys.create', compact('gatewayUsers'));
}
/**
* Store a newly created API key.
*/
public function store(Request $request)
{
$validated = $request->validate([
'key_name' => 'required|string|max:255',
'user_id' => 'required|string|exists:gateway_users,user_id',
'expires_at' => 'nullable|date|after:now',
'metadata' => 'nullable|json',
]);
try {
// Generate a unique API token
$token = 'llmg_' . Str::random(48);
// Parse metadata if provided
$metadata = null;
if (!empty($validated['metadata'])) {
$metadata = json_decode($validated['metadata'], true);
if (json_last_error() !== JSON_ERROR_NONE) {
return back()->with('error', 'Invalid JSON in metadata field');
}
}
// Create API key directly in database
$apiKey = ApiKey::create([
'token' => $token,
'user_id' => $validated['user_id'],
'key_name' => $validated['key_name'],
'key_alias' => $validated['key_name'], // Use key_name as alias
'expires' => $validated['expires_at'] ?? null,
'metadata' => $metadata,
'permissions' => [], // Default empty permissions
'models' => [], // Default empty models
]);
// Store the token in session for one-time display
session()->flash('new_api_key', $token);
session()->flash('new_api_key_id', $apiKey->token);
return redirect()->route('keys.index')
->with('success', 'API Key created successfully! Make sure to copy it now - it won\'t be shown again.');
} catch (\Exception $e) {
Log::error('Exception creating API key', ['error' => $e->getMessage()]);
return back()->with('error', 'Failed to create API key: ' . $e->getMessage());
}
}
/**
* Display the specified API key.
*/
public function show(string $id)
{
$apiKey = ApiKey::with(['gatewayUser', 'usageLogs'])->findOrFail($id);
// Get usage statistics
$stats = [
'total_requests' => $apiKey->usageLogs()->count(),
'total_cost' => $apiKey->usageLogs()->sum('cost'),
'total_tokens' => $apiKey->usageLogs()->sum('total_tokens'),
'last_30_days_requests' => $apiKey->usageLogs()
->where('timestamp', '>=', now()->subDays(30))
->count(),
];
// Get recent activity
$recentLogs = $apiKey->usageLogs()
->orderByDesc('timestamp')
->limit(20)
->get();
return view('keys.show', compact('apiKey', 'stats', 'recentLogs'));
}
/**
* Revoke the specified API key.
*/
public function revoke(string $id)
{
try {
$apiKey = ApiKey::findOrFail($id);
// Delete the API key from database
$apiKey->delete();
return redirect()->route('keys.index')
->with('success', 'API Key revoked successfully');
} catch (\Exception $e) {
Log::error('Exception revoking API key', ['error' => $e->getMessage()]);
return back()->with('error', 'Failed to revoke API key: ' . $e->getMessage());
}
}
/**
* Remove the specified API key.
*/
public function destroy(string $id)
{
// This is an alias for revoke
return $this->revoke($id);
}
}