2021-08-18 11:52:25 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
2022-04-27 14:28:36 +00:00
|
|
|
import { getSafeRedirectUrl } from "@calcom/lib/getSafeRedirectUrl";
|
2022-03-23 22:00:30 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
import { decodeOAuthState } from "../../_utils/decodeOAuthState";
|
2022-05-02 20:39:35 +00:00
|
|
|
import getAppKeysFromSlug from "../../_utils/getAppKeysFromSlug";
|
2021-09-17 21:31:44 +00:00
|
|
|
|
2022-03-23 22:00:30 +00:00
|
|
|
const scopes = ["OnlineMeetings.ReadWrite"];
|
2021-04-21 22:10:48 +00:00
|
|
|
|
2022-05-02 20:39:35 +00:00
|
|
|
let client_id = "";
|
|
|
|
let client_secret = "";
|
|
|
|
|
2021-04-21 22:10:48 +00:00
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-08-19 12:27:01 +00:00
|
|
|
const { code } = req.query;
|
2022-03-23 22:00:30 +00:00
|
|
|
console.log("🚀 ~ file: callback.ts ~ line 14 ~ handler ~ code", req.query);
|
2021-08-19 12:27:01 +00:00
|
|
|
|
2021-11-03 10:47:52 +00:00
|
|
|
if (typeof code !== "string") {
|
|
|
|
res.status(400).json({ message: "No code returned" });
|
|
|
|
return;
|
|
|
|
}
|
2021-08-19 12:27:01 +00:00
|
|
|
|
2022-05-13 12:20:50 +00:00
|
|
|
const appKeys = await getAppKeysFromSlug("msteams");
|
2022-05-02 20:39:35 +00:00
|
|
|
if (typeof appKeys.client_id === "string") client_id = appKeys.client_id;
|
|
|
|
if (typeof appKeys.client_secret === "string") client_secret = appKeys.client_secret;
|
|
|
|
if (!client_id) return res.status(400).json({ message: "Office 365 client_id missing." });
|
|
|
|
if (!client_secret) return res.status(400).json({ message: "Office 365 client_secret missing." });
|
|
|
|
|
2021-11-03 10:47:52 +00:00
|
|
|
const toUrlEncoded = (payload: Record<string, string>) =>
|
2021-08-19 12:27:01 +00:00
|
|
|
Object.keys(payload)
|
|
|
|
.map((key) => key + "=" + encodeURIComponent(payload[key]))
|
|
|
|
.join("&");
|
|
|
|
|
|
|
|
const body = toUrlEncoded({
|
2022-05-02 20:39:35 +00:00
|
|
|
client_id,
|
2021-08-19 12:27:01 +00:00
|
|
|
grant_type: "authorization_code",
|
|
|
|
code,
|
|
|
|
scope: scopes.join(" "),
|
2022-05-02 20:39:35 +00:00
|
|
|
redirect_uri: WEBAPP_URL + "/api/integrations/office365video/callback",
|
|
|
|
client_secret,
|
2021-08-19 12:27:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const response = await fetch("https://login.microsoftonline.com/common/oauth2/v2.0/token", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
|
|
|
|
},
|
|
|
|
body,
|
|
|
|
});
|
|
|
|
|
|
|
|
const responseBody = await response.json();
|
|
|
|
|
|
|
|
if (!response.ok) {
|
2022-03-23 22:00:30 +00:00
|
|
|
return res.redirect("/apps/installed?error=" + JSON.stringify(responseBody));
|
2021-08-19 12:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const whoami = await fetch("https://graph.microsoft.com/v1.0/me", {
|
|
|
|
headers: { Authorization: "Bearer " + responseBody.access_token },
|
|
|
|
});
|
|
|
|
const graphUser = await whoami.json();
|
|
|
|
|
|
|
|
// In some cases, graphUser.mail is null. Then graphUser.userPrincipalName most likely contains the email address.
|
|
|
|
responseBody.email = graphUser.mail ?? graphUser.userPrincipalName;
|
|
|
|
responseBody.expiry_date = Math.round(+new Date() / 1000 + responseBody.expires_in); // set expiry date in seconds
|
|
|
|
delete responseBody.expires_in;
|
|
|
|
|
|
|
|
await prisma.credential.create({
|
|
|
|
data: {
|
2022-03-23 22:00:30 +00:00
|
|
|
type: "office365_video",
|
2021-08-19 12:27:01 +00:00
|
|
|
key: responseBody,
|
2022-03-23 22:00:30 +00:00
|
|
|
userId: req.session?.user.id,
|
2022-05-02 20:39:35 +00:00
|
|
|
appId: "msteams",
|
2021-08-19 12:27:01 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-11-03 10:47:52 +00:00
|
|
|
const state = decodeOAuthState(req);
|
2022-04-27 14:28:36 +00:00
|
|
|
return res.redirect(getSafeRedirectUrl(state?.returnTo) ?? "/apps/installed");
|
2021-04-21 22:10:48 +00:00
|
|
|
}
|