/
hed1ad
/
CloudGuard_API_Security_Platform
Обзор
Документация
Войти
/
hed1ad
/
CloudGuard_API_Security_Platform
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
frontend/lib/api.ts
192 строки
5 KB
hed1ad
Add Auth + Reg
08 ноя 2025, 23:28
08 ноя 2025, 23:28
28c30c5
Код
Авторство
О чём код?
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api/v1'; const AUTH_API_URL = process.env.NEXT_PUBLIC_API_URL?.replace('/api/v1', '') || 'http://localhost:8000'; export interface Scan { scan_id: string; target_url: string; status: 'queued' | 'running' | 'completed' | 'failed'; created_at: string; started_at?: string; completed_at?: string; error_message?: string; vulnerabilities_found?: number; } export interface Vulnerability { id: string; type: string; severity: 'CRITICAL' | 'HIGH' | 'MEDIUM' | 'LOW'; endpoint: string; method: string; description: string; evidence?: any; } export interface ScanDetail extends Scan { vulnerabilities: Vulnerability[]; } function getAuthHeaders(): HeadersInit { const token = localStorage.getItem('auth_token'); return { 'Content-Type': 'application/json', ...(token ? { 'Authorization': `Bearer ${token}` } : {}), }; } export async function createScan(targetUrl: string, endpoints: string[]): Promise<Scan> { const response = await fetch(`${API_URL}/scans`, { method: 'POST', headers: getAuthHeaders(), body: JSON.stringify({ target_url: targetUrl, endpoints: endpoints, }), }); if (!response.ok) { if (response.status === 401) { throw new Error('Unauthorized. Please log in again.'); } throw new Error(`Failed to create scan: ${response.statusText}`); } const data = await response.json(); return { scan_id: data.id, target_url: data.target_url, status: data.status, created_at: data.created_at, }; } export async function listScans(): Promise<Scan[]> { const token = localStorage.getItem('auth_token'); const response = await fetch(`${API_URL}/scans`, { headers: { ...(token ? { 'Authorization': `Bearer ${token}` } : {}), }, }); if (!response.ok) { if (response.status === 401) { throw new Error('Unauthorized. Please log in again.'); } throw new Error(`Failed to fetch scans: ${response.statusText}`); } const data = await response.json(); return data.map((scan: any) => ({ scan_id: scan.id, target_url: scan.target_url, status: scan.status, created_at: scan.created_at, error_message: scan.error_message, vulnerabilities_found: scan.vulnerabilities_found, })); } export async function getScan(scanId: string): Promise<ScanDetail> { const token = localStorage.getItem('auth_token'); const response = await fetch(`${API_URL}/scans/${scanId}`, { headers: { ...(token ? { 'Authorization': `Bearer ${token}` } : {}), }, }); if (!response.ok) { if (response.status === 401) { throw new Error('Unauthorized. Please log in again.'); } throw new Error(`Failed to fetch scan: ${response.statusText}`); } const data = await response.json(); return { scan_id: data.id, target_url: data.target_url, status: data.status, created_at: data.created_at, started_at: data.started_at, completed_at: data.completed_at, error_message: data.error_message, vulnerabilities: data.vulnerabilities, }; } // Auth API export interface User { username: string; email: string; full_name: string; disabled?: boolean; } export interface LoginCredentials { username: string; password: string; } export interface RegisterData { username: string; email: string; full_name: string; password: string; } export interface AuthResponse { access_token: string; token_type: string; } export async function register(data: RegisterData): Promise<User> { const response = await fetch(`${AUTH_API_URL}/register`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }); if (!response.ok) { const error = await response.json(); throw new Error(error.detail || 'Registration failed'); } return response.json(); } export async function login(credentials: LoginCredentials): Promise<AuthResponse> { const formData = new URLSearchParams(); formData.append('username', credentials.username); formData.append('password', credentials.password); const response = await fetch(`${AUTH_API_URL}/token`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: formData, }); if (!response.ok) { const error = await response.json(); throw new Error(error.detail || 'Login failed'); } return response.json(); } export async function getCurrentUser(token: string): Promise<User> { const response = await fetch(`${AUTH_API_URL}/users/me`, { headers: { 'Authorization': `Bearer ${token}`, }, }); if (!response.ok) { throw new Error('Failed to get user info'); } return response.json(); }