This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
orbitdb-access-controller.js
132 lines (107 loc) · 3.6 KB
/
orbitdb-access-controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict'
const pMapSeries = require('p-map-series')
const AccessController = require('./access-controller-interface')
const ensureAddress = require('./utils/ensure-ac-address')
const type = 'orbitdb'
class OrbitDBAccessController extends AccessController {
constructor (orbitdb, options) {
super()
this._orbitdb = orbitdb
this._db = null
this._options = options || {}
}
// Returns the type of the access controller
static get type () { return type }
// Returns the address of the OrbitDB used as the AC
get address () {
return this._db.address
}
// Return true if entry is allowed to be added to the database
async canAppend (entry, identityProvider) {
// 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 false
}
get capabilities () {
if (this._db) {
const capabilities = this._db.index
const toSet = (e) => {
const key = e[0]
capabilities[key] = new Set([...(capabilities[key] || []), ...e[1]])
}
// Merge with the access controller of the database
// and make sure all values are Sets
Object.entries({
...capabilities,
// Add the root access controller's 'write' access list
// as admins on this controller
...{ admin: new Set([...(capabilities.admin || []), ...this._db.access.write]) }
}).forEach(toSet)
return capabilities
}
return {}
}
get (capability) {
return this.capabilities[capability] || new Set([])
}
async close () {
await this._db.close()
}
async load (address) {
if (this._db) { await this._db.close() }
// Force '<address>/_access' naming for the database
this._db = await this._orbitdb.keyvalue(ensureAddress(address), {
// use ipfs controller as a immutable "root controller"
accessController: {
type: 'ipfs',
write: this._options.admin || [this._orbitdb.identity.id]
},
sync: true
})
this._db.events.on('ready', this._onUpdate.bind(this))
this._db.events.on('write', this._onUpdate.bind(this))
this._db.events.on('replicated', this._onUpdate.bind(this))
await this._db.load()
}
async save () {
// return the manifest data
return {
address: this._db.address.toString()
}
}
async grant (capability, key) {
// Merge current keys with the new key
const capabilities = new Set([...(this._db.get(capability) || []), ...[key]])
await this._db.put(capability, Array.from(capabilities.values()))
}
async revoke (capability, key) {
const capabilities = new Set(this._db.get(capability) || [])
capabilities.delete(key)
if (capabilities.size > 0) {
await this._db.put(capability, Array.from(capabilities.values()))
} else {
await this._db.del(capability)
}
}
/* Private methods */
_onUpdate () {
this.emit('updated')
}
/* Factory */
static async create (orbitdb, options = {}) {
const ac = new OrbitDBAccessController(orbitdb, options)
await ac.load(options.address || options.name || 'default-access-controller')
// Add write access from options
if (options.write && !options.address) {
await pMapSeries(options.write, async (e) => ac.grant('write', e))
}
return ac
}
}
module.exports = OrbitDBAccessController