mbkauthe 4.9.0 → 5.0.0

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.
@@ -9,9 +9,11 @@ import { mbkautheVar } from "#config.js";
9
9
  import { renderError } from "../utils/response.js";
10
10
  import { checkTrustedDevice, completeLoginProcess } from "./auth.js";
11
11
  import { AuthRepository } from "../db/AuthRepository.js";
12
+ import { createLogger } from "../utils/logger.js";
12
13
 
13
14
  const router = express.Router();
14
15
  const authRepo = new AuthRepository({ db: dblogin });
16
+ const logOAuth = createLogger("oauth");
15
17
 
16
18
  // CSRF protection middleware
17
19
  const csrfProtection = csurf({ cookie: true });
@@ -41,7 +43,7 @@ const githubClientSecret = mbkautheVar.GITHUB_APP_CLIENT_SECRET || mbkautheVar.G
41
43
  // Common OAuth strategy handler
42
44
  const createOAuthStrategy = async (provider, profile, done) => {
43
45
  try {
44
- console.log(`[mbkauthe] ${provider} OAuth callback for user: ${profile.emails?.[0]?.value || profile.id}`);
46
+ logOAuth(`${provider} OAuth callback for user: ${profile.emails?.[0]?.value || profile.id}`);
45
47
 
46
48
  const isGitHub = provider === 'GitHub';
47
49
 
@@ -76,6 +78,8 @@ const createOAuthStrategy = async (provider, profile, done) => {
76
78
  id: user.id,
77
79
  username: user.UserName,
78
80
  role: user.Role,
81
+ allowedApps: user.AllowedApps,
82
+ TwoFAStatus: user.TwoFAStatus,
79
83
  };
80
84
 
81
85
  if (isGitHub) {
@@ -142,7 +146,7 @@ if ((mbkautheVar.GOOGLE_LOGIN_ENABLED || "").toLowerCase() === "true") {
142
146
 
143
147
  // Print consolidated OAuth summary
144
148
  if (enabledProviders.length > 0) {
145
- console.log(`[mbkauthe] Social providers: ${enabledProviders.join(', ')}`);
149
+ logOAuth(`Social providers: ${enabledProviders.join(', ')}`);
146
150
  }
147
151
 
148
152
  // Serialize/Deserialize user for OAuth login
@@ -175,7 +179,7 @@ const createOAuthInitiation = (provider, enabledFlag, clientIdFlag, clientSecret
175
179
  // Store CSRF token for validation on callback
176
180
  const csrfToken = req.csrfToken();
177
181
  req.session.oauthCsrfToken = csrfToken;
178
- console.log(`[mbkauthe] ${provider} OAuth initiation started`);
182
+ logOAuth(`${provider} OAuth initiation started`);
179
183
 
180
184
  // Store redirect parameter in session before OAuth flow
181
185
  const redirect = req.query.redirect;
@@ -200,7 +204,7 @@ const createOAuthInitiation = (provider, enabledFlag, clientIdFlag, clientSecret
200
204
  pagename: 'Login'
201
205
  });
202
206
  }
203
- console.log(`[mbkauthe] ${provider} OAuth session saved successfully`);
207
+ logOAuth(`${provider} OAuth session saved successfully`);
204
208
  passport.authenticate(`${provider.toLowerCase()}-login`, { state: csrfToken })(req, res, next);
205
209
  });
206
210
  } else {
@@ -292,25 +296,11 @@ const validateOAuthCallback = (req, res) => {
292
296
  return true;
293
297
  };
294
298
 
295
- const finishProviderLogin = async (req, res, provider, username, detailValue = '') => {
296
- const user = await authRepo.getUserWithTwoFA(username, `${provider.toLowerCase()}-callback-get-user`);
297
-
298
- if (!user) {
299
- console.error(`[mbkauthe] ${provider} login: User not found: ${username}`);
300
- return renderError(res, req, {
301
- code: 404,
302
- error: 'User Not Found',
303
- message: `Your ${provider} account is linked, but the user account no longer exists in our system.`,
304
- page: '/mbkauthe/login',
305
- pagename: 'Login',
306
- details: `${provider} identifier: ${detailValue}\nPlease contact your administrator.`
307
- });
308
- }
309
-
299
+ const finishProviderLogin = async (req, res, provider, user, detailValue = '') => {
310
300
  // Check for trusted device
311
301
  const trustedDeviceUser = await checkTrustedDevice(req, user.UserName);
312
302
  if (trustedDeviceUser && (mbkautheVar.MBKAUTH_TWO_FA_ENABLE || "").toLowerCase() === "true" && user.TwoFAStatus) {
313
- console.log(`[mbkauthe] ${provider} trusted device login for user: ${user.UserName}, skipping 2FA only`);
303
+ logOAuth(`${provider} trusted device login for user: ${user.UserName}, skipping 2FA only`);
314
304
  return await handleOAuthRedirect(req, res, user, 'trusted', provider.toLowerCase());
315
305
  }
316
306
 
@@ -326,7 +316,7 @@ const finishProviderLogin = async (req, res, provider, username, detailValue = '
326
316
  loginMethod: provider.toLowerCase(),
327
317
  redirectUrl: oauthRedirect || null
328
318
  };
329
- console.log(`[mbkauthe] ${provider} login: 2FA required for user: ${username}`);
319
+ logOAuth(`${provider} login: 2FA required for user: ${user.UserName}`);
330
320
  return res.redirect('/mbkauthe/2fa');
331
321
  }
332
322
 
@@ -373,7 +363,13 @@ const createOAuthCallback = (provider, strategy) => {
373
363
  req,
374
364
  res,
375
365
  provider,
376
- oauthUser.username,
366
+ {
367
+ id: oauthUser.id,
368
+ UserName: oauthUser.username,
369
+ Role: oauthUser.role,
370
+ AllowedApps: oauthUser.allowedApps,
371
+ TwoFAStatus: oauthUser.TwoFAStatus
372
+ },
377
373
  provider === 'GitHub' ? (oauthUser.githubUsername || oauthUser.username) : (oauthUser.googleEmail || oauthUser.username)
378
374
  );
379
375
  } catch (err) {
@@ -415,7 +411,7 @@ const handleOAuthRedirect = async (req, res, user, type, method = null) => {
415
411
  res.json = function (data) {
416
412
  if (data.success && statusCode === 200) {
417
413
  const redirectUrl = oauthRedirect || mbkautheVar.loginRedirectURL || '/dashboard';
418
- console.log(`[mbkauthe] ${method || 'social'} ${type} login: Redirecting to ${redirectUrl}`);
414
+ logOAuth(`${method || 'social'} ${type} login: Redirecting to ${redirectUrl}`);
419
415
  res.json = originalJson;
420
416
  res.status = originalStatus;
421
417
  return res.redirect(redirectUrl);