168 lines
6.0 KiB
SQL
168 lines
6.0 KiB
SQL
-- VR-MCP: OAuth 2.1 storage for remote MCP client onboarding.
|
|
--
|
|
-- Tokens and authorization codes are stored hash-only. The MCP resource stays
|
|
-- host-root (`mcp.hivegrid.de` / `staging-mcp.hivegrid.de`), while the
|
|
-- authorization server lives on the app host.
|
|
--
|
|
-- ---------------------------------------------------------------------------
|
|
-- INVERSE / ROLLBACK (forward-only project - reference only, NOT auto-applied):
|
|
-- DROP INDEX IF EXISTS "mcp_oauth_access_client_expires_idx";
|
|
-- DROP INDEX IF EXISTS "mcp_oauth_access_tenant_user_idx";
|
|
-- DROP INDEX IF EXISTS "McpOAuthAccessToken_tokenHash_key";
|
|
-- DROP TABLE IF EXISTS "McpOAuthAccessToken";
|
|
-- DROP INDEX IF EXISTS "mcp_oauth_refresh_client_expires_idx";
|
|
-- DROP INDEX IF EXISTS "mcp_oauth_refresh_tenant_user_idx";
|
|
-- DROP INDEX IF EXISTS "McpOAuthRefreshToken_tokenHash_key";
|
|
-- DROP TABLE IF EXISTS "McpOAuthRefreshToken";
|
|
-- DROP INDEX IF EXISTS "mcp_oauth_code_tenant_user_idx";
|
|
-- DROP INDEX IF EXISTS "mcp_oauth_code_client_expires_idx";
|
|
-- DROP INDEX IF EXISTS "McpOAuthAuthorizationCode_codeHash_key";
|
|
-- DROP TABLE IF EXISTS "McpOAuthAuthorizationCode";
|
|
-- DROP INDEX IF EXISTS "mcp_oauth_client_active_created_idx";
|
|
-- DROP INDEX IF EXISTS "McpOAuthClient_clientId_key";
|
|
-- DROP TABLE IF EXISTS "McpOAuthClient";
|
|
-- ---------------------------------------------------------------------------
|
|
|
|
CREATE TABLE IF NOT EXISTS "McpOAuthClient" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
|
"clientId" TEXT NOT NULL,
|
|
"clientName" TEXT NOT NULL,
|
|
"redirectUris" JSONB NOT NULL,
|
|
"grantTypes" JSONB NOT NULL DEFAULT '["authorization_code","refresh_token"]',
|
|
"scopes" JSONB NOT NULL DEFAULT '["mcp:read"]',
|
|
"contacts" JSONB,
|
|
"metadata" JSONB,
|
|
"active" BOOLEAN NOT NULL DEFAULT true,
|
|
CONSTRAINT "McpOAuthClient_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "McpOAuthClient_clientId_key"
|
|
ON "McpOAuthClient"("clientId");
|
|
CREATE INDEX IF NOT EXISTS "mcp_oauth_client_active_created_idx"
|
|
ON "McpOAuthClient"("active", "createdAt");
|
|
|
|
CREATE TABLE IF NOT EXISTS "McpOAuthAuthorizationCode" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"clientId" TEXT NOT NULL,
|
|
"userId" TEXT NOT NULL,
|
|
"tenantId" TEXT NOT NULL,
|
|
"tenantSlug" TEXT NOT NULL,
|
|
"codeHash" TEXT NOT NULL,
|
|
"redirectUri" TEXT NOT NULL,
|
|
"scopes" JSONB NOT NULL,
|
|
"resource" TEXT NOT NULL,
|
|
"codeChallenge" TEXT NOT NULL,
|
|
"codeChallengeMethod" TEXT NOT NULL,
|
|
"expiresAt" TIMESTAMP(3) NOT NULL,
|
|
"consumedAt" TIMESTAMP(3),
|
|
CONSTRAINT "McpOAuthAuthorizationCode_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM pg_constraint
|
|
WHERE conname = 'McpOAuthAuthorizationCode_clientId_fkey'
|
|
) THEN
|
|
ALTER TABLE "McpOAuthAuthorizationCode"
|
|
ADD CONSTRAINT "McpOAuthAuthorizationCode_clientId_fkey"
|
|
FOREIGN KEY ("clientId") REFERENCES "McpOAuthClient"("id")
|
|
ON DELETE CASCADE ON UPDATE CASCADE;
|
|
END IF;
|
|
END $$;
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "McpOAuthAuthorizationCode_codeHash_key"
|
|
ON "McpOAuthAuthorizationCode"("codeHash");
|
|
CREATE INDEX IF NOT EXISTS "mcp_oauth_code_client_expires_idx"
|
|
ON "McpOAuthAuthorizationCode"("clientId", "expiresAt");
|
|
CREATE INDEX IF NOT EXISTS "mcp_oauth_code_tenant_user_idx"
|
|
ON "McpOAuthAuthorizationCode"("tenantId", "userId");
|
|
|
|
CREATE TABLE IF NOT EXISTS "McpOAuthRefreshToken" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"rotatedAt" TIMESTAMP(3),
|
|
"clientId" TEXT NOT NULL,
|
|
"userId" TEXT NOT NULL,
|
|
"tenantId" TEXT NOT NULL,
|
|
"tenantSlug" TEXT NOT NULL,
|
|
"tokenHash" TEXT NOT NULL,
|
|
"scopes" JSONB NOT NULL,
|
|
"resource" TEXT NOT NULL,
|
|
"expiresAt" TIMESTAMP(3) NOT NULL,
|
|
"revokedAt" TIMESTAMP(3),
|
|
CONSTRAINT "McpOAuthRefreshToken_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM pg_constraint
|
|
WHERE conname = 'McpOAuthRefreshToken_clientId_fkey'
|
|
) THEN
|
|
ALTER TABLE "McpOAuthRefreshToken"
|
|
ADD CONSTRAINT "McpOAuthRefreshToken_clientId_fkey"
|
|
FOREIGN KEY ("clientId") REFERENCES "McpOAuthClient"("id")
|
|
ON DELETE CASCADE ON UPDATE CASCADE;
|
|
END IF;
|
|
END $$;
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "McpOAuthRefreshToken_tokenHash_key"
|
|
ON "McpOAuthRefreshToken"("tokenHash");
|
|
CREATE INDEX IF NOT EXISTS "mcp_oauth_refresh_tenant_user_idx"
|
|
ON "McpOAuthRefreshToken"("tenantId", "userId");
|
|
CREATE INDEX IF NOT EXISTS "mcp_oauth_refresh_client_expires_idx"
|
|
ON "McpOAuthRefreshToken"("clientId", "expiresAt");
|
|
|
|
CREATE TABLE IF NOT EXISTS "McpOAuthAccessToken" (
|
|
"id" TEXT NOT NULL,
|
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
|
"clientId" TEXT NOT NULL,
|
|
"userId" TEXT NOT NULL,
|
|
"tenantId" TEXT NOT NULL,
|
|
"tenantSlug" TEXT NOT NULL,
|
|
"tokenHash" TEXT NOT NULL,
|
|
"scopes" JSONB NOT NULL,
|
|
"resource" TEXT NOT NULL,
|
|
"expiresAt" TIMESTAMP(3) NOT NULL,
|
|
"revokedAt" TIMESTAMP(3),
|
|
"refreshTokenId" TEXT,
|
|
CONSTRAINT "McpOAuthAccessToken_pkey" PRIMARY KEY ("id")
|
|
);
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM pg_constraint
|
|
WHERE conname = 'McpOAuthAccessToken_clientId_fkey'
|
|
) THEN
|
|
ALTER TABLE "McpOAuthAccessToken"
|
|
ADD CONSTRAINT "McpOAuthAccessToken_clientId_fkey"
|
|
FOREIGN KEY ("clientId") REFERENCES "McpOAuthClient"("id")
|
|
ON DELETE CASCADE ON UPDATE CASCADE;
|
|
END IF;
|
|
END $$;
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1 FROM pg_constraint
|
|
WHERE conname = 'McpOAuthAccessToken_refreshTokenId_fkey'
|
|
) THEN
|
|
ALTER TABLE "McpOAuthAccessToken"
|
|
ADD CONSTRAINT "McpOAuthAccessToken_refreshTokenId_fkey"
|
|
FOREIGN KEY ("refreshTokenId") REFERENCES "McpOAuthRefreshToken"("id")
|
|
ON DELETE SET NULL ON UPDATE CASCADE;
|
|
END IF;
|
|
END $$;
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS "McpOAuthAccessToken_tokenHash_key"
|
|
ON "McpOAuthAccessToken"("tokenHash");
|
|
CREATE INDEX IF NOT EXISTS "mcp_oauth_access_tenant_user_idx"
|
|
ON "McpOAuthAccessToken"("tenantId", "userId");
|
|
CREATE INDEX IF NOT EXISTS "mcp_oauth_access_client_expires_idx"
|
|
ON "McpOAuthAccessToken"("clientId", "expiresAt");
|