Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private async authenticatePeer(peer: SimplePeer.Instance, peerId?: string) {
const peerPublicKey = peerId ? sodium.from_hex(peerId) : undefined
const userPublicKey = await mutualHandshake(peer, localUser().id, peerPublicKey)
if (!userPublicKey) {
const addr = `${(peer as any).remoteAddress}:${(peer as any).remotePort}`
peer.destroy()
throw new NetworkError(
NetworkErrorCode.PeerAuthenticationFailure,
`Failed to authenticate with peer [${addr}]`
)
}
const userId = sodium.to_hex(userPublicKey)
const netId = new NetUniqueId(userPublicKey)
const conn = new RTCPeerConn(netId, peer)
console.debug(`Authenticated peer ${userId}`, conn)
this.emit('connection', conn)
}
}
if (publicKey && privateKey) {
localKeyPair = {
publicKey: sodium.from_hex(publicKey),
privateKey: sodium.from_hex(privateKey),
keyType: 'curve25519'
}
}
}
if (!localKeyPair) {
localKeyPair = keyPair()
try {
localStorage.setItem(PUBKEY_PROP, sodium.to_hex(localKeyPair.publicKey))
localStorage.setItem(SECKEY_PROP, sodium.to_hex(localKeyPair.privateKey))
} catch (e) {}
}
return localKeyPair
}
export const equal = (a: Data, b: Data) => sodium.to_hex(a) === sodium.to_hex(b)
private validateAuth(client: Client, challenge: string) {
if (client.status !== ClientStatus.PendingAuth) return
const secret =
typeof challenge === 'string' &&
sodium.from_base64(challenge, sodium.base64_variants.URLSAFE_NO_PADDING)
const secretHex = secret && sodium.to_hex(secret)
const authSecretHex = client.authSecret && sodium.to_hex(client.authSecret)
if (secret && authSecretHex === secretHex) {
client.status = ClientStatus.Authed
if (typeof client.pendingRoom === 'string') {
const room = client.pendingRoom
client.pendingRoom = undefined
this.createRoom(client, room)
}
} else {
this.log(`Client [${client.id}] failed to solve challenge: ${authSecretHex} !== ${secretHex}`)
client.socket.close()
}
}
const privateKey = localStorage.getItem(SECKEY_PROP)
if (publicKey && privateKey) {
localKeyPair = {
publicKey: sodium.from_hex(publicKey),
privateKey: sodium.from_hex(privateKey),
keyType: 'curve25519'
}
}
}
if (!localKeyPair) {
localKeyPair = keyPair()
try {
localStorage.setItem(PUBKEY_PROP, sodium.to_hex(localKeyPair.publicKey))
localStorage.setItem(SECKEY_PROP, sodium.to_hex(localKeyPair.privateKey))
} catch (e) {}
}
return localKeyPair
}
private validateAuth(client: Client, challenge: string) {
if (client.status !== ClientStatus.PendingAuth) return
const secret =
typeof challenge === 'string' &&
sodium.from_base64(challenge, sodium.base64_variants.URLSAFE_NO_PADDING)
const secretHex = secret && sodium.to_hex(secret)
const authSecretHex = client.authSecret && sodium.to_hex(client.authSecret)
if (secret && authSecretHex === secretHex) {
client.status = ClientStatus.Authed
if (typeof client.pendingRoom === 'string') {
const room = client.pendingRoom
client.pendingRoom = undefined
this.createRoom(client, room)
}
} else {
this.log(`Client [${client.id}] failed to solve challenge: ${authSecretHex} !== ${secretHex}`)
client.socket.close()
}
}