fix issue where user could not connect to caldav

Uses base caldav adapter to ensure connectivity

rethrows all errors on CalDavCalendarAdapter
This commit is contained in:
femyeda 2021-08-16 09:34:49 -05:00
parent 4f35dfa5f0
commit 49e7cbf803
2 changed files with 26 additions and 39 deletions

View file

@ -103,6 +103,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
}; };
} catch (reason) { } catch (reason) {
console.error(reason); console.error(reason);
throw reason;
} }
} }
@ -153,6 +154,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
return null; return null;
} catch (reason) { } catch (reason) {
console.error(reason); console.error(reason);
throw reason;
} }
} }
@ -186,6 +188,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
return null; return null;
} catch (reason) { } catch (reason) {
console.error(reason); console.error(reason);
throw reason;
} }
} }
@ -212,6 +215,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
return events; return events;
} catch (reason) { } catch (reason) {
console.error(reason); console.error(reason);
throw reason;
} }
} }
@ -235,6 +239,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
})); }));
} catch (reason) { } catch (reason) {
console.error(reason); console.error(reason);
throw reason;
} }
} }
@ -295,6 +300,7 @@ export class CalDavCalendar implements CalendarApiAdapter {
return events; return events;
} catch (reason) { } catch (reason) {
console.error(reason); console.error(reason);
throw reason;
} }
} }

View file

@ -3,7 +3,7 @@ import { getSession } from "next-auth/client";
import prisma from "../../../../lib/prisma"; import prisma from "../../../../lib/prisma";
import { symmetricEncrypt } from "@lib/crypto"; import { symmetricEncrypt } from "@lib/crypto";
import logger from "@lib/logger"; import logger from "@lib/logger";
import { davRequest, getBasicAuthHeaders } from "tsdav"; import { CalDavCalendar } from "@lib/integrations/CalDav/CalDavCalendarAdapter";
export default async function handler(req: NextApiRequest, res: NextApiResponse) { export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === "POST") { if (req.method === "POST") {
@ -26,36 +26,18 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
}, },
}); });
const header = getBasicAuthHeaders({
username,
password,
});
try { try {
const [response] = await davRequest({ const dav = new CalDavCalendar({
url: url, id: 0,
init: { type: "caldav_calendar",
method: "PROPFIND", key: symmetricEncrypt(
namespace: "d", JSON.stringify({ username, password, url }),
body: { process.env.CALENDSO_ENCRYPTION_KEY
propfind: { ),
_attributes: { userId: session.user.id,
"xmlns:d": "DAV:",
},
prop: { "d:current-user-principal": {} },
},
},
headers: header,
},
}); });
if (!response.ok) { await dav.listCalendars();
logger.error("Could not add this caldav account", response?.statusText);
logger.error(response.error);
return res.status(200).json({ message: "Could not add this caldav account" });
}
if (response.ok) {
await prisma.credential.create({ await prisma.credential.create({
data: { data: {
type: "caldav_calendar", type: "caldav_calendar",
@ -66,7 +48,6 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
userId: session.user.id, userId: session.user.id,
}, },
}); });
}
} catch (reason) { } catch (reason) {
logger.error("Could not add this caldav account", reason); logger.error("Could not add this caldav account", reason);
return res.status(500).json({ message: "Could not add this caldav account" }); return res.status(500).json({ message: "Could not add this caldav account" });