t-mullen / simple-signal

Signalling solution for simple-peer with socket.io
MIT License
162 stars 27 forks source link

How to deny a connection? #30

Closed ricardopolo closed 3 years ago

ricardopolo commented 3 years ago

@t-mullen Hi! Thanks for this package.

I see you say we can use discovery data to pass credentials. But, how can we deny a connection in the discover event? Thanks!

t-mullen commented 3 years ago

You get to define how discovery works. I recommend resolving with an error of some kind if auth fails. There is no built-in way to fail discovery.

Here's a basic example:

Server:

const isAuthValid = (auth) => { /* define this */ }
signalServer.on('discover', (request) => {
  if (isAuthValid(request.discoveryData.auth)) { //<-- check credentials
    const clientID = request.socket.id
    allIDs.add(clientID)
    request.discover({ ids: Array.from(allIDs), authFailed: false }) 
  } else {
    // auth failed, don't track client ID or return any other client IDs
    request.discover({ id: [], authFailed: true }) // <- probably a descriptive error message too
  }
})

Client:

signalClient.on('discover', async ({ ids, authFailed }) => {
  if (authFailed) {
    throw new Error('Authentication failed')
  } else {
    // continue as usual
  }
})

signalClient.discover({ auth: 'your password or token or whatever' })
ricardopolo commented 3 years ago

Thanks!!