-
Notifications
You must be signed in to change notification settings - Fork 1
/
manage.js
executable file
·275 lines (244 loc) · 11.4 KB
/
manage.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
#!/usr/bin/env node
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const readline = require("readline-sync");
const sqlite3 = require('better-sqlite3');
const crypto = require('crypto');
const argv = yargs(hideBin(process.argv))
.detectLocale(false)
.strict()
.alias('h', 'help')
.alias('v', 'version')
.demandCommand(1)
.command('user <operation> [options]', 'Magage user accounts.', (yargs) => {
return yargs.command('add [options]', 'Create a new user.', (yargs) => {
return yargs.option('fullName', {
alias: 'n',
require: true
}).option('email', {
alias: 'e',
require: true
}).option('password', {
alias: 'p',
require: true
});
}).command('delete [options]', 'Delete a user.', (yargs) => {
return yargs.option('email', {
alias: 'e',
require: true
});
}).command('changepw [options]', 'Change the password of a user.', (yargs) => {
return yargs.option('email', {
alias: 'e',
require: true
}).option('newPassword', {
alias: 'p'
});
}).command('info [options]', 'Show details about a user.', (yargs) => {
return yargs.option('email', {
alias: 'e',
conflicts: 'id'
}).options('id', {
alias: 'i',
conflicts: 'email'
}).check(({email, id}) => {
if(!email && !id) {
throw new Error('One of the following option is required: email, id');
}
return true;
});
}).command('list', 'List all users.', (yargs) => {
return yargs.option('json', {
alias: 'j'
});
});
})
.argv;
const db = sqlite3('./data/mailspring-api.db');
db.transaction(() => {
db.exec('CREATE TABLE IF NOT EXISTS identities(id VARCHAR PRIMARY KEY, firstName VARCHAR, lastName VARCHAR, emailAddress VARCHAR, passwordHash VARCHAR, createdAt VARCHAR, stripePlan VARCHAR, stripePlanEffective VARCHAR, stripeCustomerId VARCHAR, stripePeriodEnd VARCHAR, featureUsage VARCHAR);');
db.exec('CREATE TABLE IF NOT EXISTS sessions(token VARCHAR PRIMARY KEY, identityId VARCHAR, lastLogin INTEGER);');
db.exec('CREATE TABLE IF NOT EXISTS events(id INTEGER PRIMARY KEY AUTOINCREMENT, event VARCHAR, object VARCHAR, objectId INTEGER, identityId VARCHAR, accountId VARCHAR, timestamp INTEGER);')
db.exec('CREATE TABLE IF NOT EXISTS objects(id INTEGER PRIMARY KEY AUTOINCREMENT, object_id VARCHAR, object VARCHAR, object_type VARCHAR, aid VARCHAR, identity_id VARCHAR, plugin_id VARCHAR, v INTEGER, value VARCHAR, timestamp INTEGER);');
db.exec('CREATE TABLE IF NOT EXISTS shared_pages(id INTEGER PRIMARY KEY AUTOINCREMENT, identityId VARCHAR, key VARCHAR, html VARCHAR, timestamp INTEGER);');
db.exec('CREATE TABLE IF NOT EXISTS shared_assets(id INTEGER PRIMARY KEY AUTOINCREMENT, identityId VARCHAR, key VARCHAR, filename VARCHAR, filetype VARCHAR, file BLOB, timestamp INTEGER);');
})();
switch(argv._[0]) {
case 'user':
manageUsers();
break;
}
db.close();
function manageUsers() {
switch(argv._[1]) {
case 'add':
db.transaction(() => {
let stmt = db.prepare('SELECT id FROM identities WHERE emailAddress = ?;');
let identity = stmt.get(argv.email);
if(identity) {
console.log('Email address already in use.');
return;
}
const identityId = crypto.randomUUID();
let date = new Date();
date.setMilliseconds(0);
date = date.toISOString();
const fullNameSplit = argv.fullName.split(' ');
const passwordHash = hashPassword(argv.password.toString());
identity = {
id: identityId,
firstName: fullNameSplit[0],
lastName: fullNameSplit[1] || '',
emailAddress: argv.email,
passwordHash: passwordHash,
object: 'identity',
createdAt: date,
stripePlan: 'Pro',
stripePlanEffective: 'Pro',
stripeCustomerId: identityId,
stripePeriodEnd: date,
featureUsage: {
snooze: {
quota: 1000,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'pro-limit'
},
'send-later': {
quota: 1000,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'pro-limit'
},
'thread-sharing': {
quota: 1000,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'pro-limit'
},
'link-tracking': {
quota: 1000,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'pro-limit'
},
'open-tracking': {
quota: 1000,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'pro-limit'
},
'contact-profiles': {
quota: 1000,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'pro-limit'
},
'send-reminders': {
quota: 1000,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'pro-limit'
},
translation: {
quota: 1000,
period: 'weekly',
usedInPeriod: 0,
featureLimitName: 'pro-limit'
}
}
}
stmt = db.prepare('INSERT INTO identities(id, firstName, lastName, emailAddress, passwordHash, createdAt, stripePlan, stripePlanEffective, stripeCustomerId, stripePeriodEnd, featureUsage) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);');
stmt.run(identity.id, identity.firstName, identity.lastName, identity.emailAddress, identity.passwordHash, identity.createdAt, identity.stripePlan, identity.stripePlanEffective, identity.stripeCustomerId, identity.stripePeriodEnd, JSON.stringify(identity.featureUsage));
delete identity.passwordHash;
console.log('Created user:');
console.log(JSON.stringify(identity, null, 4));
})();
break;
case 'delete':
db.transaction(() => {
let stmt = db.prepare('SELECT id, firstName, lastName FROM identities WHERE emailAddress = ?;');
let identity = stmt.get(argv.email);
if(!identity) {
console.log('There is no user with this email address.');
return;
}
stmt = db.prepare('DELETE FROM objects WHERE identity_id = ?;');
stmt.run(identity.id);
stmt = db.prepare('DELETE FROM events WHERE identityId = ?;');
stmt.run(identity.id);
stmt = db.prepare('DELETE FROM shared_pages WHERE identityId = ?;');
stmt.run(identity.id);
stmt = db.prepare('DELETE FROM shares_assets WHERE identityId = ?;');
stmt.run(identity.id);
stmt = db.prepare('DELETE FROM sessions WHERE identityId = ?;');
stmt.run(identity.id);
stmt = db.prepare('DELETE FROM identities WHERE id = ?;');
stmt.run(identity.id);
console.log(`Deleted user "${identity.firstName} ${identity.lastName}"`);
})();
break;
case 'changepw':
db.transaction(() => {
let stmt = db.prepare('SELECT id, firstName, lastName FROM identities WHERE emailAddress = ?;');
let identity = stmt.get(argv.email);
if(!identity) {
console.log('There is no user with this email address.');
return;
}
let newPassword = argv.newPassword;
if(!newPassword) {
newPassword = readline.question('Please enter a new password: ', {hideEchoBack: true});
}
stmt = db.prepare('UPDATE identities SET passwordHash = ? WHERE id = ?;');
stmt.run(hashPassword(newPassword.toString()), identity.id);
stmt = db.prepare('DELETE FROM sessions WHERE identityId = ?;');
stmt.run(identity.id);
console.log(`Updated password for user "${identity.firstName} ${identity.lastName}"`);
})();
break;
case 'info':
db.transaction(() => {
let stmt = db.prepare(`SELECT * FROM identities WHERE ${argv.id ? 'id' : 'emailAddress'} = ?;`);
let identity = stmt.get(argv.id || argv.email);
if(!identity) {
console.log('There is no user with this id or email address.');
return;
}
delete identity.passwordHash;
identity.featureUsage = JSON.parse(identity.featureUsage);
console.log(JSON.stringify(identity, null, 4));
})();
break;
case 'list':
db.transaction(() => {
let stmt = db.prepare('SELECT * FROM identities;');
let identitys = stmt.all();
if(identitys.length == 0) {
console.log('No users registered yet.');
return;
}
if(argv.json) {
identitys.forEach(identity => {
delete identity.passwordHash;
identity.featureUsage = JSON.parse(identity.featureUsage);
});
console.log(JSON.stringify(identitys, null, 4));
} else {
console.log('All registered users:');
identitys.forEach(identity => {
console.log(`- ${identity.firstName} ${identity.lastName} | ${identity.emailAddress} | ${identity.id}`);
});
}
})();
break;
}
}
function hashPassword(password) {
const salt = crypto.randomBytes(16).toString('hex');
const hash = crypto.pbkdf2Sync(password, salt, 1000, 64, 'sha512').toString('hex');
return hash+'.'+salt;
}
process.on('SIGINT', () => {
process.exit();
});