Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iCal link #146

Merged
merged 19 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/migrations/41-ical-link.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE ctfnote.settings
ADD COLUMN "ical_password" TEXT DEFAULT encode(gen_random_bytes(16), 'hex');

GRANT SELECT ("ical_password") ON ctfnote.settings TO user_guest;

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"graphile-utils": "^4.11.2",
"graphql": "^15.6.1",
"graphql-upload": "^12.0.0",
"ical-generator": "^3.2.1",
"postgraphile": "^4.12.8",
"postgraphile-plugin-connection-filter": "^2.2.2",
"postgres-migrations": "^5.3.0"
Expand Down
9 changes: 8 additions & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import createTasKPlugin from "./plugins/createTask";
import importCtfPlugin from "./plugins/importCtf";
import uploadLogoPlugin from "./plugins/uploadLogo";
import uploadScalar from "./plugins/uploadScalar";
import { Pool } from "pg";
import { icalRoute } from "./routes/ical";
import ConnectionFilterPlugin from "postgraphile-plugin-connection-filter";

function getDbUrl(role: "user" | "admin") {
Expand Down Expand Up @@ -75,6 +77,10 @@ function createOptions() {
}

function createApp(postgraphileOptions: PostGraphileOptions) {
const pool = new Pool({
connectionString: getDbUrl("user"),
});

const app = express();
app.use(graphqlUploadExpress());
app.use(
Expand All @@ -85,7 +91,8 @@ function createApp(postgraphileOptions: PostGraphileOptions) {
},
})
);
app.use(postgraphile(getDbUrl("user"), "ctfnote", postgraphileOptions));
app.use(postgraphile(pool, "ctfnote", postgraphileOptions));
app.use("/calendar.ics", icalRoute(pool));
return app;
}

Expand Down
72 changes: 72 additions & 0 deletions api/src/routes/ical.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { ICalCalendar } from "ical-generator";
import { Request, Response, Handler } from "express";
import { Pool } from "pg";

type CtfRow = {
id: number;
title: string;
start_time: string;
end_time: string;
ctf_url: string;
description: string;
};

type IcalPasswordRow = {
ical_password: string;
};

export function icalRoute(pool: Pool): Handler {
async function checkIcalPassword(
userPass: string | undefined
): Promise<boolean> {
const r = await pool.query<IcalPasswordRow>(
"SELECT ical_password FROM ctfnote.settings"
);
const db_password = r.rows[0].ical_password;
// If the password is null or empty allow any user
if (!db_password) return true;
return db_password === userPass;
}

async function getCtfs(): Promise<CtfRow[]> {
const r = await pool.query<CtfRow>(
"SELECT id, title, start_time, end_time, ctf_url, description FROM ctfnote.ctf"
);

return r.rows;
}

return async function (req: Request, res: Response): Promise<void> {
const { key } = req.query;

if (
!(typeof key == "string" || key == undefined) ||
!(await checkIcalPassword(key))
) {
res.status(403);
res.send("Forbidden\n");
return;
}

const cal = new ICalCalendar();
const ctfs = await getCtfs();

for (const ctf of ctfs) {

const proto = req.headers["x-forwarded-proto"] || req.protocol;
const host = req.headers["x-forwarded-host"] || req.headers.host;
const ctf_url = `${proto}://${host}/#/ctf/${ctf.id}/info`;

console.log(ctf_url);
cal.createEvent({
start: ctf.start_time,
end: ctf.end_time,
description: ctf.description,
summary: ctf.title,
url: ctf_url,
});
}

cal.serve(res);
};
}
Loading