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

Feat/rm provider #42

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
507 changes: 301 additions & 206 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@
"ipfs-repo": "~0.26.6",
"ipfsd-ctl": "~0.42.4",
"mocha": "^5.2.0",
"orbit-db": "~0.21.4",
"orbit-db-identity-provider": "~0.1.0",
"orbit-db-keystore": "~0.2.1",
"orbit-db": "orbitdb/orbit-db#feat/identities",
"orbit-db-identity-provider": "orbitdb/orbit-db-identity-provider#feat/keystore",
"orbit-db-keystore": "~0.3.3",
"standard": "^14.1.0",
"web3": "1.0.0-beta.37"
},
Expand Down
2 changes: 1 addition & 1 deletion src/access-controller-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AccessController extends EventEmitter {
be allowed in the database. Return true if the entry is allowed,
false is not allowed
*/
async canAppend (entry, identityProvider) {}
async canAppend (entry) {}

/* Add and remove access */
async grant (access, identity) { return false }
Expand Down
10 changes: 2 additions & 8 deletions src/contract-access-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,13 @@ class ContractAccessController extends AccessController {
return { address: cid }
}

async canAppend (entry, identityProvider) {
async canAppend (entry) {
// Write the custom access control logic here
if (!isValidEthAddress(this.web3, entry.identity.id)) {
console.warn(`WARNING: "${entry.identity.id}" is not a valid eth address`)
return Promise.resolve(false)
}
const isPermitted = await this.contract.methods.isPermitted(entry.identity.id, this.web3.utils.fromAscii('write')).call()
if (isPermitted) {
const verifiedIdentity = await identityProvider.verifyIdentity(entry.identity)
// Allow access if identity verifies
return Promise.resolve(verifiedIdentity)
}
return Promise.resolve(false)
return this.contract.methods.isPermitted(entry.identity.id, this.web3.utils.fromAscii('write')).call()
}

async grant (capability, identifier, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/deposit-contract-access-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class DepositContractAccessController extends AccessController {
return { address: cid }
}

async canAppend (entry, identityProvider) {
async canAppend (entry) {
// Write the custom access control logic here
if (!isValidEthAddress(this.web3, entry.identity.id)) {
console.warn(`WARNING: "${entry.identity.id}" is not a valid eth address`)
Expand Down
5 changes: 2 additions & 3 deletions src/ipfs-access-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ class IPFSAccessController extends AccessController {
return this._write
}

async canAppend (entry, identityProvider) {
async canAppend (entry) {
// Allow if access list contain the writer's publicKey or is '*'
const key = entry.identity.id
if (this.write.includes(key) || this.write.includes('*')) {
// check identity is valid
return identityProvider.verifyIdentity(entry.identity)
return true
}
return false
}
Expand Down
2 changes: 1 addition & 1 deletion src/legacy-ipfs-access-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LegacyIPFSAccessController extends AccessController {
return this._write
}

async canAppend (entry, identityProvider) {
async canAppend (entry) {
// Allow if access list contain the writer's publicKey or is '*'
const publicKey = entry.key
if (this.write.includes(publicKey) ||
Expand Down
6 changes: 2 additions & 4 deletions src/orbitdb-access-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ class OrbitDBAccessController extends AccessController {
}

// Return true if entry is allowed to be added to the database
async canAppend (entry, identityProvider) {
async canAppend (entry) {
// Write keys and admins keys are allowed
const access = new Set([...this.get('write'), ...this.get('admin')])
// If the ACL contains the writer's public key or it contains '*'
if (access.has(entry.identity.id) || access.has('*')) {
const verifiedIdentity = await identityProvider.verifyIdentity(entry.identity)
// Allow access if identity verifies
return verifiedIdentity
return true
}

return false
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ensure-ac-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const ensureAddress = address => {
const suffix = address.toString().split('/').pop()
return suffix === '_access'
? address
: path.join(address, '/_access')
: (path.posix || path).join(address, '/_access')
}
module.exports = ensureAddress
17 changes: 9 additions & 8 deletions test/access-controller-handlers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const assert = require('assert')
const rmrf = require('rimraf')
const Web3 = require('web3')
const OrbitDB = require('orbit-db')
const IdentityProvider = require('orbit-db-identity-provider')
const Keystore = require('orbit-db-keystore')
const AccessControllers = require('../')
const ContractAccessController = require('../src/contract-access-controller.js')
Expand All @@ -28,7 +27,7 @@ Object.keys(testAPIs).forEach(API => {
describe(`orbit-db - Access Controller Handlers (${API})`, function () {
this.timeout(config.timeout)

let web3, contract, ipfsd1, ipfsd2, ipfs1, ipfs2, id1, id2
let web3, contract, ipfsd1, ipfsd2, ipfs1, ipfs2
let orbitdb1, orbitdb2

before(async () => {
Expand All @@ -43,22 +42,24 @@ Object.keys(testAPIs).forEach(API => {
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api

const keystore1 = Keystore.create(dbPath1 + '/keys')
const keystore2 = Keystore.create(dbPath2 + '/keys')
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')

id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
// id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
// id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })

orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identity: id1
id: 'A',
keystore: keystore1
})

orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identity: id2
id: 'B',
keystore: keystore2
})
})

Expand Down
15 changes: 8 additions & 7 deletions test/contract-access-controller-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,11 @@ Object.keys(testAPIs).forEach(API => {
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api

const keystore1 = Keystore.create(dbPath1 + '/keys')
const keystore2 = Keystore.create(dbPath2 + '/keys')
const keystore = new Keystore(dbPath1 + '/keys')
IdentityProvider.addIdentityProvider(EthIdentityProvider)

id1 = await IdentityProvider.createIdentity({ type: 'ethereum', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ type: 'ethereum', keystore: keystore2 })
const identities = new IdentityProvider({ keystore })
id1 = await identities.createIdentity({ type: 'ethereum' })
id2 = await identities.createIdentity({ type: 'ethereum' })

web3 = new Web3(ganache.provider())
accounts = await web3.eth.getAccounts()
Expand All @@ -71,13 +70,15 @@ Object.keys(testAPIs).forEach(API => {
orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identity: id1
identity: id1,
identities
})

orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identity: id2
identity: id2,
identities
})
})

Expand Down
29 changes: 15 additions & 14 deletions test/contract-access-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,28 @@ Object.keys(testAPIs).forEach(API => {
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api

const keystore1 = Keystore.create(dbPath1 + '/keys')
const keystore2 = Keystore.create(dbPath2 + '/keys')
const keystore = new Keystore(dbPath1 + '/keys')

IdentityProvider.addIdentityProvider(EthIdentityProvider)

id1 = await IdentityProvider.createIdentity({ type: EthIdentityProvider.type, keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ type: EthIdentityProvider.type, keystore: keystore2 })
const identities = new IdentityProvider({ keystore })
id1 = await identities.createIdentity({ type: EthIdentityProvider.type })
id2 = await identities.createIdentity({ type: EthIdentityProvider.type })

web3 = new Web3(ganache.provider())
accounts = await web3.eth.getAccounts()

orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identity: id1
identity: id1,
identities
})

orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identity: id2
identity: id2,
identities
})
})

Expand Down Expand Up @@ -132,16 +133,16 @@ Object.keys(testAPIs).forEach(API => {
// doesn't matter what we put here, only identity is used for the check
}
await accessController.grant('write', id1.id)
const canAppend = await accessController.canAppend(mockEntry, id1.provider)
const canAppend = await accessController.canAppend(mockEntry)
assert.strictEqual(canAppend, true)
})

it('grants access to multiple keys', async () => {
const canAppend1 = await accessController.canAppend({ identity: orbitdb1.identity }, orbitdb1.identity.provider)
const canAppend2 = await accessController.canAppend({ identity: orbitdb2.identity }, orbitdb2.identity.provider)
const canAppend1 = await accessController.canAppend({ identity: orbitdb1.identity })
const canAppend2 = await accessController.canAppend({ identity: orbitdb2.identity })

await accessController.grant('write', orbitdb2.identity.id)
const canAppend3 = await accessController.canAppend({ identity: orbitdb2.identity }, orbitdb2.identity.provider)
const canAppend3 = await accessController.canAppend({ identity: orbitdb2.identity })

assert.strictEqual(canAppend1, true)
assert.strictEqual(canAppend2, false)
Expand Down Expand Up @@ -174,9 +175,9 @@ Object.keys(testAPIs).forEach(API => {
})

it('has correct capabalities', async () => {
const canAppend1 = await accessController.canAppend({ identity: orbitdb1.identity }, orbitdb1.identity.provider)
const canAppend2 = await accessController.canAppend({ identity: orbitdb2.identity }, orbitdb2.identity.provider)
const canAppend3 = await accessController.canAppend({ identity: { id: 'someotherid' } }, orbitdb1.identity.provider)
const canAppend1 = await accessController.canAppend({ identity: orbitdb1.identity })
const canAppend2 = await accessController.canAppend({ identity: orbitdb2.identity })
const canAppend3 = await accessController.canAppend({ identity: { id: 'someotherid' } })

assert.strictEqual(canAppend1, true)
assert.strictEqual(canAppend2, true)
Expand Down
11 changes: 6 additions & 5 deletions test/ipfs-access-controller-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,22 @@ Object.keys(testAPIs).forEach(API => {
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api

const keystore1 = Keystore.create(dbPath1 + '/keys')
const keystore2 = Keystore.create(dbPath2 + '/keys')

id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
const keystore = new Keystore(dbPath1 + '/keys')
const identities = new IdentityProvider({ keystore })
id1 = await identities.createIdentity({ id: 'A' })
id2 = await identities.createIdentity({ id: 'B' })

orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identities: identities,
identity: id1
})

orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identities: identities,
identity: id2
})
})
Expand Down
16 changes: 10 additions & 6 deletions test/ipfs-access-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,26 @@ Object.keys(testAPIs).forEach(API => {
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api

const keystore1 = Keystore.create(dbPath1 + '/keys')
const keystore2 = Keystore.create(dbPath2 + '/keys')
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')

id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
const identities1 = new IdentityProvider({ keystore: keystore1 })
const identities2 = new IdentityProvider({ keystore: keystore2 })
id1 = await identities1.createIdentity({ id: 'A' })
id2 = await identities2.createIdentity({ id: 'B' })

orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identity: id1
identity: id1,
identities: identities1
})

orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identity: id2
identity: id2,
identities: identities2
})
})

Expand Down
17 changes: 10 additions & 7 deletions test/orbit-db-access-controller-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,25 @@ Object.keys(testAPIs).forEach(API => {
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api

const keystore1 = Keystore.create(dbPath1 + '/keys')
const keystore2 = Keystore.create(dbPath2 + '/keys')

id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')
const identities1 = new IdentityProvider({ keystore: keystore1 })
const identities2 = new IdentityProvider({ keystore: keystore2 })
id1 = await identities1.createIdentity({ id: 'A' })
id2 = await identities2.createIdentity({ id: 'B' })

orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identity: id1
identity: id1,
identities: identities1
})

orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identity: id2
identity: id2,
identities: identities2
})
})

