Skip to content

Commit 58aa7ed

Browse files
committed
Enforce MFA before finalizing OIDC and SAML sessions
Added MFA enforcement logic to OIDC and SAML session finalization, ensuring MFA is checked even for valid sessions. Also added debug logging for login settings and MFA factor checks, and updated .gitignore to exclude .cursor files.
1 parent bdfa090 commit 58aa7ed

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ public/dist
1616
/blob-report/
1717
/out
1818
/docker
19-
/machinekey
19+
/machinekey
20+
21+
.cursor

apps/login/src/lib/oidc.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,38 @@ export async function loginWithOIDCAndSession({
102102
}
103103
}
104104

105+
// Enforce MFA even when the session is valid (or regardless of validity) before finalizing
106+
if (selectedSession.factors?.user) {
107+
try {
108+
const [methods, loginSettings] = await Promise.all([
109+
listAuthenticationMethodTypes({
110+
serviceUrl,
111+
userId: selectedSession.factors.user.id,
112+
}),
113+
getLoginSettings({
114+
serviceUrl,
115+
organization: selectedSession.factors?.user?.organizationId,
116+
}),
117+
]);
118+
119+
const mfaFactorCheck = await checkMFAFactors(
120+
serviceUrl,
121+
selectedSession,
122+
loginSettings,
123+
methods.authMethodTypes,
124+
selectedSession.factors?.user?.organizationId,
125+
`oidc_${authRequest}`,
126+
);
127+
128+
if (mfaFactorCheck?.redirect) {
129+
const absoluteUrl = constructUrl(request, mfaFactorCheck.redirect);
130+
return NextResponse.redirect(absoluteUrl.toString());
131+
}
132+
} catch (error) {
133+
console.warn("Failed to enforce MFA before finalize (OIDC)", error);
134+
}
135+
}
136+
105137
const cookie = sessionCookies.find(
106138
(cookie) => cookie.id === selectedSession?.id,
107139
);

apps/login/src/lib/saml.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,38 @@ export async function loginWithSAMLAndSession({
174174
}
175175
}
176176

177+
// Enforce MFA even when the session is valid (or regardless of validity) before finalizing
178+
if (selectedSession.factors?.user) {
179+
try {
180+
const [methods, loginSettings] = await Promise.all([
181+
listAuthenticationMethodTypes({
182+
serviceUrl,
183+
userId: selectedSession.factors.user.id,
184+
}),
185+
getLoginSettings({
186+
serviceUrl,
187+
organization: selectedSession.factors?.user?.organizationId,
188+
}),
189+
]);
190+
191+
const mfaFactorCheck = await checkMFAFactors(
192+
serviceUrl,
193+
selectedSession,
194+
loginSettings,
195+
methods.authMethodTypes,
196+
selectedSession.factors?.user?.organizationId,
197+
`saml_${samlRequest}`,
198+
);
199+
200+
if (mfaFactorCheck?.redirect) {
201+
const absoluteUrl = constructUrl(request, mfaFactorCheck.redirect);
202+
return NextResponse.redirect(absoluteUrl.toString());
203+
}
204+
} catch (error) {
205+
console.warn("Failed to enforce MFA before finalize (SAML)", error);
206+
}
207+
}
208+
177209
const cookie = sessionCookies.find(
178210
(cookie) => cookie.id === selectedSession?.id,
179211
);

apps/login/src/lib/server/idp.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,13 @@ export async function createNewSessionFromIdpIntent(
180180
session,
181181
loginSettings,
182182
authMethods ?? [],
183-
command.organization,
183+
session.factors.user.organizationId,
184184
command.requestId,
185185
);
186+
187+
console.log("mfaFactorCheck", mfaFactorCheck);
188+
189+
return 'test';
186190
if (mfaFactorCheck?.redirect) {
187191
return mfaFactorCheck;
188192
}

0 commit comments

Comments
 (0)