crdt-server/index.mjs

108 lines
4.4 KiB
JavaScript
Raw Normal View History

2024-03-04 15:44:49 +00:00
import { Authorizer } from '@authorizerdev/authorizer-js';
2024-03-04 15:43:44 +00:00
import { Server } from '@hocuspocus/server'
2024-02-17 11:40:34 +00:00
2024-03-04 15:43:44 +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-03-04 16:01:45 +00:00
// console.info(authorizer)
2024-03-04 15:43:44 +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;
},
onAuthenticate(data) {
2024-03-04 16:25:01 +00:00
return new Promise((resolve, reject) => {
const headers = data.requestHeaders
if (!headers) {
console.error('Request headers not found');
return Promise.reject('required header is not present')
}
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:25:01 +00:00
const token = data.token || headers['authorization'] || ''
2024-03-04 16:12:23 +00:00
2024-03-04 16:25:01 +00:00
const params = {
token_type: 'access_token',
token
};
2024-03-04 15:43:44 +00:00
2024-03-04 16:25:01 +00:00
if (!token) {
console.error('Authorization token not found');
return Promise.reject('token is not found')
}
2024-03-04 15:43:44 +00:00
2024-03-04 16:25:01 +00:00
authorizer.validateJWTToken(params)
.then(response => {
if (!response?.data?.is_valid) {
console.error('Invalid authorization token');
return Promise.reject('token is invalid')
}
2024-03-04 15:43:44 +00:00
2024-03-04 16:25:01 +00:00
const { sub: user_id, allowed_roles: roles } = response.data.claims
console.debug(`user_id: ${user_id} roles: ${roles}`)
2024-03-04 15:43:44 +00:00
2024-03-04 16:25:01 +00:00
if (roles.includes('editor')) {
return Promise.resolve({
id: user_id,
roles: Array.isArray(roles) ? roles : roles.split(',')
})
2024-03-04 15:43:44 +00:00
}
2024-03-04 16:25:01 +00:00
authorizer.getProfile(params).then((r) => {
console.debug(r)
const { profile: author } = r.data.app_data
const author_id = author.get('id')
if(author_id) {
const query = `
query {
get_shout(shout_id: $shout_id) {
id
slug
authors
}
2024-03-04 15:43:44 +00:00
}
2024-03-04 16:25:01 +00:00
`;
fetch('https://core.discours.io/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables: { shout_id } }),
2024-03-04 15:43:44 +00:00
})
2024-03-04 16:25:01 +00:00
.then(res => res.json())
.then(data => {
console.debug(data)
const { authors } = data.get_shout;
if (authors.includes(author_id)) {
return {
id: user_id,
author: author_id,
roles: Array.isArray(roles) ? roles : roles.split(','),
};
}
return Promise.reject('not in authors list')
})
.catch(e => {
console.error('Error fetching shout data:', e.message);
console.error(e.stack);
return Promise.reject('error fetching shout 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);
return Promise.reject('token is invalid')
});
})
}
2024-02-17 12:25:44 +00:00
});
2024-02-17 11:40:34 +00:00
2024-03-04 16:01:45 +00:00
server.listen().then(r => console.info('started'));