crdt-server/index.mjs

137 lines
5.0 KiB
JavaScript
Raw Normal View History

2024-04-09 17:09:35 +00:00
import { Authorizer } from "@authorizerdev/authorizer-js";
import { Server } from "@hocuspocus/server";
import Sentry from "@sentry/node";
2024-04-09 17:11:20 +00:00
const s = Sentry.init({ dsn: process.env.GLITCHTIP_DSN });
console.debug(s);
2024-02-17 11:40:34 +00:00
2024-03-04 16:37:18 +00:00
const port = process.env.PORT || 4000;
2024-02-17 11:40:34 +00:00
const authorizer = new Authorizer({
2024-04-09 17:09:35 +00:00
clientID: process.env.AUTHORIZER_CLIENT_ID || "",
authorizerURL: process.env.AUTHORIZER_URL || "https://auth.discours.io",
redirectURL: process.env.REDIRECT_URL || "https://testing.discours.io",
2024-02-17 11:55:31 +00:00
});
2024-04-09 17:11:20 +00:00
console.debug(authorizer);
2024-02-17 11:55:31 +00:00
const startServer = async () => {
2024-04-09 17:11:20 +00:00
console.info("Starting server...");
2024-02-17 11:40:34 +00:00
const server = await Server.configure({
2024-02-17 12:20:43 +00:00
port: process.env.PORT || 4000,
2024-02-17 11:40:34 +00:00
async onConnect({ connection }) {
2024-02-17 11:55:31 +00:00
connection.requiresAuthentication = true;
2024-03-04 15:43:44 +00:00
},
onAuthenticate(data) {
2024-04-09 17:09:35 +00:00
return new Promise((resolve, reject) => {
const headers = data.requestHeaders;
if (!headers) {
console.error("Request headers not found");
return reject(new Error("Required header is not present"));
}
2024-03-04 15:43:44 +00:00
2024-04-09 17:09:35 +00:00
const shout_id = parseInt(data.documentName.replace("shout-", ""), 10);
console.debug(`shout_id extracted: ${shout_id}`);
2024-03-04 15:43:44 +00:00
2024-04-09 17:09:35 +00:00
const token = data.token || headers["authorization"] || "";
if (!token) {
console.error("Authorization token not found");
return reject(new Error("Token is not found"));
}
2024-03-04 15:43:44 +00:00
2024-04-09 17:09:35 +00:00
authorizer
.validateJWTToken({ token_type: "access_token", token })
.then((response) => {
if (!response?.data?.is_valid) {
console.error("Invalid authorization token");
return reject(new Error("Token is invalid"));
}
2024-03-04 15:43:44 +00:00
2024-04-09 17:09:35 +00:00
const { sub: user, allowed_roles: roles } = response.data.claims;
console.debug(`user: ${user} roles: ${roles}`);
2024-03-04 15:43:44 +00:00
2024-04-09 17:09:35 +00:00
if (roles.includes("editor")) {
return resolve({
id: user,
roles: Array.isArray(roles) ? roles : roles.split(","),
});
}
2024-03-04 15:43:44 +00:00
2024-04-09 17:09:35 +00:00
fetch("https://core.discours.io/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: `
2024-03-04 16:37:18 +00:00
query {
2024-03-04 16:57:07 +00:00
get_author_id(user: "${user}") {
2024-03-04 16:37:18 +00:00
id
slug
user
}
}
`,
2024-04-09 17:09:35 +00:00
}),
})
.then((res) => res.json())
.then(({ data }) => {
// console.debug(data)
const { id: author_id } = data.get_author_id;
if (author_id) {
fetch("https://core.discours.io/", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: `query {
2024-03-04 17:05:20 +00:00
get_shout(shout_id: ${shout_id}) {
2024-03-04 16:37:18 +00:00
id
slug
2024-03-04 17:12:53 +00:00
authors { id }
2024-03-04 16:37:18 +00:00
}
}
2024-04-09 17:09:35 +00:00
`,
}),
})
.then((res) => res.json())
.then(({ data }) => {
// console.debug('shout data:', data)
const { authors } = data.get_shout;
if (authors.some((author) => author.id === author_id)) {
return resolve({
id: user,
roles: Array.isArray(roles)
? roles
: roles.split(","),
2024-03-04 16:37:18 +00:00
});
2024-04-09 17:09:35 +00:00
}
return reject(new Error("User is not in authors list"));
})
.catch((e) => {
console.error("Error fetching shout data:", e.message);
console.error(e.stack);
return reject(new Error("Error fetching shout data"));
});
}
})
.catch((e) => {
console.error("Error fetching author data:", e.message);
console.error(e.stack);
return reject(new Error("Error fetching author data"));
});
})
.catch((e) => {
console.error("Error validating authorization token:", e.message);
console.error(e.stack);
return reject(new Error("Error validating authorization token"));
});
});
2024-03-04 16:37:18 +00:00
},
2024-04-09 17:09:35 +00:00
});
};
2024-02-17 11:40:34 +00:00
2024-04-09 17:09:35 +00:00
process.on("unhandledRejection", (reason, promise) => {
Sentry.captureException(reason);
});
2024-04-09 17:09:35 +00:00
process.on("uncaughtException", (error) => {
Sentry.captureException(error);
});
2024-02-17 11:55:31 +00:00
startServer();