-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-gateway-config.json
474 lines (432 loc) · 27.8 KB
/
sync-gateway-config.json
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
{
"SSLCert": "/etc/ssl/private/cloud.openmoney.cc.sync.crt",
"SSLKey": "/etc/ssl/private/cloud.openmoney.cc.key.insecure",
"CORS": {
"Origin": ["https://cloud.openmoney.cc"],
"LoginOrigin":["https://cloud.openmoney.cc"],
"Headers": ["Accept","DNT","X-Mx-ReqToken","Keep-Alive","User-Agent","X-Requested-With","If-Modified-Since","Cache-Control","Content-Type","Authorization"],
"MaxAge": 1728000
},
"interface":":4984",
"adminInterface":":4985",
"log":["CRUD", "CRUD+", "HTTP", "HTTP+", "Access", "Cache", "Shadow", "Shadow+", "Changes", "Changes+"],
"databases":{
"openmoney_shadow":{
"server": "http://cloud.openmoney.cc:8091",
"bucket": "openmoney_shadow",
"shadow": {
"server": "http://cloud.openmoney.cc:8091",
"bucket": "openmoney"},
"sync":`function(doc, olddoc) {
// attach the .equals method to Array's prototype to call it on any array
Array.prototype.equals = function (array) {
// if the other array is a falsy value, return
if (!array)
return false;
// compare lengths - can save a lot of time
if (this.length != array.length)
return false;
for (var i = 0, l=this.length; i < l; i++) {
// Check if we have nested arrays
if (this[i] instanceof Array && array[i] instanceof Array) {
// recurse into the nested arrays
if (!this[i].equals(array[i]))
return false;
}
else if (this[i] != array[i]) {
// Warning - two different object instances will never be equal: {x:20} != {x:20}
return false;
}
}
return true;
}
function base64_decode(data) {
// discuss at: http://phpjs.org/functions/base64_decode/
// original by: Tyler Akins (http://rumkin.com)
// improved by: Thunder.m
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// input by: Aman Gupta
// input by: Brett Zamir (http://brett-zamir.me)
// bugfixed by: Onno Marsman
// bugfixed by: Pellentesque Malesuada
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
// returns 1: 'Kevin van Zonneveld'
// example 2: base64_decode('YQ===');
// returns 2: 'a'
var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
ac = 0,
dec = '',
tmp_arr = [];
if (!data) {
return data;
}
data += '';
do { // unpack four hexets into three octets using index points in b64
h1 = b64.indexOf(data.charAt(i++));
h2 = b64.indexOf(data.charAt(i++));
h3 = b64.indexOf(data.charAt(i++));
h4 = b64.indexOf(data.charAt(i++));
bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
o1 = bits >> 16 & 0xff;
o2 = bits >> 8 & 0xff;
o3 = bits & 0xff;
if (h3 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1);
} else if (h4 == 64) {
tmp_arr[ac++] = String.fromCharCode(o1, o2);
} else {
tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
}
} while (i < data.length);
dec = tmp_arr.join('');
return dec
}
function base64_encode(data) {
// discuss at: http://phpjs.org/functions/base64_encode/
// original by: Tyler Akins (http://rumkin.com)
// improved by: Bayron Guevara
// improved by: Thunder.m
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// improved by: Rafał Kukawski (http://kukawski.pl)
// bugfixed by: Pellentesque Malesuada
// example 1: base64_encode('Kevin van Zonneveld');
// returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
// example 2: base64_encode('a');
// returns 2: 'YQ=='
var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
ac = 0,
enc = '',
tmp_arr = [];
if (!data) {
return data;
}
do { // pack three octets into four hexets
o1 = data.charCodeAt(i++);
o2 = data.charCodeAt(i++);
o3 = data.charCodeAt(i++);
bits = o1 << 16 | o2 << 8 | o3;
h1 = bits >> 18 & 0x3f;
h2 = bits >> 12 & 0x3f;
h3 = bits >> 6 & 0x3f;
h4 = bits & 0x3f;
// use hexets to index into b64, and append result to encoded string
tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
} while (i < data.length);
enc = tmp_arr.join('');
var r = data.length % 3;
return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);
}
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
var channelDelimiter = "@";
if (doc._id != null) {
var table = doc._id.substring(0,doc._id.indexOf(","));
var index = doc._id.substring(doc._id.indexOf(",")+1);
if ( (olddoc != null && olddoc.type == "users" ) || (olddoc == null && doc.type == "users") ) {
if (olddoc == null) {
if (!doc.username || !doc.password || !doc.password_encryption_algorithm || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
channel(doc.type + channelDelimiter + doc.username);
access(doc.username, doc.type + channelDelimiter + doc.username);
} else {
if (doc._deleted) {
requireUser(olddoc.username);
channel(olddoc.type + channelDelimiter + olddoc.username);
access(olddoc.username, olddoc.type + channelDelimiter + olddoc.username);
} else {
if (!doc.username || !doc.password || !doc.password_encryption_algorithm || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
if (olddoc.username != doc.username) {
throw({forbidden: "Cannot change username! Delete first then create new user!"});
}
requireUser(olddoc.username);
channel(doc.type + channelDelimiter + doc.username);
access(doc.username, doc.type + channelDelimiter + doc.username);
}
}
} else if ( (olddoc != null && olddoc.type == "trading_name") || (olddoc == null && doc.type == "trading_name") ) {
var currency = base64_encode( doc.currency.toLowerCase() );
if (olddoc == null) {
if (!doc.steward || !doc.trading_name || !doc.name || !doc.currency || !doc.created ) {
throw( { forbidden: "Missing required properties" } );
}
channel(doc.type + channelDelimiter + doc.name.toLowerCase() + channelDelimiter + currency);
channel("trading_name_steward" + channelDelimiter + doc.name.toLowerCase() + channelDelimiter + currency);
channel("currency" + channelDelimiter + currency);
access( doc.steward , doc.type + channelDelimiter + doc.name.toLowerCase() + channelDelimiter + currency )
access( doc.steward , "trading_name_steward" + channelDelimiter + doc.name.toLowerCase() + channelDelimiter + currency )
} else {
currency = base64_encode( olddoc.currency.toLowerCase() );
if (doc._deleted) {
requireUser(olddoc.steward);
channel(olddoc.type + channelDelimiter + olddoc.name.toLowerCase() + channelDelimiter + currency);
channel("trading_name_steward" + channelDelimiter + olddoc.name.toLowerCase() + channelDelimiter + currency);
channel("currency" + channelDelimiter + currency);
access( olddoc.steward , olddoc.type + channelDelimiter + olddoc.name.toLowerCase() + channelDelimiter + currency );
access( olddoc.steward , "trading_name_steward" + channelDelimiter + olddoc.name.toLowerCase() + channelDelimiter + currency );
} else {
if (!doc.steward || !doc.name || !doc.trading_name || !doc.currency || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
if (doc.trading_name != olddoc.trading_name || doc.name != olddoc.name || doc.currency != olddoc.currency || doc.created != olddoc.created) {
throw({forbidden: "unmutable properties"});
}
if (! doc.steward.equals( olddoc.steward ) ) {
requireUser(olddoc.steward);
}
channel(olddoc.type + channelDelimiter + olddoc.name.toLowerCase() + channelDelimiter + currency);
channel("trading_name_steward" + channelDelimiter + olddoc.name.toLowerCase() + channelDelimiter + currency);
channel("currency" + channelDelimiter + currency);
access( olddoc.steward , olddoc.type + channelDelimiter + olddoc.name.toLowerCase() + channelDelimiter + currency );
access( olddoc.steward , "trading_name_steward" + channelDelimiter + olddoc.name.toLowerCase() + channelDelimiter + currency);
}
}
} else if ( (olddoc != null && olddoc.type == "trading_name_view" ) || (olddoc == null && doc.type == "trading_name_view") ) {
var currency = base64_encode( doc.currency.toLowerCase() );
if (olddoc == null) {
if (!doc.steward || !doc.trading_name || !doc.currency || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
doc.steward.forEach(function (steward) {
channel(doc.type + channelDelimiter + steward + channelDelimiter + doc.trading_name.toLowerCase() + channelDelimiter + currency);
access(steward, doc.type + channelDelimiter + steward + channelDelimiter + doc.trading_name.toLowerCase() + channelDelimiter + currency);
access(steward, "trading_name" + channelDelimiter + doc.trading_name.toLowerCase() + channelDelimiter + currency);
})
} else {
currency = base64_encode( olddoc.currency.toLowerCase() );
if (doc._deleted) {
requireUser(olddoc.steward);
channel(olddoc.type + channelDelimiter + olddoc.trading_name.toLowerCase() + channelDelimiter + currency);
access(olddoc.steward, olddoc.type + channelDelimiter + olddoc.trading_name.toLowerCase() + channelDelimiter + currency);
olddoc.steward.forEach(function (steward) {
channel(olddoc.type + channelDelimiter + steward + channelDelimiter + olddoc.trading_name.toLowerCase() + channelDelimiter + currency);
access(steward, olddoc.type + channelDelimiter + steward + channelDelimiter + olddoc.trading_name.toLowerCase() + channelDelimiter + currency);
access(steward, "trading_name" + channelDelimiter + olddoc.trading_name.toLowerCase() + channelDelimiter + currency);
})
} else {
if (!doc.steward || !doc.trading_name || !doc.currency || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
if (doc.currency != olddoc.currency || doc.created != olddoc.created || doc.trading_name != olddoc.trading_name) {
throw({forbidden: "unmutable properties"});
}
if (! doc.steward.equals( olddoc.steward ) ) {
requireUser(olddoc.steward);
}
olddoc.steward.forEach(function (steward) {
channel(olddoc.type + channelDelimiter + steward + channelDelimiter + olddoc.trading_name.toLowerCase() + channelDelimiter + currency);
access(steward, olddoc.type + channelDelimiter + steward + channelDelimiter + olddoc.trading_name.toLowerCase() + channelDelimiter + currency);
access(steward, "trading_name" + channelDelimiter + olddoc.trading_name.toLowerCase() + channelDelimiter + currency);
})
}
}
} else if ( (olddoc != null && olddoc.type == "space" ) || (olddoc == null && doc.type == "space") ) {
var space = doc.space;
if (olddoc == null) {
if (!doc.steward || typeof doc.space == 'undefined' || !doc.created) {
throw({forbidden: "Missing required properties"});
}
channel(doc.type + channelDelimiter + doc.space.toLowerCase());
access(doc.steward, doc.type + channelDelimiter + doc.space.toLowerCase());
} else {
if (doc._deleted) {
requireUser(olddoc.steward);
channel(olddoc.type + channelDelimiter + olddoc.space.toLowerCase());
access(olddoc.steward, olddoc.type + channelDelimiter + olddoc.space.toLowerCase());
} else {
if (!doc.steward || typeof doc.space == 'undefined' || !doc.created) {
throw({forbidden: "Missing required properties"});
}
if (doc.space != olddoc.space || doc.created != olddoc.created){
throw({forbidden: "unmutable properties"});
}
if (! doc.steward.equals( olddoc.steward ) ) {
requireUser(olddoc.steward);
}
channel(doc.type + channelDelimiter + doc.space.toLowerCase());
access(doc.steward, doc.type + channelDelimiter + doc.space.toLowerCase());
}
}
} else if ( (olddoc != null && olddoc.type == "space_view" ) || (olddoc == null && doc.type == "space_view") ) {
var space = doc.space.toLowerCase();
if (olddoc == null) {
if (!doc.steward || !doc.space || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
channel(doc.type + channelDelimiter + space);
access(doc.steward, doc.type + channelDelimiter + space);
//access(doc.steward, "space" + channelDelimiter + space);
} else {
space = olddoc.space.toLowerCase();
if (doc._deleted) {
requireUser(olddoc.steward);
channel(olddoc.type + channelDelimiter + space);
//access(olddoc.steward, olddoc.type + channelDelimiter + space);
} else {
if (!doc.steward || !doc.currency || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
if (doc.space != olddoc.space || doc.created != olddoc.created) {
throw({forbidden: "unmutable properties"});
}
if (! doc.steward.equals( olddoc.steward ) ) {
requireUser(olddoc.steward);
}
channel(olddoc.type + channelDelimiter + space);
access(doc.steward, olddoc.type + channelDelimiter + space);
//access(doc.steward, "space" + channelDelimiter + space);
}
}
} else if ( (olddoc != null && olddoc.type == "currency") || (olddoc == null && doc.type == "currency") ) {
var currency = base64_encode( doc.currency.toLowerCase() );
if (olddoc == null) {
if (!doc.steward || !doc.currency || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
channel(doc.type + channelDelimiter + currency);
access(doc.steward, doc.type + channelDelimiter + currency);
} else {
if (doc._deleted) {
requireUser(olddoc.steward);
channel(olddoc.type + channelDelimiter + currency);
access(olddoc.steward, olddoc.type + channelDelimiter + currency);
} else {
if (!doc.steward || !doc.currency || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
if (doc.currency != olddoc.currency || doc.created != olddoc.created) {
throw({forbidden: "unmutable properties"});
}
if (! doc.steward.equals( olddoc.steward ) ) {
requireUser(olddoc.steward);
}
channel(olddoc.type + channelDelimiter + currency);
access(doc.steward, olddoc.type + channelDelimiter + currency);
}
}
} else if ( (olddoc != null && olddoc.type == "currency_view" ) || (olddoc == null && doc.type == "currency_view") ) {
var currency = base64_encode( doc.currency.toLowerCase() );
if (olddoc == null) {
if (!doc.steward || !doc.currency || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
channel(doc.type + channelDelimiter + currency);
access(doc.steward, doc.type + channelDelimiter + currency);
//access(doc.steward, "currency" + channelDelimiter + currency);
} else {
currency = base64_encode( olddoc.currency );
if (doc._deleted) {
requireUser(olddoc.steward);
channel(olddoc.type + channelDelimiter + currency);
access(olddoc.steward, olddoc.type + channelDelimiter + currency);
} else {
if (!doc.steward || !doc.currency || !doc.created ) {
throw({forbidden: "Missing required properties"});
}
if (doc.currency != olddoc.currency || doc.created != olddoc.created) {
throw({forbidden: "unmutable properties"});
}
if (! doc.steward.equals( olddoc.steward ) ) {
requireUser(olddoc.steward);
}
channel(olddoc.type + channelDelimiter + currency);
access(doc.steward, olddoc.type + channelDelimiter + currency);
//access(doc.steward, "currency" + channelDelimiter + currency);
}
}
} else if ( (olddoc != null && olddoc.type == "trading_name_journal" ) || (olddoc == null && doc.type == "trading_name_journal") ) {
var from = doc.from;
var to = doc.to;
var timestamp = doc.timestamp;
var channelTimestamp = timestamp;
var currency = base64_encode( doc.currency.toLowerCase() );
if (olddoc == null) {
if (!doc.from || !doc.to || typeof doc.amount == 'undefined' || parseFloat( doc.amount ) < 0 || !doc.currency || !doc.timestamp ) {
throw({forbidden: "Missing required properties"});
}
channel("trading_name_steward" + channelDelimiter + from + channelDelimiter + currency);
channel("trading_name_steward" + channelDelimiter + to + channelDelimiter + currency);
channel("currency" + channelDelimiter + currency);
//channel(table + channelDelimiter + from + channelDelimiter + to + channelDelimiter + channelTimestamp);
} else {
if (doc._deleted) {
throw({forbidden: "journal entries cannot be deleted!"});
} else {
if (!doc.from || !doc.to || typeof doc.amount == 'undefined' || parseFloat( doc.amount ) < 0 || !doc.currency || !doc.timestamp ) {
throw({forbidden: "Missing required properties"});
}
if (doc.from != olddoc.from || doc.to != olddoc.to || doc.amount != olddoc.amount || doc.currency != olddoc.currency || doc.timestamp != olddoc.timestamp) {
throw({forbidden: "unmutable properties"});
}
//channel(table + channelDelimiter + from + channelDelimiter + to + channelDelimiter + channelTimestamp);
channel("trading_name_steward" + channelDelimiter + from + channelDelimiter + currency)
channel("trading_name_steward" + channelDelimiter + to + channelDelimiter + currency)
channel("currency" + channelDelimiter + currency);
//requireUser([olddoc.from , olddoc.to]);
}
}
} else if ( (olddoc != null && olddoc.type == "beamtag") || (olddoc == null && doc.type == "beamtag") ) {
//var username = index.substring(0,index.indexOf(",")) ;
//var beamTag = index.substring( index.indexOf( "," ) + 1 );
if (olddoc == null) {
if (!doc.username || !doc.hashTag || !doc.created) {
throw({forbidden: "Missing required properties"});
}
channel(doc.type + channelDelimiter + doc.username + channelDelimiter + doc.hashTag);
access(doc.username, doc.type + channelDelimiter + doc.username + channelDelimiter + doc.hashTag);
} else {
if (doc._deleted) {
requireUser(olddoc.username);
channel(olddoc.type + channelDelimiter + olddoc.username + channelDelimiter + olddoc.hashTag);
access(olddoc.username, olddoc.type + channelDelimiter + olddoc.username + channelDelimiter + olddoc.hashTag);
} else {
if (!doc.username || !doc.hashTag || !doc.created) {
throw( { forbidden: "Missing required properties" } );
}
if (doc.username != olddoc.username || doc.hashTag != olddoc.hashTag || doc.created != olddoc.created) {
if (doc.username != olddoc.username) {
throw( { forbidden: "unmutable username: " + olddoc.username + "!=" + doc.username } );
}
if (doc.hashTag != olddoc.hashTag) {
throw( { forbidden: "unmutable hashTag: " + olddoc.hashTag + "!=" + doc.hashTag } );
}
if (doc.created != olddoc.created) {
throw( { forbidden: "unmutable created: " + olddoc.created + "!=" + doc.created } );
}
}
requireUser(olddoc.username);
channel(doc.type + channelDelimiter + doc.username + channelDelimiter + doc.hashTag);
access(doc.username, doc.type + channelDelimiter + doc.username + channelDelimiter + doc.hashTag);
}
}
} else if ( (olddoc != null && olddoc.type == "profile") || (olddoc == null && doc.type == "profile") ) {
if (!doc.type || !doc.username ) {
throw({forbidden: "Missing required properties"});
}
if (doc.username != 'anonymous') {
channel(doc.type + channelDelimiter + doc.username)
access(doc.username, doc.type + channelDelimiter + doc.username);
}
if (olddoc != null) {
if(olddoc.username != 'anonymous') {
requireUser(olddoc.username);
channel(olddoc.type + channelDelimiter + olddoc.username)
access(olddoc.username, olddoc.type + channelDelimiter + olddoc.username);
}
}
}
}
}`, "users": {
"GUEST": {"disabled": true}
}
}
}
}