Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
refactor: rewrite jackson lib to use same DB with jackson-specific tables
Cursor Agentaaa2b01e62bc
diff --git a/.env.example b/.env.exampleindex 45ad4276..7b5951a0 100644--- a/.env.example+++ b/.env.example@@ -81,14 +81,8 @@ NEXT_PUBLIC_WEBHOOK_BASE_URL=NEXT_PUBLIC_WEBHOOK_BASE_HOST=# [[JACKSON SAML/SCIM]]-# External URL of your app (used as Jackson's externalUrl and SAML audience)-JACKSON_EXTERNAL_URL=https://your-app.com-# The SAML audience URI that you'll give to IdP admins+# The SAML audience URI that you'll give to IdP admins (e.g. https://saml.your-app.com)SAML_AUDIENCE=https://saml.your-app.com-# Jackson's internal SAML callback path (relative, matches your API route)-SAML_PATH=/api/auth/saml/callback-# Jackson encryption key (32+ chars)-JACKSON_ENCRYPTION_KEY=your-random-32-char-encryption-key# Product identifier for Jackson (your app name)JACKSON_PRODUCT=papermark# Public product identifier for client-side usediff --git a/app/(auth)/auth/saml/page-client.tsx b/app/(auth)/auth/saml/page-client.tsxindex c6a3f600..b5cb8c6b 100644--- a/app/(auth)/auth/saml/page-client.tsx+++ b/app/(auth)/auth/saml/page-client.tsx@@ -13,7 +13,7 @@ export default function SAMLCallbackClient() {const [errorMessage, setErrorMessage] = useState<string>("");useEffect(() => {- const code = searchParams.get("code");+ const code = searchParams?.get("code");if (code) {// Use NextAuth's signIn with the saml-idp credentials providersignIn("saml-idp", {diff --git a/lib/jackson.ts b/lib/jackson.tsindex 6624d8f8..d12236fe 100644--- a/lib/jackson.ts+++ b/lib/jackson.ts@@ -1,58 +1,51 @@-import jackson, {- type IConnectionAPIController,- type IDirectorySyncController,- type IOAuthController,- type JacksonOption,+import type {+ IConnectionAPIController,+ IDirectorySyncController,+ IOAuthController,+ JacksonOption,} from "@boxyhq/saml-jackson";+import samlJackson from "@boxyhq/saml-jackson";-const g = global as any;+export const samlAudience =+ process.env.SAML_AUDIENCE || "https://saml.papermark.com";-export default async function init(): Promise<{- oauthController: IOAuthController;- connectionController: IConnectionAPIController;- directorySyncController: IDirectorySyncController;-}> {- // Singleton — reuse across hot reloads in dev+const opts: JacksonOption = {+ externalUrl: process.env.NEXTAUTH_URL || "https://app.papermark.com",+ samlPath: "/api/auth/saml/callback",+ samlAudience,+ db: {+ engine: "sql",+ type: "postgres",+ url:+ process.env.POSTGRES_PRISMA_URL_NON_POOLING ||+ (process.env.POSTGRES_PRISMA_URL as string),+ },+ idpEnabled: true, // to allow folks to SSO directly from their IDP+ scimPath: "/api/scim/v2.0",+ clientSecretVerifier: process.env.NEXTAUTH_SECRET as string,+};++declare global {+ var apiController: IConnectionAPIController | undefined;+ var oauthController: IOAuthController | undefined;+ var directorySyncController: IDirectorySyncController | undefined;+}++export async function jackson() {if (- g.oauthController &&- g.connectionController &&- g.directorySyncController+ !globalThis.apiController ||+ !globalThis.oauthController ||+ !globalThis.directorySyncController) {- return {- oauthController: g.oauthController,- connectionController: g.connectionController,- directorySyncController: g.directorySyncController,- };+ const ret = await samlJackson(opts);+ globalThis.apiController = ret.connectionAPIController;+ globalThis.oauthController = ret.oauthController;+ globalThis.directorySyncController = ret.directorySyncController;}- const opts: JacksonOption = {- externalUrl: process.env.JACKSON_EXTERNAL_URL || process.env.NEXTAUTH_URL!,- samlAudience: process.env.SAML_AUDIENCE || process.env.JACKSON_EXTERNAL_URL!,- samlPath: process.env.SAML_PATH || "/api/auth/saml/callback",- db: {- engine: "sql",- type: "postgres",- url: process.env.POSTGRES_PRISMA_URL_NON_POOLING || process.env.POSTGRES_PRISMA_URL!,- encryptionKey: process.env.JACKSON_ENCRYPTION_KEY!,- },- idpEnabled: true,- scimPath: "/api/scim/v2.0",- openid: {},- };-- const {- oauthController,- connectionAPIController: connectionController,- directorySyncController,- } = await jackson(opts);-- g.oauthController = oauthController;- g.connectionController = connectionController;- g.directorySyncController = directorySyncController;-return {- oauthController,- connectionController,- directorySyncController,+ apiController: globalThis.apiController,+ oauthController: globalThis.oauthController,+ directorySyncController: globalThis.directorySyncController,};}diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].tsindex e56a8378..6fae7b1e 100644--- a/pages/api/auth/[...nextauth].ts+++ b/pages/api/auth/[...nextauth].ts@@ -15,7 +15,7 @@ import { dub } from "@/lib/dub";import { isBlacklistedEmail } from "@/lib/edge-config/blacklist";import { sendVerificationRequestEmail } from "@/lib/emails/send-verification-request";import hanko from "@/lib/hanko";-import jackson from "@/lib/jackson";+import { jackson } from "@/lib/jackson";import prisma from "@/lib/prisma";import { CustomUser } from "@/lib/types";import { log } from "@/lib/utils";diff --git a/pages/api/auth/saml/authorize.ts b/pages/api/auth/saml/authorize.tsindex 7a49c11a..958807fa 100644--- a/pages/api/auth/saml/authorize.ts+++ b/pages/api/auth/saml/authorize.ts@@ -1,6 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";-import jackson from "@/lib/jackson";+import { jackson } from "@/lib/jackson";export default async function handler(req: NextApiRequest,diff --git a/pages/api/auth/saml/callback.ts b/pages/api/auth/saml/callback.tsindex c1ff80a4..61b771d3 100644--- a/pages/api/auth/saml/callback.ts+++ b/pages/api/auth/saml/callback.ts@@ -1,6 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";-import jackson from "@/lib/jackson";+import { jackson } from "@/lib/jackson";// Disable body parsing — we need the raw form dataexport const config = {diff --git a/pages/api/auth/saml/token.ts b/pages/api/auth/saml/token.tsindex 4dc723c1..e218597d 100644--- a/pages/api/auth/saml/token.ts+++ b/pages/api/auth/saml/token.ts@@ -1,6 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";-import jackson from "@/lib/jackson";+import { jackson } from "@/lib/jackson";export default async function handler(req: NextApiRequest,diff --git a/pages/api/auth/saml/userinfo.ts b/pages/api/auth/saml/userinfo.tsindex 1460d842..8fac833d 100644--- a/pages/api/auth/saml/userinfo.ts+++ b/pages/api/auth/saml/userinfo.ts@@ -1,6 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";-import jackson from "@/lib/jackson";+import { jackson } from "@/lib/jackson";export default async function handler(req: NextApiRequest,diff --git a/pages/api/scim/v2.0/[...path].ts b/pages/api/scim/v2.0/[...path].tsindex ee67cc2e..7dc1d21d 100644--- a/pages/api/scim/v2.0/[...path].ts+++ b/pages/api/scim/v2.0/[...path].ts@@ -1,6 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";-import jackson from "@/lib/jackson";+import { jackson } from "@/lib/jackson";import prisma from "@/lib/prisma";export default async function handler(diff --git a/pages/api/teams/[teamId]/directory-sync/index.ts b/pages/api/teams/[teamId]/directory-sync/index.tsindex 543bb0c4..67f7e846 100644--- a/pages/api/teams/[teamId]/directory-sync/index.ts+++ b/pages/api/teams/[teamId]/directory-sync/index.ts@@ -3,7 +3,7 @@ import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";import { errorhandler } from "@/lib/errorHandler";-import jackson from "@/lib/jackson";+import { jackson } from "@/lib/jackson";import prisma from "@/lib/prisma";import { CustomUser } from "@/lib/types";diff --git a/pages/api/teams/[teamId]/saml/index.ts b/pages/api/teams/[teamId]/saml/index.tsindex 4754e20b..ca9685b1 100644--- a/pages/api/teams/[teamId]/saml/index.ts+++ b/pages/api/teams/[teamId]/saml/index.ts@@ -3,7 +3,7 @@ import { NextApiRequest, NextApiResponse } from "next";import { getServerSession } from "next-auth/next";import { errorhandler } from "@/lib/errorHandler";-import jackson from "@/lib/jackson";+import { jackson } from "@/lib/jackson";import prisma from "@/lib/prisma";import { CustomUser } from "@/lib/types";@@ -43,9 +43,9 @@ export default async function handle(if (req.method === "GET") {// GET /api/teams/:teamId/saml — list SAML connectionstry {- const { connectionController } = await jackson();+ const { apiController } = await jackson();- const connections = await connectionController.getConnections({+ const connections = await apiController.getConnections({tenant: teamId,product: process.env.JACKSON_PRODUCT!,});@@ -57,7 +57,7 @@ export default async function handle(} else if (req.method === "POST") {// POST /api/teams/:teamId/saml — create a new SAML connectiontry {- const { connectionController } = await jackson();+ const { apiController } = await jackson();const { rawMetadata, metadataUrl, name, description } = req.body;@@ -68,7 +68,7 @@ export default async function handle(});}- const connection = await connectionController.createSAMLConnection({+ const connection = await apiController.createSAMLConnection({defaultRedirectUrl: `${process.env.NEXTAUTH_URL}/auth/saml`,redirectUrl: JSON.stringify([`${process.env.NEXTAUTH_URL}`,@@ -108,7 +108,7 @@ export default async function handle(} else if (req.method === "DELETE") {// DELETE /api/teams/:teamId/saml — remove a SAML connectiontry {- const { connectionController } = await jackson();+ const { apiController } = await jackson();const { clientID, clientSecret } = req.body;@@ -118,13 +118,13 @@ export default async function handle(.json({ error: "clientID and clientSecret are required" });}- await connectionController.deleteConnections({+ await apiController.deleteConnections({clientID,clientSecret,});// Check if there are remaining connections- const remaining = await connectionController.getConnections({+ const remaining = await apiController.getConnections({tenant: teamId,product: process.env.JACKSON_PRODUCT!,});diff --git a/prisma/migrations/20260210000000_add_saml_scim_to_team/migration.sql b/prisma/migrations/20260210000000_add_saml_scim_to_team/migration.sqlindex c01a2cab..124561c6 100644--- a/prisma/migrations/20260210000000_add_saml_scim_to_team/migration.sql+++ b/prisma/migrations/20260210000000_add_saml_scim_to_team/migration.sql@@ -1,7 +1,44 @@--- AlterTable+-- AlterTable: Add SAML/SCIM fields to TeamALTER TABLE "Team" ADD COLUMN "samlEnabled" BOOLEAN NOT NULL DEFAULT false;ALTER TABLE "Team" ADD COLUMN "samlProvider" TEXT;ALTER TABLE "Team" ADD COLUMN "samlConnectionId" TEXT;ALTER TABLE "Team" ADD COLUMN "scimEnabled" BOOLEAN NOT NULL DEFAULT false;ALTER TABLE "Team" ADD COLUMN "scimProvider" TEXT;ALTER TABLE "Team" ADD COLUMN "scimDirectoryId" TEXT;++-- CreateTable: Jackson SAML/SCIM internal tables+CREATE TABLE "jackson_index" (+ "id" SERIAL NOT NULL,+ "key" VARCHAR(250) NOT NULL,+ "storeKey" VARCHAR(250) NOT NULL,++ CONSTRAINT "jackson_index_pkey" PRIMARY KEY ("id")+);++CREATE TABLE "jackson_store" (+ "key" VARCHAR(250) NOT NULL,+ "value" TEXT NOT NULL,+ "iv" VARCHAR(64),+ "tag" VARCHAR(64),+ "namespace" VARCHAR(64),+ "createdAt" TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP,+ "modifiedAt" TIMESTAMP(0),++ CONSTRAINT "jackson_store_pkey" PRIMARY KEY ("key")+);++CREATE TABLE "jackson_ttl" (+ "key" VARCHAR(250) NOT NULL,+ "expiresAt" BIGINT NOT NULL,++ CONSTRAINT "jackson_ttl_pkey" PRIMARY KEY ("key")+);++-- CreateIndex+CREATE INDEX "_jackson_index_key_store" ON "jackson_index"("key", "storeKey");++-- CreateIndex+CREATE INDEX "_jackson_store_namespace" ON "jackson_store"("namespace");++-- CreateIndex+CREATE INDEX "_jackson_ttl_expires_at" ON "jackson_ttl"("expiresAt");diff --git a/prisma/schema/jackson.prisma b/prisma/schema/jackson.prismanew file mode 100644index 00000000..39f7c296--- /dev/null+++ b/prisma/schema/jackson.prisma@@ -0,0 +1,31 @@+// Jackson SAML/SCIM tables+// These are managed by @boxyhq/saml-jackson internally.+// We define them here so Prisma is aware of the tables and+// migrations stay in sync with the main app schema.++model jackson_index {+ id Int @id @default(autoincrement())+ key String @db.VarChar(250)+ storeKey String @db.VarChar(250)++ @@index([key, storeKey], map: "_jackson_index_key_store")+}++model jackson_store {+ key String @id @db.VarChar(250)+ value String @db.Text+ iv String? @db.VarChar(64)+ tag String? @db.VarChar(64)+ namespace String? @db.VarChar(64)+ createdAt DateTime @default(now()) @db.Timestamp(0)+ modifiedAt DateTime? @db.Timestamp(0)++ @@index([namespace], map: "_jackson_store_namespace")+}++model jackson_ttl {+ key String @id @db.VarChar(250)+ expiresAt BigInt++ @@index([expiresAt], map: "_jackson_ttl_expires_at")+}