mfa set cleanup

This commit is contained in:
peintnermax
2024-04-25 15:39:13 +02:00
parent 44bf7ace42
commit a6a5f7268b
2 changed files with 37 additions and 13 deletions

View File

@@ -26,10 +26,13 @@ export default async function Page({
({ session, token }) => { ({ session, token }) => {
if (session && session.factors?.user?.id) { if (session && session.factors?.user?.id) {
if (method === "time-based") { if (method === "time-based") {
// inconsistency with token: email works with machine token, totp works with session token
return registerTOTP(session.factors.user.id, token); return registerTOTP(session.factors.user.id, token);
} else if (method === "sms") { } else if (method === "sms") {
// does not work
return addOTPSMS(session.factors.user.id); return addOTPSMS(session.factors.user.id);
} else if (method === "email") { } else if (method === "email") {
// works
return addOTPEmail(session.factors.user.id); return addOTPEmail(session.factors.user.id);
} else { } else {
throw new Error("Invalid method"); throw new Error("Invalid method");
@@ -54,16 +57,15 @@ export default async function Page({
return ( return (
<DynamicTheme branding={branding}> <DynamicTheme branding={branding}>
<div className="flex flex-col items-center space-y-4"> <div className="flex flex-col items-center space-y-4">
<h1>Register TOTP</h1> <h1>Register 2-factor</h1>
{totpResponse && "uri" in totpResponse && "secret" in totpResponse ? (
<>
<p className="ztdl-p"> <p className="ztdl-p">
Scan the QR Code or navigate to the URL manually. Scan the QR Code or navigate to the URL manually.
</p> </p>
<div> <div>
{/* {auth && <div>{auth.to}</div>} */} {/* {auth && <div>{auth.to}</div>} */}
{totpResponse &&
"uri" in totpResponse &&
"secret" in totpResponse && (
<TOTPRegister <TOTPRegister
uri={totpResponse.uri as string} uri={totpResponse.uri as string}
secret={totpResponse.secret as string} secret={totpResponse.secret as string}
@@ -72,9 +74,18 @@ export default async function Page({
authRequestId={authRequestId} authRequestId={authRequestId}
organization={organization} organization={organization}
></TOTPRegister> ></TOTPRegister>
</div>{" "}
</>
) : (
<p className="ztdl-p">
{method === "email"
? "Code via email was successfully added."
: method === "sms"
? "Code via SMS was successfully added."
: ""}
</p>
)} )}
</div> </div>
</div>
</DynamicTheme> </DynamicTheme>
); );
} }

View File

@@ -112,9 +112,22 @@ export async function addOTPEmail(
} }
export async function addOTPSMS( export async function addOTPSMS(
userId: string userId: string,
token?: string
): Promise<AddOTPSMSResponse | undefined> { ): Promise<AddOTPSMSResponse | undefined> {
const userService = user.getUser(server); let userService;
if (token) {
const authConfig: ZitadelServerOptions = {
name: "zitadel login",
apiUrl: process.env.ZITADEL_API_URL ?? "",
token: token,
};
const sessionUser = initializeServer(authConfig);
userService = user.getUser(sessionUser);
} else {
userService = user.getUser(server);
}
return userService.addOTPSMS({ userId }, {}); return userService.addOTPSMS({ userId }, {});
} }