feat: refactor server web socket adapter

This commit is contained in:
antonleviathan 2023-02-09 22:30:46 -05:00
parent ecfc8b7392
commit 24f888594f
No known key found for this signature in database
GPG Key ID: 44A86CFF1FDF0E85
4 changed files with 47 additions and 14 deletions

View File

@ -6,7 +6,7 @@ import { randomBytes } from 'crypto'
import { WebSocket } from 'ws'
import { ContextMetadata, Factory } from '../@types/base'
import { createAuthEventMessage, createNoticeMessage, createOutgoingEventMessage } from '../utils/messages'
import { createAuthEventMessage, createCommandResult, createNoticeMessage, createOutgoingEventMessage } from '../utils/messages'
import { IAbortable, IMessageHandler } from '../@types/message-handlers'
import { IncomingMessage, MessageType, OutgoingMessage } from '../@types/messages'
import { IWebSocketAdapter, IWebSocketServerAdapter } from '../@types/adapters'
@ -181,18 +181,43 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
const message = attemptValidation(messageSchema)(JSON.parse(raw.toString('utf8')))
if (
!this.authenticated
&& this.settings().authentication.enabled
&& message[0] !== MessageType.AUTH
) {
const challenge = this.setNewAuthChallenge()
this.webSocketServer.emit(
WebSocketServerAdapterEvent.Broadcast,
createAuthEventMessage(challenge)
)
if (!this.authenticated && this.settings().authentication.enabled) {
switch(message[0]) {
case MessageType.REQ: {
// Need to emit event?
const challenge = this.setNewAuthChallenge()
return
this.webSocketServer.emit(
WebSocketServerAdapterEvent.Broadcast,
createAuthEventMessage(challenge)
)
return
}
case MessageType.EVENT: {
// Need to emit event?
const challenge = this.setNewAuthChallenge()
this.webSocketServer.emit(
WebSocketServerAdapterEvent.Broadcast,
createCommandResult(message[1].id, false, 'rejected: unauthorized')
)
this.webSocketServer.emit(
WebSocketServerAdapterEvent.Broadcast,
createAuthEventMessage(challenge)
)
return
}
default: {
this.sendMessage(createNoticeMessage('invalid: asdcf'))
return
}
}
}
message[ContextMetadataKey] = {

View File

@ -1,4 +1,5 @@
import {
Before,
Then,
When,
World,
@ -19,10 +20,16 @@ import {
} from '../helpers'
import { Event } from '../../../../src/@types/event'
import { isDraft } from '../shared'
import { SettingsStatic } from '../../../../src/utils/settings'
chai.use(sinonChai)
const { expect } = chai
Before(function () {
const settings = SettingsStatic.createSettings()
settings.authentication.enabled = false
})
When(/(\w+) subscribes to last event from (\w+)$/, async function(this: World<Record<string, any>>, from: string, to: string) {
const ws = this.parameters.clients[from] as WebSocket
const event = this.parameters.events[to].pop()

View File

@ -4,6 +4,7 @@ import {
World,
} from '@cucumber/cucumber'
import chai from 'chai'
import { EventKinds } from '../../../../src/constants/base'
import { SettingsStatic } from '../../../../src/utils/settings'
import sinonChai from 'sinon-chai'
@ -18,9 +19,8 @@ Given(/the relay requires the client to authenticate/, async function (this: Wor
settings.authentication.enabled = true
})
Then(/(\w+) receives an authentication challenge/, async function (name: string) {
Then(/(\w+) receives an authentication challenge "([^"]+?)"/, async function (name: string) {
const ws = this.parameters.clients[name] as WebSocket
const outgoingAuthMessage = await waitForAuth(ws)
const event = outgoingAuthMessage[1]
expect(event.kind).to.equal(EventKinds.AUTH)

View File

@ -40,6 +40,7 @@ BeforeAll({ timeout: 1000 }, async function () {
rrDbClient = getReadReplicaDbClient()
await dbClient.raw('SELECT 1=1')
Sinon.stub(SettingsStatic, 'watchSettings')
const settings = SettingsStatic.createSettings()
SettingsStatic._settings = pipe(