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

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

View File

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

View File

@ -1,12 +1,11 @@
{ {
"name": "discours-editing-server", "name": "discours-editing-server",
"version": "1.0.2", "version": "1.0.3",
"description": "discours.io webrtc p2p network helping crdt-server", "description": "discours.io webrtc p2p network helping crdt-server",
"main": "index.cjs", "main": "index.mjs",
"type": "module", "type": "module",
"scripts": { "scripts": {
"fix": "npx @biomejs/biome check --apply-unsafe .", "start": "node ./index.mjs"
"start": "node ./index.cjs"
}, },
"repository": { "repository": {
"type": "git", "type": "git",