Merge pull request #459 from femyeda/caldav-connection-issues

This commit is contained in:
Peer_Rich 2021-08-16 16:39:00 +02:00 committed by GitHub
commit ec9fefefec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 39 deletions

View File

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

View File

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