2021-08-18 11:52:25 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-12-10 21:14:54 +00:00
|
|
|
import { stringify } from "querystring";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
2023-09-20 18:01:54 +00:00
|
|
|
import { encodeOAuthState } from "../../_utils/oauth/encodeOAuthState";
|
2021-04-21 22:10:48 +00:00
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
const scopes = ["User.Read", "Calendars.Read", "Calendars.ReadWrite", "offline_access"];
|
2021-04-21 22:10:48 +00:00
|
|
|
|
2022-05-02 23:13:34 +00:00
|
|
|
let client_id = "";
|
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
|
|
if (req.method === "GET") {
|
2022-05-02 23:13:34 +00:00
|
|
|
const appKeys = await getAppKeysFromSlug("office365-calendar");
|
|
|
|
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
|
|
|
|
if (!client_id) return res.status(400).json({ message: "Office 365 client_id missing." });
|
2021-11-03 10:47:52 +00:00
|
|
|
const state = encodeOAuthState(req);
|
2021-12-10 21:14:54 +00:00
|
|
|
const params = {
|
|
|
|
response_type: "code",
|
|
|
|
scope: scopes.join(" "),
|
2022-05-02 23:13:34 +00:00
|
|
|
client_id,
|
|
|
|
redirect_uri: WEBAPP_URL + "/api/integrations/office365calendar/callback",
|
2021-12-10 21:14:54 +00:00
|
|
|
state,
|
|
|
|
};
|
|
|
|
const query = stringify(params);
|
|
|
|
const url = `https://login.microsoftonline.com/common/oauth2/v2.0/authorize?${query}`;
|
2021-11-03 10:47:52 +00:00
|
|
|
res.status(200).json({ url });
|
2021-08-19 12:27:01 +00:00
|
|
|
}
|
2021-04-21 22:10:48 +00:00
|
|
|
}
|