first commit
This commit is contained in:
41
calcom/example-apps/credential-sync/pages/api/getToken.ts
Normal file
41
calcom/example-apps/credential-sync/pages/api/getToken.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { CALCOM_CREDENTIAL_SYNC_HEADER_NAME, CALCOM_CREDENTIAL_SYNC_SECRET } from "../../constants";
|
||||
import { generateGoogleCalendarAccessToken, generateZoomAccessToken } from "../../lib/integrations";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const secret = req.headers[CALCOM_CREDENTIAL_SYNC_HEADER_NAME];
|
||||
console.log("getToken hit");
|
||||
try {
|
||||
if (!secret) {
|
||||
return res.status(403).json({ message: "secret header not set" });
|
||||
}
|
||||
if (secret !== CALCOM_CREDENTIAL_SYNC_SECRET) {
|
||||
return res.status(403).json({ message: "Invalid secret" });
|
||||
}
|
||||
|
||||
const calcomUserId = req.body.calcomUserId;
|
||||
const appSlug = req.body.appSlug;
|
||||
console.log("getToken Params", {
|
||||
calcomUserId,
|
||||
appSlug,
|
||||
});
|
||||
let accessToken;
|
||||
if (appSlug === "google-calendar") {
|
||||
accessToken = await generateGoogleCalendarAccessToken();
|
||||
} else if (appSlug === "zoom") {
|
||||
accessToken = await generateZoomAccessToken();
|
||||
} else {
|
||||
throw new Error("Unhandled values");
|
||||
}
|
||||
if (!accessToken) {
|
||||
throw new Error("Unable to generate token");
|
||||
}
|
||||
res.status(200).json({
|
||||
_1: true,
|
||||
access_token: accessToken,
|
||||
});
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import type { NextApiRequest } from "next";
|
||||
|
||||
import { symmetricEncrypt } from "@calcom/lib/crypto";
|
||||
|
||||
import {
|
||||
CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY,
|
||||
CALCOM_CREDENTIAL_SYNC_SECRET,
|
||||
CALCOM_CREDENTIAL_SYNC_HEADER_NAME,
|
||||
CALCOM_ADMIN_API_KEY,
|
||||
} from "../../constants";
|
||||
import { generateGoogleCalendarAccessToken, generateZoomAccessToken } from "../../lib/integrations";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res) {
|
||||
const isInvalid = req.query["invalid"] === "1";
|
||||
const userId = parseInt(req.query["userId"] as string);
|
||||
const appSlug = req.query["appSlug"];
|
||||
|
||||
try {
|
||||
let accessToken;
|
||||
if (appSlug === "google-calendar") {
|
||||
accessToken = await generateGoogleCalendarAccessToken();
|
||||
} else if (appSlug === "zoom") {
|
||||
accessToken = await generateZoomAccessToken();
|
||||
} else {
|
||||
throw new Error(`Unhandled appSlug: ${appSlug}`);
|
||||
}
|
||||
|
||||
if (!accessToken) {
|
||||
return res.status(500).json({ error: "Could not get access token" });
|
||||
}
|
||||
|
||||
const result = await fetch(
|
||||
`http://localhost:3002/api/v1/credential-sync?apiKey=${CALCOM_ADMIN_API_KEY}&userId=${userId}`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
[CALCOM_CREDENTIAL_SYNC_HEADER_NAME]: CALCOM_CREDENTIAL_SYNC_SECRET,
|
||||
},
|
||||
body: JSON.stringify({
|
||||
appSlug,
|
||||
encryptedKey: symmetricEncrypt(
|
||||
JSON.stringify({
|
||||
access_token: isInvalid ? "1233231231231" : accessToken,
|
||||
}),
|
||||
CALCOM_APP_CREDENTIAL_ENCRYPTION_KEY
|
||||
),
|
||||
}),
|
||||
}
|
||||
);
|
||||
|
||||
const clonedResult = result.clone();
|
||||
try {
|
||||
if (result.ok) {
|
||||
const json = await result.json();
|
||||
return res.status(200).json(json);
|
||||
} else {
|
||||
return res.status(400).json({ error: await clonedResult.text() });
|
||||
}
|
||||
} catch (e) {
|
||||
return res.status(400).json({ error: await clonedResult.text() });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return res.status(400).json({ message: "Internal Server Error", error: error.message });
|
||||
}
|
||||
}
|
||||
57
calcom/example-apps/credential-sync/pages/index.tsx
Normal file
57
calcom/example-apps/credential-sync/pages/index.tsx
Normal file
@@ -0,0 +1,57 @@
|
||||
import { useRouter, useSearchParams, usePathname } from "next/navigation";
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export default function Index() {
|
||||
const [data, setData] = useState("");
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const pathname = usePathname();
|
||||
const appSlug = searchParams.get("appSlug");
|
||||
const userId = searchParams.get("userId");
|
||||
|
||||
useEffect(() => {
|
||||
let isRedirectNeeded = false;
|
||||
const newSearchParams = new URLSearchParams(new URL(document.URL).searchParams);
|
||||
if (!userId) {
|
||||
newSearchParams.set("userId", "1");
|
||||
isRedirectNeeded = true;
|
||||
}
|
||||
|
||||
if (!appSlug) {
|
||||
newSearchParams.set("appSlug", "google-calendar");
|
||||
isRedirectNeeded = true;
|
||||
}
|
||||
|
||||
if (isRedirectNeeded) {
|
||||
router.push(`${pathname}?${newSearchParams.toString()}`);
|
||||
}
|
||||
}, [router, pathname, userId, appSlug]);
|
||||
|
||||
async function updateToken({ invalid } = { invalid: false }) {
|
||||
const res = await fetch(
|
||||
`/api/setTokenInCalCom?invalid=${invalid ? 1 : 0}&userId=${userId}&appSlug=${appSlug}`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const data = await res.json();
|
||||
setData(JSON.stringify(data));
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Welcome to Credential Sync Playground</h1>
|
||||
<p>
|
||||
You are managing credentials for cal.com <strong>userId={userId}</strong> for{" "}
|
||||
<strong>appSlug={appSlug}</strong>. Update query params to manage a different user or app{" "}
|
||||
</p>
|
||||
<button onClick={() => updateToken({ invalid: true })}>Give an invalid token to Cal.com</button>
|
||||
<button onClick={() => updateToken()}>Give a valid token to Cal.com</button>
|
||||
<div>{data}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user