2023-06-08 16:21:02 +02:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import { useState } from "react";
|
|
|
|
|
import { Button, ButtonVariants } from "./Button";
|
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import { Spinner } from "./Spinner";
|
|
|
|
|
import Alert from "./Alert";
|
2024-04-01 14:16:12 +02:00
|
|
|
import { AuthRequest, RegisterPasskeyResponse } from "@zitadel/server";
|
2024-05-13 16:17:12 -04:00
|
|
|
import { coerceToArrayBuffer, coerceToBase64Url } from "@/utils/base64";
|
2023-06-08 16:21:02 +02:00
|
|
|
type Inputs = {};
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
sessionId: string;
|
2023-06-19 16:28:29 +02:00
|
|
|
isPrompt: boolean;
|
2024-04-01 14:16:12 +02:00
|
|
|
authRequestId?: string;
|
|
|
|
|
organization?: string;
|
2023-06-08 16:21:02 +02:00
|
|
|
};
|
|
|
|
|
|
2024-04-01 14:16:12 +02:00
|
|
|
export default function RegisterPasskey({
|
|
|
|
|
sessionId,
|
|
|
|
|
isPrompt,
|
|
|
|
|
organization,
|
|
|
|
|
authRequestId,
|
|
|
|
|
}: Props) {
|
2023-06-08 16:21:02 +02:00
|
|
|
const { register, handleSubmit, formState } = useForm<Inputs>({
|
|
|
|
|
mode: "onBlur",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [error, setError] = useState<string>("");
|
|
|
|
|
|
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
async function submitRegister() {
|
|
|
|
|
setError("");
|
|
|
|
|
setLoading(true);
|
2023-06-30 14:13:03 +02:00
|
|
|
const res = await fetch("/api/passkeys", {
|
2023-06-08 16:21:02 +02:00
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
sessionId,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await res.json();
|
|
|
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
setError(response.details);
|
|
|
|
|
return Promise.reject(response.details);
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-15 13:58:32 +02:00
|
|
|
async function submitVerify(
|
|
|
|
|
passkeyId: string,
|
|
|
|
|
passkeyName: string,
|
|
|
|
|
publicKeyCredential: any,
|
2024-05-13 16:17:12 -04:00
|
|
|
sessionId: string,
|
2023-06-15 13:58:32 +02:00
|
|
|
) {
|
|
|
|
|
setLoading(true);
|
2023-06-30 14:13:03 +02:00
|
|
|
const res = await fetch("/api/passkeys/verify", {
|
2023-06-15 13:58:32 +02:00
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
passkeyId,
|
|
|
|
|
passkeyName,
|
|
|
|
|
publicKeyCredential,
|
|
|
|
|
sessionId,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const response = await res.json();
|
|
|
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
setError(response.details);
|
|
|
|
|
return Promise.reject(response.details);
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
function submitRegisterAndContinue(value: Inputs): Promise<boolean | void> {
|
2023-06-13 17:43:37 +02:00
|
|
|
return submitRegister().then((resp: RegisterPasskeyResponse) => {
|
2023-06-15 13:58:32 +02:00
|
|
|
const passkeyId = resp.passkeyId;
|
2023-06-19 15:59:16 +02:00
|
|
|
|
2023-06-13 17:43:37 +02:00
|
|
|
if (
|
|
|
|
|
resp.publicKeyCredentialCreationOptions &&
|
|
|
|
|
resp.publicKeyCredentialCreationOptions.publicKey
|
|
|
|
|
) {
|
|
|
|
|
resp.publicKeyCredentialCreationOptions.publicKey.challenge =
|
|
|
|
|
coerceToArrayBuffer(
|
|
|
|
|
resp.publicKeyCredentialCreationOptions.publicKey.challenge,
|
2024-05-13 16:17:12 -04:00
|
|
|
"challenge",
|
2023-06-13 17:43:37 +02:00
|
|
|
);
|
|
|
|
|
resp.publicKeyCredentialCreationOptions.publicKey.user.id =
|
|
|
|
|
coerceToArrayBuffer(
|
|
|
|
|
resp.publicKeyCredentialCreationOptions.publicKey.user.id,
|
2024-05-13 16:17:12 -04:00
|
|
|
"userid",
|
2023-06-13 17:43:37 +02:00
|
|
|
);
|
|
|
|
|
if (
|
|
|
|
|
resp.publicKeyCredentialCreationOptions.publicKey.excludeCredentials
|
|
|
|
|
) {
|
|
|
|
|
resp.publicKeyCredentialCreationOptions.publicKey.excludeCredentials.map(
|
|
|
|
|
(cred: any) => {
|
|
|
|
|
cred.id = coerceToArrayBuffer(
|
|
|
|
|
cred.id as string,
|
2024-05-13 16:17:12 -04:00
|
|
|
"excludeCredentials.id",
|
2023-06-13 17:43:37 +02:00
|
|
|
);
|
|
|
|
|
return cred;
|
2024-05-13 16:17:12 -04:00
|
|
|
},
|
2023-06-13 17:43:37 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
navigator.credentials
|
|
|
|
|
.create(resp.publicKeyCredentialCreationOptions)
|
|
|
|
|
.then((resp) => {
|
|
|
|
|
if (
|
|
|
|
|
resp &&
|
|
|
|
|
(resp as any).response.attestationObject &&
|
|
|
|
|
(resp as any).response.clientDataJSON &&
|
|
|
|
|
(resp as any).rawId
|
|
|
|
|
) {
|
|
|
|
|
const attestationObject = (resp as any).response
|
|
|
|
|
.attestationObject;
|
|
|
|
|
const clientDataJSON = (resp as any).response.clientDataJSON;
|
|
|
|
|
const rawId = (resp as any).rawId;
|
|
|
|
|
|
2023-06-15 13:58:32 +02:00
|
|
|
const data = {
|
2023-06-13 17:43:37 +02:00
|
|
|
id: resp.id,
|
|
|
|
|
rawId: coerceToBase64Url(rawId, "rawId"),
|
|
|
|
|
type: resp.type,
|
|
|
|
|
response: {
|
|
|
|
|
attestationObject: coerceToBase64Url(
|
|
|
|
|
attestationObject,
|
2024-05-13 16:17:12 -04:00
|
|
|
"attestationObject",
|
2023-06-13 17:43:37 +02:00
|
|
|
),
|
|
|
|
|
clientDataJSON: coerceToBase64Url(
|
|
|
|
|
clientDataJSON,
|
2024-05-13 16:17:12 -04:00
|
|
|
"clientDataJSON",
|
2023-06-13 17:43:37 +02:00
|
|
|
),
|
|
|
|
|
},
|
2023-06-15 13:58:32 +02:00
|
|
|
};
|
2023-06-27 18:08:22 +02:00
|
|
|
return submitVerify(passkeyId, "", data, sessionId).then(() => {
|
2024-04-01 14:16:12 +02:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
|
|
|
|
|
if (organization) {
|
|
|
|
|
params.set("organization", organization);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (authRequestId) {
|
2024-04-03 14:35:24 +02:00
|
|
|
params.set("authRequestId", authRequestId);
|
2024-04-01 14:16:12 +02:00
|
|
|
params.set("sessionId", sessionId);
|
2024-04-03 14:35:24 +02:00
|
|
|
// params.set("altPassword", ${false}); // without setting altPassword this does not allow password
|
|
|
|
|
// params.set("loginName", resp.loginName);
|
2024-04-01 14:16:12 +02:00
|
|
|
|
2024-04-03 14:35:24 +02:00
|
|
|
router.push("/passkey/login?" + params);
|
2024-04-01 14:16:12 +02:00
|
|
|
} else {
|
|
|
|
|
router.push("/accounts?" + params);
|
|
|
|
|
}
|
2023-06-27 18:08:22 +02:00
|
|
|
});
|
2023-06-13 17:43:37 +02:00
|
|
|
} else {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
setError("An error on registering passkey");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
console.error(error);
|
|
|
|
|
setLoading(false);
|
2023-06-29 10:26:51 +02:00
|
|
|
setError(error);
|
2023-06-13 17:43:37 +02:00
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-06-08 16:21:02 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { errors } = formState;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form className="w-full">
|
|
|
|
|
{error && (
|
|
|
|
|
<div className="py-4">
|
|
|
|
|
<Alert>{error}</Alert>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<div className="mt-8 flex w-full flex-row items-center">
|
2023-06-19 16:28:29 +02:00
|
|
|
{isPrompt ? (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant={ButtonVariants.Secondary}
|
2024-04-01 14:16:12 +02:00
|
|
|
onClick={() => {
|
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
if (authRequestId) {
|
2024-04-17 16:27:41 +02:00
|
|
|
params.set("authRequest", authRequestId);
|
|
|
|
|
}
|
|
|
|
|
if (sessionId) {
|
|
|
|
|
params.set("sessionId", sessionId);
|
2024-04-01 14:16:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (organization) {
|
|
|
|
|
params.set("organization", organization);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 16:27:41 +02:00
|
|
|
router.push("/login?" + params);
|
2024-04-01 14:16:12 +02:00
|
|
|
}}
|
2023-06-19 16:28:29 +02:00
|
|
|
>
|
|
|
|
|
skip
|
|
|
|
|
</Button>
|
|
|
|
|
) : (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
variant={ButtonVariants.Secondary}
|
|
|
|
|
onClick={() => router.back()}
|
|
|
|
|
>
|
|
|
|
|
back
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
|
2023-06-08 16:21:02 +02:00
|
|
|
<span className="flex-grow"></span>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
className="self-end"
|
|
|
|
|
variant={ButtonVariants.Primary}
|
|
|
|
|
disabled={loading || !formState.isValid}
|
|
|
|
|
onClick={handleSubmit(submitRegisterAndContinue)}
|
|
|
|
|
>
|
|
|
|
|
{loading && <Spinner className="h-5 w-5 mr-2" />}
|
|
|
|
|
continue
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|