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
|
(function() {
var Promise, bcrypt, config, db, fs, request;
Promise = require('bluebird');
db = require("../redis");
config = require("../config");
bcrypt = require('bcryptjs');
fs = require('fs');
request = require('request');
module.exports = function(sessions) {
return {
exists: function(req, res) {
return db.hgetall("user:" + req.params.user.toLowerCase(), function(err, obj) {
if (obj != null) {
res.write('true');
} else {
res.write('false');
}
return res.end();
});
},
index: function(req, res) {
var result;
result = {
'users': []
};
return db.keysAsync("user:*").then(function(users) {
return Promise.all(users.map(function(key) {
return db.hgetallAsync(key).then(function(user) {
delete user['password'];
return result.users.push(user);
});
}));
}).then(function() {
res.writeHead(200, {
"Content-Type": "application/json"
});
res.write(JSON.stringify(result));
return res.end();
});
},
json: function(req, res) {
if (!req.params.user) {
res.end();
}
return db.llen((req.params.user.toLowerCase()) + ":transactions", function(err, len) {
return db.hgetall("user:" + (req.params.user.toLowerCase()), function(err, obj) {
delete obj['password'];
obj['index'] = len;
res.writeHead(200, {
"Content-Type": "application/json"
});
res.write(JSON.stringify(obj));
return res.end();
});
});
},
show: function(req, res) {
return db.hgetall("user:" + req.params.user.toLowerCase(), function(err, obj) {
var ext, options, path;
if (obj) {
options = {
user: req.params.user.toLowerCase(),
layout: 'layout',
navigation: true,
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
};
if (req.query.verified != null) {
options.verified = true;
}
if (obj.logo && obj.logo.length > 3) {
ext = obj.logo.substr(obj.logo.length - 3);
path = "public/img/logos/" + obj.username + "." + ext;
fs.lstat(path, function(err, stats) {
if ((ext === 'jpg' || ext === 'png' || ext === 'gif') && (err || !stats.isFile())) {
try {
return request("" + obj.logo).pipe(fs.createWriteStream(path));
} catch (undefined) {}
}
});
}
res.render('users/show', options);
return delete req.session.verified;
} else {
return res.render('sessions/new', {
notice: true,
layout: 'layout',
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
});
}
});
},
"new": function(req, res) {
return res.render('users/new', {
layout: 'layout',
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
});
},
create: function(req, res) {
var errormsg, userkey;
errormsg = "";
userkey = "user:" + req.body.username;
return db.hgetall(userkey, function(err, obj) {
if (obj) {
errormsg += "Username exists";
}
if (req.body.confirm !== req.body.password) {
errormsg += "Passwords must match";
}
if (errormsg) {
return res.render('users/new', {
layout: 'layout',
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
}),
error: errormsg
});
}
bcrypt.hash(req.body.password, 12, function(err, hash) {
db.sadd("users", userkey);
return db.hmset(userkey, {
username: req.body.username,
password: hash,
email: req.body.email,
commission: req.body.commission,
unit: req.body.unit,
pubkey: req.body.pubkey,
privkey: req.body.privkey
}, function() {
req.session.redirect = "/" + req.body.username + "/edit";
return sessions.create(req, res);
});
});
return require('crypto').randomBytes(48, function(ex, buf) {
var host, token, url;
token = buf.toString('base64').replace(/\//g, '').replace(/\+/g, '');
db.set("token:" + token, req.body.username);
host = req.hostname;
if (host === 'localhost') {
host += ':3000';
}
url = req.protocol + "://" + host + "/verify/" + token;
return res.render('users/welcome', {
user: req.body.username.toLowerCase(),
layout: 'mail',
url: url,
privkey: req.body.privkey,
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
}, function(err, html) {
var content, from_email, helper, mail, sg, subject, to_email;
helper = require('sendgrid').mail;
from_email = new helper.Email('[email protected]');
to_email = new helper.Email(req.body.email);
subject = 'Welcome to CoinOS';
content = new helper.Content('text/html', html);
mail = new helper.Mail(from_email, subject, to_email, content);
sg = require('sendgrid')(config.sendgrid_token);
request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
return sg.API(request, function(error, response) {
console.log(response.statusCode);
console.log(response.body);
return console.log(response.headers);
});
});
});
});
},
edit: function(req, res) {
return res.render('users/edit', {
user: req.params.user.toLowerCase(),
layout: 'layout',
navigation: true,
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
});
},
profile: function(req, res) {
return res.render('users/profile', {
user: req.params.user.toLowerCase(),
layout: 'layout',
navigation: true,
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
});
},
update: function(req, res) {
if (req.body.password === '') {
delete req.body.password;
}
db.hmset("user:" + req.params.user.toLowerCase(), req.body, function() {
req.session.user = req.body;
delete req.session.user.password;
if (req.body.password != null) {
return bcrypt.hash(req.body.password, 12, function(err, password) {
return db.hmset("user:" + (req.params.user.toLowerCase()), {
email: email,
password: password,
phone: phone
}, function() {
if (req.xhr) {
res.send({});
return res.end();
} else {
return res.redirect("/" + (req.params.user.toLowerCase()));
}
});
});
} else {
if (req.xhr) {
res.send({});
return res.end();
} else {
return res.redirect("/" + (req.params.user.toLowerCase()));
}
}
});
if (process.env.NODE_ENV === 'production' && (req.body.privkey != null) && req.body.privkey !== '' && req.body.email !== '') {
return res.render('users/key', {
user: req.params.user.toLowerCase(),
layout: 'mail',
key: req.body.privkey,
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
}, function(err, html) {
var content, from_email, helper, mail, sg, subject, to_email;
helper = require('sendgrid').mail;
from_email = new helper.Email('[email protected]');
to_email = new helper.Email(req.body.email);
subject = 'CoinOS Wallet Key';
content = new helper.Content('text/html', html);
mail = new helper.Mail(from_email, subject, to_email, content);
sg = require('sendgrid')(config.sendgrid_token);
request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
return sg.API(request, function(error, response) {
console.log(response.statusCode);
console.log(response.body);
return console.log(response.headers);
});
});
}
},
verify: function(req, res) {
return db.get("token:" + req.params.token, function(err, reply) {
if (err || !reply) {
res.write("Invalid Verification Token");
return res.end();
} else {
return db.hset("user:" + (reply.toString()), "verified", "true", function() {
return res.redirect("/" + (reply.toString()) + "?verified");
});
}
});
},
wallet: function(req, res) {
return res.render('users/wallet', {
user: req.params.user.toLowerCase(),
layout: 'layout',
navigation: true,
js: (function() {
return global.js;
}),
css: (function() {
return global.css;
})
});
}
};
};
}).call(this);
|