peers / peerjs

Simple peer-to-peer with WebRTC.
https://peerjs.com
MIT License
12.48k stars 1.43k forks source link

An error in Peer constructor options type definition #897

Closed tripolskypetr closed 2 years ago

tripolskypetr commented 3 years ago

It looks like the peerjs-server can process something called a token. I think there is an error in Peer constructor options type defenition.

import { ExpressPeerServer } from 'peer';
...
const peer = ExpressPeerServer(server);
...
peer.on('connection', async (client) => {
  const token = client.getToken();
//                    ^^^^^^^^^^^

As you can see, the index.ts contains the following PeerJSOption interface. This file is not auto updating after compilation

interface PeerJSOption {
  key?: string;
  host?: string;
  port?: number;
  path?: string;
  secure?: boolean;
  config?: RTCConfiguration;
  debug?: number;
}

But peer.ts which is internal interface implementation has the token field

class PeerOptions implements PeerJSOption {
  debug?: LogLevel; // 1: Errors, 2: Warnings, 3: All logs
  host?: string;
  port?: number;
  path?: string;
  key?: string;
  token?: string;
  config?: any;
  secure?: boolean;
  pingInterval?: number;
  logFunction?: (logLevel: LogLevel, ...rest: any[]) => void;
}

Will the following code from stackoverflow handle the authentication? If so, please update the interface definition

You can send a token in the options, for example a JWT with the userId encoded:

const myPeer = new Peer(userId, {
    host: '/',
    token: userJwt
})

Receive it when the user connects and validate it. If the ids don't match, close the socket:

peerServer.on('connection', client => { 
    const decodedToken = decodeToken(client.token)
    if (!decodedToken || decodedToken.userId != client.id)
        client.socket.close()
})
github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 1.4.0-rc.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 1.4.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: