Skip to content

Commit

Permalink
fix: server location on credentials instead app keys
Browse files Browse the repository at this point in the history
  • Loading branch information
vachmara committed May 7, 2024
1 parent d55aaea commit 939a2cd
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 30 deletions.
15 changes: 0 additions & 15 deletions packages/app-store/_utils/updateAppServerLocation.ts

This file was deleted.

14 changes: 8 additions & 6 deletions packages/app-store/zohocalendar/api/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { Prisma } from "@calcom/prisma/client";
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
import getInstalledAppPath from "../../_utils/getInstalledAppPath";
import { decodeOAuthState } from "../../_utils/oauth/decodeOAuthState";
import updateAppServerLocation from "../../_utils/updateAppServerLocation";
import config from "../config.json";
import type { ZohoAuthCredentials } from "../types/ZohoCalendar";
import { appKeysSchema as zohoKeysSchema } from "../zod";
Expand All @@ -33,16 +32,17 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
return;
}

if (!req.session?.user?.id) {
return res.status(401).json({ message: "You must be logged in to do this" });
if (location && typeof location !== "string") {
res.status(400).json({ message: "`location` must be a string" });
return;
}

if (location && typeof location === "string") {
updateAppServerLocation(config.slug, location);
if (!req.session?.user?.id) {
return res.status(401).json({ message: "You must be logged in to do this" });
}

const appKeys = await getAppKeysFromSlug(config.slug);
const { client_id, client_secret, server_location } = zohoKeysSchema.parse(appKeys);
const { client_id, client_secret } = zohoKeysSchema.parse(appKeys);

const params = {
client_id,
Expand All @@ -51,6 +51,7 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
redirect_uri: `${WEBAPP_URL}/api/integrations/${config.slug}/callback`,
code,
};
const server_location = location === "us" ? "com" : location;

const query = stringify(params);

Expand All @@ -72,6 +73,7 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
access_token: responseBody.access_token,
refresh_token: responseBody.refresh_token,
expires_in: Math.round(+new Date() / 1000 + responseBody.expires_in),
server_location: server_location || "com",
};

function getCalenderUri(domain: string): string {
Expand Down
13 changes: 5 additions & 8 deletions packages/app-store/zohocalendar/lib/CalendarService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export default class ZohoCalendarService implements Calendar {
const refreshAccessToken = async () => {
try {
const appKeys = await getAppKeysFromSlug("zohocalendar");
const { client_id, client_secret, server_location } = zohoKeysSchema.parse(appKeys);

const { client_id, client_secret } = zohoKeysSchema.parse(appKeys);
const server_location = zohoCredentials.server_location;
const params = {
client_id,
grant_type: "refresh_token",
Expand Down Expand Up @@ -65,6 +65,7 @@ export default class ZohoCalendarService implements Calendar {
access_token: token.access_token,
refresh_token: zohoCredentials.refresh_token,
expires_in: Math.round(+new Date() / 1000 + token.expires_in),
server_location,
};
await prisma.credential.update({
where: { id: credential.id },
Expand All @@ -87,9 +88,7 @@ export default class ZohoCalendarService implements Calendar {

private fetcher = async (endpoint: string, init?: RequestInit | undefined) => {
const credentials = await this.auth.getToken();
const appKeys = await getAppKeysFromSlug("zohocalendar");
const { server_location } = zohoKeysSchema.parse(appKeys);
return fetch(`https://calendar.zoho.${server_location}/api/v1${endpoint}`, {
return fetch(`https://calendar.zoho.${credentials.server_location}/api/v1${endpoint}`, {
method: "GET",
...init,
headers: {
Expand All @@ -102,9 +101,7 @@ export default class ZohoCalendarService implements Calendar {

private getUserInfo = async () => {
const credentials = await this.auth.getToken();
const appKeys = await getAppKeysFromSlug("zohocalendar");
const { server_location } = zohoKeysSchema.parse(appKeys);
const response = await fetch(`https://accounts.zoho.${server_location}/oauth/user/info`, {
const response = await fetch(`https://accounts.zoho.${credentials.server_location}/oauth/user/info`, {
method: "GET",
headers: {
Authorization: `Bearer ${credentials.access_token}`,
Expand Down
1 change: 1 addition & 0 deletions packages/app-store/zohocalendar/types/ZohoCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type ZohoAuthCredentials = {
access_token: string;
refresh_token: string;
expires_in: number;
server_location: string;
};

export type FreeBusy = {
Expand Down
1 change: 0 additions & 1 deletion packages/app-store/zohocalendar/zod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ export const appDataSchema = z.object({});
export const appKeysSchema = z.object({
client_id: z.string().min(1),
client_secret: z.string().min(1),
server_location: z.string().optional(),
});

0 comments on commit 939a2cd

Please sign in to comment.