2023-02-16 22:39:57 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
2022-07-12 17:43:53 +00:00
|
|
|
import z from "zod";
|
2022-01-13 20:05:23 +00:00
|
|
|
|
2022-10-18 20:34:32 +00:00
|
|
|
import jackson from "@calcom/features/ee/sso/lib/jackson";
|
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
2022-01-13 20:05:23 +00:00
|
|
|
|
|
|
|
const extractAuthToken = (req: NextApiRequest) => {
|
|
|
|
const authHeader = req.headers["authorization"];
|
|
|
|
const parts = (authHeader || "").split(" ");
|
2022-10-18 20:34:32 +00:00
|
|
|
if (parts.length > 1) return parts[1];
|
2022-01-13 20:05:23 +00:00
|
|
|
|
2022-10-18 20:34:32 +00:00
|
|
|
// check for query param
|
|
|
|
let arr: string[] = [];
|
|
|
|
const { access_token } = requestQuery.parse(req.query);
|
|
|
|
arr = arr.concat(access_token);
|
|
|
|
if (arr[0].length > 0) return arr[0];
|
|
|
|
|
|
|
|
throw new HttpError({ statusCode: 401, message: "Unauthorized" });
|
2022-01-13 20:05:23 +00:00
|
|
|
};
|
|
|
|
|
2022-07-12 17:43:53 +00:00
|
|
|
const requestQuery = z.object({
|
|
|
|
access_token: z.string(),
|
|
|
|
});
|
|
|
|
|
2022-10-18 20:34:32 +00:00
|
|
|
async function getHandler(req: NextApiRequest) {
|
|
|
|
const { oauthController } = await jackson();
|
|
|
|
const token = extractAuthToken(req);
|
|
|
|
return await oauthController.userInfo(token);
|
2022-01-13 20:05:23 +00:00
|
|
|
}
|
2022-10-18 20:34:32 +00:00
|
|
|
|
|
|
|
export default defaultHandler({
|
|
|
|
GET: Promise.resolve({ default: defaultResponder(getHandler) }),
|
|
|
|
});
|