7c20b518d4
Repositorio estava ha ~5 sessoes sem commit. Consolida tudo desde d088a89.
Ver commit.md na raiz para descricao completa por sessao.
# Numeros
- A# auditoria abertos: 0/30
- V# verificacoes abertos: 5/52 (todos adiados com plano)
- T# testes escritos: 10/10
- Vitest: 192/192
- SQL integration: 33/33
- E2E (Playwright, novo): 5/5
- Migrations: 17 (10 novas Sessao 6)
- Areas auditadas: 7 (+documentos com 10 V#)
# Highlights Sessao 6 (hoje)
- V#34/V#41 Opcao B2: tenant_features com plano + override (RPC SECURITY DEFINER, tela /saas/tenant-features)
- A#20 rev2 self-hosted: defesa em 5 camadas (honeypot + rate limit + math captcha condicional + paranoid mode + dashboard /saas/security)
- Documentos hardening (V#43-V#49): tenant scoping em storage policies (vazamento entre clinicas eliminado), RPC validate_share_token, signatures policy granular
- SaaS Twilio Config (/saas/twilio-config): UI editavel para SID/webhook/cotacao; AUTH_TOKEN permanece em env var
- T#9 + T#10: useAgendaEvents.spec.js + Playwright E2E (descobriu bug no front que foi corrigido)
# Sessoes anteriores (1-5) consolidadas
- Sessao 1: auth/router/session, normalizeRole extraido
- Sessao 2: agenda - composables/services consolidados
- Sessao 3: pacientes - tenant_id em todas queries
- Sessao 4: security review pagina publica - 14/15 vulnerabilidades corrigidas
- Sessao 5: SaaS - P0 (A#30: 7 tabelas com RLS off corrigidas)
# .gitignore ajustado
- supabase/* + !supabase/functions/ (mantem 10 edge functions, ignora .temp/migrations gerados pelo CLI)
- database-novo/backups/ (regeneravel via db.cjs backup)
- test-results/ + playwright-report/
- .claude/settings.local.json (config local com senha de dev removida do tracking)
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
154 lines
5.1 KiB
JavaScript
154 lines
5.1 KiB
JavaScript
/*
|
|
|--------------------------------------------------------------------------
|
|
| Agência PSI
|
|
|--------------------------------------------------------------------------
|
|
| Criado e desenvolvido por Leonardo Nohama
|
|
|
|
|
| Tecnologia aplicada à escuta.
|
|
| Estrutura para o cuidado.
|
|
|
|
|
| Arquivo: src/services/DocumentShareLinks.service.js
|
|
| Data: 2026
|
|
| Local: São Carlos/SP — Brasil
|
|
|--------------------------------------------------------------------------
|
|
| © 2026 — Todos os direitos reservados
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
import { supabase } from '@/lib/supabase/client';
|
|
|
|
// ── Helpers ──────────────────────────────────────────────────
|
|
|
|
async function getOwnerId() {
|
|
const { data, error } = await supabase.auth.getUser();
|
|
if (error) throw error;
|
|
const uid = data?.user?.id;
|
|
if (!uid) throw new Error('Sessão inválida.');
|
|
return uid;
|
|
}
|
|
|
|
async function getActiveTenantId(uid) {
|
|
const { data, error } = await supabase
|
|
.from('tenant_members')
|
|
.select('tenant_id')
|
|
.eq('user_id', uid)
|
|
.eq('status', 'active')
|
|
.order('created_at', { ascending: false })
|
|
.limit(1)
|
|
.single();
|
|
if (error) throw error;
|
|
if (!data?.tenant_id) throw new Error('Tenant não encontrado.');
|
|
return data.tenant_id;
|
|
}
|
|
|
|
// ── Criar link temporario ───────────────────────────────────
|
|
|
|
/**
|
|
* Gera link temporario para compartilhar documento com profissional externo.
|
|
*
|
|
* @param {string} documentoId
|
|
* @param {object} opts - { expiracaoHoras: 48, usosMax: 5 }
|
|
* @returns {object} registro com token para montar a URL
|
|
*/
|
|
export async function createShareLink(documentoId, opts = {}) {
|
|
if (!documentoId) throw new Error('Documento não informado.');
|
|
|
|
const ownerId = await getOwnerId();
|
|
const tenantId = await getActiveTenantId(ownerId);
|
|
|
|
const expiracaoHoras = opts.expiracaoHoras || 48;
|
|
const expiraEm = new Date();
|
|
expiraEm.setHours(expiraEm.getHours() + expiracaoHoras);
|
|
|
|
const { data, error } = await supabase
|
|
.from('document_share_links')
|
|
.insert({
|
|
documento_id: documentoId,
|
|
tenant_id: tenantId,
|
|
expira_em: expiraEm.toISOString(),
|
|
usos_max: opts.usosMax || 5,
|
|
criado_por: ownerId
|
|
})
|
|
.select('*')
|
|
.single();
|
|
|
|
if (error) throw error;
|
|
return data;
|
|
}
|
|
|
|
// ── Listar links de um documento ────────────────────────────
|
|
|
|
export async function listShareLinks(documentoId) {
|
|
if (!documentoId) return [];
|
|
|
|
const ownerId = await getOwnerId();
|
|
|
|
const { data, error } = await supabase
|
|
.from('document_share_links')
|
|
.select('*')
|
|
.eq('documento_id', documentoId)
|
|
.eq('criado_por', ownerId)
|
|
.order('criado_em', { ascending: false });
|
|
|
|
if (error) throw error;
|
|
return data || [];
|
|
}
|
|
|
|
// ── Validar token (acesso publico) ──────────────────────────
|
|
|
|
/**
|
|
* Valida token de compartilhamento e retorna dados do documento.
|
|
* Incrementa o contador de usos.
|
|
*
|
|
* @param {string} token
|
|
* @returns {object|null} - { link, document } ou null se invalido/expirado
|
|
*/
|
|
export async function validateShareToken(token) {
|
|
if (!token) return null;
|
|
|
|
// V#46: SELECT direto pela tabela com policy "public read by token" foi
|
|
// removido. Agora chama RPC validate_share_token (SECURITY DEFINER) que
|
|
// valida + incrementa usos + loga acesso atomicamente. Sem race condition.
|
|
const { data, error } = await supabase.rpc('validate_share_token', { p_token: token });
|
|
if (error) return null;
|
|
if (!data?.document_id) return null;
|
|
|
|
return {
|
|
link: { id: null, token, documento_id: data.document_id }, // estrutura mínima compat
|
|
document: {
|
|
id: data.document_id,
|
|
nome_original: data.nome_original,
|
|
mime_type: data.mime_type,
|
|
bucket_path: data.bucket_path,
|
|
storage_bucket: data.bucket
|
|
}
|
|
};
|
|
}
|
|
|
|
// ── Desativar link ──────────────────────────────────────────
|
|
|
|
export async function deactivateShareLink(linkId) {
|
|
if (!linkId) throw new Error('ID inválido.');
|
|
|
|
const ownerId = await getOwnerId();
|
|
|
|
const { error } = await supabase
|
|
.from('document_share_links')
|
|
.update({ ativo: false })
|
|
.eq('id', linkId)
|
|
.eq('criado_por', ownerId);
|
|
|
|
if (error) throw error;
|
|
return true;
|
|
}
|
|
|
|
// ── Montar URL publica ──────────────────────────────────────
|
|
|
|
/**
|
|
* Monta a URL de compartilhamento a partir do token.
|
|
* A rota publica deve ser configurada no router.
|
|
*/
|
|
export function buildShareUrl(token) {
|
|
const base = window.location.origin;
|
|
return `${base}/shared/document/${token}`;
|
|
}
|