getKeyById($keyId); if (!$keyInfo) { throw new Exception("API key not found"); } if (!$keyInfo['is_active']) { throw new Exception("API key is already revoked"); } // Revoke the key $success = $apiKeyModel->revokeKey($keyId); if (!$success) { throw new Exception("Failed to revoke API key"); } // Log the action $auditLog = new AuditLogModel($conn); $auditLog->log( $_SESSION['user']['user_id'], 'revoke', 'api_key', $keyId, ['key_name' => $keyInfo['key_name'], 'key_prefix' => $keyInfo['key_prefix']] ); // Clear output buffer ob_end_clean(); // Return success header('Content-Type: application/json'); echo json_encode([ 'success' => true, 'message' => 'API key revoked successfully' ]); } catch (Exception $e) { ob_end_clean(); header('Content-Type: application/json'); http_response_code(isset($conn) ? 400 : 500); echo json_encode([ 'success' => false, 'error' => $e->getMessage() ]); }