Skip to content

Commit

Permalink
fix: peer authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
ivelin committed Jan 22, 2020
1 parent 7cba09b commit 9a1c24c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
32 changes: 28 additions & 4 deletions src/store/pnp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,25 @@ function setPeerConnectionHandlers ({ state, commit, dispatch }, peerConnection)
// Handle incoming data (messages only since this is the signal sender)
peerConnection.on('data', function (data) {
// addMessage('<span class=\'peerMsg\'>Peer:</span> ' + data)
console.log('pnpService: Data message received: %s', data)
console.debug('Remote Peer Data message received (type %s): %s',
typeof (data), data)
// if data is authentication challenge response, verify it
// for now we asume authentication passed
const authMessage = true
let authMessage
if (typeof (data) === 'string') {
try {
authMessage = JSON.parse(data)
} catch (e) {
console.error('Error while JSON parsing data message', data)
}
}
if (authMessage) {
const authPassed = true
const authPassed = authMessage.name === 'Ambianic-Edge'
if (authPassed) {
console.debug('Remote peer authenticated as:', authMessage.name)
commit(PEER_CONNECTED, peerConnection)
} else {
commit(USER_MESSAGE, 'Remote peer authentication failed.')
}
}
})
Expand All @@ -232,6 +243,11 @@ function setPeerConnectionHandlers ({ state, commit, dispatch }, peerConnection)
commit(PEER_DISCONNECTED)
commit(USER_MESSAGE, 'Connection to remote peer closed')
})

peerConnection.on('error', function (err) {
commit(PEER_CONNECTION_ERROR, err)
console.debug('Error from peer DataConnection', err)
})
}

const actions = {
Expand Down Expand Up @@ -356,6 +372,7 @@ const actions = {
// Check URL params for commands that should be sent immediately
// var command = getUrlParam('command')
// if (command)
console.log('Preparing to send message via peer DataConnection')
const msg = JSON.stringify({
type: 'http-request',
method: 'GET',
Expand All @@ -365,6 +382,7 @@ const actions = {
}
})
peerConnection.send(msg)
console.log('Peer DataConnection sending message', msg)
console.log('DataChannel transport capabilities',
peerConnection.dataChannel)
},
Expand All @@ -377,7 +395,13 @@ const actions = {
async [REMOVE_REMOTE_PEER_ID] ({ state, commit, dispatch }) {
if (state.peerConnectionStatus !== PEER_DISCONNECTED) {
const conn = state.peerConnection
conn.close()
if (conn) {
try {
conn.close()
} catch (err) {
console.debug('Error while closing peer DataConnection.', err)
}
}
commit(PEER_DISCONNECTED)
}
commit(REMOTE_PEER_ID_REMOVED)
Expand Down
2 changes: 1 addition & 1 deletion src/views/EdgeConnect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
v-model="removeEdgeDialog"
>
<v-card>
<v-card-title class="headline">Remove device connection?</v-card-title>
<v-card-title class="headline">Remove device?</v-card-title>

<v-card-text>
If you remove the connection to this Ambianic Edge device,
Expand Down

0 comments on commit 9a1c24c

Please sign in to comment.