Expand Down
17 changes: 10 additions & 7 deletions test/orbit-db-access-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,25 @@ Object.keys(testAPIs).forEach(API => {
ipfs1 = ipfsd1.api
ipfs2 = ipfsd2.api

const keystore1 = Keystore.create(dbPath1 + '/keys')
const keystore2 = Keystore.create(dbPath2 + '/keys')

id1 = await IdentityProvider.createIdentity({ id: 'A', keystore: keystore1 })
id2 = await IdentityProvider.createIdentity({ id: 'B', keystore: keystore2 })
const keystore1 = new Keystore(dbPath1 + '/keys')
const keystore2 = new Keystore(dbPath2 + '/keys')
const identities1 = new IdentityProvider({ keystore: keystore1 })
const identities2 = new IdentityProvider({ keystore: keystore2 })
id1 = await identities1.createIdentity({ id: 'A' })
id2 = await identities2.createIdentity({ id: 'B' })

orbitdb1 = await OrbitDB.createInstance(ipfs1, {
AccessControllers: AccessControllers,
directory: dbPath1,
identity: id1
identity: id1,
identities: identities1
})

orbitdb2 = await OrbitDB.createInstance(ipfs2, {
AccessControllers: AccessControllers,
directory: dbPath2,
identity: id2
identity: id2,
identities: identities2
})
})

Expand Down