feat: signed challenge sent integration test

This commit is contained in:
antonleviathan
2023-02-10 15:37:46 -05:00
parent 70ace4f7f1
commit 7e793c63bd
3 changed files with 29 additions and 14 deletions

View File

@ -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
}
}

View File

@ -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

View File

@ -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)
})