Skip to content
This repository has been archived by the owner on Sep 18, 2020. It is now read-only.

Commit

Permalink
Sync and authorize other devices apps
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Ray committed Jul 11, 2019
1 parent 29cecdd commit 0085be8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 23 deletions.
56 changes: 47 additions & 9 deletions src/lib/masq.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ const requiredParametersProfile = [
* Open or create a hyperdb instance
* @param {string} name The indexeddb store name
*/
const openOrCreateDB = (name) => {
return createPromisifiedHyperDB(name)
const openOrCreateDB = (name, key = null) => {
return createPromisifiedHyperDB(name, key)
}

const dispatchMasqError = (errorCode) => {
Expand Down Expand Up @@ -153,7 +153,11 @@ class Masq {
const dbName = `app-${profileId}-${app.id}`
const db = openOrCreateDB(dbName)
this.appsDBs[dbName] = db
db.on('ready', () => this._startReplicate(db))
db.on('ready', () => {
const discoveryKey = db.discoveryKey.toString('hex')
this.appsDBs[discoveryKey] = db
this._startReplicate(db)
})
})

const profile = await this.getAndDecrypt('/profile')
Expand All @@ -164,10 +168,13 @@ class Masq {
if (!device) {
const { name, os } = DetectBrowser.detect()
await this.addDevice({
name: `${capitalize(name)} - ${os}`
name: `${capitalize(name)} - ${os}`,
apps: []
})
}

this._watchAndAuthorizeApps()

return profile
}

Expand Down Expand Up @@ -360,6 +367,7 @@ class Masq {
checkObject(app, requiredParametersApp)
const dbName = `app-${this.profileId}-${app.id}`
const discoveryKey = this.appsDBs[dbName].discoveryKey.toString('hex')
// missing key
const sw = this.swarms[discoveryKey]
sw.close()
window.indexedDB.deleteDatabase(dbName)
Expand All @@ -376,7 +384,8 @@ class Masq {
await this.encryptAndPut(`/devices/${id}`, {
id,
...device,
localKey: this.profileDB.local.key.toString('hex')
localKey: this.profileDB.local.key.toString('hex'),
apps: []
})
}

Expand Down Expand Up @@ -632,17 +641,46 @@ class Masq {
peer.send(JSON.stringify(encryptedMsg))
}

_createDBAndSyncApp (dbName) {
_watchAndAuthorizeApps () {
watch(this.profileDB, this.nonce, '/devices', async () => {
const allDevices = await this.getDevices()
const otherDevices = allDevices.filter(d => {
return d.localKey !== this.profileDB.local.key.toString('hex')
})

for (let otherDevice of otherDevices) {
for (let app of otherDevice.apps) {
if (!this.appsDBs[app.discoveryKey]) {
// Add app
continue
}

const keyBuf = Buffer.from(app.localKey, 'hex')

if (await this.appsDBs[app.discoveryKey].authorizedAsync(keyBuf)) {
} else {
await this.appsDBs[app.discoveryKey].authorizeAsync(keyBuf)
}
}
}
})
}

_createDBAndSyncApp (dbName, hex = null) {
return new Promise((resolve, reject) => {
const db = openOrCreateDB(dbName)
const db = openOrCreateDB(dbName, hex)
this.appsDBs[dbName] = db
db.on('ready', async () => {
const discoveryKey = db.discoveryKey.toString('hex')
this.appsDBs[discoveryKey] = db
const device = await this.getDevice()
await this.updateDevice({
...device,
apps: [{
apps: [...device.apps, {
id: dbName,
localKey: db.local.key
key: db.key.toString('hex'),
discoveryKey: db.discoveryKey.toString('hex'),
localKey: db.local.key.toString('hex')
}]
})
this._startReplicate(db)
Expand Down
26 changes: 18 additions & 8 deletions src/lib/sync-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ class SyncProfile {
await this.masq.openProfile(id, 'pass')

await this.masq.addDevice({
name: 'new device'
name: 'new device',
apps: []
})

// We have now the profile synced, stop replication.
Expand Down Expand Up @@ -111,14 +112,23 @@ class SyncProfile {
await sendEncryptedJSON({ msg: 'writeAccessGranted' }, this.key, this.peer)
}

async pullApps (masq) {
console.log('pullApps')
// const apps = await masq.getApps()
async pullApps (masq, prefix = '') {
const device = await masq.getDevice()
const allDevices = await masq.getDevices()
const otherDevices = allDevices.filter(d => {
return d.localKey !== masq.profileDB.local.key.toString('hex')
})

// this.db = await createPromisifiedHyperDB('profile-' + id, key)
// await dbReady(this.db)
// Start to replicate the profile
// this.masq._startReplicate(this.db)
for (let otherDevice of otherDevices) {
// d.apps, copy key, and add own localKey
const newApps = otherDevice.apps.filter(({ key }) => {
return !(device.apps.find(app => app.key === key))
})

for (let newApp of newApps) {
await masq._createDBAndSyncApp(newApp.id + prefix, newApp.key)
}
}
}
}

Expand Down
45 changes: 39 additions & 6 deletions test/sync-profile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ describe('sync-profile', function () {
// we need to save a device
await masq.addDevice({ name: 'device test' })
await masq.addApp(app)

await masq._createDBAndSyncApp('app')

const devicesBeforeSync = await masq.getDevices()
Expand Down Expand Up @@ -139,12 +138,46 @@ describe('sync-profile', function () {
expect(devices[1].localKey).to.have.lengthOf(64)
expect(devices[0].localKey).to.not.equal(devices[1].localKey)

// expect(devices[1].apps).to.have.lengthOf(1)
let device1 = await masq.getDevice()
expect(device1.apps).to.have.lengthOf(1)
expect(device1.apps[0].id).to.exist
expect(device1.apps[0].key).to.have.lengthOf(64)
expect(device1.apps[0].localKey).to.have.lengthOf(64)
expect(device1.apps[0].localKey).to.equal(device1.apps[0].key)

// await masq.addApp({ name: 'new app', description: 'test', appId: 'id' })
})
expect(masq.appsDBs[device1.apps[0].id]).to.exist
expect(masq.appsDBs[device1.apps[0].id]._authorized).to.have.lengthOf(1)

await sp2.pullApps(masq2, '-copy')

let device2 = await masq2.getDevice()
expect(device2.apps).to.have.lengthOf(1)
expect(device2.apps[0].id).to.exist
expect(device2.apps[0].key).to.have.lengthOf(64)
expect(device2.apps[0].localKey).to.have.lengthOf(64)
expect(device2.apps[0].localKey).to.not.equal(device2.apps[0].key)

expect(masq2.appsDBs[device2.apps[0].id]).to.exist

await new Promise((resolve) => { setTimeout(resolve, 5000) })

// it('should pull apps', async () => {
const appid = device1.apps[0].id
expect(masq.appsDBs[appid]._authorized).to.have.lengthOf(2)
expect(masq2.appsDBs[appid + '-copy']._authorized).to.have.lengthOf(2)

// })
await masq2._createDBAndSyncApp('app2')
await waitForSync(masq2.profileDB, masq.profileDB)

await sp1.pullApps(masq, '-copy')

device1 = await masq.getDevice()
device2 = await masq2.getDevice()
expect(device1.apps).to.have.lengthOf(2)
expect(device2.apps).to.have.lengthOf(2)

await new Promise((resolve) => { setTimeout(resolve, 5000) })

expect(masq.appsDBs['app2' + '-copy']._authorized).to.have.lengthOf(2)
expect(masq2.appsDBs['app2']._authorized).to.have.lengthOf(2)
})
})

0 comments on commit 0085be8

Please sign in to comment.