mirror of
https://github.com/Cameri/nostream.git
synced 2025-03-26 17:52:30 +01:00
feat: refactor server web socket adapter
This commit is contained in:
parent
ecfc8b7392
commit
24f888594f
@ -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] = {
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
|
@ -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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user