2024-03-04 15:44:49 +00:00
|
|
|
import { Authorizer } from '@authorizerdev/authorizer-js';
|
2024-03-04 16:37:18 +00:00
|
|
|
import { Server } from '@hocuspocus/server';
|
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-03-04 15:43:44 +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-02-17 12:25:44 +00:00
|
|
|
const server = Server.configure({
|
2024-03-04 15:43:44 +00:00
|
|
|
port,
|
|
|
|
onConnect({ connection }) {
|
|
|
|
connection.requiresAuthentication = true;
|
2024-04-07 18:14:38 +00:00
|
|
|
const Sentry = require("@sentry/node");
|
2024-04-08 07:51:11 +00:00
|
|
|
Sentry.init({ dsn: process.env.SENTRY_DSN });
|
2024-04-07 18:14:38 +00:00
|
|
|
|
2024-02-17 11:55:31 +00:00
|
|
|
const startServer = async () => {
|
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-03-04 16:25:01 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2024-03-04 16:37:18 +00:00
|
|
|
const headers = data.requestHeaders;
|
2024-03-04 16:25:01 +00:00
|
|
|
if (!headers) {
|
|
|
|
console.error('Request headers not found');
|
2024-03-04 16:37:18 +00:00
|
|
|
return reject(new Error('Required header is not present'));
|
2024-03-04 16:25:01 +00:00
|
|
|
}
|
2024-03-04 15:43:44 +00:00
|
|
|
|
2024-03-04 16:37:18 +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-03-04 16:37:18 +00:00
|
|
|
const token = data.token || headers['authorization'] || '';
|
2024-03-04 16:25:01 +00:00
|
|
|
if (!token) {
|
|
|
|
console.error('Authorization token not found');
|
2024-03-04 16:37:18 +00:00
|
|
|
return reject(new Error('Token is not found'));
|
2024-03-04 16:25:01 +00:00
|
|
|
}
|
2024-03-04 15:43:44 +00:00
|
|
|
|
2024-03-04 16:37:18 +00:00
|
|
|
authorizer.validateJWTToken({ token_type: 'access_token', token })
|
2024-03-04 16:25:01 +00:00
|
|
|
.then(response => {
|
|
|
|
if (!response?.data?.is_valid) {
|
|
|
|
console.error('Invalid authorization token');
|
2024-03-04 16:37:18 +00:00
|
|
|
return reject(new Error('Token is invalid'));
|
2024-03-04 16:25:01 +00:00
|
|
|
}
|
2024-03-04 15:43:44 +00:00
|
|
|
|
2024-03-04 16:54:00 +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-03-04 16:25:01 +00:00
|
|
|
if (roles.includes('editor')) {
|
2024-03-04 16:37:18 +00:00
|
|
|
return resolve({
|
2024-03-04 16:54:00 +00:00
|
|
|
id: user,
|
2024-03-04 16:37:18 +00:00
|
|
|
roles: Array.isArray(roles) ? roles : roles.split(','),
|
|
|
|
});
|
2024-03-04 15:43:44 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 16:45:17 +00:00
|
|
|
fetch('https://core.discours.io/', {
|
2024-03-04 16:37:18 +00:00
|
|
|
method: 'POST',
|
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
|
body: JSON.stringify({
|
|
|
|
query: `
|
|
|
|
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-03-04 16:25:01 +00:00
|
|
|
})
|
2024-03-04 16:37:18 +00:00
|
|
|
.then(res => res.json())
|
2024-03-04 17:01:11 +00:00
|
|
|
.then(({ data }) => {
|
2024-03-04 17:26:52 +00:00
|
|
|
// console.debug(data)
|
2024-03-04 16:58:44 +00:00
|
|
|
const { id: author_id } = data.get_author_id
|
2024-03-04 16:37:18 +00:00
|
|
|
if (author_id) {
|
2024-03-04 16:45:17 +00:00
|
|
|
fetch('https://core.discours.io/', {
|
2024-03-04 16:37:18 +00:00
|
|
|
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-03-04 17:05:20 +00:00
|
|
|
`
|
2024-03-04 16:37:18 +00:00
|
|
|
}),
|
|
|
|
})
|
|
|
|
.then(res => res.json())
|
2024-03-04 17:20:00 +00:00
|
|
|
.then(({data}) => {
|
2024-03-04 17:26:52 +00:00
|
|
|
// console.debug('shout data:', data)
|
2024-03-04 17:18:26 +00:00
|
|
|
const { authors } = data.get_shout;
|
|
|
|
if (authors.some(author => author.id === author_id)) {
|
2024-03-04 16:37:18 +00:00
|
|
|
return resolve({
|
2024-03-04 17:13:30 +00:00
|
|
|
id: user,
|
2024-03-04 16:37:18 +00:00
|
|
|
author: author_id,
|
|
|
|
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'));
|
|
|
|
});
|
2024-03-04 15:43:44 +00:00
|
|
|
})
|
2024-03-04 16:25:01 +00:00
|
|
|
.catch(e => {
|
|
|
|
console.error('Error validating authorization token:', e.message);
|
|
|
|
console.error(e.stack);
|
2024-03-04 16:37:18 +00:00
|
|
|
return reject(new Error('Error validating authorization token'));
|
2024-03-04 16:25:01 +00:00
|
|
|
});
|
2024-03-04 16:37:18 +00:00
|
|
|
});
|
|
|
|
},
|
2024-02-17 12:25:44 +00:00
|
|
|
});
|
2024-02-17 11:40:34 +00:00
|
|
|
|
2024-04-07 18:14:38 +00:00
|
|
|
process.on('unhandledRejection', (reason, promise) => {
|
|
|
|
Sentry.captureException(reason);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('uncaughtException', (error) => {
|
|
|
|
Sentry.captureException(error);
|
|
|
|
});
|
|
|
|
|
2024-02-17 11:55:31 +00:00
|
|
|
startServer();
|