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.
- package/docs/api.md +29 -178
- package/docs/db.md +1 -1
- package/docs/db.sql +305 -253
- package/index.js +5 -3
- package/lib/config/cookies.js +84 -18
- package/lib/config/index.js +3 -1
- package/lib/config/tokenScopes.js +1 -1
- package/lib/createTable.js +95 -8
- package/lib/db/AuthRepository.js +57 -16
- package/lib/db/BaseRepository.js +9 -1
- package/lib/db/dialects/postgres.js +1 -1
- package/lib/main.js +5 -5
- package/lib/middleware/auth.js +201 -218
- package/lib/middleware/index.js +13 -14
- package/lib/middleware/scopeValidator.js +8 -3
- package/lib/pool.js +5 -6
- package/lib/routes/auth.js +42 -47
- package/lib/routes/dbLogs.js +247 -29
- package/lib/routes/misc.js +6 -4
- package/lib/routes/oauth.js +19 -23
- package/lib/utils/dbQueryLogger.js +485 -80
- package/lib/utils/errors.js +1 -1
- package/lib/utils/logger.js +12 -0
- package/lib/utils/timingSafeToken.js +1 -1
- package/package.json +1 -1
- package/public/main.css +1 -1
- package/test.spec.js +515 -48
- package/views/pages/dbLogs.handlebars +618 -420
package/lib/routes/oauth.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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);
|