From a02a342f9406f4f51945e572e5a71409a0da780b Mon Sep 17 00:00:00 2001 From: Joao Santos Date: Tue, 22 May 2018 19:44:31 +0100 Subject: [PATCH 1/7] authenticates users --- package.json | 1 + src/backend/auth-token.js | 13 +++++++++++-- src/backend/auth.js | 37 +++++++++++++++++++++++++++++++++++-- src/backend/index.js | 2 +- src/peers.js | 6 ++++++ 5 files changed, 54 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 0251d02..20843e0 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "history": "^4.7.2", "ipfs": "~0.28.2", "libp2p-crypto": "^0.10.3", + "peer-pad-ethereum-signature": "^1.0.3", "pify": "^3.0.0", "prop-types": "^15.6.0", "quill": "^1.3.3", diff --git a/src/backend/auth-token.js b/src/backend/auth-token.js index b245e12..3a315e5 100644 --- a/src/backend/auth-token.js +++ b/src/backend/auth-token.js @@ -1,8 +1,12 @@ 'use strict' const waterfall = require('async/waterfall') +var peerPadEthereumSignature = require('peer-pad-ethereum-signature') -module.exports = async function authTokenFromIpfsId (ipfs, keys) { +// exports.verifyIpfsIdSignature = function (ipfs, from, signature) { +// exports.signIpfsId = function (ipfs) { + +module.exports = async function authTokenFromIpfsId (ipfs, keys, ethereumWalletInfo) { return new Promise((resolve, reject) => { waterfall( [ @@ -18,7 +22,12 @@ module.exports = async function authTokenFromIpfsId (ipfs, keys) { } }, (token, cb) => { - cb(null, token && token.toString('base64')) + peerPadEthereumSignature.signIpfsId(ipfs).then(signatureData => { + let send = {ethereumWalletInfo: JSON.stringify(signatureData).toString('base64'), token: token && token.toString('base64')} + send = JSON.stringify(send) + // cb(null, token && token.toString('base64')) + cb(null, send) + }) } ], (err, token) => { diff --git a/src/backend/auth.js b/src/backend/auth.js index 76c9f61..b717cf0 100644 --- a/src/backend/auth.js +++ b/src/backend/auth.js @@ -1,5 +1,5 @@ 'use strict' - +var peerPadEthereumSignature = require('peer-pad-ethereum-signature') const EventEmitter = require('events') module.exports = function Auth (keys, roomEmitter) { @@ -14,6 +14,7 @@ module.exports = function Auth (keys, roomEmitter) { roomEmitter.on('peer left', (peerId) => { delete capabilitiesByPeer[peerId] + // step1 : emitir evento auth.emit('change', peerId, null) }) @@ -57,8 +58,11 @@ module.exports = function Auth (keys, roomEmitter) { const capabilities = capabilitiesByPeer[peerId] || {} return Object.keys(capabilities).filter((capability) => capabilities[capability] === true) } - +// function verifySignature (peer, payload, signature, callback) { + // payload = authtoken + // payload = Buffer.from(JSON.parse(payload.toString()).token, 'base64') + const capabilities = capabilitiesByPeer[peer] if (!signature) { if (capabilities && capabilities.read && !capabilities.write) { @@ -71,7 +75,36 @@ module.exports = function Auth (keys, roomEmitter) { } } + function checkEthereumSignature (ethereumWalletInfo, sender) { + return peerPadEthereumSignature.verifyIpfsIdSignature(sender, ethereumWalletInfo) + } + function checkAuth (authToken, y, sender) { + return new Promise(function (resolve, reject) { + if (!authToken) { + console.log('Empty token...') + resolve('nop') + } + var ethereumWalletInfo = JSON.parse(JSON.parse(authToken).ethereumWalletInfo) + let token = JSON.parse(authToken).token + + let checkIPFSSignature = checkIpfsIdAuth(token, y, sender) + let EthereumSignatureCheck = checkEthereumSignature(ethereumWalletInfo, sender) + + Promise.all([EthereumSignatureCheck, checkIPFSSignature]).then(checks => { + if (checks[0] && checks[1]) { + // emitir evento aqui + auth.emit('authenticatedEthereum', ethereumWalletInfo.from, null) + resolve('write') + } else { + resolve('bad signature') + } + } + ) + }) + } + + function checkIpfsIdAuth (authToken, y, sender) { return new Promise((resolve, reject) => { if (!authToken) { // TODO: is this correct? diff --git a/src/backend/index.js b/src/backend/index.js index 5f1d1ef..8fdc209 100644 --- a/src/backend/index.js +++ b/src/backend/index.js @@ -45,7 +45,7 @@ class Backend extends EventEmitter { // ---- initialize keys this._keys = await parseKeys(b58Decode(options.readKey), options.writeKey && b58Decode(options.writeKey)) - const token = await authToken(this.ipfs, this._keys) + const token = await authToken(this.ipfs, this._keys) // ethereum wallet this.auth = Auth(this._keys, this.room) this.crdt = await CRDT(this._options.name, token, this._keys, this.ipfs, this.room, this.auth) this.crdt.share.access.observeDeep(this.auth.observer()) diff --git a/src/peers.js b/src/peers.js index 4a31bd4..a18b52d 100644 --- a/src/peers.js +++ b/src/peers.js @@ -24,6 +24,12 @@ class Peers extends EventEmitter { this._roomChanged() }) + backend.auth.on('authenticatedEthereum', (peerId, ethereumId) => { + const peer = this._ensurePeer(peerId) + peer.ethereumId = ethereumId + this._roomChanged() + }) + backend.crdt.share.peerAliases.observe((event) => { const peerName = event.name if (['update', 'insert', 'add'].indexOf(event.type) >= 0) { From d9e844b72b23e312ac33ed3b43673fbd6b9ba602 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Wed, 23 May 2018 11:21:36 +0100 Subject: [PATCH 2/7] ethereum conditional check --- src/backend/auth-token.js | 20 +++++++++++++------- src/backend/auth.js | 31 ++++++++++++++++++------------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/backend/auth-token.js b/src/backend/auth-token.js index 3a315e5..243e6ea 100644 --- a/src/backend/auth-token.js +++ b/src/backend/auth-token.js @@ -21,13 +21,19 @@ module.exports = async function authTokenFromIpfsId (ipfs, keys, ethereumWalletI keys.write.sign(Buffer.from(nodeId), cb) } }, - (token, cb) => { - peerPadEthereumSignature.signIpfsId(ipfs).then(signatureData => { - let send = {ethereumWalletInfo: JSON.stringify(signatureData).toString('base64'), token: token && token.toString('base64')} - send = JSON.stringify(send) - // cb(null, token && token.toString('base64')) - cb(null, send) - }) + (ipfsToken, cb) => { + ipfsToken = ipfsToken && ipfsToken.toString('base64') + const token = { + token: ipfsToken + } + if (window.web3) { + peerPadEthereumSignature.signIpfsId(ipfs).then(signatureData => { + token.ethereumWalletInfo = JSON.stringify(signatureData).toString('base64') + cb(null, token) + }) + } else { + cb(null, token) + } } ], (err, token) => { diff --git a/src/backend/auth.js b/src/backend/auth.js index b717cf0..035d094 100644 --- a/src/backend/auth.js +++ b/src/backend/auth.js @@ -82,24 +82,29 @@ module.exports = function Auth (keys, roomEmitter) { function checkAuth (authToken, y, sender) { return new Promise(function (resolve, reject) { if (!authToken) { - console.log('Empty token...') resolve('nop') } - var ethereumWalletInfo = JSON.parse(JSON.parse(authToken).ethereumWalletInfo) - let token = JSON.parse(authToken).token + // authToken = JSON.parse(Buffer.from(authToken)) + const ethereumSignatureCheck = authToken.ethereumWalletInfo && checkEthereumSignature(authToken.ethereumWalletInfo, sender) + const ethereumWalletInfo = authToken.ethereumWalletInfo && JSON.parse(authToken.ethereumWalletInfo) + + const token = authToken.token + const verifications = [checkIpfsIdAuth(token, y, sender)] + if (ethereumSignatureCheck) { + verifications.push(ethereumSignatureCheck) + } - let checkIPFSSignature = checkIpfsIdAuth(token, y, sender) - let EthereumSignatureCheck = checkEthereumSignature(ethereumWalletInfo, sender) + return Promise.all(verifications) + .then(([ipfsVerResult, ethVerResult]) => { + if (ethereumSignatureCheck) { + if (!ethVerResult) { + return 'bad ethereum signature' + } + auth.emit('authenticatedEthereum', sender, ethereumWalletInfo.from) + } - Promise.all([EthereumSignatureCheck, checkIPFSSignature]).then(checks => { - if (checks[0] && checks[1]) { - // emitir evento aqui - auth.emit('authenticatedEthereum', ethereumWalletInfo.from, null) - resolve('write') - } else { - resolve('bad signature') + return ipfsVerResult ? 'write' : 'bad signature' } - } ) }) } From 8f6c0416e04cd15c6464bdb2115bc34d497ae949 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Wed, 23 May 2018 11:34:01 +0100 Subject: [PATCH 3/7] fixed eth wallet info decode --- src/backend/auth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/auth.js b/src/backend/auth.js index 035d094..ab4609f 100644 --- a/src/backend/auth.js +++ b/src/backend/auth.js @@ -85,7 +85,7 @@ module.exports = function Auth (keys, roomEmitter) { resolve('nop') } // authToken = JSON.parse(Buffer.from(authToken)) - const ethereumSignatureCheck = authToken.ethereumWalletInfo && checkEthereumSignature(authToken.ethereumWalletInfo, sender) + const ethereumSignatureCheck = authToken.ethereumWalletInfo && checkEthereumSignature(JSON.parse(authToken.ethereumWalletInfo), sender) const ethereumWalletInfo = authToken.ethereumWalletInfo && JSON.parse(authToken.ethereumWalletInfo) const token = authToken.token From a7ac08922d0d8713788bd91dde9ef0ef8e5e2f25 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Wed, 23 May 2018 11:34:53 +0100 Subject: [PATCH 4/7] do not break of no auth token --- src/backend/auth.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/auth.js b/src/backend/auth.js index ab4609f..ab3005f 100644 --- a/src/backend/auth.js +++ b/src/backend/auth.js @@ -82,7 +82,7 @@ module.exports = function Auth (keys, roomEmitter) { function checkAuth (authToken, y, sender) { return new Promise(function (resolve, reject) { if (!authToken) { - resolve('nop') + return 'nop' } // authToken = JSON.parse(Buffer.from(authToken)) const ethereumSignatureCheck = authToken.ethereumWalletInfo && checkEthereumSignature(JSON.parse(authToken.ethereumWalletInfo), sender) From bed8ce3a7dba8704ee91cffd0a7d00f64dfc8bfc Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Wed, 23 May 2018 11:47:48 +0100 Subject: [PATCH 5/7] fixed promisy promises stuff --- src/backend/auth.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/auth.js b/src/backend/auth.js index ab3005f..0644416 100644 --- a/src/backend/auth.js +++ b/src/backend/auth.js @@ -82,7 +82,7 @@ module.exports = function Auth (keys, roomEmitter) { function checkAuth (authToken, y, sender) { return new Promise(function (resolve, reject) { if (!authToken) { - return 'nop' + return resolve('read') } // authToken = JSON.parse(Buffer.from(authToken)) const ethereumSignatureCheck = authToken.ethereumWalletInfo && checkEthereumSignature(JSON.parse(authToken.ethereumWalletInfo), sender) @@ -94,16 +94,16 @@ module.exports = function Auth (keys, roomEmitter) { verifications.push(ethereumSignatureCheck) } - return Promise.all(verifications) + Promise.all(verifications) .then(([ipfsVerResult, ethVerResult]) => { if (ethereumSignatureCheck) { if (!ethVerResult) { - return 'bad ethereum signature' + return reject(new Error('bad ethereum signature')) } auth.emit('authenticatedEthereum', sender, ethereumWalletInfo.from) } - return ipfsVerResult ? 'write' : 'bad signature' + return ipfsVerResult ? resolve('write') : reject(new Error('bad signature')) } ) }) From 674993cbc228c0220345430e340833f38d718b82 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Wed, 23 May 2018 12:45:36 +0100 Subject: [PATCH 6/7] propagating the entire ethereum info --- src/backend/auth.js | 2 +- src/peers.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/backend/auth.js b/src/backend/auth.js index 0644416..bddc516 100644 --- a/src/backend/auth.js +++ b/src/backend/auth.js @@ -100,7 +100,7 @@ module.exports = function Auth (keys, roomEmitter) { if (!ethVerResult) { return reject(new Error('bad ethereum signature')) } - auth.emit('authenticatedEthereum', sender, ethereumWalletInfo.from) + auth.emit('authenticatedEthereum', sender, ethereumWalletInfo) } return ipfsVerResult ? resolve('write') : reject(new Error('bad signature')) diff --git a/src/peers.js b/src/peers.js index a18b52d..b63d911 100644 --- a/src/peers.js +++ b/src/peers.js @@ -24,9 +24,9 @@ class Peers extends EventEmitter { this._roomChanged() }) - backend.auth.on('authenticatedEthereum', (peerId, ethereumId) => { + backend.auth.on('authenticatedEthereum', (peerId, ethereumInfo) => { const peer = this._ensurePeer(peerId) - peer.ethereumId = ethereumId + peer.ethereumInfo = ethereumInfo this._roomChanged() }) From f26e4570ff2d45a340195624d52c4d9d915cac44 Mon Sep 17 00:00:00 2001 From: Pedro Teixeira Date: Wed, 23 May 2018 19:09:01 +0100 Subject: [PATCH 7/7] eth auth --- package.json | 2 +- src/backend/auth-token.js | 11 ++++++++--- src/backend/auth.js | 8 +++----- src/peers.js | 1 + 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 20843e0..3745b72 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "history": "^4.7.2", "ipfs": "~0.28.2", "libp2p-crypto": "^0.10.3", - "peer-pad-ethereum-signature": "^1.0.3", + "peer-pad-ethereum-signature": "^1.0.4", "pify": "^3.0.0", "prop-types": "^15.6.0", "quill": "^1.3.3", diff --git a/src/backend/auth-token.js b/src/backend/auth-token.js index 243e6ea..9a23981 100644 --- a/src/backend/auth-token.js +++ b/src/backend/auth-token.js @@ -27,10 +27,15 @@ module.exports = async function authTokenFromIpfsId (ipfs, keys, ethereumWalletI token: ipfsToken } if (window.web3) { - peerPadEthereumSignature.signIpfsId(ipfs).then(signatureData => { - token.ethereumWalletInfo = JSON.stringify(signatureData).toString('base64') + const did = window.localStorage.getItem('DID') + if (did) { + peerPadEthereumSignature.signIpfsId(ipfs, did).then(signatureData => { + token.ethereumWalletInfo = JSON.stringify(signatureData).toString('base64') + cb(null, token) + }) + } else { cb(null, token) - }) + } } else { cb(null, token) } diff --git a/src/backend/auth.js b/src/backend/auth.js index bddc516..3503c12 100644 --- a/src/backend/auth.js +++ b/src/backend/auth.js @@ -85,8 +85,8 @@ module.exports = function Auth (keys, roomEmitter) { return resolve('read') } // authToken = JSON.parse(Buffer.from(authToken)) + console.log('authToken:', authToken) const ethereumSignatureCheck = authToken.ethereumWalletInfo && checkEthereumSignature(JSON.parse(authToken.ethereumWalletInfo), sender) - const ethereumWalletInfo = authToken.ethereumWalletInfo && JSON.parse(authToken.ethereumWalletInfo) const token = authToken.token const verifications = [checkIpfsIdAuth(token, y, sender)] @@ -96,11 +96,9 @@ module.exports = function Auth (keys, roomEmitter) { Promise.all(verifications) .then(([ipfsVerResult, ethVerResult]) => { + console.log('ethVerResult:', ethVerResult) if (ethereumSignatureCheck) { - if (!ethVerResult) { - return reject(new Error('bad ethereum signature')) - } - auth.emit('authenticatedEthereum', sender, ethereumWalletInfo) + auth.emit('authenticatedEthereum', sender, ethVerResult) } return ipfsVerResult ? resolve('write') : reject(new Error('bad signature')) diff --git a/src/peers.js b/src/peers.js index b63d911..2fd501a 100644 --- a/src/peers.js +++ b/src/peers.js @@ -63,6 +63,7 @@ class Peers extends EventEmitter { } _roomChanged () { + console.log('room changed:', this.all()) this.emit('change') }