Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
app.post("/v1/conduit/register", async (req, res) => {
// Check that they provided a public key.
if (!z.object({
pubkey: z.string()
}).check(req.body)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
// Generate a new unique code.
const code = await db.generateCode(req.body.pubkey);
console.log("[+] New Conduit registered. Code: " + code);
// Sign a JWT for the machine and return it.
res.json({
ok: true,
token: tokens.createConnectionToken(code)
});
app.post("/v1/notifications/register", async (req, res) => {
if (!z.object({
uuid: z.string(),
platform: z.string(),
token: z.string().nullable()
}).check(req.body) || !NOTIFICATION_PLATFORMS.includes(req.body.platform)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
console.log(`[+] Updated notification token for ${req.body.uuid} to '${req.body.token}'`);
await db.registerOrUpdateDeviceNotifications(req.body.uuid, req.body.platform as NotificationPlatform, req.body.token);
return res.json({
ok: true
});
});
app.post("/v1/notifications/respond", async (req, res) => {
if (!z.object({
token: z.string(),
response: z.string()
}).check(req.query)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
const token = await tokens.verifyInstantResponseToken(req.query.token);
if (!token) {
return res.status(400).json({
ok: false,
error: "Invalid token."
});
}
console.log("[+] Got response " + req.query.response + " for computer " + token.code);
app.post("/v1/notifications/register", async (req, res) => {
if (!z.object({
uuid: z.string(),
platform: z.string(),
token: z.string().nullable()
}).check(req.body) || !NOTIFICATION_PLATFORMS.includes(req.body.platform)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
console.log(`[+] Updated notification token for ${req.body.uuid} to '${req.body.token}'`);
await db.registerOrUpdateDeviceNotifications(req.body.uuid, req.body.platform as NotificationPlatform, req.body.token);
return res.json({
ok: true
});
});
app.post("/v1/notifications/subscribe", async (req, res) => {
if (!z.object({
uuid: z.string(),
token: z.string(),
type: z.string()
}).check(req.body) || !NOTIFICATION_TYPES.includes(req.body.type)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
const code = await tokens.verifyPushNotificationToken(req.body.token);
if (!code) return res.status(403).json({
ok: false,
error: "Invalid token."
});
console.log(`[+] Device ${req.body.uuid} subscribed to ${req.body.type} notifications.`);
await db.subscribeDeviceNotifications(req.body.uuid, code, req.body.type as NotificationType);
app.post("/v1/notifications/register", async (req, res) => {
if (!z.object({
uuid: z.string(),
platform: z.string(),
token: z.string().nullable()
}).check(req.body) || !NOTIFICATION_PLATFORMS.includes(req.body.platform)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
console.log(`[+] Updated notification token for ${req.body.uuid} to '${req.body.token}'`);
await db.registerOrUpdateDeviceNotifications(req.body.uuid, req.body.platform as NotificationPlatform, req.body.token);
return res.json({
ok: true
});
});
app.post("/v1/notifications/respond", async (req, res) => {
if (!z.object({
token: z.string(),
response: z.string()
}).check(req.query)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
const token = await tokens.verifyInstantResponseToken(req.query.token);
if (!token) {
return res.status(400).json({
ok: false,
error: "Invalid token."
});
}
app.post("/v1/notifications/subscribe", async (req, res) => {
if (!z.object({
uuid: z.string(),
token: z.string(),
type: z.string()
}).check(req.body) || !NOTIFICATION_TYPES.includes(req.body.type)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
const code = await tokens.verifyPushNotificationToken(req.body.token);
if (!code) return res.status(403).json({
ok: false,
error: "Invalid token."
});