2023-06-08 16:21:02 +02:00
|
|
|
"use client";
|
|
|
|
|
|
2024-09-26 22:50:55 -04:00
|
|
|
import { coerceToArrayBuffer, coerceToBase64Url } from "@/helpers/base64";
|
2024-09-05 13:48:33 +02:00
|
|
|
import { registerPasskeyLink, verifyPasskey } from "@/lib/server/passkeys";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
2023-06-08 16:21:02 +02:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { useForm } from "react-hook-form";
|
2024-09-26 22:50:55 -04:00
|
|
|
import { Alert } from "./alert";
|
|
|
|
|
import { BackButton } from "./back-button";
|
|
|
|
|
import { Button, ButtonVariants } from "./button";
|
|
|
|
|
import { Spinner } from "./spinner";
|
2024-07-16 16:51:13 +02:00
|
|
|
|
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-09-26 22:50:55 -04:00
|
|
|
export function RegisterPasskey({
|
2024-04-01 14:16:12 +02:00
|
|
|
sessionId,
|
|
|
|
|
isPrompt,
|
|
|
|
|
organization,
|
|
|
|
|
authRequestId,
|
|
|
|
|
}: Props) {
|
2024-09-03 10:45:55 +02:00
|
|
|
const { handleSubmit, formState } = useForm<Inputs>({
|
2023-06-08 16:21:02 +02:00
|
|
|
mode: "onBlur",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [error, setError] = useState<string>("");
|
|
|
|
|
|
|
|
|
|
const [loading, setLoading] = useState<boolean>(false);
|
|
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
|
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);
|
2024-09-03 09:35:42 +02:00
|
|
|
const response = await verifyPasskey({
|
|
|
|
|
passkeyId,
|
|
|
|
|
passkeyName,
|
|
|
|
|
publicKeyCredential,
|
|
|
|
|
sessionId,
|
2024-09-20 10:21:22 +02:00
|
|
|
}).catch(() => {
|
|
|
|
|
setError("Could not verify Passkey");
|
2023-06-15 13:58:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setLoading(false);
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-03 09:35:42 +02:00
|
|
|
async function submitRegisterAndContinue(): Promise<boolean | void> {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
const resp = await registerPasskeyLink({
|
|
|
|
|
sessionId,
|
2024-09-20 10:21:22 +02:00
|
|
|
}).catch(() => {
|
|
|
|
|
setError("Could not register passkey");
|
2024-09-03 09:35:42 +02:00
|
|
|
});
|
|
|
|
|
setLoading(false);
|
|
|
|
|
|
|
|
|
|
if (!resp) {
|
|
|
|
|
setError("An error on registering passkey");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const passkeyId = resp.passkeyId;
|
|
|
|
|
const options: CredentialCreationOptions =
|
|
|
|
|
(resp.publicKeyCredentialCreationOptions as CredentialCreationOptions) ??
|
|
|
|
|
{};
|
|
|
|
|
|
|
|
|
|
if (!options.publicKey) {
|
|
|
|
|
setError("An error on registering passkey");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
options.publicKey.challenge = coerceToArrayBuffer(
|
|
|
|
|
options.publicKey.challenge,
|
|
|
|
|
"challenge",
|
|
|
|
|
);
|
|
|
|
|
options.publicKey.user.id = coerceToArrayBuffer(
|
|
|
|
|
options.publicKey.user.id,
|
|
|
|
|
"userid",
|
|
|
|
|
);
|
|
|
|
|
if (options.publicKey.excludeCredentials) {
|
|
|
|
|
options.publicKey.excludeCredentials.map((cred: any) => {
|
|
|
|
|
cred.id = coerceToArrayBuffer(
|
|
|
|
|
cred.id as string,
|
|
|
|
|
"excludeCredentials.id",
|
2024-04-07 03:56:46 -04:00
|
|
|
);
|
2024-09-03 09:35:42 +02:00
|
|
|
return cred;
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-04-01 14:16:12 +02:00
|
|
|
|
2024-09-03 09:35:42 +02:00
|
|
|
const credentials = await navigator.credentials.create(options);
|
2024-04-01 14:16:12 +02:00
|
|
|
|
2024-09-03 09:35:42 +02:00
|
|
|
if (
|
|
|
|
|
!credentials ||
|
|
|
|
|
!(credentials as any).response?.attestationObject ||
|
|
|
|
|
!(credentials as any).response?.clientDataJSON ||
|
|
|
|
|
!(credentials as any).rawId
|
|
|
|
|
) {
|
|
|
|
|
setError("An error on registering passkey");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-04-01 14:16:12 +02:00
|
|
|
|
2024-09-03 09:35:42 +02:00
|
|
|
const attestationObject = (credentials as any).response.attestationObject;
|
|
|
|
|
const clientDataJSON = (credentials as any).response.clientDataJSON;
|
|
|
|
|
const rawId = (credentials as any).rawId;
|
|
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
id: credentials.id,
|
|
|
|
|
rawId: coerceToBase64Url(rawId, "rawId"),
|
|
|
|
|
type: credentials.type,
|
|
|
|
|
response: {
|
|
|
|
|
attestationObject: coerceToBase64Url(
|
|
|
|
|
attestationObject,
|
|
|
|
|
"attestationObject",
|
|
|
|
|
),
|
|
|
|
|
clientDataJSON: coerceToBase64Url(clientDataJSON, "clientDataJSON"),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const verificationResponse = await submitVerify(
|
|
|
|
|
passkeyId,
|
|
|
|
|
"",
|
|
|
|
|
data,
|
|
|
|
|
sessionId,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (!verificationResponse) {
|
|
|
|
|
setError("Could not verify Passkey!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 14:31:43 +02:00
|
|
|
continueAndLogin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function continueAndLogin() {
|
2024-09-03 09:35:42 +02:00
|
|
|
const params = new URLSearchParams();
|
|
|
|
|
|
|
|
|
|
if (organization) {
|
|
|
|
|
params.set("organization", organization);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (authRequestId) {
|
|
|
|
|
params.set("authRequestId", authRequestId);
|
2024-09-17 13:28:12 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-17 14:31:43 +02:00
|
|
|
params.set("sessionId", sessionId);
|
2024-09-17 13:28:12 +02:00
|
|
|
|
2024-09-17 14:31:43 +02:00
|
|
|
router.push("/passkey?" + params);
|
2023-06-08 16:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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={() => {
|
2024-09-17 13:28:12 +02:00
|
|
|
continueAndLogin();
|
2024-04-01 14:16:12 +02:00
|
|
|
}}
|
2023-06-19 16:28:29 +02:00
|
|
|
>
|
|
|
|
|
skip
|
|
|
|
|
</Button>
|
|
|
|
|
) : (
|
2024-07-15 16:00:17 +02:00
|
|
|
<BackButton />
|
2023-06-19 16:28:29 +02:00
|
|
|
)}
|
|
|
|
|
|
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>
|
|
|
|
|
);
|
|
|
|
|
}
|