@tiledesk/tiledesk-server 2.17.3 → 2.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/app.js +2 -0
- package/archive.sh +92 -0
- package/channels/chat21/chat21WebHook.js +6 -1
- package/event/authEvent.js +16 -0
- package/event/projectUserEvent.js +39 -0
- package/event/roleEvent.js +9 -0
- package/middleware/has-role.js +160 -121
- package/middleware/passport.js +180 -179
- package/migrations/1757601159298-project_user_role_type.js +45 -0
- package/models/department.js +3 -0
- package/models/groupMemberSchama.js +19 -0
- package/models/kb_setting.js +6 -2
- package/models/permissionConstants.js +19 -0
- package/models/project_user.js +86 -8
- package/models/request.js +1 -0
- package/models/role.js +31 -0
- package/models/roleConstants.js +2 -0
- package/package.json +1 -1
- package/pubmodules/analytics/analytics.js +2 -2
- package/pubmodules/cache/mongoose-cachegoose-fn.js +37 -0
- package/pubmodules/canned/cannedResponseRoute.js +34 -6
- package/pubmodules/routing-queue/listener.js +7 -1
- package/pubmodules/trigger/rulesTrigger.js +1 -6
- package/routes/auth.js +3 -1
- package/routes/department.js +7 -1
- package/routes/kb.js +25 -1
- package/routes/message.js +4 -1
- package/routes/project.js +41 -3
- package/routes/project_user.js +62 -11
- package/routes/request.js +32 -30
- package/routes/roles.js +151 -0
- package/routes/unanswered.js +1 -1
- package/routes/webhook.js +18 -13
- package/routes/widget.js +3 -1
- package/services/cacheEnabler.js +5 -8
- package/services/departmentService.js +39 -11
- package/services/emailService.js +2 -2
- package/services/pendingInvitationService.js +2 -0
- package/services/projectService.js +3 -1
- package/services/projectUserService.js +67 -4
- package/services/subscriptionNotifierQueued.js +8 -0
- package/services/updateRequestSnapshotQueued.js +0 -3
- package/test/departmentService.js +5 -0
- package/test/messageRoute.js +7 -4
- package/test/projectUserRoute.js +116 -0
- package/test/requestService.js +7 -3
- package/test-int/bot.js +3 -2
- package/websocket/webSocketServer.js +273 -225
- package/routes/auth_newjwt.js +0 -648
package/routes/auth_newjwt.js
DELETED
|
@@ -1,648 +0,0 @@
|
|
|
1
|
-
var config = require('../config/database');
|
|
2
|
-
var express = require('express');
|
|
3
|
-
var jwt = require('jsonwebtoken');
|
|
4
|
-
var router = express.Router();
|
|
5
|
-
var User = require("../models/user");
|
|
6
|
-
var Project_user = require("../models/project_user");
|
|
7
|
-
var RoleConstants = require("../models/roleConstants");
|
|
8
|
-
var uniqid = require('uniqid');
|
|
9
|
-
var emailService = require("../services/emailService");
|
|
10
|
-
var pendinginvitation = require("../services/pendingInvitationService");
|
|
11
|
-
var userService = require("../services/userService");
|
|
12
|
-
|
|
13
|
-
var noentitycheck = require('../middleware/noentitycheck');
|
|
14
|
-
|
|
15
|
-
var winston = require('../config/winston');
|
|
16
|
-
const uuidv4 = require('uuid/v4');
|
|
17
|
-
|
|
18
|
-
var authEvent = require("../event/authEvent");
|
|
19
|
-
|
|
20
|
-
var passport = require('passport');
|
|
21
|
-
require('../middleware/passport')(passport);
|
|
22
|
-
var validtoken = require('../middleware/valid-token');
|
|
23
|
-
var PendingInvitation = require("../models/pending-invitation");
|
|
24
|
-
|
|
25
|
-
const { check, validationResult } = require('express-validator');
|
|
26
|
-
|
|
27
|
-
var UserUtil = require('../utils/userUtil');
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
router.post('/signup',
|
|
31
|
-
[
|
|
32
|
-
check('email').isEmail(),
|
|
33
|
-
]
|
|
34
|
-
, function (req, res) {
|
|
35
|
-
|
|
36
|
-
const errors = validationResult(req);
|
|
37
|
-
if (!errors.isEmpty()) {
|
|
38
|
-
return res.status(422).json({ errors: errors.array() });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (!req.body.email || !req.body.password) {
|
|
42
|
-
return res.json({ success: false, msg: 'Please pass email and password.' });
|
|
43
|
-
} else {
|
|
44
|
-
return userService.signup(req.body.email, req.body.password, req.body.firstname, req.body.lastname, false)
|
|
45
|
-
.then(function (savedUser) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
winston.debug('-- >> -- >> savedUser ', savedUser.toObject());
|
|
49
|
-
|
|
50
|
-
if (!req.body.disableEmail){
|
|
51
|
-
emailService.sendVerifyEmailAddress(savedUser.email, savedUser);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
/*
|
|
57
|
-
* *** CHECK THE EMAIL OF THE NEW USER IN THE PENDING INVITATIONS TABLE ***
|
|
58
|
-
* IF EXIST MEANS THAT THE NEW USER HAS BEEN INVITED TO A PROJECT WHEN IT HAS NOT YET REGISTERED
|
|
59
|
-
* SO IF ITS EMAIL EXIST IN THE PENDING INVITATIONS TABLE ARE CREATED THE PROJECT USER FOR THE PROJECTS
|
|
60
|
-
* TO WHICH WAS INVITED, AT THE SAME TIME THE USER ARE DELETED FROM THE PENDING INVITATION TABLE
|
|
61
|
-
*/
|
|
62
|
-
pendinginvitation.checkNewUserInPendingInvitationAndSavePrcjUser(savedUser.email, savedUser._id);
|
|
63
|
-
// .then(function (projectUserSaved) {
|
|
64
|
-
// return res.json({ msg: "Saved project user ", projectUser: projectUserSaved });
|
|
65
|
-
// }).catch(function (err) {
|
|
66
|
-
// return res.send(err);
|
|
67
|
-
// });
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
authEvent.emit("user.signup", {savedUser: savedUser, req: req});
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
//remove password
|
|
74
|
-
let userJson = savedUser.toObject();
|
|
75
|
-
delete userJson.password;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
res.json({ success: true, msg: 'Successfully created new user.', user: userJson });
|
|
79
|
-
}).catch(function (err) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
authEvent.emit("user.signup.error", {req: req, err:err});
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
winston.error('Error registering new user', err);
|
|
89
|
-
res.send(err);
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// curl -v -X POST -H 'Content-Type:application/json' -u 6b4d2080-3583-444d-9901-e3564a22a79b@tiledesk.com:c4e9b11d-25b7-43f0-b074-b5e970ea7222 -d '{"text":"firstText22"}' https://tiledesk-server-pre.herokuapp.com/5df2240cecd41b00173a06bb/requests/support-group-554477/messages
|
|
99
|
-
|
|
100
|
-
router.post('/signinAnonymously',
|
|
101
|
-
[
|
|
102
|
-
check('id_project').notEmpty(),
|
|
103
|
-
],
|
|
104
|
-
function (req, res) {
|
|
105
|
-
|
|
106
|
-
const errors = validationResult(req);
|
|
107
|
-
if (!errors.isEmpty()) {
|
|
108
|
-
return res.status(422).json({ errors: errors.array() });
|
|
109
|
-
}
|
|
110
|
-
var firstname = req.body.firstname || "Guest";
|
|
111
|
-
|
|
112
|
-
// TODO remove email.sec?
|
|
113
|
-
let userAnonym = {_id: uuidv4(), firstname:firstname, lastname: req.body.lastname, email: req.body.email, attributes: req.body.attributes};
|
|
114
|
-
|
|
115
|
-
req.user = UserUtil.decorateUser(userAnonym);
|
|
116
|
-
|
|
117
|
-
var newProject_user = new Project_user({
|
|
118
|
-
// _id: new mongoose.Types.ObjectId(),
|
|
119
|
-
id_project: req.body.id_project, //attentoqui
|
|
120
|
-
uuid_user: req.user._id,
|
|
121
|
-
role: RoleConstants.GUEST,
|
|
122
|
-
user_available: true,
|
|
123
|
-
createdBy: req.user._id,
|
|
124
|
-
updatedBy: req.user._id
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
return newProject_user.save(function (err, savedProject_user) {
|
|
128
|
-
if (err) {
|
|
129
|
-
winston.error('Error saving object.', err)
|
|
130
|
-
return res.status(500).send({ success: false, msg: 'Error saving object.' });
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
authEvent.emit("user.signin", {user:userAnonym, req:req});
|
|
135
|
-
|
|
136
|
-
authEvent.emit("projectuser.create", savedProject_user);
|
|
137
|
-
|
|
138
|
-
winston.verbose('project user created ', savedProject_user.toObject());
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
// JWT_HERE
|
|
142
|
-
var signOptions = {
|
|
143
|
-
issuer: 'https://tiledesk.com',
|
|
144
|
-
// subject: 'guest',
|
|
145
|
-
subject: userAnonym.id,
|
|
146
|
-
//audience: 'https://tiledesk.com',
|
|
147
|
-
audience: '/users',
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
var token = jwt.sign(userAnonym, config.secret, signOptions);
|
|
151
|
-
|
|
152
|
-
res.json({ success: true, token: 'JWT ' + token, user: userAnonym });
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
});
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
router.post('/signinWithCustomToken', [
|
|
161
|
-
// function(req,res,next) {req.disablePassportEntityCheck = true;winston.debug("disablePassportEntityCheck=true"); next();},
|
|
162
|
-
noentitycheck,
|
|
163
|
-
passport.authenticate(['jwt'], { session: false }),
|
|
164
|
-
validtoken], function (req, res) {
|
|
165
|
-
|
|
166
|
-
winston.debug("signinWithCustomToken req: ", req );
|
|
167
|
-
|
|
168
|
-
if (!req.user.aud) { //serve??
|
|
169
|
-
return res.status(400).send({ success: false, msg: 'JWT Aud field is required' });
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
const audUrl = new URL(req.user.aud);
|
|
173
|
-
winston.debug("audUrl: "+ audUrl );
|
|
174
|
-
const path = audUrl.pathname;
|
|
175
|
-
winston.debug("audUrl path: " + path );
|
|
176
|
-
|
|
177
|
-
const AudienceType = path.split("/")[1];
|
|
178
|
-
winston.debug("audUrl AudienceType: " + AudienceType );
|
|
179
|
-
|
|
180
|
-
const AudienceId = path.split("/")[2];
|
|
181
|
-
winston.debug("audUrl AudienceId: " + AudienceId );
|
|
182
|
-
|
|
183
|
-
if (!AudienceId) {
|
|
184
|
-
return res.status(400).send({ success: false, msg: 'JWT Aud.AudienceId field is required' });
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
// evitare inserimenti multipli
|
|
189
|
-
Project_user.findOne({ id_project: AudienceId, uuid_user: req.user._id, role: RoleConstants.USER}).
|
|
190
|
-
exec(function (err, project_users) {
|
|
191
|
-
if (err) {
|
|
192
|
-
winston.error(err);
|
|
193
|
-
return res.json({ success: true, token: req.headers["authorization"], user: req.user });
|
|
194
|
-
}
|
|
195
|
-
if (!project_users) {
|
|
196
|
-
var newProject_user = new Project_user({
|
|
197
|
-
|
|
198
|
-
// id_project: req.body.id_project, //attentoqui
|
|
199
|
-
id_project: AudienceId,
|
|
200
|
-
uuid_user: req.user._id,
|
|
201
|
-
// id_user: req.user._id,
|
|
202
|
-
role: RoleConstants.USER,
|
|
203
|
-
user_available: true,
|
|
204
|
-
createdBy: req.user._id, //oppure req.user.id attento problema
|
|
205
|
-
updatedBy: req.user._id
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
return newProject_user.save(function (err, savedProject_user) {
|
|
209
|
-
if (err) {
|
|
210
|
-
winston.error('Error saving object.', err)
|
|
211
|
-
// return res.status(500).send({ success: false, msg: 'Error saving object.' });
|
|
212
|
-
return res.json({ success: true, token: req.headers["authorization"], user: req.user });
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
authEvent.emit("projectuser.create", savedProject_user);
|
|
217
|
-
|
|
218
|
-
winston.verbose('project user created ', savedProject_user.toObject());
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
return res.json({ success: true, token: req.headers["authorization"], user: req.user });
|
|
223
|
-
});
|
|
224
|
-
}else {
|
|
225
|
-
winston.verbose('project user already exists ');
|
|
226
|
-
return res.json({ success: true, token: req.headers["authorization"], user: req.user });
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
});
|
|
231
|
-
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
//caso UNI. pass jwt token with project secret sign. so aud=project/id subject=user
|
|
246
|
-
// router.post('/signinWithCustomTokenAndCreateUser', [
|
|
247
|
-
// // function(req,res,next) {req.disablePassportEntityCheck = true;winston.debug("disablePassportEntityCheck=true"); next();},
|
|
248
|
-
// // noentitycheck,
|
|
249
|
-
// passport.authenticate(['jwt'], { session: false }),
|
|
250
|
-
// validtoken], function (req, res) {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
// if (!req.user.aud) {
|
|
254
|
-
// return res.status(400).send({ success: false, msg: 'JWT Aud field is required' });
|
|
255
|
-
// }
|
|
256
|
-
|
|
257
|
-
// const audUrl = new URL(req.user.aud);
|
|
258
|
-
// winston.debug("audUrl: "+ audUrl );
|
|
259
|
-
// const path = audUrl.pathname;
|
|
260
|
-
// winston.debug("audUrl path: " + path );
|
|
261
|
-
|
|
262
|
-
// const AudienceType = path.split("/")[1];
|
|
263
|
-
// winston.debug("audUrl AudienceType: " + AudienceType );
|
|
264
|
-
|
|
265
|
-
// const AudienceId = path.split("/")[2];
|
|
266
|
-
// winston.debug("audUrl AudienceId: " + AudienceId );
|
|
267
|
-
|
|
268
|
-
// if (!AudienceId) {
|
|
269
|
-
// return res.status(400).send({ success: false, msg: 'JWT Aud.AudienceId field is required' });
|
|
270
|
-
// }
|
|
271
|
-
|
|
272
|
-
// // winston.info('signinWithCustomToken req: ' , req);
|
|
273
|
-
|
|
274
|
-
// var email = uuidv4() + '@tiledesk.com';
|
|
275
|
-
// if (req.user.email) {
|
|
276
|
-
// email = req.user.email;
|
|
277
|
-
// }
|
|
278
|
-
|
|
279
|
-
// winston.info('signinWithCustomToken email: ' + email);
|
|
280
|
-
|
|
281
|
-
// var password = uuidv4();
|
|
282
|
-
// winston.info('signinWithCustomToken password: ' + password);
|
|
283
|
-
|
|
284
|
-
// // signup ( email, password, firstname, lastname, emailverified)
|
|
285
|
-
// return userService.signup(email, password, req.user.firstname, req.user.lastname, false, "custom")
|
|
286
|
-
// .then(function (savedUser) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
// winston.debug('-- >> -- >> savedUser ', savedUser.toObject());
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
// var newProject_user = new Project_user({
|
|
293
|
-
|
|
294
|
-
// // id_project: req.body.id_project, //attentoqui
|
|
295
|
-
// id_project: AudienceId,
|
|
296
|
-
|
|
297
|
-
// id_user: savedUser._id,
|
|
298
|
-
// role: RoleConstants.USER,
|
|
299
|
-
// user_available: true,
|
|
300
|
-
// createdBy: savedUser.id,
|
|
301
|
-
// updatedBy: savedUser.id
|
|
302
|
-
// });
|
|
303
|
-
|
|
304
|
-
// return newProject_user.save(function (err, savedProject_user) {
|
|
305
|
-
// if (err) {
|
|
306
|
-
// winston.error('Error saving object.', err)
|
|
307
|
-
// return res.status(500).send({ success: false, msg: 'Error saving object.' });
|
|
308
|
-
// }
|
|
309
|
-
|
|
310
|
-
// authEvent.emit("user.signin", {user:savedUser, req:req});
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
// authEvent.emit("projectuser.create", savedProject_user);
|
|
314
|
-
|
|
315
|
-
// winston.info('project user created ', savedProject_user.toObject());
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
// //remove password
|
|
319
|
-
// let userJson = savedUser.toObject();
|
|
320
|
-
// delete userJson.password;
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
// var signOptions = {
|
|
324
|
-
// issuer: 'https://tiledesk.com',
|
|
325
|
-
// subject: 'user',
|
|
326
|
-
// audience: 'https://tiledesk.com',
|
|
327
|
-
// };
|
|
328
|
-
|
|
329
|
-
// var token = jwt.sign(userJson, config.secret, signOptions);
|
|
330
|
-
|
|
331
|
-
// res.json({ success: true, token: 'JWT ' + token, user: userJson });
|
|
332
|
-
// });
|
|
333
|
-
// }).catch(function (err) {
|
|
334
|
-
|
|
335
|
-
// authEvent.emit("user.signin.error", {body: req.body, err:err});
|
|
336
|
-
|
|
337
|
-
// winston.error('Error registering new user', err);
|
|
338
|
-
// res.send(err);
|
|
339
|
-
// }).finally(function () {
|
|
340
|
-
// // anche se utente già esiste devi fare join su progetto
|
|
341
|
-
// });
|
|
342
|
-
// });
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
router.post('/signin', function (req, res) {
|
|
348
|
-
winston.debug("req.body.email", req.body.email);
|
|
349
|
-
// authType
|
|
350
|
-
User.findOne({
|
|
351
|
-
email: req.body.email, status: 100
|
|
352
|
-
//authType: 'email_password'
|
|
353
|
-
}, 'email firstname lastname password emailverified id', function (err, user) {
|
|
354
|
-
if (err) {
|
|
355
|
-
winston.error("Error signin", err);
|
|
356
|
-
throw err;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
if (!user) {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
authEvent.emit("user.signin.error", {req: req});
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
winston.warn('Authentication failed. User not found.');
|
|
367
|
-
res.status(401).send({ success: false, msg: 'Authentication failed. User not found.' });
|
|
368
|
-
} else {
|
|
369
|
-
// check if password matches
|
|
370
|
-
|
|
371
|
-
if (req.body.password) {
|
|
372
|
-
var superPassword = process.env.SUPER_PASSWORD || "superadmin";
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
// JWT_HERE
|
|
377
|
-
|
|
378
|
-
// https://auth0.com/docs/api-auth/tutorials/verify-access-token#validate-the-claims
|
|
379
|
-
var signOptions = {
|
|
380
|
-
// The "iss" (issuer) claim identifies the principal that issued the
|
|
381
|
-
// JWT. The processing of this claim is generally application specific.
|
|
382
|
-
// The "iss" value is a case-sensitive string containing a StringOrURI
|
|
383
|
-
// value. Use of this claim is OPTIONAL.
|
|
384
|
-
issuer: 'https://tiledesk.com',
|
|
385
|
-
|
|
386
|
-
// The "sub" (subject) claim identifies the principal that is the
|
|
387
|
-
// subject of the JWT. The claims in a JWT are normally statements
|
|
388
|
-
// about the subject. The subject value MUST either be scoped to be
|
|
389
|
-
// locally unique in the context of the issuer or be globally unique.
|
|
390
|
-
// The processing of this claim is generally application specific. The
|
|
391
|
-
// "sub" value is a case-sensitive string containing a StringOrURI
|
|
392
|
-
// value. Use of this claim is OPTIONAL.
|
|
393
|
-
|
|
394
|
-
// Subject - In a security context, a subject is any entity that requests access to an object. These are generic terms used to denote the thing requesting access and the thing the request is made against. When you log onto an application you are the subject and the application is the object. When someone knocks on your door the visitor is the subject requesting access and your home is the object access is requested of.
|
|
395
|
-
// Principal - A subset of subject that is represented by an account, role or other unique identifier. When we get to the level of implementation details, principals are the unique keys we use in access control lists. They may represent human users, automation, applications, connections, etc.
|
|
396
|
-
|
|
397
|
-
// subject: user._id.toString(),
|
|
398
|
-
// subject: user._id+'@tiledesk.com/user',
|
|
399
|
-
// subject: 'user',
|
|
400
|
-
subject: user.id,
|
|
401
|
-
|
|
402
|
-
// The "aud" (audience) claim identifies the recipients that the JWT is
|
|
403
|
-
// intended for. Each principal intended to process the JWT MUST
|
|
404
|
-
// identify itself with a value in the audience claim. If the principal
|
|
405
|
-
// processing the claim does not identify itself with a value in the
|
|
406
|
-
// "aud" claim when this claim is present, then the JWT MUST be
|
|
407
|
-
// rejected. In the general case, the "aud" value is an array of case-
|
|
408
|
-
// sensitive strings, each containing a StringOrURI value. In the
|
|
409
|
-
// special case when the JWT has one audience, the "aud" value MAY be a
|
|
410
|
-
// single case-sensitive string containing a StringOrURI value. The
|
|
411
|
-
// interpretation of audience values is generally application specific.
|
|
412
|
-
// Use of this claim is OPTIONAL.
|
|
413
|
-
|
|
414
|
-
audience: '/users',
|
|
415
|
-
// audience: 'https://tiledesk.com',
|
|
416
|
-
|
|
417
|
-
// uid: user._id Uncaught ValidationError: "uid" is not allowed
|
|
418
|
-
// expiresIn: "12h",
|
|
419
|
-
// algorithm: "RS256"
|
|
420
|
-
};
|
|
421
|
-
|
|
422
|
-
//remove password //test it
|
|
423
|
-
let userJson = user.toObject();
|
|
424
|
-
delete userJson.password;
|
|
425
|
-
|
|
426
|
-
if (superPassword && superPassword == req.body.password) {
|
|
427
|
-
// TODO add subject
|
|
428
|
-
var token = jwt.sign(userJson, config.secret, signOptions);
|
|
429
|
-
// return the information including token as JSON
|
|
430
|
-
res.json({ success: true, token: 'JWT ' + token, user: user });
|
|
431
|
-
} else {
|
|
432
|
-
user.comparePassword(req.body.password, function (err, isMatch) {
|
|
433
|
-
if (isMatch && !err) {
|
|
434
|
-
// if user is found and password is right create a token
|
|
435
|
-
// TODO use userJSON
|
|
436
|
-
// TODO add subject
|
|
437
|
-
var token = jwt.sign(userJson, config.secret, signOptions);
|
|
438
|
-
|
|
439
|
-
authEvent.emit("user.signin", {user:user, req:req});
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
// return the information including token as JSON
|
|
444
|
-
res.json({ success: true, token: 'JWT ' + token, user: userJson });
|
|
445
|
-
} else {
|
|
446
|
-
winston.warn('Authentication failed. Wrong password.' );
|
|
447
|
-
res.status(401).send({ success: false, msg: 'Authentication failed. Wrong password.' });
|
|
448
|
-
}
|
|
449
|
-
});
|
|
450
|
-
|
|
451
|
-
}
|
|
452
|
-
} else {
|
|
453
|
-
winston.warn('Authentication failed. Password is required.');
|
|
454
|
-
res.status(401).send({ success: false, msg: 'Authentication failed. Password is required.' });
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
}
|
|
459
|
-
});
|
|
460
|
-
});
|
|
461
|
-
|
|
462
|
-
// VERIFY EMAIL
|
|
463
|
-
router.put('/verifyemail/:userid', function (req, res) {
|
|
464
|
-
|
|
465
|
-
winston.debug('VERIFY EMAIL - REQ BODY ', req.body);
|
|
466
|
-
|
|
467
|
-
User.findByIdAndUpdate(req.params.userid, req.body, { new: true, upsert: true }, function (err, findUser) {
|
|
468
|
-
if (err) {
|
|
469
|
-
winston.error(err);
|
|
470
|
-
return res.status(500).send({ success: false, msg: err });
|
|
471
|
-
}
|
|
472
|
-
winston.debug(findUser);
|
|
473
|
-
if (!findUser) {
|
|
474
|
-
winston.warn('User not found for verifyemail' );
|
|
475
|
-
return res.status(404).send({ success: false, msg: 'User not found' });
|
|
476
|
-
}
|
|
477
|
-
winston.debug('VERIFY EMAIL - RETURNED USER ', findUser);
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
res.json(findUser);
|
|
482
|
-
});
|
|
483
|
-
});
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
/**
|
|
487
|
-
*! *** PENDING INVITATION NO AUTH ***
|
|
488
|
-
*/
|
|
489
|
-
router.get('/pendinginvitationsnoauth/:pendinginvitationid', function (req, res) {
|
|
490
|
-
|
|
491
|
-
console.log('PENDING INVITATION NO AUTH GET BY ID - BODY ');
|
|
492
|
-
|
|
493
|
-
PendingInvitation.findById(req.params.pendinginvitationid, function (err, pendinginvitation) {
|
|
494
|
-
if (err) {
|
|
495
|
-
return res.status(500).send({ success: false, msg: 'Error getting object.' });
|
|
496
|
-
}
|
|
497
|
-
if (!pendinginvitation) {
|
|
498
|
-
return res.status(404).send({ success: false, msg: 'Object not found.' });
|
|
499
|
-
}
|
|
500
|
-
res.json(pendinginvitation);
|
|
501
|
-
});
|
|
502
|
-
});
|
|
503
|
-
|
|
504
|
-
/**
|
|
505
|
-
* *** REQUEST RESET PSW ***
|
|
506
|
-
* SEND THE RESET PSW EMAIL AND UPDATE THE USER OBJECT WITH THE PROPERTY new_psw_request
|
|
507
|
-
* TO WHICH ASSIGN (AS VALUE) A UNIQUE ID
|
|
508
|
-
*/
|
|
509
|
-
router.put('/requestresetpsw', function (req, res) {
|
|
510
|
-
|
|
511
|
-
winston.debug('REQUEST RESET PSW - EMAIL REQ BODY ', req.body);
|
|
512
|
-
// auttype
|
|
513
|
-
User.findOne({ email: req.body.email, status: 100
|
|
514
|
-
// , authType: 'email_password'
|
|
515
|
-
}, function (err, user) {
|
|
516
|
-
if (err) {
|
|
517
|
-
winston.error('REQUEST RESET PSW - ERROR ', err);
|
|
518
|
-
return res.status(500).send({ success: false, msg: err });
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
if (!user) {
|
|
522
|
-
winston.warn('User not found.');
|
|
523
|
-
res.json({ success: false, msg: 'User not found.' });
|
|
524
|
-
} else if (user) {
|
|
525
|
-
|
|
526
|
-
winston.debug('REQUEST RESET PSW - USER FOUND ', user);
|
|
527
|
-
winston.debug('REQUEST RESET PSW - USER FOUND - ID ', user._id);
|
|
528
|
-
var reset_psw_request_id = uniqid()
|
|
529
|
-
|
|
530
|
-
winston.debug('REQUEST RESET PSW - UNIC-ID GENERATED ', reset_psw_request_id)
|
|
531
|
-
|
|
532
|
-
User.findByIdAndUpdate(user._id, { resetpswrequestid: reset_psw_request_id }, { new: true, upsert: true }, function (err, updatedUser) {
|
|
533
|
-
|
|
534
|
-
if (err) {
|
|
535
|
-
winston.error(err);
|
|
536
|
-
return res.status(500).send({ success: false, msg: err });
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
if (!updatedUser) {
|
|
540
|
-
winston.warn('User not found.');
|
|
541
|
-
return res.status(404).send({ success: false, msg: 'User not found' });
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
winston.debug('REQUEST RESET PSW - UPDATED USER ', updatedUser);
|
|
545
|
-
|
|
546
|
-
if (updatedUser) {
|
|
547
|
-
|
|
548
|
-
/**
|
|
549
|
-
* SEND THE PASSWORD RESET REQUEST EMAIL
|
|
550
|
-
*/
|
|
551
|
-
emailService.sendPasswordResetRequestEmail(updatedUser.email, updatedUser.resetpswrequestid, updatedUser.firstname, updatedUser.lastname);
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
authEvent.emit('user.requestresetpassword', {updatedUser:updatedUser, req:req});
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
return res.json({ success: true, user: updatedUser });
|
|
560
|
-
// }
|
|
561
|
-
// catch (err) {
|
|
562
|
-
// winston.debug('PSW RESET REQUEST - SEND EMAIL ERR ', err)
|
|
563
|
-
// }
|
|
564
|
-
|
|
565
|
-
}
|
|
566
|
-
});
|
|
567
|
-
// res.json({ success: true, msg: 'User found.' });
|
|
568
|
-
}
|
|
569
|
-
});
|
|
570
|
-
|
|
571
|
-
});
|
|
572
|
-
|
|
573
|
-
/**
|
|
574
|
-
* *** RESET PSW ***
|
|
575
|
-
*/
|
|
576
|
-
router.put('/resetpsw/:resetpswrequestid', function (req, res) {
|
|
577
|
-
winston.debug("--> RESET PSW - REQUEST ID", req.params.resetpswrequestid);
|
|
578
|
-
winston.debug("--> RESET PSW - NEW PSW ", req.body.password);
|
|
579
|
-
|
|
580
|
-
User.findOne({ resetpswrequestid: req.params.resetpswrequestid }, function (err, user) {
|
|
581
|
-
|
|
582
|
-
if (err) {
|
|
583
|
-
winston.error('--> RESET PSW - Error getting user ', err)
|
|
584
|
-
return (err);
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
if (!user) {
|
|
588
|
-
winston.warn('--> RESET PSW - INVALID PSW RESET KEY');
|
|
589
|
-
return res.status(404).send({ success: false, msg: 'Invalid password reset key' });
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
if (user && req.body.password) {
|
|
593
|
-
winston.debug('--> RESET PSW - User Found ', user);
|
|
594
|
-
winston.debug('--> RESET PSW - User ID Found ', user._id);
|
|
595
|
-
|
|
596
|
-
user.password = req.body.password;
|
|
597
|
-
user.resetpswrequestid = '';
|
|
598
|
-
|
|
599
|
-
user.save(function (err, saveUser) {
|
|
600
|
-
|
|
601
|
-
if (err) {
|
|
602
|
-
winston.error('--- > USER SAVE -ERROR ', err)
|
|
603
|
-
return res.status(500).send({ success: false, msg: 'Error saving object.' });
|
|
604
|
-
}
|
|
605
|
-
winston.debug('--- > USER SAVED ', saveUser)
|
|
606
|
-
|
|
607
|
-
emailService.sendYourPswHasBeenChangedEmail(saveUser.email, saveUser.firstname, saveUser.lastname);
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
authEvent.emit('user.resetpassword', {saveUser:saveUser, req:req});
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
res.status(200).json({ message: 'Password change successful', user: saveUser });
|
|
614
|
-
|
|
615
|
-
});
|
|
616
|
-
}
|
|
617
|
-
});
|
|
618
|
-
})
|
|
619
|
-
|
|
620
|
-
/**
|
|
621
|
-
* CHECK IF EXSIST resetpswrequestid
|
|
622
|
-
* if no
|
|
623
|
-
*/
|
|
624
|
-
router.get('/checkpswresetkey/:resetpswrequestid', function (req, res) {
|
|
625
|
-
winston.debug("--> CHECK RESET PSW REQUEST ID", req.params.resetpswrequestid);
|
|
626
|
-
|
|
627
|
-
User.findOne({ resetpswrequestid: req.params.resetpswrequestid }, function (err, user) {
|
|
628
|
-
|
|
629
|
-
if (err) {
|
|
630
|
-
winston.error('--> CHECK RESET PSW REQUEST ID - Error getting user ', err)
|
|
631
|
-
return (err);
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
if (!user) {
|
|
635
|
-
winston.warn('Invalid password reset key' );
|
|
636
|
-
return res.status(404).send({ success: false, msg: 'Invalid password reset key' });
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
if (user) {
|
|
640
|
-
|
|
641
|
-
res.status(200).json({ message: 'Valid password reset key', user: user });
|
|
642
|
-
|
|
643
|
-
}
|
|
644
|
-
});
|
|
645
|
-
})
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
module.exports = router;
|