|
| 1 | +import type { NextApiRequest, NextApiResponse } from "next"; |
| 2 | +import { stringify } from "querystring"; |
| 3 | + |
| 4 | +import { WEBAPP_URL } from "@calcom/lib/constants"; |
| 5 | +import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl"; |
| 6 | +import logger from "@calcom/lib/logger"; |
| 7 | +import { defaultHandler, defaultResponder } from "@calcom/lib/server"; |
| 8 | + |
| 9 | +import createOAuthAppCredential from "../../_utils/createOAuthAppCredential"; |
| 10 | +import { decodeOAuthState } from "../../_utils/decodeOAuthState"; |
| 11 | +import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug"; |
| 12 | +import getInstalledAppPath from "../../_utils/getInstalledAppPath"; |
| 13 | +import config from "../config.json"; |
| 14 | +import type { ZohoAuthCredentials } from "../types/ZohoCalendar"; |
| 15 | +import { appKeysSchema as zohoKeysSchema } from "../zod"; |
| 16 | + |
| 17 | +const log = logger.getChildLogger({ prefix: [`[[zohocalendar/api/callback]`] }); |
| 18 | + |
| 19 | +const OAUTH_BASE_URL = "https://accounts.zoho.com/oauth/v2"; |
| 20 | + |
| 21 | +async function getHandler(req: NextApiRequest, res: NextApiResponse) { |
| 22 | + const { code } = req.query; |
| 23 | + const state = decodeOAuthState(req); |
| 24 | + |
| 25 | + if (code && typeof code !== "string") { |
| 26 | + res.status(400).json({ message: "`code` must be a string" }); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + if (!req.session?.user?.id) { |
| 31 | + return res.status(401).json({ message: "You must be logged in to do this" }); |
| 32 | + } |
| 33 | + |
| 34 | + const appKeys = await getAppKeysFromSlug(config.slug); |
| 35 | + const { client_id, client_secret } = zohoKeysSchema.parse(appKeys); |
| 36 | + |
| 37 | + const params = { |
| 38 | + client_id, |
| 39 | + grant_type: "authorization_code", |
| 40 | + client_secret, |
| 41 | + redirect_uri: `${WEBAPP_URL}/api/integrations/${config.slug}/callback`, |
| 42 | + code, |
| 43 | + }; |
| 44 | + |
| 45 | + const query = stringify(params); |
| 46 | + |
| 47 | + const response = await fetch(`${OAUTH_BASE_URL}/token?${query}`, { |
| 48 | + method: "POST", |
| 49 | + headers: { |
| 50 | + "Content-Type": "application/json; charset=utf-8", |
| 51 | + }, |
| 52 | + }); |
| 53 | + |
| 54 | + const responseBody = await response.json(); |
| 55 | + |
| 56 | + if (!response.ok || responseBody.error) { |
| 57 | + log.error("get access_token failed", responseBody); |
| 58 | + return res.redirect("/apps/installed?error=" + JSON.stringify(responseBody)); |
| 59 | + } |
| 60 | + |
| 61 | + const key: ZohoAuthCredentials = { |
| 62 | + access_token: responseBody.access_token, |
| 63 | + refresh_token: responseBody.refresh_token, |
| 64 | + expires_in: Math.round(+new Date() / 1000 + responseBody.expires_in), |
| 65 | + }; |
| 66 | + |
| 67 | + await createOAuthAppCredential({ appId: config.slug, type: config.type }, key, req); |
| 68 | + |
| 69 | + res.redirect( |
| 70 | + getSafeRedirectUrl(state?.returnTo) ?? getInstalledAppPath({ variant: config.variant, slug: config.slug }) |
| 71 | + ); |
| 72 | +} |
| 73 | + |
| 74 | +export default defaultHandler({ |
| 75 | + GET: Promise.resolve({ default: defaultResponder(getHandler) }), |
| 76 | +}); |
0 commit comments