crdt-server/index.ts
Untone 8940086e96
Some checks failed
deploy / deploy (push) Failing after 32s
1.0.2
2024-02-05 14:49:38 +03:00

44 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 = false // FIXME
},
async onAuthenticate(data: onAuthenticatePayload) {
// Danger! This wont be called for that connection attempt.
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}`)
} else {
console.debug('no valid auth token presented')
}
}
}
},
}).listen()
server.listen()