mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-05 05:42:16 +00:00
Merge commit from fork
* fix: Improve host header handling and deployment mode detection * ZITADEL_CLOUD env * refactor service config * service config * cleanup * cleanup * util for original and public host * instanceHost, publicHost * cleanup constructUrl * cleanup middleware * add x-zitadel-forward-host as fallback for current cloud setup * fix build * fix: lint * fix: update test mocks * fix: provide basepath to unit tests * host handling * unit tests * imports * fix: getPublicHost instead of getInstanceHost * unit tests * update nextjs, implement jwt verify for logout_token * pass logout information in jwt to login v2 * handle hosts in jwks url * point idp callbacks to the api and not the login UI * logout_hint out of logout token, typecheck * rm unused imports * fix oidc integration test --------- Co-authored-by: Livio Spring <[email protected]>
This commit is contained in:
co-authored by
Livio Spring
parent
f9e3871bb3
commit
df75be96ff
@@ -6,37 +6,30 @@ import { GetSessionResponse } from "@zitadel/proto/zitadel/session/v2/session_se
|
||||
import { AuthenticationMethodType } from "@zitadel/proto/zitadel/user/v2/user_service_pb";
|
||||
import { getMostRecentCookieWithLoginname } from "./cookies";
|
||||
import { shouldEnforceMFA } from "./verify-helper";
|
||||
import { getLoginSettings, getSession, getUserByID, listAuthenticationMethodTypes } from "./zitadel";
|
||||
import { getLoginSettings, getSession, getUserByID, listAuthenticationMethodTypes, ServiceConfig } from "./zitadel";
|
||||
|
||||
type LoadMostRecentSessionParams = {
|
||||
serviceUrl: string;
|
||||
serviceConfig: ServiceConfig;
|
||||
sessionParams: {
|
||||
loginName?: string;
|
||||
organization?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export async function loadMostRecentSession({
|
||||
serviceUrl,
|
||||
sessionParams,
|
||||
}: LoadMostRecentSessionParams): Promise<Session | undefined> {
|
||||
export async function loadMostRecentSession({ serviceConfig, sessionParams }: LoadMostRecentSessionParams): Promise<Session | undefined> {
|
||||
const recent = await getMostRecentCookieWithLoginname({
|
||||
loginName: sessionParams.loginName,
|
||||
organization: sessionParams.organization,
|
||||
});
|
||||
|
||||
return getSession({
|
||||
serviceUrl,
|
||||
sessionId: recent.id,
|
||||
sessionToken: recent.token,
|
||||
}).then((resp: GetSessionResponse) => resp.session);
|
||||
return getSession({ serviceConfig, sessionId: recent.id, sessionToken: recent.token }).then((resp: GetSessionResponse) => resp.session);
|
||||
}
|
||||
|
||||
/**
|
||||
* mfa is required, session is not valid anymore (e.g. session expired, user logged out, etc.)
|
||||
* to check for mfa for automatically selected session -> const response = await listAuthenticationMethodTypes(userId);
|
||||
**/
|
||||
export async function isSessionValid({ serviceUrl, session }: { serviceUrl: string; session: Session }): Promise<boolean> {
|
||||
export async function isSessionValid({ serviceConfig, session }: { serviceConfig: ServiceConfig; session: Session }): Promise<boolean> {
|
||||
// session can't be checked without user
|
||||
if (!session.factors?.user) {
|
||||
console.warn("Session has no user");
|
||||
@@ -51,20 +44,14 @@ export async function isSessionValid({ serviceUrl, session }: { serviceUrl: stri
|
||||
const validPasskey = session?.factors?.webAuthN?.verifiedAt;
|
||||
|
||||
// Get login settings to determine if MFA is actually required by policy
|
||||
const loginSettings = await getLoginSettings({
|
||||
serviceUrl,
|
||||
organization: session.factors?.user?.organizationId,
|
||||
});
|
||||
const loginSettings = await getLoginSettings({ serviceConfig, organization: session.factors?.user?.organizationId });
|
||||
|
||||
// Use the existing shouldEnforceMFA function to determine if MFA is required
|
||||
const isMfaRequired = shouldEnforceMFA(session, loginSettings);
|
||||
|
||||
// Only enforce MFA validation if MFA is required by policy
|
||||
if (isMfaRequired) {
|
||||
const authMethodTypes = await listAuthenticationMethodTypes({
|
||||
serviceUrl,
|
||||
userId: session.factors.user.id,
|
||||
});
|
||||
const authMethodTypes = await listAuthenticationMethodTypes({ serviceConfig, userId: session.factors.user.id });
|
||||
|
||||
const authMethods = authMethodTypes.authMethodTypes;
|
||||
// Filter to only MFA methods (exclude PASSWORD and PASSKEY)
|
||||
@@ -134,10 +121,7 @@ export async function isSessionValid({ serviceUrl, session }: { serviceUrl: stri
|
||||
|
||||
// Check email verification if EMAIL_VERIFICATION environment variable is enabled
|
||||
if (process.env.EMAIL_VERIFICATION === "true") {
|
||||
const userResponse = await getUserByID({
|
||||
serviceUrl,
|
||||
userId: session.factors.user.id,
|
||||
});
|
||||
const userResponse = await getUserByID({ serviceConfig, userId: session.factors.user.id });
|
||||
|
||||
const humanUser = userResponse?.user?.type.case === "human" ? userResponse?.user.type.value : undefined;
|
||||
|
||||
@@ -151,12 +135,12 @@ export async function isSessionValid({ serviceUrl, session }: { serviceUrl: stri
|
||||
}
|
||||
|
||||
export async function findValidSession({
|
||||
serviceUrl,
|
||||
serviceConfig,
|
||||
sessions,
|
||||
authRequest,
|
||||
samlRequest,
|
||||
}: {
|
||||
serviceUrl: string;
|
||||
serviceConfig: ServiceConfig;
|
||||
sessions: Session[];
|
||||
authRequest?: AuthRequest;
|
||||
samlRequest?: SAMLRequest;
|
||||
@@ -189,7 +173,7 @@ export async function findValidSession({
|
||||
|
||||
// return the first valid session according to settings
|
||||
for (const session of sessionsWithHint) {
|
||||
if (await isSessionValid({ serviceUrl, session })) {
|
||||
if (await isSessionValid({ serviceConfig, session })) {
|
||||
return session;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user