Add local password-TOTP sessions and scoped revocable MCP tokens

This commit is contained in:
2026-11-17 20:05:28 +01:00
parent 7fe20df34f
commit 9fa0fb81ff
17 changed files with 1804 additions and 27 deletions
+51
View File
@@ -0,0 +1,51 @@
-- A TOTP timestep may authenticate once per independently provisioned secret.
CREATE TABLE local_totp_replay (
tenant_id uuid NOT NULL,
principal text NOT NULL,
secret_sha256 text NOT NULL CHECK (secret_sha256 ~ '^[0-9a-f]{64}$'),
last_step bigint NOT NULL CHECK (last_step >= 0),
consumed_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (tenant_id, principal, secret_sha256),
FOREIGN KEY (tenant_id, principal) REFERENCES memberships (tenant_id, principal)
);
ALTER TABLE local_totp_replay ENABLE ROW LEVEL SECURITY;
ALTER TABLE local_totp_replay FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_scope ON local_totp_replay
USING (tenant_id::text = current_setting('app.tenant_id', true))
WITH CHECK (tenant_id::text = current_setting('app.tenant_id', true));
REVOKE ALL ON local_totp_replay FROM PUBLIC;
GRANT SELECT ON local_totp_replay TO rmm_app;
-- The application cannot modify memberships. The owner performs the row lock
-- and atomic replay consumption in the caller's selected tenant transaction.
CREATE FUNCTION local_totp_consume(selected_principal text, secret_hash text, selected_step bigint)
RETURNS boolean LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog,public AS $$
DECLARE selected_tenant uuid;
BEGIN
selected_tenant := nullif(current_setting('app.tenant_id', true), '')::uuid;
IF selected_tenant IS NULL THEN RETURN false; END IF;
PERFORM 1 FROM memberships WHERE tenant_id=selected_tenant AND principal=selected_principal
AND kind='human' AND revoked_at IS NULL FOR SHARE;
IF NOT FOUND THEN RETURN false; END IF;
INSERT INTO local_totp_replay(tenant_id,principal,secret_sha256,last_step)
VALUES(selected_tenant,selected_principal,secret_hash,selected_step)
ON CONFLICT(tenant_id,principal,secret_sha256) DO UPDATE
SET last_step=excluded.last_step,consumed_at=now()
WHERE local_totp_replay.last_step < excluded.last_step;
RETURN FOUND;
END;
$$;
REVOKE ALL ON FUNCTION local_totp_consume(text,text,bigint) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION local_totp_consume(text,text,bigint) TO rmm_app;
DO $$
DECLARE current_tenant uuid;
BEGIN
FOR current_tenant IN SELECT tenant_id FROM tenants LOOP
PERFORM set_config('app.tenant_id', current_tenant::text, true);
UPDATE memberships SET permissions = array_append(permissions, 'tokens:manage')
WHERE tenant_id = current_tenant AND kind = 'human' AND revoked_at IS NULL
AND 'plans:approve' = ANY(permissions) AND NOT ('tokens:manage' = ANY(permissions));
END LOOP;
END;
$$;
+90
View File
@@ -0,0 +1,90 @@
CREATE TABLE mcp_tokens (
tenant_id uuid NOT NULL REFERENCES tenants,
token_id uuid NOT NULL,
principal text NOT NULL,
name text NOT NULL CHECK(length(name) BETWEEN 1 AND 120 AND name !~ '[[:cntrl:]]'),
token_hash text NOT NULL UNIQUE CHECK(token_hash ~ '^[0-9a-f]{64}$'),
scopes text[] NOT NULL CHECK(cardinality(scopes) BETWEEN 1 AND 11 AND scopes <@ ARRAY[
'inventory:read','logs:read','operations:read','operations:plan','operations:submit',
'jobs:cancel','scripts:execute','system:execute','root:execute','updates:apply','catalog:write']::text[]),
created_by text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL CHECK(expires_at > created_at AND expires_at <= created_at + interval '365 days'),
last_used_at timestamptz,
revoked_at timestamptz,
PRIMARY KEY(tenant_id,token_id),
UNIQUE(tenant_id,principal),
CHECK(principal='mcp:'||token_id::text),
FOREIGN KEY(tenant_id,principal) REFERENCES memberships(tenant_id,principal),
FOREIGN KEY(tenant_id,created_by) REFERENCES memberships(tenant_id,principal)
);
ALTER TABLE mcp_tokens ENABLE ROW LEVEL SECURITY;
ALTER TABLE mcp_tokens FORCE ROW LEVEL SECURITY;
CREATE POLICY tenant_scope ON mcp_tokens
USING(tenant_id::text=current_setting('app.tenant_id',true))
WITH CHECK(tenant_id::text=current_setting('app.tenant_id',true));
GRANT SELECT,UPDATE(last_used_at) ON mcp_tokens TO rmm_app;
-- Only this exact-digest routing index can be read without a tenant context.
-- It grants no membership or token authority. Every scoped lookup checks them live.
CREATE TABLE mcp_token_routes (
token_hash text PRIMARY KEY CHECK(token_hash ~ '^[0-9a-f]{64}$'),
tenant_id uuid NOT NULL,
token_id uuid NOT NULL,
FOREIGN KEY(tenant_id,token_id) REFERENCES mcp_tokens(tenant_id,token_id)
);
REVOKE ALL ON mcp_token_routes FROM PUBLIC,rmm_app;
CREATE FUNCTION mcp_token_route(digest text) RETURNS TABLE(tenant_id uuid,token_id uuid)
LANGUAGE sql STABLE SECURITY DEFINER SET search_path=pg_catalog AS $$
SELECT r.tenant_id,r.token_id FROM public.mcp_token_routes r WHERE r.token_hash=digest;
$$;
REVOKE ALL ON FUNCTION mcp_token_route(text) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION mcp_token_route(text) TO rmm_app;
CREATE FUNCTION issue_mcp_token(actor text,id uuid,label text,digest text,requested text[],expiry timestamptz)
RETURNS void LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog AS $$
DECLARE
tenant uuid := current_setting('app.tenant_id')::uuid;
member text := 'mcp:'||id::text;
BEGIN
IF id='00000000-0000-0000-0000-000000000000'::uuid OR NOT EXISTS(
SELECT 1 FROM public.memberships m WHERE m.tenant_id=tenant AND m.principal=actor
AND m.kind='human' AND m.revoked_at IS NULL AND 'tokens:manage'=ANY(m.permissions)
AND requested <@ m.permissions
) THEN RAISE EXCEPTION 'MCP_TOKEN_FORBIDDEN' USING ERRCODE='42501'; END IF;
INSERT INTO public.memberships(tenant_id,principal,kind,permissions) VALUES(tenant,member,'automation',requested);
INSERT INTO public.mcp_tokens(tenant_id,token_id,principal,name,token_hash,scopes,created_by,expires_at)
VALUES(tenant,id,member,label,digest,requested,actor,expiry);
INSERT INTO public.mcp_token_routes VALUES(digest,tenant,id);
INSERT INTO public.audit_events(tenant_id,event_id,principal,event_type,data)
VALUES(tenant,id,actor,'mcp_token_created',jsonb_build_object('token_id',id,'principal',member,'scopes',requested,'expires_at',expiry));
END $$;
CREATE FUNCTION revoke_mcp_token(actor text,id uuid,event uuid) RETURNS boolean
LANGUAGE plpgsql SECURITY DEFINER SET search_path=pg_catalog AS $$
DECLARE
tenant uuid := current_setting('app.tenant_id')::uuid;
member text;
BEGIN
IF NOT EXISTS(SELECT 1 FROM public.memberships m WHERE m.tenant_id=tenant AND m.principal=actor
AND m.kind='human' AND m.revoked_at IS NULL AND 'tokens:manage'=ANY(m.permissions))
THEN RAISE EXCEPTION 'MCP_TOKEN_FORBIDDEN' USING ERRCODE='42501'; END IF;
SELECT t.principal INTO member FROM public.mcp_tokens t WHERE t.tenant_id=tenant AND t.token_id=id FOR UPDATE;
IF member IS NULL THEN RETURN false; END IF;
UPDATE public.mcp_tokens SET revoked_at=coalesce(revoked_at,now()) WHERE tenant_id=tenant AND token_id=id;
UPDATE public.memberships SET revoked_at=coalesce(revoked_at,now()) WHERE tenant_id=tenant AND principal=member;
INSERT INTO public.audit_events(tenant_id,event_id,principal,event_type,data)
VALUES(tenant,event,actor,'mcp_token_revoked',jsonb_build_object('token_id',id,'principal',member));
RETURN true;
END $$;
REVOKE ALL ON FUNCTION issue_mcp_token(text,uuid,text,text,text[],timestamptz),revoke_mcp_token(text,uuid,uuid) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION issue_mcp_token(text,uuid,text,text,text[],timestamptz),revoke_mcp_token(text,uuid,uuid) TO rmm_app;
-- SECURITY INVOKER intentionally retains the caller's tenant RLS context.
CREATE FUNCTION mcp_principal_permission(member text,permission text) RETURNS boolean
LANGUAGE sql STABLE SET search_path=pg_catalog AS $$
SELECT EXISTS(SELECT 1 FROM public.mcp_tokens t JOIN public.memberships m USING(tenant_id,principal)
WHERE t.principal=member AND t.revoked_at IS NULL AND t.expires_at>now()
AND m.kind='automation' AND m.revoked_at IS NULL AND permission=ANY(t.scopes) AND permission=ANY(m.permissions));
$$;
REVOKE ALL ON FUNCTION mcp_principal_permission(text,text) FROM PUBLIC;
GRANT EXECUTE ON FUNCTION mcp_principal_permission(text,text) TO rmm_app;