The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
made initial version work
Steven Tey5c92c1971204
diff --git a/components/app/modals/saml-modal.tsx b/components/app/modals/saml-modal.tsxnew file mode 100644index 0000000000..452458f2b9--- /dev/null+++ b/components/app/modals/saml-modal.tsx@@ -0,0 +1,122 @@+import {+ Dispatch,+ SetStateAction,+ useCallback,+ useMemo,+ useState,+} from "react";+import Modal from "#/ui/modal";+import Button from "#/ui/button";+import { toast } from "sonner";+import { ArrowRight } from "lucide-react";+import { Logo } from "#/ui/icons";+import useProject from "#/lib/swr/use-project";+import { InfoTooltip, SimpleTooltipContent } from "#/ui/tooltip";+import { HOME_DOMAIN } from "#/lib/constants";++function SAMLModal({+ showSAMLModal,+ setShowSAMLModal,+}: {+ showSAMLModal: boolean;+ setShowSAMLModal: Dispatch<SetStateAction<boolean>>;+}) {+ const { slug } = useProject();+ const [submitting, setSubmitting] = useState(false);++ return (+ <Modal showModal={showSAMLModal} setShowModal={setShowSAMLModal}>+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-8 sm:px-16">+ <div className="flex items-center space-x-3 py-4">+ <img+ src="/_static/icons/okta.svg"+ alt="Okta logo"+ className="h-10 w-10"+ />+ <ArrowRight className="h-5 w-5 text-gray-600" />+ <Logo />+ </div>+ <h3 className="text-lg font-medium">Okta SAML</h3>+ <p className="text-center text-sm text-gray-500">+ Configure Okta SAML for your Dub account.+ </p>+ </div>++ <div className="flex flex-col space-y-6 bg-gray-50 px-4 py-8 text-left sm:px-16">+ <form+ onSubmit={async (e) => {+ e.preventDefault();+ setSubmitting(true);+ fetch(`/api/projects/${slug}/saml`, {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ metadataUrl: e.currentTarget.metadataUrl.value,+ }),+ }).then((res) => {+ if (res.ok) {+ toast.success("Successfully configured SAML");+ setShowSAMLModal(false);+ } else {+ toast.error("Failed to configure SAML");+ }+ setSubmitting(false);+ });+ }}+ className="flex flex-col space-y-4"+ >+ <div>+ <div className="flex items-center space-x-2">+ <h2 className="text-sm font-medium text-gray-900">+ Metadata URL+ </h2>+ <InfoTooltip+ content={+ <SimpleTooltipContent+ title={`Your Short.io API Key can be found in your Short.io account under "Integrations & API".`}+ cta="Read the guide."+ href={`${HOME_DOMAIN}/help/article/migrating-from-short`}+ />+ }+ />+ </div>+ <input+ id="metadataUrl"+ name="metadataUrl"+ autoFocus+ type="url"+ placeholder="https://"+ autoComplete="off"+ required+ className="mt-1 block w-full appearance-none rounded-md border border-gray-300 px-3 py-2 placeholder-gray-400 shadow-sm focus:border-black focus:outline-none focus:ring-black sm:text-sm"+ />+ </div>+ <Button text="Submit" loading={submitting} />+ </form>+ </div>+ </Modal>+ );+}++export function useSAMLModal() {+ const [showSAMLModal, setShowSAMLModal] = useState(false);++ const SAMLModalCallback = useCallback(() => {+ return (+ <SAMLModal+ showSAMLModal={showSAMLModal}+ setShowSAMLModal={setShowSAMLModal}+ />+ );+ }, [showSAMLModal, setShowSAMLModal]);++ return useMemo(+ () => ({+ setShowSAMLModal,+ SAMLModal: SAMLModalCallback,+ }),+ [setShowSAMLModal, SAMLModalCallback],+ );+}diff --git a/components/layout/app/settings-layout.tsx b/components/layout/app/settings-layout.tsxindex 3d9abcae0f..d49f154945 100644--- a/components/layout/app/settings-layout.tsx+++ b/components/layout/app/settings-layout.tsx@@ -26,6 +26,10 @@ export default function SettingsLayout({ children }: { children: ReactNode }) {name: "People",href: `/${slug}/settings/people`,},+ {+ name: "Security",+ href: `/${slug}/settings/security`,+ },]: []),];diff --git a/pages/api/oauth/authorize.ts b/pages/api/oauth/authorize.tsnew file mode 100644index 0000000000..a284675188--- /dev/null+++ b/pages/api/oauth/authorize.ts@@ -0,0 +1,43 @@+import jackson from "#/lib/jackson";+import { NextApiRequest, NextApiResponse } from "next";++export default async function handler(+ req: NextApiRequest,+ res: NextApiResponse,+) {+ const { method } = req;++ try {+ switch (method) {+ case "GET":+ case "POST":+ await handleAuthorize(req, res);+ break;+ default:+ res.setHeader("Allow", "GET, POST");+ res.status(405).json({+ error: { message: `Method ${method} Not Allowed` },+ });+ }+ } catch (err: any) {+ res.status(500).json({ error: { message: err.message } });+ }+}++const handleAuthorize = async (req: NextApiRequest, res: NextApiResponse) => {+ const { oauthController } = await jackson();++ const requestParams = req.method === "GET" ? req.query : req.body;++ const { redirect_url, authorize_form } = await oauthController.authorize(+ requestParams,+ );+ console.log(JSON.stringify({ redirect_url, authorize_form }, null, 2));++ if (redirect_url) {+ res.redirect(302, redirect_url);+ } else {+ res.setHeader("Content-Type", "text/html; charset=utf-8");+ res.send(authorize_form);+ }+};diff --git a/pages/api/projects/[slug]/saml.ts b/pages/api/projects/[slug]/saml.tsindex e98f1a1323..a2fc5439fd 100644--- a/pages/api/projects/[slug]/saml.ts+++ b/pages/api/projects/[slug]/saml.ts@@ -1,16 +1,32 @@import { withProjectAuth } from "#/lib/auth";-import prisma from "#/lib/prisma";import jackson from "#/lib/jackson";export default withProjectAuth(async (req, res, project) => {- // POST /api/projects/[slug]/logo – upload a new logo- if (req.method === "POST") {- const { metadataUrl } = req.body as { metadataUrl: string };+ // GET /api/projects/[slug]/saml – get all SAML connections+ if (req.method === "GET") {+ const { apiController } = await jackson();++ const connections = await apiController.getConnections({+ tenant: project.id,+ product: "dub",+ });++ const response = {+ connections,+ issuer: "https://saml.dub.sh",+ acs: `${process.env.NEXTAUTH_URL}/api/auth/saml/callback`,+ };++ return res.status(200).json(response);++ // POST /api/projects/[slug]/saml – create a new SAML connection+ } else if (req.method === "POST") {+ const { metadataUrl } = req.body;const { apiController } = await jackson();const data = await apiController.createSAMLConnection({- encodedRawMetadata: metadataUrl,+ encodedRawMetadata: "",metadataUrl,defaultRedirectUrl: process.env.NEXTAUTH_URL as string,redirectUrl: process.env.NEXTAUTH_URL as string,@@ -20,7 +36,7 @@ export default withProjectAuth(async (req, res, project) => {return res.status(200).json(data);} else {- res.setHeader("Allow", ["POST"]);+ res.setHeader("Allow", ["GET", "POST"]);return res.status(405).end(`Method ${req.method} Not Allowed`);}});diff --git a/pages/app/[slug]/settings/index.tsx b/pages/app/[slug]/settings/index.tsxindex 0021929acb..9bf7130fdb 100644--- a/pages/app/[slug]/settings/index.tsx+++ b/pages/app/[slug]/settings/index.tsx@@ -22,7 +22,7 @@ export default function ProjectSettingsGeneral() {placeholder: "My Project",maxLength: 32,}}- helpText="Max 32 characters,"+ helpText="Max 32 characters."handleSubmit={(updateData) =>fetch(`/api/projects/${slug}`, {method: "PUT",diff --git a/pages/app/[slug]/settings/security.tsx b/pages/app/[slug]/settings/security.tsxnew file mode 100644index 0000000000..6c04e87440--- /dev/null+++ b/pages/app/[slug]/settings/security.tsx@@ -0,0 +1,95 @@+import Button from "#/ui/button";+import { useSAMLModal } from "@/components/app/modals/saml-modal";+import SettingsLayout from "@/components/layout/app/settings-layout";+import { Lock } from "lucide-react";+import useProject from "#/lib/swr/use-project";+import useSWR from "swr";+import type { SAMLSSORecord } from "@boxyhq/saml-jackson";+import { fetcher } from "#/lib/utils";+import { useMemo } from "react";++export default function ProjectSecurity() {+ const { SAMLModal, setShowSAMLModal } = useSAMLModal();+ const { slug } = useProject();+ const { data: sso, isLoading } = useSWR<{ connections: SAMLSSORecord[] }>(+ slug && `/api/projects/${slug}/saml`,+ fetcher,+ );++ const data = useMemo(() => {+ if (!slug || isLoading) {+ return {+ logo: null,+ title: null,+ description: null,+ };+ } else if (sso && sso.connections.length > 0) {+ return {+ logo: (+ <img+ src="/_static/icons/okta.svg"+ alt={sso.connections[0].idpMetadata.friendlyProviderName + " logo"}+ className="h-8 w-8"+ />+ ),+ title: sso.connections[0].idpMetadata.friendlyProviderName,+ description: "SAML SSO is configured for your project.",+ };+ } else+ return {+ logo: (+ <div className="rounded-full border border-gray-200 p-2">+ <Lock className="h-4 w-4 text-gray-600" />+ </div>+ ),+ title: "SAML",+ description: "Choose an identity provider to get started.",+ };+ }, [isLoading, sso]);++ return (+ <SettingsLayout>+ <SAMLModal />+ <div className="rounded-lg border border-gray-200 bg-white">+ <div className="relative flex flex-col space-y-6 p-5 sm:p-10">+ <div className="flex flex-col space-y-3">+ <h2 className="text-xl font-medium">SAML Single Sign-On</h2>+ <p className="text-sm text-gray-500">+ Set up SAML Single Sign-On (SSO) to allow your team to sign in to+ Dub with your identity provider.+ </p>+ </div>++ <div className="mt-2 flex items-center justify-between rounded-md border border-gray-200 px-4 py-3">+ <div className="flex items-center space-x-4">+ {data.logo || (+ <div className="h-8 w-8 animate-pulse rounded-full bg-gray-100" />+ )}+ <div className="flex flex-col">+ {data.title ? (+ <h3 className="font-medium">{data.title}</h3>+ ) : (+ <div className="h-5 w-20 animate-pulse rounded-md bg-gray-100" />+ )}+ {data.description ? (+ <p className="text-sm text-gray-500">+ Choose an identity provider to get started.+ </p>+ ) : (+ <div className="mt-2 h-4 w-40 animate-pulse rounded-md bg-gray-100" />+ )}+ </div>+ </div>+ <div>+ <Button text="Configure" onClick={() => setShowSAMLModal(true)} />+ </div>+ </div>+ </div>++ <div className="flex items-center justify-between rounded-b-lg border-t border-gray-200 bg-gray-50 px-3 py-5 sm:px-10">+ <p className="text-sm text-gray-500">Learn more about SAML SSO</p>+ </div>+ </div>+ </SettingsLayout>+ );+}diff --git a/public/_static/icons/okta.svg b/public/_static/icons/okta.svgnew file mode 100644index 0000000000..5b773418ea--- /dev/null+++ b/public/_static/icons/okta.svg@@ -0,0 +1,7 @@+<svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1594 1595" width="1594" height="1595">+ <title>oktaddd-svg</title>+ <style>+ .s0 { fill: #000000 }+ </style>+ <path id="Layer" fill-rule="evenodd" class="s0" d="m877 11.5l-32.8 403.8c-15.5-1.8-31-2.7-46.9-2.7-19.9 0-39.4 1.3-58.5 4.4l-18.6-195.6c-0.4-6.2 4.5-11.6 10.7-11.6h33.2l-16-197.8c-0.4-6.2 4.5-11.6 10.2-11.6h108.5c6.2 0 11.1 5.4 10.2 11.1zm-166.1 410.4c-35 8-68.2 20.8-98.8 37.6l-84.6-177.5c-2.6-5.3 0-11.9 5.8-14.2l31.4-11.5-82.8-180.6c-2.6-5.3 0-11.9 5.8-14.2l101.9-37.2c5.7-2.2 11.9 1.4 13.7 7.1 0.4 0 107.6 390.5 107.6 390.5zm-357.4-278l234.3 330.2c-29.7 19.5-56.7 42.5-79.7 69.1l-140.5-138.1c-4.4-4.4-3.9-11.5 0.5-15.5l25.7-21.3-139.6-141.2c-4.4-4.4-3.9-11.5 0.9-15.5l82.9-69.5c4.8-4 11.5-3.1 15 1.8zm136.9 421.4c-21.3 27.9-38.5 58.9-51.4 92.1l-178.9-81.9c-5.8-2.2-8-9.3-4.9-14.6l16.8-28.8-179.8-85c-5.3-2.6-7.5-9.3-4.4-14.6l54-93.8c3.1-5.3 10.2-7.1 15.1-3.6zm-466 24.8c0.9-6.2 7.1-9.7 12.8-8.4l392 102.3c-10.2 33.2-15.9 68.2-16.8 104.5l-196.2-16c-6.2-0.4-10.7-6.2-9.3-12.4l5.7-32.7-198-18.6c-6.2-0.5-10.1-6.2-9.3-12.4l18.6-106.7zm-15 264.7l403.5-37.2c1.8 35.9 8.9 70.9 19.9 103.6l-189.5 52.3c-5.8 1.3-12-2.2-12.9-8.4l-5.7-32.8-192.3 50c-5.7 1.4-11.9-2.2-12.8-8.4l-19.1-106.7c-0.9-6.2 3.1-11.9 9.3-12.4zm63.4 280.7c-3.1-5.3-0.9-11.9 4.4-14.6l365.9-173.5c13.7 32.7 32.3 63.3 54.4 90.7l-160.3 114.2c-4.9 3.6-12 2.2-15.1-3.1l-16.8-29.2-163.4 112.9c-4.9 3.5-12 1.8-15.1-3.5 0 0-54.5-93.9-54-93.9zm525.7-9.3l-111.6 162c-3.5 5.4-10.6 6.2-15.5 2.3l-25.7-21.7-115.1 162c-3.6 4.9-10.2 5.8-15.1 1.8l-83.3-69.5c-4.8-4-5.3-11.1-0.9-15.5l284.8-288.2c24.4 25.6 52.3 48.2 82.4 66.8zm-138.6 395.8c-5.8-2.2-8.4-8.9-5.8-14.2l168.8-368.3c31 15.9 64.7 27.9 99.7 34.5l-49.7 190.4c-1.3 5.7-7.9 9.3-13.7 7.1l-31.4-11.5-52.7 191.7c-1.8 5.7-8 9.3-13.8 7l-101.8-37.1zm337.5-340c19.9 0 39.4-1.4 58.4-4.5l18.6 195.7c0.5 6.2-4.4 11.5-10.6 11.5h-33.2l15.9 197.9c0.9 6.2-3.9 11.5-10.1 11.5h-108.6c-5.7 0-10.6-5.3-10.2-11.5l32.8-403.7c15.5 2.2 31 3.1 47 3.1zm174.5-728.3c-31-15.5-64.2-27.5-99.7-34.5l49.6-190.4c1.8-5.8 8-9.3 13.8-7.1l31.4 11.5 52.7-191.7c1.8-5.7 8-9.3 13.8-7.1l101.8 37.2c5.8 2.2 8.5 8.4 5.8 14.2zm391.6-207.2l-284.9 288.2c-23.9-25.7-51.3-48.2-81.9-66.8l111.6-162.1c3.6-4.8 10.7-6.2 15.5-2.2l25.7 21.7 115.2-162c3.5-4.9 10.6-5.8 15-1.8l83.3 69.5c4.9 4 4.9 11.1 0.5 15.5zm153.7 227.1l-365.9 173.6c-14.2-32.8-32.3-63.4-54.5-90.8l160.3-114.2c4.9-4 12-2.2 15.1 3.1l16.8 28.8 163.5-112.9c4.9-3.1 11.9-1.8 15 3.5l54.5 93.9c3.1 5.3 1.4 11.9-4.4 14.6zm58 146.5l18.6 106.7c0.9 6.2-3.1 11.5-9.3 12.4l-403.5 37.6c-1.8-36.3-8.9-70.8-19.9-103.6l189.6-52.2c5.7-1.8 11.9 2.2 12.8 8.4l5.8 32.8 192.2-50.1c5.8-1.3 12 2.3 12.8 8.5zm-18.6 391.3l-392-102.3c10.2-33.2 16-68.1 16.9-104.4l196.2 15.9c6.2 0.9 10.2 6.2 9.3 12.4l-5.8 32.8 198 18.6c6.2 0.8 10.2 6.1 9.3 12.3l-18.6 106.7c-0.9 6.2-7.1 9.8-12.8 8.5zm-104.1 243.9c-3.1 5.3-10.2 6.6-15.1 3.5l-333.5-230.2c21.3-27.9 38.5-58.9 51.4-92.1l178.9 81.9c5.8 2.7 8 9.3 4.9 14.6l-16.8 28.8 179.8 85c5.3 2.7 7.5 9.3 4.4 14.6zm-446.5-135.9c29.7-19 56.3-42.5 79.8-69l140.4 138.1c4.4 4.4 4.4 11.5-0.5 15.5l-25.7 21.2 139.6 141.3c4 4.4 4 11.5-0.9 15.4l-82.8 69.6c-4.5 3.9-11.6 3.1-15.1-1.8l-234.3-330.3zm-1.8 449.8c-5.7 2.2-11.9-1.3-13.7-7.1l-107.2-390.4c35-8 68.2-20.8 98.8-37.7l84.6 177.6c2.6 5.7 0 12.4-5.8 14.1l-31.4 11.5 82.8 180.7c2.6 5.7 0 11.9-5.8 14.1l-101.8 37.2z"/>+</svg>\ No newline at end of file