hapijs / nes

WebSocket adapter plugin for hapi routes
Other
502 stars 87 forks source link

Unknown authentication strategy #285

Closed jaulz closed 4 years ago

jaulz commented 5 years ago

If we set the mode to optional, the very first auth reverification after the initial connection will fail:

  error: AssertionError [ERR_ASSERTION]: Unknown authentication strategy: 
      at module.exports.internals.Auth.verify (/.../node_modules/@hapi/hapi/lib/auth.js:114:14)
      at module.exports.internals.Socket.internals.Socket._verifyAuth (/.../node_modules/@hapi/nes/lib/socket.js:617:32)
      at module.exports.internals.Socket.internals.Socket._lifecycle (/.../node_modules/@hapi/nes/lib/socket.js:280:16)
      at module.exports.internals.Socket.internals.Socket._onMessage (/.../node_modules/@hapi/nes/lib/socket.js:246:48)
      at WebSocket.<anonymous> (/.../node_modules/@hapi/nes/lib/socket.js:53:46)
      at WebSocket.emit (events.js:203:13)
      at Receiver.receiverOnMessage (/.../node_modules/ws/lib/websocket.js:789:20)
      at Receiver.emit (events.js:203:13)
      at Receiver.dataMessage (/.../node_modules/ws/lib/receiver.js:422:14)
      at Receiver.getData (/.../node_modules/ws/lib/receiver.js:352:17)
      at Receiver.startLoop (/.../node_modules/ws/lib/receiver.js:138:22)
      at Receiver._write (/.../node_modules/ws/lib/receiver.js:74:10)
      at doWrite (_stream_writable.js:417:12)
      at writeOrBuffer (_stream_writable.js:401:5)
      at Receiver.Writable.write (_stream_writable.js:301:11)
      at Socket.socketOnData (/.../node_modules/ws/lib/websocket.js:864:35)
      at Socket.emit (events.js:203:13)
      at addChunk (_stream_readable.js:294:12)
      at readableAddChunk (_stream_readable.js:275:11)
      at Socket.Readable.push (_stream_readable.js:210:10)
      at TCP.onStreamRead (internal/stream_base_commons.js:166:17)

I tracked it down and these line cause the issues:

        const strategy = this._strategies[auth.strategy];
        Hoek.assert(strategy, 'Unknown authentication strategy:', auth.strategy);

this._strategies contains my defined strategies but auth.strategy is empty and thus it fails.

This is my code for Nes in general:

    await this.server.register({
      plugin: Nes,
      options: {
        auth: {
          type: 'direct',
          endpoint: '/_api/socket',
          password: keys[0],
          route: {
            mode: 'optional',
            strategy: 'jwt',
          },
        },
        origin,
      },
    })
hueniverse commented 4 years ago

Why are you setting auth mode on the authentication endpoint?! I am aware it is possible but I don't understand the use case. The auth endpoint is used only for auth. There is no purpose to it other than that. If you want to set optional auth, you do that on the actual endpoints nes is serving.

Am I missing something?

jaulz commented 4 years ago

@hueniverse my use case is the following: any user should be able to connect in order to receive updates but once a user signs in he will reauthenticate and receive personal updates.

hueniverse commented 4 years ago

That doesn't mean you want the authentication endpoint to have optional authentication. If you simply omit auth from the connection, you will have optional authentication.

lock[bot] commented 4 years ago

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.