mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-25 05:58:45 +00:00
fix(login): Organization domain scope, Support for External Passkey Registration (#10729)
Closes #10727 Closes #10577 # Which Problems Are Solved This PR fixes the organization domain scope when provided and introduces a deep-link feature for external applications, that sends users directly into passkey registration flow using either session-based or sessionless flows. Previously, the `/passkey/set` page only supported session-based registration, limiting external application integration scenarios. The `/passkey/set` page now supports: - `code` search parameter for automatic passkey registration - `userId` parameter for sessionless flows (similar to `/verify` and `/password/set` pages) - Auto-submit functionality when verification codes are provided # How the Problems Are Solved The organization scope is fixed by the backend handler for OIDC flows, now correctly submitting a `suffix` queryparam to the /loginname url which is used to show in the input field. The passkey code support is implemented by support multiple integration patterns: - **Session-based**: `/passkey/set?sessionId=123&code=abc123` (existing flow) - **Sessionless**: `/passkey/set?userId=123456&code=abc123` (new flow) External Application Integration Flow 1. External app triggers passkey register and obtains code 2. User verification link containing `userId`, `code` and `id` parameters 3. User clicks link → `/passkey/set?userId=123&code=abc&id=123` 4. Page loads user information using `userId` parameter 5. Auto-submit triggers passkey registration when `code` and `id` is present 6. User completes WebAuthn request 7. Passkey is registered and user continues authentication flow This enables external applications to seamlessly integrate passkey registration into their user onboarding
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
"use client";
|
||||
|
||||
import { coerceToArrayBuffer, coerceToBase64Url } from "@/helpers/base64";
|
||||
import {
|
||||
registerPasskeyLink,
|
||||
verifyPasskeyRegistration,
|
||||
} from "@/lib/server/passkeys";
|
||||
import { registerPasskeyLink, verifyPasskeyRegistration } from "@/lib/server/passkeys";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { Alert } from "./alert";
|
||||
import { BackButton } from "./back-button";
|
||||
@@ -17,18 +14,16 @@ import { Translated } from "./translated";
|
||||
type Inputs = {};
|
||||
|
||||
type Props = {
|
||||
sessionId: string;
|
||||
sessionId?: string;
|
||||
userId?: string;
|
||||
isPrompt: boolean;
|
||||
requestId?: string;
|
||||
organization?: string;
|
||||
code?: string;
|
||||
codeId?: string;
|
||||
};
|
||||
|
||||
export function RegisterPasskey({
|
||||
sessionId,
|
||||
isPrompt,
|
||||
organization,
|
||||
requestId,
|
||||
}: Props) {
|
||||
export function RegisterPasskey({ sessionId, userId, isPrompt, organization, requestId, code, codeId }: Props) {
|
||||
const { handleSubmit, formState } = useForm<Inputs>({
|
||||
mode: "onBlur",
|
||||
});
|
||||
@@ -43,14 +38,16 @@ export function RegisterPasskey({
|
||||
passkeyId: string,
|
||||
passkeyName: string,
|
||||
publicKeyCredential: any,
|
||||
sessionId: string,
|
||||
currentSessionId?: string,
|
||||
currentUserId?: string,
|
||||
) {
|
||||
setLoading(true);
|
||||
const response = await verifyPasskeyRegistration({
|
||||
passkeyId,
|
||||
passkeyName,
|
||||
publicKeyCredential,
|
||||
sessionId,
|
||||
sessionId: currentSessionId,
|
||||
userId: currentUserId,
|
||||
})
|
||||
.catch(() => {
|
||||
setError("Could not verify Passkey");
|
||||
@@ -63,11 +60,28 @@ export function RegisterPasskey({
|
||||
return response;
|
||||
}
|
||||
|
||||
async function submitRegisterAndContinue(): Promise<boolean | void> {
|
||||
const submitRegisterAndContinue = useCallback(async (): Promise<boolean | void> => {
|
||||
// Require either sessionId or userId
|
||||
if (!sessionId && !userId) {
|
||||
setError("Missing session or user information");
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
const resp = await registerPasskeyLink({
|
||||
sessionId,
|
||||
})
|
||||
|
||||
let regReq;
|
||||
|
||||
if (sessionId) {
|
||||
regReq = { sessionId };
|
||||
} else if (userId && code && codeId) {
|
||||
regReq = { userId, code, codeId };
|
||||
} else {
|
||||
setError("Missing code for user-based registration");
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const resp = await registerPasskeyLink(regReq)
|
||||
.catch(() => {
|
||||
setError("Could not register passkey");
|
||||
return;
|
||||
@@ -92,29 +106,18 @@ export function RegisterPasskey({
|
||||
}
|
||||
|
||||
const passkeyId = resp.passkeyId;
|
||||
const options: CredentialCreationOptions =
|
||||
(resp.publicKeyCredentialCreationOptions as CredentialCreationOptions) ??
|
||||
{};
|
||||
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",
|
||||
);
|
||||
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",
|
||||
);
|
||||
cred.id = coerceToArrayBuffer(cred.id as string, "excludeCredentials.id");
|
||||
return cred;
|
||||
});
|
||||
}
|
||||
@@ -140,20 +143,12 @@ export function RegisterPasskey({
|
||||
rawId: coerceToBase64Url(rawId, "rawId"),
|
||||
type: credentials.type,
|
||||
response: {
|
||||
attestationObject: coerceToBase64Url(
|
||||
attestationObject,
|
||||
"attestationObject",
|
||||
),
|
||||
attestationObject: coerceToBase64Url(attestationObject, "attestationObject"),
|
||||
clientDataJSON: coerceToBase64Url(clientDataJSON, "clientDataJSON"),
|
||||
},
|
||||
};
|
||||
|
||||
const verificationResponse = await submitVerify(
|
||||
passkeyId,
|
||||
"",
|
||||
data,
|
||||
sessionId,
|
||||
);
|
||||
const verificationResponse = await submitVerify(passkeyId, "", data, sessionId, userId);
|
||||
|
||||
if (!verificationResponse) {
|
||||
setError("Could not verify Passkey!");
|
||||
@@ -161,7 +156,14 @@ export function RegisterPasskey({
|
||||
}
|
||||
|
||||
continueAndLogin();
|
||||
}
|
||||
}, [sessionId, userId, code]);
|
||||
|
||||
// Auto-submit when code is provided (similar to VerifyForm)
|
||||
useEffect(() => {
|
||||
if (code) {
|
||||
submitRegisterAndContinue();
|
||||
}
|
||||
}, [code, submitRegisterAndContinue]);
|
||||
|
||||
function continueAndLogin() {
|
||||
const params = new URLSearchParams();
|
||||
@@ -174,7 +176,13 @@ export function RegisterPasskey({
|
||||
params.set("requestId", requestId);
|
||||
}
|
||||
|
||||
params.set("sessionId", sessionId);
|
||||
if (sessionId) {
|
||||
params.set("sessionId", sessionId);
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
params.set("userId", userId);
|
||||
}
|
||||
|
||||
router.push("/passkey?" + params);
|
||||
}
|
||||
@@ -211,8 +219,7 @@ export function RegisterPasskey({
|
||||
onClick={handleSubmit(submitRegisterAndContinue)}
|
||||
data-testid="submit-button"
|
||||
>
|
||||
{loading && <Spinner className="mr-2 h-5 w-5" />}{" "}
|
||||
<Translated i18nKey="set.submit" namespace="passkey" />
|
||||
{loading && <Spinner className="mr-2 h-5 w-5" />} <Translated i18nKey="set.submit" namespace="passkey" />
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
Reference in New Issue
Block a user