2024-09-26 22:50:55 -04:00
|
|
|
import { Alert, AlertType } from "@/components/alert";
|
|
|
|
|
import { DynamicTheme } from "@/components/dynamic-theme";
|
2024-12-16 09:21:12 +01:00
|
|
|
import { IdpSignin } from "@/components/idp-signin";
|
2024-09-11 17:12:43 +02:00
|
|
|
import { idpTypeToIdentityProviderType, PROVIDER_MAPPING } from "@/lib/idp";
|
2024-07-17 17:22:12 +02:00
|
|
|
import {
|
|
|
|
|
addIDPLink,
|
|
|
|
|
createUser,
|
|
|
|
|
getBrandingSettings,
|
2024-08-21 11:11:00 +02:00
|
|
|
getIDPByID,
|
2024-08-21 11:18:12 +02:00
|
|
|
listUsers,
|
2024-07-17 17:22:12 +02:00
|
|
|
retrieveIDPIntent,
|
|
|
|
|
} from "@/lib/zitadel";
|
2024-08-21 12:13:24 +02:00
|
|
|
import { AutoLinkingOption } from "@zitadel/proto/zitadel/idp/v2/idp_pb";
|
2024-10-09 14:03:01 +02:00
|
|
|
import { BrandingSettings } from "@zitadel/proto/zitadel/settings/v2/branding_settings_pb";
|
|
|
|
|
import { getLocale, getTranslations } from "next-intl/server";
|
2024-04-07 03:56:46 -04:00
|
|
|
|
2024-10-09 14:03:01 +02:00
|
|
|
async function loginFailed(branding?: BrandingSettings) {
|
|
|
|
|
const locale = getLocale();
|
|
|
|
|
const t = await getTranslations({ locale, namespace: "idp" });
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<DynamicTheme branding={branding}>
|
|
|
|
|
<div className="flex flex-col items-center space-y-4">
|
|
|
|
|
<h1>{t("loginError.title")}</h1>
|
|
|
|
|
<div className="w-full">
|
|
|
|
|
{<Alert type={AlertType.ALERT}>{t("loginError.title")}</Alert>}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</DynamicTheme>
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-12-16 09:24:31 +01:00
|
|
|
|
|
|
|
|
async function loginSuccess(
|
|
|
|
|
userId: string,
|
|
|
|
|
idpIntent: { idpIntentId: string; idpIntentToken: string },
|
2024-12-16 09:43:37 +01:00
|
|
|
authRequestId?: string,
|
2024-12-16 09:24:31 +01:00
|
|
|
branding?: BrandingSettings,
|
|
|
|
|
) {
|
|
|
|
|
const locale = getLocale();
|
|
|
|
|
const t = await getTranslations({ locale, namespace: "idp" });
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<DynamicTheme branding={branding}>
|
|
|
|
|
<div className="flex flex-col items-center space-y-4">
|
|
|
|
|
<h1>{t("loginSuccess.title")}</h1>
|
2024-12-16 10:30:18 +01:00
|
|
|
<p className="ztdl-p">{t("loginSuccess.description")}</p>
|
|
|
|
|
|
|
|
|
|
<IdpSignin
|
|
|
|
|
userId={userId}
|
|
|
|
|
idpIntent={idpIntent}
|
|
|
|
|
authRequestId={authRequestId}
|
|
|
|
|
/>
|
2024-12-16 09:24:31 +01:00
|
|
|
</div>
|
|
|
|
|
</DynamicTheme>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-16 09:58:55 +01:00
|
|
|
async function linkingSuccess(
|
|
|
|
|
userId: string,
|
|
|
|
|
idpIntent: { idpIntentId: string; idpIntentToken: string },
|
|
|
|
|
authRequestId?: string,
|
|
|
|
|
branding?: BrandingSettings,
|
|
|
|
|
) {
|
|
|
|
|
const locale = getLocale();
|
|
|
|
|
const t = await getTranslations({ locale, namespace: "idp" });
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<DynamicTheme branding={branding}>
|
|
|
|
|
<div className="flex flex-col items-center space-y-4">
|
|
|
|
|
<h1>{t("linkingSuccess.title")}</h1>
|
2024-12-16 10:30:18 +01:00
|
|
|
<p className="ztdl-p">{t("linkingSuccess.description")}</p>
|
|
|
|
|
|
|
|
|
|
<IdpSignin
|
|
|
|
|
userId={userId}
|
|
|
|
|
idpIntent={idpIntent}
|
|
|
|
|
authRequestId={authRequestId}
|
|
|
|
|
/>
|
2024-12-16 09:58:55 +01:00
|
|
|
</div>
|
|
|
|
|
</DynamicTheme>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-22 11:27:37 +01:00
|
|
|
export default async function Page(props: {
|
|
|
|
|
searchParams: Promise<Record<string | number | symbol, string | undefined>>;
|
|
|
|
|
params: Promise<{ provider: string }>;
|
|
|
|
|
}) {
|
2024-11-22 11:25:03 +01:00
|
|
|
const params = await props.params;
|
|
|
|
|
const searchParams = await props.searchParams;
|
2024-10-09 14:03:01 +02:00
|
|
|
const locale = getLocale();
|
|
|
|
|
const t = await getTranslations({ locale, namespace: "idp" });
|
2024-11-29 13:38:56 +01:00
|
|
|
const { id, token, authRequestId, organization, link } = searchParams;
|
2023-07-28 15:23:17 +02:00
|
|
|
const { provider } = params;
|
|
|
|
|
|
2024-04-07 03:56:46 -04:00
|
|
|
const branding = await getBrandingSettings(organization);
|
2024-10-09 14:03:01 +02:00
|
|
|
|
|
|
|
|
if (!provider || !id || !token) {
|
|
|
|
|
return loginFailed(branding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const intent = await retrieveIDPIntent(id, token);
|
|
|
|
|
|
|
|
|
|
const { idpInformation, userId } = intent;
|
|
|
|
|
|
2024-11-29 13:38:56 +01:00
|
|
|
// sign in user. If user should be linked continue
|
|
|
|
|
if (userId && !link) {
|
2024-10-09 14:03:01 +02:00
|
|
|
// TODO: update user if idp.options.isAutoUpdate is true
|
|
|
|
|
|
2024-12-16 09:24:31 +01:00
|
|
|
return loginSuccess(
|
|
|
|
|
userId,
|
|
|
|
|
{ idpIntentId: id, idpIntentToken: token },
|
2024-12-16 09:43:56 +01:00
|
|
|
authRequestId,
|
2024-12-16 09:24:31 +01:00
|
|
|
branding,
|
2024-12-16 09:21:12 +01:00
|
|
|
);
|
2024-10-09 14:03:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!idpInformation) {
|
|
|
|
|
return loginFailed(branding);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const idp = await getIDPByID(idpInformation.idpId);
|
|
|
|
|
const options = idp?.config?.options;
|
|
|
|
|
|
|
|
|
|
if (!idp) {
|
|
|
|
|
throw new Error("IDP not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const providerType = idpTypeToIdentityProviderType(idp.type);
|
|
|
|
|
|
|
|
|
|
// search for potential user via username, then link
|
|
|
|
|
if (options?.isLinkingAllowed) {
|
|
|
|
|
let foundUser;
|
|
|
|
|
const email = PROVIDER_MAPPING[providerType](idpInformation).email?.email;
|
|
|
|
|
|
|
|
|
|
if (options.autoLinking === AutoLinkingOption.EMAIL && email) {
|
|
|
|
|
foundUser = await listUsers({ email }).then((response) => {
|
|
|
|
|
return response.result ? response.result[0] : null;
|
|
|
|
|
});
|
|
|
|
|
} else if (options.autoLinking === AutoLinkingOption.USERNAME) {
|
|
|
|
|
foundUser = await listUsers(
|
|
|
|
|
options.autoLinking === AutoLinkingOption.USERNAME
|
|
|
|
|
? { userName: idpInformation.userName }
|
|
|
|
|
: { email },
|
|
|
|
|
).then((response) => {
|
|
|
|
|
return response.result ? response.result[0] : null;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
foundUser = await listUsers({
|
|
|
|
|
userName: idpInformation.userName,
|
|
|
|
|
email,
|
|
|
|
|
}).then((response) => {
|
|
|
|
|
return response.result ? response.result[0] : null;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (foundUser) {
|
|
|
|
|
const idpLink = await addIDPLink(
|
|
|
|
|
{
|
|
|
|
|
id: idpInformation.idpId,
|
|
|
|
|
userId: idpInformation.userId,
|
|
|
|
|
userName: idpInformation.userName,
|
|
|
|
|
},
|
|
|
|
|
foundUser.userId,
|
|
|
|
|
).catch((error) => {
|
2023-07-31 11:56:23 +02:00
|
|
|
return (
|
2024-04-01 13:57:39 +02:00
|
|
|
<DynamicTheme branding={branding}>
|
|
|
|
|
<div className="flex flex-col items-center space-y-4">
|
2024-10-09 14:03:01 +02:00
|
|
|
<h1>{t("linkingError.title")}</h1>
|
2024-04-01 13:57:39 +02:00
|
|
|
<div className="w-full">
|
|
|
|
|
{
|
|
|
|
|
<Alert type={AlertType.ALERT}>
|
2024-10-09 14:03:01 +02:00
|
|
|
{t("linkingError.description")}
|
2024-04-01 13:57:39 +02:00
|
|
|
</Alert>
|
|
|
|
|
}
|
|
|
|
|
</div>
|
2023-07-31 11:56:23 +02:00
|
|
|
</div>
|
2024-04-01 13:57:39 +02:00
|
|
|
</DynamicTheme>
|
2023-07-31 11:56:23 +02:00
|
|
|
);
|
|
|
|
|
});
|
2024-10-09 14:03:01 +02:00
|
|
|
|
2024-12-16 10:14:38 +01:00
|
|
|
if (idpLink) {
|
|
|
|
|
return linkingSuccess(
|
2024-12-16 10:21:00 +01:00
|
|
|
foundUser.userId,
|
2024-12-16 10:14:38 +01:00
|
|
|
{ idpIntentId: id, idpIntentToken: token },
|
|
|
|
|
authRequestId,
|
|
|
|
|
branding,
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-10-09 14:03:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options?.isCreationAllowed && options.isAutoCreation) {
|
|
|
|
|
const newUser = await createUser(providerType, idpInformation);
|
|
|
|
|
|
|
|
|
|
if (newUser) {
|
|
|
|
|
return (
|
|
|
|
|
<DynamicTheme branding={branding}>
|
2024-05-03 10:09:18 +02:00
|
|
|
<div className="flex flex-col items-center space-y-4">
|
2024-10-09 14:03:01 +02:00
|
|
|
<h1>{t("registerSuccess.title")}</h1>
|
|
|
|
|
<div>{t("registerSuccess.description")}</div>
|
2024-05-03 10:09:18 +02:00
|
|
|
</div>
|
2024-10-09 14:03:01 +02:00
|
|
|
</DynamicTheme>
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-07-28 15:23:17 +02:00
|
|
|
}
|
2024-10-09 14:03:01 +02:00
|
|
|
|
|
|
|
|
// return login failed if no linking or creation is allowed and no user was found
|
2024-12-16 10:00:43 +01:00
|
|
|
return loginFailed(branding);
|
2023-07-28 15:23:17 +02:00
|
|
|
}
|