From 7e793c63bd8072d38a64648fa0564e9634d1f417 Mon Sep 17 00:00:00 2001 From: antonleviathan Date: Fri, 10 Feb 2023 15:37:46 -0500 Subject: [PATCH] feat: signed challenge sent integration test --- src/adapters/web-socket-adapter.ts | 20 +++++++++---------- .../features/nip-42/nip-42.feature | 7 +++++++ .../features/nip-42/nip-42.feature.ts | 16 ++++++++++++--- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/adapters/web-socket-adapter.ts b/src/adapters/web-socket-adapter.ts index ee50e13..b60d557 100644 --- a/src/adapters/web-socket-adapter.ts +++ b/src/adapters/web-socket-adapter.ts @@ -1,5 +1,5 @@ +import { ContextMetadataKey, EventKinds } from '../constants/base' import cluster from 'cluster' -import { ContextMetadataKey } from '../constants/base' import { EventEmitter } from 'stream' import { IncomingMessage as IncomingHttpMessage } from 'http' import { randomBytes } from 'crypto' @@ -182,31 +182,29 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter const message = attemptValidation(messageSchema)(JSON.parse(raw.toString('utf8'))) debug('recv client msg: %o', message) - if (!this.authenticated && this.settings().authentication.enabled) { + if ( + !this.authenticated + && message[1].kind !== EventKinds.AUTH + && this.settings().authentication.enabled + ) { switch(message[0]) { case MessageType.REQ: { - // Need to emit event? const challenge = this.setNewAuthChallenge() - this.sendMessage(createAuthMessage(challenge)) - return } case MessageType.EVENT: { - // Need to emit event? - const challenge = this.setNewAuthChallenge() - this.sendMessage(createCommandResult(message[1].id, false, 'rejected: unauthorized')) - this.sendMessage(createAuthMessage(challenge)) - return } default: { - this.sendMessage(createNoticeMessage('invalid: asdcf')) + const challenge = this.setNewAuthChallenge() + this.sendMessage(createCommandResult(message[1].id, false, 'rejected: unauthorized')) + this.sendMessage(createAuthMessage(challenge)) return } } diff --git a/test/integration/features/nip-42/nip-42.feature b/test/integration/features/nip-42/nip-42.feature index e1782aa..879fbbe 100644 --- a/test/integration/features/nip-42/nip-42.feature +++ b/test/integration/features/nip-42/nip-42.feature @@ -4,3 +4,10 @@ Feature: NIP-42 And the relay requires the client to authenticate When Alice sends a text_note event with content "hello nostr" unsuccessfully Then Alice receives an authentication challenge + + Scenario: Alice sends a signed challenge event + Given someone called Alice + And the relay requires the client to authenticate + When Alice sends a text_note event with content "hello nostr" unsuccessfully + And Alice receives an authentication challenge + Then Alice sends a signed_challenge_event diff --git a/test/integration/features/nip-42/nip-42.feature.ts b/test/integration/features/nip-42/nip-42.feature.ts index 4a4d708..de889cb 100644 --- a/test/integration/features/nip-42/nip-42.feature.ts +++ b/test/integration/features/nip-42/nip-42.feature.ts @@ -4,10 +4,11 @@ import { World, } from '@cucumber/cucumber' import chai from 'chai' - -import { SettingsStatic } from '../../../../src/utils/settings' import sinonChai from 'sinon-chai' -import { waitForAuth } from '../helpers' + +import { createEvent, sendEvent, waitForAuth } from '../helpers' +import { EventKinds } from '../../../../src/constants/base' +import { SettingsStatic } from '../../../../src/utils/settings' import { WebSocket } from 'ws' chai.use(sinonChai) @@ -26,3 +27,12 @@ Then(/(\w+) receives an authentication challenge/, async function (name: string) this.parameters.challenges[name].push(challenge) }) +Then(/(\w+) sends a signed_challenge_event/, async function (name: string) { + const challenge = this.parameters.challenges[name].pop() + const ws = this.parameters.clients[name] as WebSocket + const { pubkey, privkey } = this.parameters.identities[name] + + const event: any = await createEvent({ pubkey, kind: EventKinds.AUTH, content: challenge }, privkey) + await sendEvent(ws, event, true) + this.parameters.events[name].push(event) +})