This repository has been archived by the owner on Nov 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
install.js
executable file
·172 lines (122 loc) · 4.08 KB
/
install.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
// Load modules
var Fs = require('fs');
var Cryptiles = require('cryptiles');
var Async = require('async');
var MongoDB = require('mongodb');
var Config = require('./config');
// Declare internals
var internals = {};
internals.token = function () {
return Cryptiles.randomBits(256).toString('hex');
};
internals.install = function () {
var database = new internals.Db();
database.initialize(function (err) {
if (err) {
console.error(err);
process.exit(1);
}
console.log('Database initialized');
// Create secrets
var vault = {
emailToken: internals.token(),
ozTicket: internals.token(),
yar: internals.token(),
session: internals.token(),
apiClient: {
id: '',
key: internals.token(),
algorithm: 'sha256'
},
viewClient: {
id: '',
key: '',
algorithm: 'sha256'
}
};
// Create required clients
var clients = [
{
name: 'postmile.web',
scope: ['authorized', 'login', 'reminder', 'signup', 'tos'],
key: vault.apiClient.key,
algorithm: 'sha256'
},
{
name: 'postmile.view',
scope: [],
key: '',
algorithm: 'sha256'
}
];
database.insert('client', clients, function (err, items) {
if (err) {
console.error(err);
process.exit(1);
}
// Add public invite to disable invitations
database.insert('invite', [{ code: 'public' }], function (err, items) {
if (err) {
console.error(err);
process.exit(1);
}
vault.apiClient.id = clients[0]._id.toString();
vault.viewClient.id = clients[1]._id.toString();
Fs.writeFile('./vault.json', JSON.stringify(vault, null, 4), function (err) {
if (err) {
console.error(err);
process.exit(1);
}
process.exit(0);
});
});
});
});
};
// Database interface
internals.Db = function () {
this._client = new MongoDB.Db(Config.database.db, new MongoDB.Server(Config.database.host, Config.database.port, {}), { strict: true });
this._collections = {};
};
internals.Db.prototype.initialize = function (callback) {
var self = this;
var create = function () {
var names = ['client', 'invite', 'grant', 'project', 'project.sort', 'suggestion', 'task', 'task.details', 'task.sort', 'tip', 'user', 'user.exclude', 'user.last', 'user.storage'];
Async.forEachSeries(names, function (name, next) {
self._client.createCollection(name, function (err, collection) {
if (err) {
return callback(err);
}
self._collections[name] = collection;
next();
})
},
function (err) {
callback(err);
});
};
this._client.open(function (err, client) {
if (err) {
return callback(err);
}
if (!Config.database.username) {
return create();
}
self._client.authenticate(Config.database.username, Config.database.password, function (err, result) {
if (err || !result) {
return callback(err || new Error('Authentication failed'));
}
return create();
});
});
};
internals.Db.prototype.insert = function (collectionName, items, callback) {
var collection = this._collections[collectionName];
var now = Date.now();
for (var i = 0, il = items.length; i < il; ++i) {
items[i].created = now;
items[i].modified = now;
}
collection.insert(items, callback);
};
internals.install();