synthax-fix
This commit is contained in:
parent
41895fc7f7
commit
583d8e5d8e
183
index.mjs
183
index.mjs
|
@ -1,16 +1,16 @@
|
||||||
import { Authorizer } from '@authorizerdev/authorizer-js';
|
import { Authorizer } from "@authorizerdev/authorizer-js";
|
||||||
import { Server } from '@hocuspocus/server';
|
import { Server } from "@hocuspocus/server";
|
||||||
|
import Sentry from "@sentry/node";
|
||||||
|
|
||||||
|
Sentry.init({ dsn: process.env.GLITCHTIP_DSN });
|
||||||
|
|
||||||
const port = process.env.PORT || 4000;
|
const port = process.env.PORT || 4000;
|
||||||
const authorizer = new Authorizer({
|
const authorizer = new Authorizer({
|
||||||
clientID: process.env.AUTHORIZER_CLIENT_ID || '',
|
clientID: process.env.AUTHORIZER_CLIENT_ID || "",
|
||||||
authorizerURL: process.env.AUTHORIZER_URL || 'https://auth.discours.io',
|
authorizerURL: process.env.AUTHORIZER_URL || "https://auth.discours.io",
|
||||||
redirectURL: process.env.REDIRECT_URL || 'https://testing.discours.io',
|
redirectURL: process.env.REDIRECT_URL || "https://testing.discours.io",
|
||||||
});
|
});
|
||||||
|
|
||||||
const Sentry = require("@sentry/node");
|
|
||||||
Sentry.init({ dsn: process.env.GLITCHTIP_DSN });
|
|
||||||
|
|
||||||
const startServer = async () => {
|
const startServer = async () => {
|
||||||
const server = await Server.configure({
|
const server = await Server.configure({
|
||||||
port: process.env.PORT || 4000,
|
port: process.env.PORT || 4000,
|
||||||
|
@ -18,44 +18,45 @@ const startServer = async () => {
|
||||||
connection.requiresAuthentication = true;
|
connection.requiresAuthentication = true;
|
||||||
},
|
},
|
||||||
onAuthenticate(data) {
|
onAuthenticate(data) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const headers = data.requestHeaders;
|
const headers = data.requestHeaders;
|
||||||
if (!headers) {
|
if (!headers) {
|
||||||
console.error('Request headers not found');
|
console.error("Request headers not found");
|
||||||
return reject(new Error('Required header is not present'));
|
return reject(new Error("Required header is not present"));
|
||||||
|
}
|
||||||
|
|
||||||
|
const shout_id = parseInt(data.documentName.replace("shout-", ""), 10);
|
||||||
|
console.debug(`shout_id extracted: ${shout_id}`);
|
||||||
|
|
||||||
|
const token = data.token || headers["authorization"] || "";
|
||||||
|
if (!token) {
|
||||||
|
console.error("Authorization token not found");
|
||||||
|
return reject(new Error("Token is not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
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"));
|
||||||
}
|
}
|
||||||
|
|
||||||
const shout_id = parseInt(data.documentName.replace('shout-', ''), 10);
|
const { sub: user, allowed_roles: roles } = response.data.claims;
|
||||||
console.debug(`shout_id extracted: ${shout_id}`);
|
console.debug(`user: ${user} roles: ${roles}`);
|
||||||
|
|
||||||
const token = data.token || headers['authorization'] || '';
|
if (roles.includes("editor")) {
|
||||||
if (!token) {
|
return resolve({
|
||||||
console.error('Authorization token not found');
|
id: user,
|
||||||
return reject(new Error('Token is not found'));
|
roles: Array.isArray(roles) ? roles : roles.split(","),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
authorizer.validateJWTToken({ token_type: 'access_token', token })
|
fetch("https://core.discours.io/", {
|
||||||
.then(response => {
|
method: "POST",
|
||||||
if (!response?.data?.is_valid) {
|
headers: { "Content-Type": "application/json" },
|
||||||
console.error('Invalid authorization token');
|
body: JSON.stringify({
|
||||||
return reject(new Error('Token is invalid'));
|
query: `
|
||||||
}
|
|
||||||
|
|
||||||
const { sub: user, allowed_roles: roles } = response.data.claims;
|
|
||||||
console.debug(`user: ${user} roles: ${roles}`);
|
|
||||||
|
|
||||||
if (roles.includes('editor')) {
|
|
||||||
return resolve({
|
|
||||||
id: user,
|
|
||||||
roles: Array.isArray(roles) ? roles : roles.split(','),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch('https://core.discours.io/', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({
|
|
||||||
query: `
|
|
||||||
query {
|
query {
|
||||||
get_author_id(user: "${user}") {
|
get_author_id(user: "${user}") {
|
||||||
id
|
id
|
||||||
|
@ -64,67 +65,69 @@ const startServer = async () => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`,
|
`,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
.then(res => res.json())
|
.then((res) => res.json())
|
||||||
.then(({ data }) => {
|
.then(({ data }) => {
|
||||||
// console.debug(data)
|
// console.debug(data)
|
||||||
const { id: author_id } = data.get_author_id
|
const { id: author_id } = data.get_author_id;
|
||||||
if (author_id) {
|
if (author_id) {
|
||||||
fetch('https://core.discours.io/', {
|
fetch("https://core.discours.io/", {
|
||||||
method: 'POST',
|
method: "POST",
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
query: `query {
|
query: `query {
|
||||||
get_shout(shout_id: ${shout_id}) {
|
get_shout(shout_id: ${shout_id}) {
|
||||||
id
|
id
|
||||||
slug
|
slug
|
||||||
authors { id }
|
authors { id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`
|
`,
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
.then(res => res.json())
|
.then((res) => res.json())
|
||||||
.then(({data}) => {
|
.then(({ data }) => {
|
||||||
// console.debug('shout data:', data)
|
// console.debug('shout data:', data)
|
||||||
const { authors } = data.get_shout;
|
const { authors } = data.get_shout;
|
||||||
if (authors.some(author => author.id === author_id)) {
|
if (authors.some((author) => author.id === author_id)) {
|
||||||
return resolve({
|
return resolve({
|
||||||
id: user,
|
id: user,
|
||||||
roles: Array.isArray(roles) ? roles : roles.split(','),
|
roles: Array.isArray(roles)
|
||||||
});
|
? roles
|
||||||
}
|
: roles.split(","),
|
||||||
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 => {
|
return reject(new Error("User is not in authors list"));
|
||||||
console.error('Error validating authorization token:', e.message);
|
})
|
||||||
console.error(e.stack);
|
.catch((e) => {
|
||||||
return reject(new Error('Error validating authorization token'));
|
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"));
|
||||||
|
});
|
||||||
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
|
||||||
process.on('unhandledRejection', (reason, promise) => {
|
process.on("unhandledRejection", (reason, promise) => {
|
||||||
Sentry.captureException(reason);
|
Sentry.captureException(reason);
|
||||||
});
|
});
|
||||||
|
|
||||||
process.on('uncaughtException', (error) => {
|
process.on("uncaughtException", (error) => {
|
||||||
Sentry.captureException(error);
|
Sentry.captureException(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
startServer();
|
startServer();
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
"version": "1.0.4",
|
"version": "1.0.4",
|
||||||
"description": "discours.io webrtc p2p network helping crdt-server",
|
"description": "discours.io webrtc p2p network helping crdt-server",
|
||||||
"main": "index.mjs",
|
"main": "index.mjs",
|
||||||
"type": "module",
|
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node ./index.mjs"
|
"start": "node ./index.mjs"
|
||||||
},
|
},
|
||||||
|
@ -16,6 +15,6 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@authorizerdev/authorizer-js": "^2.0.0",
|
"@authorizerdev/authorizer-js": "^2.0.0",
|
||||||
"@hocuspocus/server": "^2.11.2",
|
"@hocuspocus/server": "^2.11.2",
|
||||||
"@sentry/node": "^6.14.0"
|
"@sentry/node": "^7.109.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user