Mejora de seguridad
This commit is contained in:
@@ -29,15 +29,17 @@ const initialState: AppState = {
|
||||
// API helper function
|
||||
async function apiCall(endpoint: string, options: RequestInit = {}) {
|
||||
const authState = get(auth);
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-App': 'client',
|
||||
...(authState.user?.tenant_id ? { 'X-Tenant-ID': authState.user.tenant_id } : {}),
|
||||
...(options.headers as Record<string, string> ?? {})
|
||||
};
|
||||
if (authState.token) headers['Authorization'] = `Bearer ${authState.token}`;
|
||||
const response = await fetch(`/api/v1${endpoint}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${authState.token}`,
|
||||
...(authState.user?.tenant_id ? { 'X-Tenant-ID': authState.user.tenant_id } : {}),
|
||||
...options.headers
|
||||
}
|
||||
credentials: 'include',
|
||||
headers
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
@@ -50,26 +50,26 @@ function createAuthStore() {
|
||||
return {
|
||||
subscribe,
|
||||
|
||||
// Initialize auth from localStorage
|
||||
init: () => {
|
||||
// Rehidrata sesión desde cookie HttpOnly (no toca localStorage)
|
||||
init: async () => {
|
||||
if (typeof window !== 'undefined') {
|
||||
const token = localStorage.getItem('auth_token');
|
||||
const user = localStorage.getItem('auth_user');
|
||||
|
||||
if (token && user) {
|
||||
try {
|
||||
const parsedUser = JSON.parse(user);
|
||||
try {
|
||||
const response = await fetch('/api/v1/auth/me', {
|
||||
credentials: 'include',
|
||||
headers: { 'X-App': 'client' }
|
||||
});
|
||||
if (response.ok) {
|
||||
const user = await response.json();
|
||||
set({
|
||||
user: parsedUser,
|
||||
token,
|
||||
user,
|
||||
token: null,
|
||||
isAuthenticated: true,
|
||||
isLoading: false
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error parsing stored auth data:', error);
|
||||
localStorage.removeItem('auth_token');
|
||||
localStorage.removeItem('auth_user');
|
||||
}
|
||||
// 401/400 es esperado cuando no hay sesión activa — no es un error
|
||||
} catch (error) {
|
||||
// Ignorar errores de red en init
|
||||
}
|
||||
}
|
||||
},
|
||||
@@ -81,6 +81,7 @@ function createAuthStore() {
|
||||
try {
|
||||
const response = await fetch('/api/v1/auth/login', {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
@@ -94,12 +95,6 @@ function createAuthStore() {
|
||||
|
||||
const data: LoginResponse = await response.json();
|
||||
|
||||
// Store auth data
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('auth_token', data.access_token);
|
||||
localStorage.setItem('auth_user', JSON.stringify(data.user));
|
||||
}
|
||||
|
||||
set({
|
||||
user: data.user,
|
||||
token: data.access_token,
|
||||
@@ -113,22 +108,24 @@ function createAuthStore() {
|
||||
},
|
||||
|
||||
// Logout
|
||||
logout: () => {
|
||||
logout: async () => {
|
||||
// Llamar al backend para que borre la cookie HttpOnly
|
||||
try {
|
||||
await fetch('/api/v1/auth/logout', {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: { 'X-App': 'client' }
|
||||
});
|
||||
} catch { /* ignorar errores de red */ }
|
||||
set(initialState);
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.removeItem('auth_token');
|
||||
localStorage.removeItem('auth_user');
|
||||
// Immediate redirect after cleanup
|
||||
window.location.href = '/login';
|
||||
}
|
||||
set(initialState);
|
||||
},
|
||||
|
||||
// Update user data
|
||||
updateUser: (user: User) => {
|
||||
update(state => ({ ...state, user }));
|
||||
if (typeof window !== 'undefined') {
|
||||
localStorage.setItem('auth_user', JSON.stringify(user));
|
||||
}
|
||||
},
|
||||
|
||||
// Set loading state
|
||||
|
||||
@@ -80,18 +80,22 @@ const initialState: TicketsState = {
|
||||
async function apiCall(endpoint: string, options: RequestInit = {}) {
|
||||
const authState = get(auth);
|
||||
|
||||
if (!authState.token || !authState.user) {
|
||||
if (!authState.user) {
|
||||
throw new Error('Not authenticated');
|
||||
}
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-App': 'client',
|
||||
...(options.headers as Record<string, string>)
|
||||
};
|
||||
if (authState.token) headers['Authorization'] = `Bearer ${authState.token}`;
|
||||
if (authState.user.tenant_id) headers['X-Tenant-ID'] = authState.user.tenant_id;
|
||||
|
||||
const response = await fetch(`/api/v1${endpoint}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${authState.token}`,
|
||||
'X-Tenant-ID': authState.user.tenant_id,
|
||||
...options.headers
|
||||
}
|
||||
credentials: 'include',
|
||||
headers
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -259,16 +263,18 @@ function createTicketsStore() {
|
||||
|
||||
const authState = get(auth);
|
||||
|
||||
if (!authState.token || !authState.user) {
|
||||
if (!authState.user) {
|
||||
throw new Error('Not authenticated');
|
||||
}
|
||||
|
||||
const uploadHeaders: Record<string, string> = { 'X-App': 'client' };
|
||||
if (authState.token) uploadHeaders['Authorization'] = `Bearer ${authState.token}`;
|
||||
if (authState.user.tenant_id) uploadHeaders['X-Tenant-ID'] = authState.user.tenant_id;
|
||||
|
||||
const response = await fetch(`/api/v1/tickets/${ticketId}/attachments`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${authState.token}`,
|
||||
'X-Tenant-ID': authState.user.tenant_id
|
||||
},
|
||||
credentials: 'include',
|
||||
headers: uploadHeaders,
|
||||
body: formData
|
||||
});
|
||||
|
||||
@@ -338,16 +344,18 @@ function createTicketsStore() {
|
||||
downloadAttachment: async (ticketId: string, attachmentId: string, filename: string) => {
|
||||
const authState = get(auth);
|
||||
|
||||
if (!authState.token || !authState.user) {
|
||||
if (!authState.user) {
|
||||
throw new Error('Not authenticated');
|
||||
}
|
||||
|
||||
const dlHeaders: Record<string, string> = { 'X-App': 'client' };
|
||||
if (authState.token) dlHeaders['Authorization'] = `Bearer ${authState.token}`;
|
||||
if (authState.user.tenant_id) dlHeaders['X-Tenant-ID'] = authState.user.tenant_id;
|
||||
|
||||
const response = await fetch(`/api/v1/tickets/${ticketId}/attachments/${attachmentId}/download`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${authState.token}`,
|
||||
'X-Tenant-ID': authState.user.tenant_id
|
||||
}
|
||||
credentials: 'include',
|
||||
headers: dlHeaders
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
126
frontend-client/src/lib/utils/api.ts
Normal file
126
frontend-client/src/lib/utils/api.ts
Normal file
@@ -0,0 +1,126 @@
|
||||
/**
|
||||
* Cliente HTTP centralizado para frontend-client.
|
||||
* Usa cookies HttpOnly (client_access_token) como fuente primaria de auth,
|
||||
* con Bearer token como complemento cuando está disponible en memoria.
|
||||
*/
|
||||
import { auth } from '$lib/stores/auth';
|
||||
import { get } from 'svelte/store';
|
||||
|
||||
const API_BASE = '/api/v1';
|
||||
|
||||
interface RequestOptions extends RequestInit {
|
||||
params?: Record<string, string>;
|
||||
}
|
||||
|
||||
async function request<T>(endpoint: string, options: RequestOptions = {}): Promise<T> {
|
||||
const { params, ...init } = options;
|
||||
|
||||
let url = `${API_BASE}${endpoint}`;
|
||||
if (params) {
|
||||
const filteredParams = Object.entries(params)
|
||||
.filter(([, value]) => value !== undefined && value !== null && value !== '')
|
||||
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {});
|
||||
|
||||
if (Object.keys(filteredParams).length > 0) {
|
||||
url += `?${new URLSearchParams(filteredParams).toString()}`;
|
||||
}
|
||||
}
|
||||
|
||||
const authState = get(auth);
|
||||
const headers = new Headers(init.headers);
|
||||
|
||||
// Bearer header cuando el token está en memoria (sesión activa sin reload)
|
||||
if (authState.token) {
|
||||
headers.set('Authorization', `Bearer ${authState.token}`);
|
||||
}
|
||||
if (authState.user?.tenant_id && !headers.has('X-Tenant-ID')) {
|
||||
headers.set('X-Tenant-ID', authState.user.tenant_id);
|
||||
}
|
||||
if (!headers.has('Content-Type')) {
|
||||
headers.set('Content-Type', 'application/json');
|
||||
}
|
||||
// Identifica este frontend para que el backend use client_access_token
|
||||
headers.set('X-App', 'client');
|
||||
|
||||
const response = await fetch(url, {
|
||||
...init,
|
||||
credentials: 'include',
|
||||
headers
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
if (typeof window !== 'undefined') {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
throw new Error('Unauthorized');
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(errorData.detail || `API error: ${response.statusText}`);
|
||||
}
|
||||
|
||||
if (response.status === 204) {
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
async function downloadFile(endpoint: string, filename: string): Promise<void> {
|
||||
const authState = get(auth);
|
||||
const headers = new Headers();
|
||||
|
||||
if (authState.token) {
|
||||
headers.set('Authorization', `Bearer ${authState.token}`);
|
||||
}
|
||||
if (authState.user?.tenant_id) {
|
||||
headers.set('X-Tenant-ID', authState.user.tenant_id);
|
||||
}
|
||||
headers.set('X-App', 'client');
|
||||
|
||||
const response = await fetch(`${API_BASE}${endpoint}`, {
|
||||
method: 'GET',
|
||||
credentials: 'include',
|
||||
headers
|
||||
});
|
||||
|
||||
if (response.status === 401) {
|
||||
if (typeof window !== 'undefined') window.location.href = '/login';
|
||||
throw new Error('Unauthorized');
|
||||
}
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json().catch(() => ({}));
|
||||
throw new Error(errorData.detail || `Download error: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = filename;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
window.URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
export const api = {
|
||||
get: <T>(endpoint: string, params?: Record<string, string>) =>
|
||||
request<T>(endpoint, { method: 'GET', params }),
|
||||
|
||||
post: <T>(endpoint: string, body?: any) =>
|
||||
request<T>(endpoint, { method: 'POST', body: body !== undefined ? JSON.stringify(body) : undefined }),
|
||||
|
||||
put: <T>(endpoint: string, body?: any) =>
|
||||
request<T>(endpoint, { method: 'PUT', body: body !== undefined ? JSON.stringify(body) : undefined }),
|
||||
|
||||
patch: <T>(endpoint: string, body?: any) =>
|
||||
request<T>(endpoint, { method: 'PATCH', body: body !== undefined ? JSON.stringify(body) : undefined }),
|
||||
|
||||
delete: <T>(endpoint: string) =>
|
||||
request<T>(endpoint, { method: 'DELETE' }),
|
||||
|
||||
downloadFile: (endpoint: string, filename: string) =>
|
||||
downloadFile(endpoint, filename)
|
||||
};
|
||||
@@ -11,8 +11,8 @@
|
||||
|
||||
let mounted = false;
|
||||
|
||||
onMount(() => {
|
||||
auth.init();
|
||||
onMount(async () => {
|
||||
await auth.init();
|
||||
mounted = true;
|
||||
});
|
||||
|
||||
@@ -27,6 +27,12 @@
|
||||
</script>
|
||||
|
||||
<div class="min-h-screen bg-gray-50 font-sans">
|
||||
{#if !mounted}
|
||||
<!-- Esperando inicialización de sesión -->
|
||||
<div class="flex items-center justify-center min-h-screen bg-gray-50">
|
||||
<div class="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
|
||||
</div>
|
||||
{:else}
|
||||
{#if showHeader}
|
||||
<Header />
|
||||
{/if}
|
||||
@@ -39,6 +45,7 @@
|
||||
<footer class="py-4 text-center border-t border-gray-200 bg-white">
|
||||
<p class="text-xs text-gray-400">ServiceManagerWeb v1.9.0 · © 2026 Aduanasoft</p>
|
||||
</footer>
|
||||
{/if}
|
||||
|
||||
<!-- Toast notifications -->
|
||||
{#each $toast.toasts as toastMessage (toastMessage.id)}
|
||||
|
||||
@@ -38,11 +38,14 @@
|
||||
async function loadProfile() {
|
||||
isLoading = true;
|
||||
try {
|
||||
const headers: Record<string, string> = {
|
||||
'X-App': 'client',
|
||||
'X-Tenant-ID': $auth.user?.tenant_id ?? ''
|
||||
};
|
||||
if ($auth.token) headers['Authorization'] = `Bearer ${$auth.token}`;
|
||||
const response = await fetch('/api/v1/client-profile/', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${$auth.token}`,
|
||||
'X-Tenant-ID': $auth.user?.tenant_id ?? ''
|
||||
}
|
||||
credentials: 'include',
|
||||
headers
|
||||
});
|
||||
if (!response.ok) throw new Error((await response.json()).detail);
|
||||
profile = await response.json();
|
||||
@@ -62,13 +65,16 @@
|
||||
async function saveProfile() {
|
||||
isSaving = true;
|
||||
try {
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-App': 'client',
|
||||
'X-Tenant-ID': $auth.user?.tenant_id ?? ''
|
||||
};
|
||||
if ($auth.token) headers['Authorization'] = `Bearer ${$auth.token}`;
|
||||
const response = await fetch('/api/v1/client-profile/', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${$auth.token}`,
|
||||
'X-Tenant-ID': $auth.user?.tenant_id ?? ''
|
||||
},
|
||||
credentials: 'include',
|
||||
headers,
|
||||
body: JSON.stringify(form)
|
||||
});
|
||||
if (!response.ok) throw new Error((await response.json()).detail);
|
||||
|
||||
@@ -34,7 +34,11 @@
|
||||
try {
|
||||
const response = await fetch('/api/v1/auth/2fa/setup', {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${$auth.token}` }
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'X-App': 'client',
|
||||
...($auth.token ? { Authorization: `Bearer ${$auth.token}` } : {})
|
||||
}
|
||||
});
|
||||
if (!response.ok) throw new Error((await response.json()).detail);
|
||||
const data = await response.json();
|
||||
@@ -57,7 +61,12 @@
|
||||
try {
|
||||
const response = await fetch('/api/v1/auth/2fa/enable', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${$auth.token}` },
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-App': 'client',
|
||||
...($auth.token ? { Authorization: `Bearer ${$auth.token}` } : {})
|
||||
},
|
||||
body: JSON.stringify({ totp_code: totpSetupCode })
|
||||
});
|
||||
if (!response.ok) throw new Error((await response.json()).detail);
|
||||
@@ -84,7 +93,12 @@
|
||||
try {
|
||||
const response = await fetch('/api/v1/auth/2fa/disable', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${$auth.token}` },
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-App': 'client',
|
||||
...($auth.token ? { Authorization: `Bearer ${$auth.token}` } : {})
|
||||
},
|
||||
body: JSON.stringify({ totp_code: disableTotpCode })
|
||||
});
|
||||
if (!response.ok) throw new Error((await response.json()).detail);
|
||||
@@ -157,16 +171,20 @@
|
||||
|
||||
async function loadBusinessProfile() {
|
||||
try {
|
||||
if (!$auth.token || !$auth.user) {
|
||||
if (!$auth.user) {
|
||||
console.warn('Usuario no autenticado');
|
||||
return;
|
||||
}
|
||||
|
||||
const _lpHeaders: Record<string, string> = {
|
||||
'X-App': 'client',
|
||||
'X-Tenant-ID': $auth.user.tenant_id
|
||||
};
|
||||
if ($auth.token) _lpHeaders['Authorization'] = `Bearer ${$auth.token}`;
|
||||
|
||||
const response = await fetch('/api/v1/client-profile/', {
|
||||
headers: {
|
||||
Authorization: `Bearer ${$auth.token}`,
|
||||
'X-Tenant-ID': $auth.user.tenant_id
|
||||
}
|
||||
credentials: 'include',
|
||||
headers: _lpHeaders
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
@@ -267,9 +285,11 @@
|
||||
try {
|
||||
const response = await fetch('/api/v1/auth/profile', {
|
||||
method: 'PATCH',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${$auth.token}`
|
||||
'X-App': 'client',
|
||||
...($auth.token ? { Authorization: `Bearer ${$auth.token}` } : {})
|
||||
},
|
||||
body: JSON.stringify({
|
||||
first_name: firstName.trim(),
|
||||
@@ -300,9 +320,11 @@
|
||||
try {
|
||||
const response = await fetch('/api/v1/auth/change-password', {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${$auth.token}`
|
||||
'X-App': 'client',
|
||||
...($auth.token ? { Authorization: `Bearer ${$auth.token}` } : {})
|
||||
},
|
||||
body: JSON.stringify({
|
||||
current_password: currentPassword,
|
||||
@@ -349,13 +371,16 @@
|
||||
profileData.credit_limit = parseFloat(profileData.credit_limit);
|
||||
}
|
||||
|
||||
const _bpHeaders: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
'X-App': 'client',
|
||||
'X-Tenant-ID': $auth.user?.tenant_id ?? ''
|
||||
};
|
||||
if ($auth.token) _bpHeaders['Authorization'] = `Bearer ${$auth.token}`;
|
||||
const response = await fetch('/api/v1/client-profile/', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${$auth.token}`,
|
||||
'X-Tenant-ID': $auth.user.tenant_id
|
||||
},
|
||||
credentials: 'include',
|
||||
headers: _bpHeaders,
|
||||
body: JSON.stringify(profileData)
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user