Rustelo/resources/auth/migrations/006_service_tokens_postgres.sql
Jesús Pérez f1010e9d07
chore: update
2026-07-18 20:16:16 +01:00

21 lines
963 B
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Migration: 006 Service tokens for external notification senders
-- Database: PostgreSQL
--
-- Service tokens allow external services (CI/CD, monitoring, admin scripts) to
-- POST /api/notify without a full user JWT. Each token is identified by a
-- SHA-256 hash so the plaintext is never stored after creation.
CREATE TABLE IF NOT EXISTS service_tokens (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
token_hash TEXT NOT NULL UNIQUE,
created_by UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
scopes TEXT[] NOT NULL DEFAULT '{"notify"}',
is_active BOOLEAN NOT NULL DEFAULT TRUE,
last_used_at TIMESTAMPTZ,
expires_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_service_tokens_hash ON service_tokens(token_hash) WHERE is_active;
CREATE INDEX IF NOT EXISTS idx_service_tokens_creator ON service_tokens(created_by);