Files
zitadel/apps/login/ui/LoginPasskey.tsx

247 lines
6.6 KiB
TypeScript
Raw Normal View History

2023-06-22 16:54:55 +02:00
"use client";
2023-06-30 15:32:41 +02:00
import { useEffect, useRef, useState } from "react";
2023-06-22 16:54:55 +02:00
import { useRouter } from "next/navigation";
import { coerceToArrayBuffer, coerceToBase64Url } from "#/utils/base64";
2023-06-29 17:04:34 +02:00
import { Button, ButtonVariants } from "./Button";
import Alert from "./Alert";
import { Spinner } from "./Spinner";
2023-06-22 16:54:55 +02:00
type Props = {
2023-06-30 14:13:03 +02:00
loginName: string;
2023-08-22 13:15:33 +02:00
authRequestId?: string;
altPassword: boolean;
organization?: string;
2023-06-22 16:54:55 +02:00
};
2023-08-22 13:15:33 +02:00
export default function LoginPasskey({
loginName,
authRequestId,
altPassword,
organization,
2023-08-22 13:15:33 +02:00
}: Props) {
2023-06-22 16:54:55 +02:00
const [error, setError] = useState<string>("");
const [loading, setLoading] = useState<boolean>(false);
const router = useRouter();
2023-06-30 15:32:41 +02:00
const initialized = useRef(false);
2023-06-30 14:13:03 +02:00
useEffect(() => {
2023-06-30 15:32:41 +02:00
if (!initialized.current) {
initialized.current = true;
2023-07-05 14:09:24 +02:00
setLoading(true);
2023-06-30 15:32:41 +02:00
updateSessionForChallenge()
.then((response) => {
const pK =
2023-08-29 16:37:46 +02:00
response.challenges.webAuthN.publicKeyCredentialRequestOptions
2023-06-30 15:32:41 +02:00
.publicKey;
if (pK) {
2023-07-05 14:09:24 +02:00
submitLoginAndContinue(pK)
.then(() => {
setLoading(false);
})
.catch((error) => {
setError(error);
setLoading(false);
});
2023-06-30 15:32:41 +02:00
} else {
setError("Could not request passkey challenge");
2023-07-05 14:09:24 +02:00
setLoading(false);
2023-06-30 15:32:41 +02:00
}
})
.catch((error) => {
setError(error);
2023-07-05 14:09:24 +02:00
setLoading(false);
2023-06-30 15:32:41 +02:00
});
}
2023-06-30 14:13:03 +02:00
}, []);
async function updateSessionForChallenge() {
setLoading(true);
const res = await fetch("/api/session", {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
loginName,
organization,
2023-08-22 13:46:58 +02:00
challenges: {
webAuthN: {
domain: "",
2023-08-29 16:37:46 +02:00
userVerificationRequirement: 1,
2023-08-22 13:46:58 +02:00
},
},
2023-08-22 13:15:33 +02:00
authRequestId,
2023-06-30 14:13:03 +02:00
}),
});
setLoading(false);
if (!res.ok) {
2023-06-30 15:32:41 +02:00
const error = await res.json();
throw error.details.details;
2023-06-30 14:13:03 +02:00
}
return res.json();
}
2023-07-03 08:44:48 +02:00
async function submitLogin(data: any) {
2023-06-22 16:54:55 +02:00
setLoading(true);
2023-07-03 08:44:48 +02:00
const res = await fetch("/api/session", {
2023-07-03 09:33:39 +02:00
method: "PUT",
2023-06-22 16:54:55 +02:00
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
2023-07-03 08:44:48 +02:00
loginName,
organization,
2023-08-29 16:37:46 +02:00
webAuthN: { credentialAssertionData: data },
2023-08-22 13:15:33 +02:00
authRequestId,
2023-06-22 16:54:55 +02:00
}),
});
const response = await res.json();
setLoading(false);
if (!res.ok) {
setError(response.details);
return Promise.reject(response.details);
}
return response;
}
2023-07-05 14:09:24 +02:00
async function submitLoginAndContinue(
publicKey: any
): Promise<boolean | void> {
publicKey.challenge = coerceToArrayBuffer(
publicKey.challenge,
"publicKey.challenge"
);
publicKey.allowCredentials.map((listItem: any) => {
listItem.id = coerceToArrayBuffer(
listItem.id,
"publicKey.allowCredentials.id"
2023-07-03 08:44:48 +02:00
);
2023-07-05 14:09:24 +02:00
});
navigator.credentials
.get({
publicKey,
})
.then((assertedCredential: any) => {
if (assertedCredential) {
2023-08-29 16:37:46 +02:00
const authData = new Uint8Array(
2023-07-05 14:09:24 +02:00
assertedCredential.response.authenticatorData
);
2023-08-29 16:37:46 +02:00
const clientDataJSON = new Uint8Array(
2023-07-05 14:09:24 +02:00
assertedCredential.response.clientDataJSON
);
2023-08-29 16:37:46 +02:00
const rawId = new Uint8Array(assertedCredential.rawId);
const sig = new Uint8Array(assertedCredential.response.signature);
const userHandle = new Uint8Array(
2023-07-05 14:09:24 +02:00
assertedCredential.response.userHandle
);
2023-08-29 17:05:54 +02:00
const data = {
2023-07-05 14:09:24 +02:00
id: assertedCredential.id,
rawId: coerceToBase64Url(rawId, "rawId"),
type: assertedCredential.type,
response: {
authenticatorData: coerceToBase64Url(authData, "authData"),
clientDataJSON: coerceToBase64Url(
clientDataJSON,
"clientDataJSON"
),
signature: coerceToBase64Url(sig, "sig"),
userHandle: coerceToBase64Url(userHandle, "userHandle"),
},
2023-08-29 17:05:54 +02:00
};
2023-08-30 17:06:27 +02:00
return submitLogin(data).then((resp) => {
2024-03-06 14:44:48 +01:00
if (authRequestId && resp && resp.sessionId) {
return router.push(
`/login?` +
new URLSearchParams({
sessionId: resp.sessionId,
authRequest: authRequestId,
})
);
} else {
return router.push(
`/signedin?` +
new URLSearchParams(
authRequestId
? {
loginName: resp.factors.user.loginName,
authRequestId,
}
: {
loginName: resp.factors.user.loginName,
}
)
);
}
2023-07-05 14:09:24 +02:00
});
} else {
2023-06-30 15:32:41 +02:00
setLoading(false);
2023-07-05 14:09:24 +02:00
setError("An error on retrieving passkey");
2023-06-30 15:32:41 +02:00
return null;
2023-07-05 14:09:24 +02:00
}
})
.catch((error) => {
console.error(error);
setLoading(false);
// setError(error);
return null;
});
2023-06-22 16:54:55 +02:00
}
return (
2023-06-30 14:13:03 +02:00
<div className="w-full">
2023-06-22 16:54:55 +02:00
{error && (
<div className="py-4">
<Alert>{error}</Alert>
</div>
)}
<div className="mt-8 flex w-full flex-row items-center">
{altPassword ? (
<Button
type="button"
variant={ButtonVariants.Secondary}
2023-08-22 13:15:33 +02:00
onClick={() => {
const params = { loginName, alt: "true" };
return router.push(
"/password?" +
new URLSearchParams(
authRequestId ? { ...params, authRequestId } : params
) // alt is set because password is requested as alternative auth method, so passwordless prompt can be escaped
);
}}
>
use password
</Button>
) : (
<Button
type="button"
variant={ButtonVariants.Secondary}
onClick={() => router.back()}
>
back
</Button>
)}
2023-06-22 16:54:55 +02:00
<span className="flex-grow"></span>
<Button
type="submit"
className="self-end"
variant={ButtonVariants.Primary}
2023-07-05 14:09:24 +02:00
disabled={loading}
onClick={() => updateSessionForChallenge()}
2023-06-22 16:54:55 +02:00
>
{loading && <Spinner className="h-5 w-5 mr-2" />}
continue
</Button>
</div>
2023-06-30 14:13:03 +02:00
</div>
2023-06-22 16:54:55 +02:00
);
}