This commit implements CSRF token support for all session-based API requests to fix the "CSRF token missing" and "CSRF token mismatch" errors introduced after CSRF protection was added in commit 62c4cc84. Changes: - Created csrfService.ts utility for fetching and caching CSRF tokens - Added getPostHeadersWithCsrf() helper to authUtils for async token injection - Updated all service files (*Service.ts) to include CSRF tokens in POST/PUT/PATCH/DELETE requests - Updated components with inline fetch calls to use getCsrfToken() - Fixed CSRF middleware to use single lusca instance instead of creating new instances per request - Improved generateToken() to use req.csrfToken() when available - Added CalDAV path exemption to CSRF protection Technical details: - CSRF tokens are fetched from /api/csrf-token endpoint - Tokens are cached and reused across requests to avoid unnecessary fetches - Tokens are included in x-csrf-token header for state-changing requests - Public endpoints (login, register) remain exempt from CSRF protection - Bearer token authentication remains exempt from CSRF protection Files modified: - Backend: app.js, middleware/csrf.js - Frontend: 13 service files, 8 component files - New file: frontend/utils/csrfService.ts This ensures all session-based requests properly include CSRF tokens while maintaining support for API token authentication.
76 lines
2.2 KiB
TypeScript
76 lines
2.2 KiB
TypeScript
import { getApiPath } from '../config/paths';
|
|
import { getCsrfToken } from './csrfService';
|
|
|
|
export interface ApiKeySummary {
|
|
id: number;
|
|
name: string;
|
|
token_prefix: string;
|
|
created_at: string;
|
|
updated_at: string;
|
|
last_used_at: string | null;
|
|
expires_at: string | null;
|
|
revoked_at: string | null;
|
|
}
|
|
|
|
export interface CreateApiKeyResponse {
|
|
token: string;
|
|
apiKey: ApiKeySummary;
|
|
}
|
|
|
|
async function handleResponse<T>(response: Response): Promise<T> {
|
|
if (!response.ok) {
|
|
const errorBody = await response.json().catch(() => null);
|
|
const message = errorBody?.error || 'Request failed';
|
|
throw new Error(message);
|
|
}
|
|
return (await response.json()) as T;
|
|
}
|
|
|
|
export async function fetchApiKeys(): Promise<ApiKeySummary[]> {
|
|
const response = await fetch(getApiPath('profile/api-keys'), {
|
|
credentials: 'include',
|
|
});
|
|
return handleResponse<ApiKeySummary[]>(response);
|
|
}
|
|
|
|
export async function createApiKey(payload: {
|
|
name: string;
|
|
expires_at?: string | null;
|
|
}): Promise<CreateApiKeyResponse> {
|
|
const response = await fetch(getApiPath('profile/api-keys'), {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'x-csrf-token': await getCsrfToken(),
|
|
},
|
|
credentials: 'include',
|
|
body: JSON.stringify(payload),
|
|
});
|
|
return handleResponse<CreateApiKeyResponse>(response);
|
|
}
|
|
|
|
export async function revokeApiKey(id: number): Promise<ApiKeySummary> {
|
|
const response = await fetch(getApiPath(`profile/api-keys/${id}/revoke`), {
|
|
method: 'POST',
|
|
credentials: 'include',
|
|
headers: {
|
|
'x-csrf-token': await getCsrfToken(),
|
|
},
|
|
});
|
|
return handleResponse<ApiKeySummary>(response);
|
|
}
|
|
|
|
export async function deleteApiKey(id: number): Promise<void> {
|
|
const response = await fetch(getApiPath(`profile/api-keys/${id}`), {
|
|
method: 'DELETE',
|
|
credentials: 'include',
|
|
headers: {
|
|
'x-csrf-token': await getCsrfToken(),
|
|
},
|
|
});
|
|
if (!response.ok) {
|
|
const errorBody = await response.json().catch(() => null);
|
|
const message = errorBody?.error || 'Failed to delete API key';
|
|
throw new Error(message);
|
|
}
|
|
}
|