first commit
This commit is contained in:
35
calcom/packages/app-store/zoho-bigin/api/add.ts
Normal file
35
calcom/packages/app-store/zoho-bigin/api/add.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import axios from "axios";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
|
||||
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
||||
import { encodeOAuthState } from "../../_utils/oauth/encodeOAuthState";
|
||||
import appConfig from "../config.json";
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
if (req.method === "GET") {
|
||||
const appKeys = await getAppKeysFromSlug(appConfig.slug);
|
||||
|
||||
const clientId = typeof appKeys.client_id === "string" ? appKeys.client_id : "";
|
||||
if (!clientId) return res.status(400).json({ message: "Zoho Bigin client_id missing." });
|
||||
|
||||
const redirectUri = `${WEBAPP_URL}/api/integrations/zoho-bigin/callback`;
|
||||
|
||||
const authUrl = axios.getUri({
|
||||
url: "https://accounts.zoho.com/oauth/v2/auth",
|
||||
params: {
|
||||
scope: appConfig.scope,
|
||||
client_id: clientId,
|
||||
response_type: "code",
|
||||
redirect_uri: redirectUri,
|
||||
access_type: "offline",
|
||||
state: encodeOAuthState(req),
|
||||
},
|
||||
});
|
||||
|
||||
res.status(200).json({ url: authUrl });
|
||||
return;
|
||||
}
|
||||
res.status(400).json({ message: "Invalid request method." });
|
||||
}
|
86
calcom/packages/app-store/zoho-bigin/api/callback.ts
Normal file
86
calcom/packages/app-store/zoho-bigin/api/callback.ts
Normal file
@ -0,0 +1,86 @@
|
||||
import axios from "axios";
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import qs from "qs";
|
||||
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
||||
|
||||
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
||||
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
|
||||
import createOAuthAppCredential from "../../_utils/oauth/createOAuthAppCredential";
|
||||
import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState";
|
||||
import appConfig from "../config.json";
|
||||
|
||||
function isAuthorizedAccountsServerUrl(accountsServer: string) {
|
||||
// As per https://www.zoho.com/crm/developer/docs/api/v6/multi-dc.html#:~:text=US:%20https://accounts.zoho,https://accounts.zohocloud.ca&text=The%20%22location=us%22%20parameter,domain%20in%20all%20API%20endpoints.&text=You%20must%20make%20the%20authorization,.zoho.com.cn.
|
||||
const authorizedAccountServers = [
|
||||
"https://accounts.zoho.com",
|
||||
"https://accounts.zoho.eu",
|
||||
"https://accounts.zoho.in",
|
||||
"https://accounts.zoho.com.cn",
|
||||
"https://accounts.zoho.jp",
|
||||
"https://accounts.zohocloud.ca",
|
||||
"https://accounts.zoho.com.au",
|
||||
];
|
||||
return authorizedAccountServers.includes(accountsServer);
|
||||
}
|
||||
|
||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
const { code, "accounts-server": accountsServer } = req.query;
|
||||
const state = decodeOAuthState(req);
|
||||
|
||||
if (code && typeof code !== "string") {
|
||||
res.status(400).json({ message: "`code` must be a string" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!accountsServer || typeof accountsServer !== "string") {
|
||||
res.status(400).json({ message: "`accounts-server` is required and must be a string" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isAuthorizedAccountsServerUrl(accountsServer)) {
|
||||
res.status(400).json({ message: "`accounts-server` is not authorized" });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!req.session?.user?.id) {
|
||||
res.status(401).json({ message: "You must be logged in to do this" });
|
||||
return;
|
||||
}
|
||||
|
||||
const appKeys = await getAppKeysFromSlug(appConfig.slug);
|
||||
|
||||
const clientId = typeof appKeys.client_id === "string" ? appKeys.client_id : "";
|
||||
const clientSecret = typeof appKeys.client_secret === "string" ? appKeys.client_secret : "";
|
||||
|
||||
if (!clientId) return res.status(400).json({ message: "Zoho Bigin client_id missing." });
|
||||
if (!clientSecret) return res.status(400).json({ message: "Zoho Bigin client_secret missing." });
|
||||
|
||||
const accountsUrl = `${accountsServer}/oauth/v2/token`;
|
||||
const redirectUri = `${WEBAPP_URL}/api/integrations/${appConfig.slug}/callback`;
|
||||
|
||||
const formData = {
|
||||
client_id: clientId,
|
||||
client_secret: clientSecret,
|
||||
code: code,
|
||||
redirect_uri: redirectUri,
|
||||
grant_type: "authorization_code",
|
||||
};
|
||||
|
||||
const tokenInfo = await axios.post(accountsUrl, qs.stringify(formData), {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
|
||||
},
|
||||
});
|
||||
|
||||
tokenInfo.data.expiryDate = Math.round(Date.now() + tokenInfo.data.expires_in);
|
||||
tokenInfo.data.accountServer = accountsServer;
|
||||
|
||||
await createOAuthAppCredential({ appId: appConfig.slug, type: appConfig.type }, tokenInfo.data, req);
|
||||
|
||||
res.redirect(
|
||||
getSafeRedirectUrl(state?.returnTo) ??
|
||||
getInstalledAppPath({ variant: appConfig.variant, slug: appConfig.slug })
|
||||
);
|
||||
}
|
2
calcom/packages/app-store/zoho-bigin/api/index.ts
Normal file
2
calcom/packages/app-store/zoho-bigin/api/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export { default as add } from "./add";
|
||||
export { default as callback } from "./callback";
|
Reference in New Issue
Block a user