This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
_auth.js
397 lines (363 loc) · 13.3 KB
/
_auth.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Copyright 2018 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under
// the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT> or
// the Modified BSD license <LICENSE-BSD or https://opensource.org/licenses/BSD-3-Clause>,
// at your option.
//
// This file may not be copied, modified, or distributed except according to those terms.
//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.
const ffi = require('ffi');
const Enum = require('enum');
const ArrayType = require('ref-array');
const ref = require('ref');
const Struct = require('ref-struct');
const base = require('./_base.js');
const makeError = require('./_error.js');
const { helpersForNative, types: MDtypes } = require('./_mutable.js');
const readMDataInfoPtr = helpersForNative.readMDataInfoPtr;
const MDataInfo = MDtypes.MDataInfo;
const MDataInfoPtr = MDtypes.MDataInfoPtr;
const t = base.types;
const AppPtr = t.AppPtr;
const helpers = base.helpers;
const AppExchangeInfo = Struct({
id: 'string',
scope: 'string',
name: 'string',
vendor: 'string'
});
const ContainerPermissions = Struct({
cont_name: 'string',
access: t.PermissionSet
});
const ContainerPermissionsArray = new ArrayType(ContainerPermissions);
/// For use in `ShareMDataReq`. Represents a specific `MutableData` that is being shared.
const ShareMData = Struct({
type_tag: t.u64,
name: t.XOR_NAME,
access: t.PermissionSet
});
const ShareMDataArray = new ArrayType(ShareMData);
const AuthReq = Struct({
app: AppExchangeInfo,
app_container: ref.types.bool,
containers: ContainerPermissionsArray,
containers_len: t.usize,
containers_cap: t.usize
});
const ContainerReq = Struct({
app: AppExchangeInfo,
containers: ContainerPermissionsArray,
containers_len: t.usize,
containers_cap: t.usize
});
/// Represents a request to share mutable data
const ShareMDataReq = Struct({
/// Info about the app requesting shared access
app: AppExchangeInfo,
/// List of MD names & type tags and permissions that need to be shared
mdata: ShareMDataArray,
/// Length of the mdata array
mdata_len: t.usize,
});
const AppKeys = Struct({
/// Owner signing public key
// pub owner_key: [u8; sign::PUBLICKEYBYTES],
owner_key: t.SIGN_PUBLICKEYBYTES,
/// Data symmetric encryption key
// pub enc_key: [u8; secretbox::KEYBYTES],
enc_key: t.SYM_KEYBYTES,
/// Asymmetric sign public key.
/// This is the identity of the App in the Network.
// pub sign_pk: [u8; sign::PUBLICKEYBYTES],
sign_pk: t.SIGN_PUBLICKEYBYTES,
/// Asymmetric sign private key.
// pub sign_sk: [u8; sign::SECRETKEYBYTES],
sign_sk: t.SIGN_SECRETKEYBYTES,
/// Asymmetric enc public key.
// pub enc_pk: [u8; box_::PUBLICKEYBYTES],
enc_pk: t.ASYM_PUBLICKEYBYTES,
/// Asymmetric enc private key.
// pub enc_sk: [u8; box_::SECRETKEYBYTES],
enc_sk: t.ASYM_SECRETKEYBYTES
})
const AccessContInfo = Struct({
/// ID
// pub id: [u8; XOR_NAME_LEN],
id: t.XOR_NAME,
/// Type tag
// pub tag: u64,
tag: t.u64,
/// Nonce
// pub nonce: [u8; secretbox::NONCEBYTES],
nonce: t.SYM_NONCEBYTES
});
const ContainerInfo = Struct({
/// Container name as UTF-8 encoded null-terminated string.
name: t.CString,
/// Container's `MDataInfo`
mdata_info: MDataInfo,
/// App's permissions in the container.
permissions: t.PermissionSet
});
const ContainerInfoArray = new ArrayType(ContainerInfo);
const AccessContainerEntry = Struct({
/// Pointer to the array of `ContainerInfo`.
containers: ref.refType(ContainerInfo),
/// Size of the array.
containers_len: t.usize,
/// Internal field used by rust memory allocator.
containers_cap: t.usize
});
const AuthGranted = Struct({
/// The access keys.
app_keys: AppKeys,
/// Access container info
access_container: AccessContInfo,
/// Access container entry
access_container_entry: AccessContainerEntry,
/// Crust's bootstrap config
bootstrap_config_ptr: t.u8Pointer,
/// `bootstrap_config`'s length
bootstrap_config_len: t.usize,
/// Used by Rust memory allocator
bootstrap_config_cap: t.usize
});
const toBuffer = (ptr, len) => {
return Buffer.from(ref.reinterpret(ptr, len, 0))
}
const makeAppKeys = (appKeys) => {
return new AppKeys({
owner_key: new t.SIGN_PUBLICKEYBYTES(appKeys.owner_key),
enc_key: new t.SYM_KEYBYTES(appKeys.enc_key),
sign_pk: new t.SIGN_PUBLICKEYBYTES(appKeys.sign_pk),
sign_sk: new t.SIGN_SECRETKEYBYTES(appKeys.sign_sk),
enc_pk: new t.ASYM_PUBLICKEYBYTES(appKeys.enc_pk),
enc_sk: new t.ASYM_SECRETKEYBYTES(appKeys.enc_sk),
});
}
const makeAccessContInfo = (accessContainer) => {
return new AccessContInfo({
id: new t.XOR_NAME(accessContainer.id),
tag: accessContainer.tag,
nonce: new t.SYM_NONCEBYTES(accessContainer.nonce),
});
}
const makeAccessContainerEntry = (accessContainerEntry) => {
const accessContainerEntryCache = {
containerInfoArrayCache: null,
containerInfoCaches: [],
};
accessContainerEntryCache.containerInfoArrayCache = new ContainerInfoArray(
accessContainerEntry.map((entry) => {
const permissions = new t.PermissionSet({
Read: entry.permissions.Read,
Insert: entry.permissions.Insert,
Update: entry.permissions.Update,
Delete: entry.permissions.Delete,
ManagePermissions: entry.permissions.ManagePermissions
});
const containerInfo = {
name: ref.allocCString(entry.name),
mdata_info: helpersForNative.makeMDataInfo(entry.mdata_info),
permissions
};
accessContainerEntryCache.containerInfoCaches.push(containerInfo);
return new ContainerInfo(containerInfo);
}));
return {
accessContainerEntry: new AccessContainerEntry({
containers: accessContainerEntryCache.containerInfoArrayCache.buffer,
containers_len: accessContainerEntryCache.containerInfoArrayCache.length
}),
accessContainerEntryCache,
};
}
const makeAuthGrantedFfiStruct = (authGrantedObj) => {
const accessContainerEntry = makeAccessContainerEntry(authGrantedObj.access_container_entry);
return {
authGranted: new AuthGranted({
app_keys: makeAppKeys(authGrantedObj.app_keys),
access_container: makeAccessContInfo(authGrantedObj.access_container),
access_container_entry: accessContainerEntry.accessContainerEntry,
bootstrap_config_ptr: Buffer.from(authGrantedObj.bootstrap_config),
bootstrap_config_len: authGrantedObj.bootstrap_config.length,
}),
authGrantedCache: accessContainerEntry.accessContainerEntryCache,
};
}
const readAppKeys = (appKeys) => {
return {
owner_key: Buffer.from(appKeys.owner_key),
enc_key: Buffer.from(appKeys.enc_key),
sign_pk: Buffer.from(appKeys.sign_pk),
sign_sk: Buffer.from(appKeys.sign_sk),
enc_pk: Buffer.from(appKeys.enc_pk),
enc_sk: Buffer.from(appKeys.enc_sk),
};
}
const readAccessContainer = (accessContainer) => {
return {
id: Buffer.from(accessContainer.id),
tag: accessContainer.tag,
nonce: Buffer.from(accessContainer.nonce),
};
}
const readAccessContainerEntry = (accessContainerEntry) => {
let ptr = accessContainerEntry.containers;
const len = accessContainerEntry.containers_len;
const arrPtr = ref.reinterpret(ptr, ContainerInfo.size * len);
const arr = ContainerInfoArray(arrPtr);
let containersList = [];
for (let i = 0; i < len ; i++) {
const currValue = arr[i];
containersList.push({
name: currValue.name,
mdata_info: helpersForNative.makeMDataInfoObj(currValue.mdata_info),
permissions: helpersForNative.readPermsSet(currValue.permissions),
});
}
return containersList;
}
const readAuthGrantedPtr = (authGrantedPtr) => {
const authGranted = authGrantedPtr.deref();
const authGrantedObj = {
app_keys: readAppKeys(authGranted.app_keys),
access_container: readAccessContainer(authGranted.access_container),
access_container_entry: readAccessContainerEntry(authGranted.access_container_entry),
bootstrap_config: toBuffer(authGranted.bootstrap_config_ptr, authGranted.bootstrap_config_len)
}
return authGrantedObj;
}
function makeAppInfo(appInfoObj) {
return new AppExchangeInfo({
id: appInfoObj.id,
scope: appInfoObj.scope || ref.NULL,
name: appInfoObj.name,
vendor: appInfoObj.vendor
});
}
const translateXorName = (str) => {
const b = Buffer.from(str);
return t.XOR_NAME(b);
}
const makePermissions = (perms) => {
if (!perms) return new ContainerPermissionsArray([]);
return new ContainerPermissionsArray(Object.getOwnPropertyNames(perms).map((key) => {
const permArray = helpers.makePermissionSet(perms[key]);
return ContainerPermissions({
cont_name: key,
access: permArray
});
}));
}
const makeShareMDataPermissions = (permissions) => {
return new ShareMDataArray(permissions.map((perm) => {
const permsArray = helpers.makePermissionSet(perm.perms);
return ShareMData({
type_tag: perm.typeTag,
name: translateXorName(perm.name),
access: permsArray
});
}));
}
const remapEncodeValues = (resp) => {
return {
'id': resp[0],
'uri': helpers.fromCString(resp[1])
}
}
module.exports = {
types: {
// request
AuthReq,
ContainerReq,
ShareMDataReq,
// response
AuthGranted,
},
helpersForNative: {
makeAuthGrantedFfiStruct,
},
functions: {
encode_auth_req: [t.Void, [ ref.refType(AuthReq), 'pointer', 'pointer'] ],
encode_containers_req: [t.Void, [ref.refType(ContainerReq), 'pointer', 'pointer'] ],
encode_share_mdata_req: [t.Void, [ref.refType(ShareMDataReq), 'pointer', 'pointer'] ],
encode_unregistered_req: [t.Void, [t.u8Pointer, t.usize, 'pointer', 'pointer'] ],
decode_ipc_msg: [t.Void, [
'string', // (msg: *const c_char,
t.VoidPtr, // user_data: *mut c_void,
'pointer', // o_auth: extern "C" fn(*mut c_void, u32, FfiAuthGranted),
'pointer', // o_unregistered: extern "C" fn(*mut c_void, u32, *const u8, usize),
'pointer', // o_containers: extern "C" fn(*mut c_void, u32),
'pointer', // o_share_mdata: extern "C" fn(*mut c_void, u32),
'pointer', // o_revoked: extern "C" fn(*mut c_void),
'pointer' // o_err: extern "C" fn(*mut c_void, i32, u32)
] ],
access_container_fetch: [t.Void, [AppPtr, 'pointer', 'pointer']],
access_container_refresh_access_info: [t.Void, [AppPtr, 'pointer', 'pointer']],
access_container_get_container_mdata_info: [t.Void, [AppPtr, 'string', 'pointer', 'pointer']],
},
helpers: {
makeAppInfo,
makePermissions,
makeShareMDataPermissions,
},
api: {
access_container_fetch: helpers.Promisified(null, [ref.refType(ContainerPermissions), t.usize], (args) => {
const ptr = args[0];
const len = args[1];
if(len === 0) return {};
const arrPtr = ref.reinterpret(ptr, ContainerPermissions.size * len);
const arr = ContainerPermissionsArray(arrPtr);
const contsPerms = {};
for (let i = 0; i < len ; i++) {
const cont = arr[i];
contsPerms[cont.cont_name] = helpersForNative.readPermsSet(cont.access);
}
return contsPerms;
}),
access_container_refresh_access_info: helpers.Promisified(),
access_container_get_container_mdata_info: helpers.Promisified(null, MDataInfoPtr, readMDataInfoPtr),
encode_containers_req: helpers.Promisified(null, ['uint32', 'char *'], remapEncodeValues),
encode_auth_req: helpers.Promisified(null, ['uint32', 'char *'], remapEncodeValues),
encode_share_mdata_req: helpers.Promisified(null, ['uint32', 'char *'], remapEncodeValues),
encode_unregistered_req: helpers.Promisified((appId) => {
let str = Buffer.from(appId);
return [str, str.length];
}, ['uint32', 'char *'], remapEncodeValues),
decode_ipc_msg: (lib, fn) => {
return ((str) => {
return new Promise((resolve, reject) => {
fn.async(str,
ref.NULL,
ffi.Callback('void', [t.VoidPtr, 'uint32', ref.refType(AuthGranted)], (user_data, req_id, authGrantedPtr) => {
const authGrantedObj = readAuthGrantedPtr(authGrantedPtr);
resolve(["granted", authGrantedObj])
}),
ffi.Callback('void', [t.VoidPtr, 'uint32', t.u8Pointer, t.usize], function(user_data, req_id, connUriPtr, connUriLen) {
resolve(["unregistered", toBuffer(connUriPtr, connUriLen)])
}),
ffi.Callback('void', [t.VoidPtr, 'uint32'], (user_data, req_id) => {
resolve(["containers", req_id])
}),
ffi.Callback('void', [t.VoidPtr, 'uint32'], (user_data, req_id) => {
resolve(["share_mdata", req_id])
}),
ffi.Callback('void', [t.VoidPtr], (user_data) => {
resolve(["revoked"])
}),
ffi.Callback('void', [t.VoidPtr, t.FfiResultPtr, 'uint32'], (user_data, resultPtr, req_id) => {
const result = helpers.makeFfiResult(resultPtr);
reject(makeError(result.error_code, result.error_description))
}),
() => {}
)
})
})
}
}
}