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

This commit is contained in:
Untone 2024-02-17 14:40:34 +03:00
parent c1103a54e8
commit e140e5b9c2
5 changed files with 45 additions and 174 deletions

39
index.js Normal file
View File

@ -0,0 +1,39 @@
const { Authorizer } = require('@authorizerdev/authorizer-js')
const { Server } = require('@hocuspocus/server')
const authorizer = new Authorizer({
clientID: process.env.AUTHORIZER_CLIENT_ID,
authorizerURL: 'https://auth.discours.io',
redirectURL: 'https://testing.discours.io',
})
;(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()
})()

View File

@ -1,45 +0,0 @@
import {
ApiResponse,
Authorizer,
ConfigType,
ValidateJWTTokenInput,
ValidateJWTTokenResponse,
} from '@authorizerdev/authorizer-js'
import Server, { onAuthenticatePayload } from '@hocuspocus/server'
const authorizer = new Authorizer({
clientID: process.env.AUTHORIZER_CLIENT_ID,
authorizerURL: 'https://auth.discours.io',
redirectURL: 'https://testing.discours.io',
} as ConfigType)
const server = await Server.configure({
port: 4242,
async onConnect({ connection }) {
connection.requiresAuthentication = true
},
async onAuthenticate(data: onAuthenticatePayload) {
if (data.requestHeaders) {
const params: ValidateJWTTokenInput = {
token_type: 'access_token',
token: data.requestHeaders['authorization'] || '',
}
if (params.token) {
// NOTE: ожидаем, что клиент отправит токен
const response: ApiResponse<ValidateJWTTokenResponse> = 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()

119
package-lock.json generated
View File

@ -1,11 +1,11 @@
{
"name": "discours-hocuspocus",
"name": "discours-editing-server",
"version": "1.0.2",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "discours-hocuspocus",
"name": "discours-editing-server",
"version": "1.0.2",
"license": "MIT",
"dependencies": {
@ -13,8 +13,7 @@
"@hocuspocus/server": "2.0.1"
},
"devDependencies": {
"@biomejs/biome": "1.5.3",
"typescript": "5.3.3"
"@biomejs/biome": "1.5.3"
}
},
"node_modules/@authorizerdev/authorizer-js": {
@ -58,22 +57,6 @@
"@biomejs/cli-win32-x64": "1.5.3"
}
},
"node_modules/@biomejs/cli-darwin-arm64": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/@biomejs/cli-darwin-arm64/-/cli-darwin-arm64-1.5.3.tgz",
"integrity": "sha512-ImU7mh1HghEDyqNmxEZBoMPr8SxekkZuYcs+gynKlNW+TALQs7swkERiBLkG9NR0K1B3/2uVzlvYowXrmlW8hw==",
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">=14.*"
}
},
"node_modules/@biomejs/cli-darwin-x64": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/@biomejs/cli-darwin-x64/-/cli-darwin-x64-1.5.3.tgz",
@ -90,102 +73,6 @@
"node": ">=14.*"
}
},
"node_modules/@biomejs/cli-linux-arm64": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/@biomejs/cli-linux-arm64/-/cli-linux-arm64-1.5.3.tgz",
"integrity": "sha512-cupBQv0sNF1OKqBfx7EDWMSsKwRrBUZfjXawT4s6hKV6ALq7p0QzWlxr/sDmbKMLOaLQtw2Qgu/77N9rm+f9Rg==",
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=14.*"
}
},
"node_modules/@biomejs/cli-linux-arm64-musl": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/@biomejs/cli-linux-arm64-musl/-/cli-linux-arm64-musl-1.5.3.tgz",
"integrity": "sha512-DYuMizUYUBYfS0IHGjDrOP1RGipqWfMGEvNEJ398zdtmCKLXaUvTimiox5dvx4X15mBK5M2m8wgWUgOP1giUpQ==",
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=14.*"
}
},
"node_modules/@biomejs/cli-linux-x64": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/@biomejs/cli-linux-x64/-/cli-linux-x64-1.5.3.tgz",
"integrity": "sha512-YQrSArQvcv4FYsk7Q91Yv4uuu5F8hJyORVcv3zsjCLGkjIjx2RhjYLpTL733SNL7v33GmOlZY0eFR1ko38tuUw==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=14.*"
}
},
"node_modules/@biomejs/cli-linux-x64-musl": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/@biomejs/cli-linux-x64-musl/-/cli-linux-x64-musl-1.5.3.tgz",
"integrity": "sha512-UUHiAnlDqr2Y/LpvshBFhUYMWkl2/Jn+bi3U6jKuav0qWbbBKU/ByHgR4+NBxpKBYoCtWxhnmatfH1bpPIuZMw==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=14.*"
}
},
"node_modules/@biomejs/cli-win32-arm64": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/@biomejs/cli-win32-arm64/-/cli-win32-arm64-1.5.3.tgz",
"integrity": "sha512-HxatYH7vf/kX9nrD+pDYuV2GI9GV8EFo6cfKkahAecTuZLPxryHx1WEfJthp5eNsE0+09STGkKIKjirP0ufaZA==",
"cpu": [
"arm64"
],
"dev": true,
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=14.*"
}
},
"node_modules/@biomejs/cli-win32-x64": {
"version": "1.5.3",
"resolved": "https://registry.npmjs.org/@biomejs/cli-win32-x64/-/cli-win32-x64-1.5.3.tgz",
"integrity": "sha512-fMvbSouZEASU7mZH8SIJSANDm5OqsjgtVXlbUqxwed6BP7uuHRSs396Aqwh2+VoW8fwTpp6ybIUoC9FrzB0kyA==",
"cpu": [
"x64"
],
"dev": true,
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=14.*"
}
},
"node_modules/@hocuspocus/common": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/@hocuspocus/common/-/common-2.0.1.tgz",

View File

@ -2,11 +2,11 @@
"name": "discours-editing-server",
"version": "1.0.2",
"description": "discours.io webrtc p2p network helping crdt-server",
"main": "index.ts",
"main": "index.js",
"type": "module",
"scripts": {
"fix": "npx @biomejs/biome check --apply-unsafe .",
"start": "npx tsc index.ts"
"start": "node ./index.js"
},
"repository": {
"type": "git",
@ -19,7 +19,6 @@
"@hocuspocus/server": "2.0.1"
},
"devDependencies": {
"@biomejs/biome": "1.5.3",
"typescript": "5.3.3"
"@biomejs/biome": "1.5.3"
}
}

View File

@ -1,9 +0,0 @@
{
"compilerOptions": {
"target": "es2017",
"lib": ["es2022", "dom"],
"esModuleInterop": true,
"moduleResolution": "NodeNext",
"module": "NodeNext"
}
}