2024-02-05 11:49:38 +00:00
|
|
|
|
import {
|
|
|
|
|
ApiResponse,
|
|
|
|
|
Authorizer,
|
|
|
|
|
ConfigType,
|
|
|
|
|
ValidateJWTTokenInput,
|
|
|
|
|
ValidateJWTTokenResponse,
|
|
|
|
|
} from '@authorizerdev/authorizer-js'
|
|
|
|
|
import { Server, onAuthenticatePayload } from '@hocuspocus/server'
|
2024-02-05 11:39:28 +00:00
|
|
|
|
|
|
|
|
|
const authorizer = new Authorizer({
|
|
|
|
|
clientID: process.env.AUTHORIZER_CLIENT_ID,
|
|
|
|
|
authorizerURL: 'https://auth.discours.io',
|
2024-02-05 11:49:38 +00:00
|
|
|
|
redirectURL: 'https://testing.discours.io',
|
|
|
|
|
} as ConfigType)
|
2024-02-05 11:39:28 +00:00
|
|
|
|
|
|
|
|
|
const server = await Server.configure({
|
|
|
|
|
port: 4242,
|
|
|
|
|
async onConnect({ connection }) {
|
2024-02-05 11:49:38 +00:00
|
|
|
|
connection.requiresAuthentication = false // FIXME
|
2024-02-05 11:39:28 +00:00
|
|
|
|
},
|
|
|
|
|
async onAuthenticate(data: onAuthenticatePayload) {
|
|
|
|
|
// Danger! This won’t 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)
|
2024-02-05 11:49:38 +00:00
|
|
|
|
if (response?.data?.is_valid) {
|
2024-02-05 11:39:28 +00:00
|
|
|
|
const { sub: user, allowed_roles: roles } = response.data.claims
|
|
|
|
|
console.debug(`user_id: ${user} roles: ${roles}`)
|
|
|
|
|
} else {
|
|
|
|
|
console.debug('no valid auth token presented')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-02-05 11:49:38 +00:00
|
|
|
|
}).listen()
|
2024-02-05 11:39:28 +00:00
|
|
|
|
|
2024-02-05 11:49:38 +00:00
|
|
|
|
server.listen()
|