mjs
Some checks failed
CI/CD / test (push) Failing after 11s
CI/CD / deploy (push) Has been skipped

This commit is contained in:
2024-02-17 14:55:31 +03:00
parent e3782d4dd4
commit 570189719f
2 changed files with 21 additions and 19 deletions

42
index.mjs Normal file
View File

@@ -0,0 +1,42 @@
import { Authorizer } from '@authorizerdev/authorizer-js';
import { Server } from '@hocuspocus/server';
const authorizer = new Authorizer({
clientID: process.env.AUTHORIZER_CLIENT_ID,
authorizerURL: 'https://auth.discours.io',
redirectURL: 'https://testing.discours.io',
});
const startServer = async () => {
const server = await Server.configure({
port: 4242,
async onConnect({ connection }) {
connection.requiresAuthentication = true;
},
async onAuthenticate(data) {
if (data.requestHeaders) {
const params = {
token_type: 'access_token',
token: data.requestHeaders['authorization'] || '',
};
if (params.token) {
const response = await authorizer.validateJWTToken(params);
if (response?.data?.is_valid) {
const { sub: user, allowed_roles: roles } = response.data.claims;
console.debug(`user_id: ${user} roles: ${roles}`);
return {
id: user,
roles,
};
}
console.error('no valid auth token presented');
throw new Error('Not authorized!');
}
}
},
}).listen();
server.listen();
};
startServer();