-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathauth.controller.js
More file actions
561 lines (523 loc) · 21.3 KB
/
Copy pathauth.controller.js
File metadata and controls
561 lines (523 loc) · 21.3 KB
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/**
* Module dependencies
*/
import crypto from 'crypto';
import passport from 'passport';
import jwt from 'jsonwebtoken';
import UserService from '../../users/services/users.service.js';
import config from '../../../config/index.js';
import model from '../../../lib/middlewares/model.js';
import mails from '../../../lib/helpers/mailer/index.js';
import responses from '../../../lib/helpers/responses.js';
import errors from '../../../lib/helpers/errors.js';
import AppError from '../../../lib/helpers/AppError.js';
import UsersSchema from '../../users/models/users.schema.js';
import policy from '../../../lib/middlewares/policy.js';
import serializeAbilities from '../../../lib/helpers/abilities.js';
import AuthOrganizationService from '../../organizations/services/organizations.service.js';
import OrganizationCrudService from '../../organizations/services/organizations.crud.service.js';
import MembershipService from '../../organizations/services/organizations.membership.service.js';
import AnalyticsService from '../../../lib/services/analytics.js';
import logger from '../../../lib/services/logger.js';
const tokenCookieOptions = {
httpOnly: true,
secure: config.cookie.secure,
sameSite: config.cookie.sameSite,
};
/**
* @desc Check whether the mailer is configured with a real sender address.
* Delegates to the centralized helper in lib/helpers/mailer.
* @returns {boolean} true when SMTP mail sending is available
*/
const isMailerConfigured = () => mails.isConfigured();
import getBaseUrl from '../../../lib/helpers/getBaseUrl.js';
/**
* @desc Send a verification email to the user with a signed token link
* @param {Object} user - User object (must have email, firstName, lastName)
* @param {string} verificationToken - The email verification token
* @returns {Promise<Object>} nodemailer send result
*/
const sendVerificationEmail = async (user, verificationToken) => {
const mail = await mails.sendMail({
template: 'verify-email',
to: user.email,
subject: 'Verify your email address',
params: {
displayName: [user.firstName, user.lastName].filter(Boolean).join(' '),
url: `${getBaseUrl()}/verify-email?token=${verificationToken}`,
appName: config.app.title,
appContact: config.app.contact,
},
});
return mail;
};
/**
* @desc Endpoint to ask the service to create a user
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
const signup = async (req, res) => {
try {
if (!config.sign.up) return responses.error(res, 404, 'Signup error', 'Registration is currently deactivated')();
// Force default role on public signup — clients must not self-assign admin
const safeBody = { ...req.body, roles: ['user'] };
const user = await UserService.create(safeBody);
// Handle email verification — rollback user on failure to avoid orphaned accounts
try {
if (isMailerConfigured()) {
// Generate verification token and persist it
const verificationToken = crypto.randomBytes(20).toString('hex');
const brutUser = await UserService.getBrut({ id: user.id });
await UserService.update(brutUser, {
emailVerificationToken: verificationToken,
emailVerificationExpires: Date.now() + 24 * 3600000, // 24 hours
}, 'recover');
// Send verification email (best-effort, do not block signup)
sendVerificationEmail(user, verificationToken).catch((err) => logger.warn('auth.signup: verification email failed', { message: err?.message, stack: err?.stack }));
} else {
// No mailer configured — auto-verify so dev/test are not blocked
const brutUser = await UserService.getBrut({ id: user.id });
await UserService.update(brutUser, { emailVerified: true }, 'recover');
user.emailVerified = true;
}
} catch (verifyErr) {
try { await UserService.remove(user); } catch (_cleanupErr) { /* best-effort */ }
throw verifyErr;
}
// Handle organization provisioning based on config
// If org creation fails, rollback the just-created user
let orgResult;
try {
orgResult = await AuthOrganizationService.handleSignupOrganization(user);
} catch (orgErr) {
// Manual rollback: delete the user we just created
try {
await UserService.remove(user);
} catch (_cleanupErr) {
// Best-effort cleanup; log but don't mask original error
}
throw orgErr;
}
// Analytics identify — fire-and-forget, never break signup flow
try {
AnalyticsService.identify(String(user.id), {
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
provider: user.provider,
});
} catch (_) { /* analytics must not break auth */ }
const token = jwt.sign({ userId: user.id }, config.jwt.secret, {
expiresIn: config.jwt.expiresIn,
});
// If the org set currentOrganization, reflect it on the returned user
// (but NOT for pendingJoin — user has no active membership yet)
if (orgResult.organization && !orgResult.pendingJoin) {
user.currentOrganization = orgResult.organization._id || orgResult.organization.id;
}
return res
.status(200)
.cookie('TOKEN', token, tokenCookieOptions)
.json({
user,
tokenExpiresIn: Date.now() + config.jwt.expiresIn * 1000,
organization: orgResult.organization || null,
joined: orgResult.joined || false,
pendingJoin: orgResult.pendingJoin || false,
abilities: orgResult.abilities || [],
organizationSetupRequired: orgResult.organizationSetupRequired || false,
emailVerificationRequired: orgResult.emailVerificationRequired || false,
suggestedOrganization: orgResult.suggestedOrganization || null,
type: 'success',
message: 'Sign up',
});
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};
/**
* @desc Middleware that runs passport local authentication and intercepts account-locked errors
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
* @returns {void} Calls next on success or sends a 423/401/500 response on failure
*/
const signinAuthenticate = (req, res, next) => {
passport.authenticate('local', { session: false }, (err, user, info) => {
if (err && err.code === 'ACCOUNT_LOCKED') {
return responses.error(res, 423, 'Account locked', err.details?.message || 'Account is locked. Try again later.')(err);
}
if (err) {
return responses.error(res, 500, 'Internal Server Error', errors.getMessage(err))(err);
}
if (!user) {
return responses.error(res, 401, 'Unauthorized', info?.message || 'Unauthorized')();
}
req.user = user;
return next();
})(req, res, next);
};
/**
* @desc Endpoint to ask the service to connect a user
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
const signin = async (req, res) => {
if (!config.sign.in) return responses.error(res, 404, 'Signin error', 'Login is currently deactivated')();
const user = req.user;
// Auto-set currentOrganization if missing but active memberships exist
await OrganizationCrudService.autoSetCurrentOrganization(user);
// Load active membership for current organization to build abilities
let membership = null;
if (user.currentOrganization) {
membership = await MembershipService.findByUserAndOrganization(
user._id || user.id,
user.currentOrganization._id || user.currentOrganization,
);
}
// Analytics identify — fire-and-forget, never break signin flow
try {
AnalyticsService.identify(String(user.id || user._id), {
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
lastLoginAt: user.lastLoginAt,
});
} catch (_) { /* analytics must not break auth */ }
const token = jwt.sign({ userId: user.id }, config.jwt.secret, {
expiresIn: config.jwt.expiresIn,
});
const ability = await policy.defineAbilityFor(user, membership);
const abilities = serializeAbilities(ability);
// If user has no org, check for pending join requests
let pendingRequests = [];
if (!user.currentOrganization && config.organizations?.enabled) {
pendingRequests = await MembershipService.listPendingByUser(user._id || user.id);
}
return res
.status(200)
.cookie('TOKEN', token, tokenCookieOptions)
.json({
user,
tokenExpiresIn: Date.now() + config.jwt.expiresIn * 1000,
abilities,
pendingRequests: pendingRequests.length > 0 ? pendingRequests : undefined,
type: 'success',
message: 'Sign in',
});
};
/**
* @desc Endpoint to get a new token if old is ok
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* TODO: escape deprecated
*/
/**
* @desc Strip OAuth tokens from an additionalProvidersData map before serializing to client.
* Only the provider identity fields are safe to expose; access/refresh tokens must stay server-side.
* @param {Object|undefined} apd - raw additionalProvidersData
* @returns {Object|undefined} sanitized map with accessToken/refreshToken removed per provider
*/
const sanitizeAdditionalProvidersData = (apd) => {
if (!apd || typeof apd !== 'object') return undefined;
const sanitized = {};
for (const [prov, data] of Object.entries(apd)) {
if (data && typeof data === 'object') {
// eslint-disable-next-line no-unused-vars
const { accessToken, refreshToken, ...safe } = data;
sanitized[prov] = safe;
}
}
return Object.keys(sanitized).length > 0 ? sanitized : undefined;
};
const token = async (req, res) => {
let user = null;
if (req.user) {
// Auto-set currentOrganization if missing but active memberships exist
await OrganizationCrudService.autoSetCurrentOrganization(req.user);
user = {
id: req.user.id,
provider: req.user.provider,
roles: req.user.roles,
avatar: req.user.avatar,
email: req.user.email,
lastName: req.user.lastName,
firstName: req.user.firstName,
additionalProvidersData: sanitizeAdditionalProvidersData(req.user.additionalProvidersData),
emailVerified: req.user.emailVerified,
currentOrganization: req.user.currentOrganization,
lastLoginAt: req.user.lastLoginAt,
};
}
// Load active membership for current organization to build abilities
let membership = null;
if (req.user && req.user.currentOrganization) {
membership = await MembershipService.findByUserAndOrganization(
req.user._id || req.user.id,
req.user.currentOrganization._id || req.user.currentOrganization,
);
}
const token = jwt.sign({ userId: user.id }, config.jwt.secret, {
expiresIn: config.jwt.expiresIn,
});
const ability = await policy.defineAbilityFor(req.user, membership);
const abilities = serializeAbilities(ability);
// If user has no org, include pending join requests
let pendingRequests;
if (req.user && !req.user.currentOrganization && config.organizations?.enabled) {
const requests = await MembershipService.listPendingByUser(req.user._id || req.user.id);
if (requests.length > 0) pendingRequests = requests;
}
return res
.status(200)
.cookie('TOKEN', token, tokenCookieOptions)
.json({ user, tokenExpiresIn: Date.now() + config.jwt.expiresIn * 1000, abilities, pendingRequests });
};
/**
* @desc Endpoint for oautCall
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
const oauthCall = (req, res, next) => {
const strategy = req.params.strategy;
passport.authenticate(strategy)(req, res, next);
};
/**
* Known OAuth providers — used to validate the `provider` argument and `key` argument
* before constructing dynamic query paths, preventing prototype-pollution-style injections.
*/
const ALLOWED_PROVIDERS = new Set(['google', 'apple']);
const ALLOWED_PROVIDER_KEYS = new Set(['sub', 'id', 'email']);
/**
* @desc Resolve or create a user from an OAuth profile. Lookup order:
* 1. Primary identity (provider + providerData[key])
* 2. Linked identity (additionalProvidersData[provider][key])
* 3. Link-on-verified-email (provider-verified email matches an existing local user)
* 4. Create new user
* @param {Object} profil - OAuth user profile object
* @param {string} key - Provider key to lookup providerData (must be in ALLOWED_PROVIDER_KEYS)
* @param {string} provider - OAuth provider name (must be in ALLOWED_PROVIDERS)
* @returns {Promise<Object>} sanitized user document (existing, linked, or newly created)
*/
const checkOAuthUserProfile = async (profil, key, provider) => {
// Guard: validate provider and key against allowlists before using as dynamic object keys
if (!ALLOWED_PROVIDERS.has(provider)) {
throw new AppError('oAuth, unsupported provider', { code: 'VALIDATION_ERROR', details: { provider } });
}
if (!ALLOWED_PROVIDER_KEYS.has(key)) {
throw new AppError('oAuth, unsupported provider key', { code: 'VALIDATION_ERROR', details: { key } });
}
// 1. Primary identity: match on (provider, providerData[key]) — OAuth-first users
try {
const query = {};
query[`providerData.${key}`] = profil.providerData[key];
query.provider = provider;
const search = await UserService.search(query);
if (search.length === 1) return search[0];
} catch (err) {
throw new AppError('oAuth, find user failed', { code: 'SERVICE_ERROR', details: err });
}
// 2. Linked identity: match on additionalProvidersData[provider][key] — locals already linked
try {
const query = {};
query[`additionalProvidersData.${provider}.${key}`] = profil.providerData[key];
const search = await UserService.search(query);
if (search.length === 1) return search[0];
} catch (err) {
throw new AppError('oAuth, find linked user failed', { code: 'SERVICE_ERROR', details: err });
}
// 3. Link on verified email: if a local user exists with the same email and the OAuth
// provider vouches for it, attach providerData under additionalProvidersData.{provider}
// without overwriting user.provider (keeps password reset + local login intact).
// Atomic findOneAndUpdate avoids TOCTOU races between concurrent OAuth callbacks.
if (profil.email && profil.emailVerifiedByProvider) {
try {
const linked = await UserService.linkProviderByEmail(profil.email, provider, profil.providerData);
if (linked) return linked;
} catch (err) {
throw new AppError('oAuth, link to existing user failed', { code: 'SERVICE_ERROR', details: err });
}
}
// 4. No match → create new user
try {
const user = {
firstName: profil.firstName,
lastName: profil.lastName,
email: profil.email,
avatar: profil.avatar || '',
provider,
providerData: profil.providerData || null,
emailVerified: !!profil.emailVerifiedByProvider,
};
const result = model.getResultFromZod(user, UsersSchema.User);
// check error
const error = model.checkError(result);
if (error) throw new AppError('Schema validation error', { code: 'VALIDATION_ERROR', details: { message: error } });
// else return req.body with the data after Zod validation
return await UserService.create(result.value);
} catch (err) {
if (err instanceof AppError) throw err;
throw new AppError('oAuth', { code: 'CONTROLLER_ERROR', details: err.details || err });
}
};
/**
* @desc Endpoint for oautCallCallBack
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
const oauthCallback = async (req, res, next) => {
const strategy = req.params.strategy;
// app Auth with Strategy managed on client side
if (req.body?.strategy === false && req.body?.key) {
const allowedKeys = ['id', 'sub', 'email'];
if (!allowedKeys.includes(req.body.key)) {
return responses.error(res, 422, 'Unprocessable Entity', 'Invalid provider key')();
}
try {
let user = {
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
providerData: {},
};
user.providerData[req.body.key] = req.body.value;
user = await checkOAuthUserProfile(user, req.body.key, strategy);
const token = jwt.sign({ userId: user.id }, config.jwt.secret, {
expiresIn: config.jwt.expiresIn,
});
return res
.status(200)
.cookie('TOKEN', token, tokenCookieOptions)
.json({
user,
tokenExpiresIn: Date.now() + config.jwt.expiresIn * 1000,
type: 'success',
message: 'oAuth Ok',
});
} catch (err) {
return responses.error(
res,
422,
err instanceof AppError && err.code === 'VALIDATION_ERROR' ? errors.getMessage(err) : 'Unprocessable Entity',
errors.getMessage(err.details || err),
)(err);
}
}
// classic web oAuth
passport.authenticate(strategy, (err, user) => {
const url = getBaseUrl();
if (err) {
const _err = JSON.stringify(err);
const path = 'token?message=Unprocessable%20Entity';
res.redirect(302, `${url}/${path}&error=${_err}`);
} else if (!user) {
const _err = JSON.stringify(err);
const path = 'token?message=Could%20not%20define%20user%20in%20oAuth';
res.redirect(302, `${url}/${path}&error=${_err}`);
} else {
const token = jwt.sign({ userId: user.id }, config.jwt.secret, {
expiresIn: config.jwt.expiresIn,
});
res.cookie('TOKEN', token, tokenCookieOptions);
res.redirect(302, `${getBaseUrl()}/token`);
}
})(req, res, next);
};
/**
* @desc Endpoint to expose public auth configuration (sign flags and organizations settings)
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @returns {void} Sends the public auth configuration in the HTTP response
*/
const getConfig = (req, res) => {
const data = {
sign: {
in: !!config.sign.in,
up: !!config.sign.up,
},
oAuth: {
google: !!config.oAuth?.google?.clientID,
apple: !!config.oAuth?.apple?.clientID,
},
organizations: {
enabled: !!config.organizations?.enabled,
domainMatching: !!config.organizations?.domainMatching,
autoCreate: !!config.organizations?.autoCreate,
},
mail: {
configured: isMailerConfigured(),
},
};
// Authenticated users get extended org config
if (req.user) {
data.organizations = {
...data.organizations,
roles: config.organizations?.roles || [],
roleDescriptions: config.organizations?.roleDescriptions || {},
};
}
responses.success(res, 'Auth config')(data);
};
/**
* @desc Endpoint to verify a user email address using a token
* @param {Object} req - Express request object (req.params.token)
* @param {Object} res - Express response object
* @returns {void} Sends JSON response indicating verification success or failure
*/
const verifyEmail = async (req, res) => {
try {
const user = await UserService.getBrut({ emailVerificationToken: req.params.token });
const isExpired = !user?.emailVerificationExpires || Number(user.emailVerificationExpires) < Date.now();
if (!user || !user.email || isExpired) {
return responses.error(res, 400, 'Bad Request', 'Email verification token is invalid or has expired.')();
}
await UserService.update(user, {
emailVerified: true,
emailVerificationToken: null,
emailVerificationExpires: null,
}, 'recover');
return responses.success(res, 'Email verified successfully')({ emailVerified: true });
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};
/**
* @desc Endpoint to resend the verification email for the authenticated user
* @param {Object} req - Express request object (req.user must be set)
* @param {Object} res - Express response object
* @returns {void} Sends JSON response confirming the email was resent or an error
*/
const resendVerification = async (req, res) => {
try {
const user = await UserService.getBrut({ id: req.user.id });
if (!user || !user.email) return responses.error(res, 400, 'Bad Request', 'User not found')();
if (user.emailVerified) return responses.error(res, 400, 'Bad Request', 'Email is already verified')();
if (!isMailerConfigured()) return responses.error(res, 400, 'Bad Request', 'Mail service is not configured')();
const verificationToken = crypto.randomBytes(20).toString('hex');
await UserService.update(user, {
emailVerificationToken: verificationToken,
emailVerificationExpires: Date.now() + 24 * 3600000, // 24 hours
}, 'recover');
const mail = await sendVerificationEmail(user, verificationToken);
const acceptedCount = Array.isArray(mail?.accepted) ? mail.accepted.length : 0;
if (!acceptedCount) return responses.error(res, 400, 'Bad Request', 'Failure sending email')();
return responses.success(res, 'Verification email sent')({ status: true });
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};
export default {
signup,
signinAuthenticate,
signin,
token,
oauthCall,
oauthCallback,
checkOAuthUserProfile,
getConfig,
verifyEmail,
resendVerification,
};