Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(loginname page): refactor user discovery search #313

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion apps/login/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,5 @@ Timebased features like the multifactor init prompt or password expiry, are not
- Password Expiry Settings
- Login Settings: multifactor init prompt
- forceMFA on login settings is not checked for IDPs
- disablePhone / disableEmail from loginSettings will be implemented right after https://github.com/zitadel/zitadel/issues/9016 is merged

Also note that IDP logins are considered as valid MFA. An additional MFA check will be implemented in future if enforced.
65 changes: 38 additions & 27 deletions apps/login/src/lib/server/loginname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
listAuthenticationMethodTypes,
listIDPLinks,
listUsers,
ListUsersCommand,
startIdentityProviderFlow,
} from "../zitadel";
import { createSessionAndUpdateCookie } from "./cookie";
Expand All @@ -29,21 +30,22 @@ export type SendLoginnameCommand = {
const ORG_SUFFIX_REGEX = /(?<=@)(.+)/;

export async function sendLoginname(command: SendLoginnameCommand) {
const users = await listUsers({
loginName: command.loginName,
const loginSettingsByContext = await getLoginSettings(command.organization);

let listUsersRequest: ListUsersCommand = {
userName: command.loginName,
organizationId: command.organization,
});
};

if (!loginSettingsByContext?.disableLoginWithEmail) {
listUsersRequest.email = command.loginName;
}

const loginSettings = await getLoginSettings(command.organization);
if (!loginSettingsByContext?.disableLoginWithPhone) {
listUsersRequest.phone = command.loginName;
}

const potentialUsers = users.result.filter((u) => {
const human = u.type.case === "human" ? u.type.value : undefined;
return loginSettings?.disableLoginWithEmail
? human?.email?.isVerified && human?.email?.email !== command.loginName
: loginSettings?.disableLoginWithPhone
? human?.phone?.isVerified && human?.phone?.phone !== command.loginName
: true;
});
const { result: potentialUsers } = await listUsers(listUsersRequest);

const redirectUserToSingleIDPIfAvailable = async () => {
const identityProviders = await getActiveIdentityProviders(
Expand Down Expand Up @@ -144,9 +146,16 @@ export async function sendLoginname(command: SendLoginnameCommand) {
}
};

if (potentialUsers.length == 1 && potentialUsers[0].userId) {
if (potentialUsers.length > 1) {
return { error: "More than one user found. Provide a unique identifier." };
} else if (potentialUsers.length == 1 && potentialUsers[0].userId) {
const user = potentialUsers[0];
const userId = potentialUsers[0].userId;

const userLoginSettings = await getLoginSettings(
user.details?.resourceOwner,
);

const checks = create(ChecksSchema, {
user: { search: { case: "userId", value: userId } },
});
Expand All @@ -162,7 +171,7 @@ export async function sendLoginname(command: SendLoginnameCommand) {
}

// TODO: check if handling of userstate INITIAL is needed
if (potentialUsers[0].state === UserState.INITIAL) {
if (user.state === UserState.INITIAL) {
return { error: "Initial User not supported" };
}

Expand All @@ -172,9 +181,9 @@ export async function sendLoginname(command: SendLoginnameCommand) {

if (!methods.authMethodTypes || !methods.authMethodTypes.length) {
if (
potentialUsers[0].type.case === "human" &&
potentialUsers[0].type.value.email &&
!potentialUsers[0].type.value.email.isVerified
user.type.case === "human" &&
user.type.value.email &&
!user.type.value.email.isVerified
) {
const paramsVerify = new URLSearchParams({
loginName: session.factors?.user?.loginName,
Expand Down Expand Up @@ -219,7 +228,7 @@ export async function sendLoginname(command: SendLoginnameCommand) {
const method = methods.authMethodTypes[0];
switch (method) {
case AuthenticationMethodType.PASSWORD: // user has only password as auth method
if (!loginSettings?.allowUsernamePassword) {
if (!userLoginSettings?.allowUsernamePassword) {
return {
error:
"Username Password not allowed! Contact your administrator for more information.",
Expand All @@ -246,7 +255,7 @@ export async function sendLoginname(command: SendLoginnameCommand) {
};

case AuthenticationMethodType.PASSKEY: // AuthenticationMethodType.AUTHENTICATION_METHOD_TYPE_PASSKEY
if (loginSettings?.passkeysType === PasskeysType.NOT_ALLOWED) {
if (userLoginSettings?.passkeysType === PasskeysType.NOT_ALLOWED) {
return {
error:
"Passkeys not allowed! Contact your administrator for more information.",
Expand Down Expand Up @@ -309,22 +318,24 @@ export async function sendLoginname(command: SendLoginnameCommand) {
}
}

// user not found, check if register is enabled on organization
if (loginSettings?.allowRegister && !loginSettings?.allowUsernamePassword) {
// TODO: do we need to handle login hints for IDPs here?
// user not found, check if register is enabled on instance / organization context
if (
loginSettingsByContext?.allowRegister &&
!loginSettingsByContext?.allowUsernamePassword
) {
const resp = await redirectUserToSingleIDPIfAvailable();
if (resp) {
return resp;
}
return { error: "Could not find user" };
} else if (
loginSettings?.allowRegister &&
loginSettings?.allowUsernamePassword
loginSettingsByContext?.allowRegister &&
loginSettingsByContext?.allowUsernamePassword
) {
let orgToRegisterOn: string | undefined = command.organization;

if (
!loginSettings?.ignoreUnknownUsernames &&
!loginSettingsByContext?.ignoreUnknownUsernames &&
!orgToRegisterOn &&
command.loginName &&
ORG_SUFFIX_REGEX.test(command.loginName)
Expand All @@ -344,7 +355,7 @@ export async function sendLoginname(command: SendLoginnameCommand) {
}

// do not register user if ignoreUnknownUsernames is set
if (orgToRegisterOn && !loginSettings?.ignoreUnknownUsernames) {
if (orgToRegisterOn && !loginSettingsByContext?.ignoreUnknownUsernames) {
const params = new URLSearchParams({ organization: orgToRegisterOn });

if (command.authRequestId) {
Expand All @@ -358,7 +369,7 @@ export async function sendLoginname(command: SendLoginnameCommand) {
}
}

if (loginSettings?.ignoreUnknownUsernames) {
if (loginSettingsByContext?.ignoreUnknownUsernames) {
const paramsPasswordDefault = new URLSearchParams({
loginName: command.loginName,
});
Expand Down
68 changes: 49 additions & 19 deletions apps/login/src/lib/zitadel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,24 @@ export async function createInviteCode(userId: string, host: string | null) {
);
}

export type ListUsersCommand = {
loginName?: string;
userName?: string;
email?: string;
phone?: string;
organizationId?: string;
};

export async function listUsers({
loginName,
userName,
phone,
email,
organizationId,
}: {
loginName?: string;
userName?: string;
email?: string;
organizationId?: string;
}) {
}: ListUsersCommand) {
const queries: SearchQuery[] = [];

// either use loginName or userName, email, phone
if (loginName) {
queries.push(
create(SearchQuerySchema, {
Expand All @@ -319,42 +324,67 @@ export async function listUsers({
},
}),
);
}
} else if (userName || email || phone) {
const orQueries: SearchQuery[] = [];

if (userName) {
queries.push(
create(SearchQuerySchema, {
if (userName) {
const userNameQuery = create(SearchQuerySchema, {
query: {
case: "userNameQuery",
value: {
userName: userName,
method: TextQueryMethod.EQUALS,
},
},
}),
);
}
});
orQueries.push(userNameQuery);
}

if (email) {
const emailQuery = create(SearchQuerySchema, {
query: {
case: "emailQuery",
value: {
emailAddress: email,
method: TextQueryMethod.EQUALS,
},
},
});
orQueries.push(emailQuery);
}

if (phone) {
const phoneQuery = create(SearchQuerySchema, {
query: {
case: "phoneQuery",
value: {
number: phone,
method: TextQueryMethod.EQUALS,
},
},
});
orQueries.push(phoneQuery);
}

if (organizationId) {
queries.push(
create(SearchQuerySchema, {
query: {
case: "organizationIdQuery",
case: "orQuery",
value: {
organizationId,
queries: orQueries,
},
},
}),
);
}

if (email) {
if (organizationId) {
queries.push(
create(SearchQuerySchema, {
query: {
case: "emailQuery",
case: "organizationIdQuery",
value: {
emailAddress: email,
organizationId,
},
},
}),
Expand Down
Loading