-
Notifications
You must be signed in to change notification settings - Fork 3
feat(journey-app): delete webauthn devices using device client #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright (c) 2026 Ping Identity Corporation. All rights reserved. | ||
| * | ||
| * This software may be modified and distributed under the terms | ||
| * of the MIT license. See the LICENSE file for details. | ||
| */ | ||
|
|
||
| export function renderDeleteDevicesSection( | ||
| journeyEl: HTMLDivElement, | ||
| deleteWebAuthnDevice: () => Promise<string>, | ||
| ): void { | ||
| const deleteWebAuthnDeviceButton = document.createElement('button'); | ||
| deleteWebAuthnDeviceButton.type = 'button'; | ||
| deleteWebAuthnDeviceButton.id = 'deleteWebAuthnDeviceButton'; | ||
| deleteWebAuthnDeviceButton.innerText = 'Delete Webauthn Device'; | ||
|
|
||
| const deviceStatus = document.createElement('pre'); | ||
| deviceStatus.id = 'deviceStatus'; | ||
| deviceStatus.style.minHeight = '1.5em'; | ||
|
|
||
| journeyEl.appendChild(deleteWebAuthnDeviceButton); | ||
| journeyEl.appendChild(deviceStatus); | ||
|
|
||
| deleteWebAuthnDeviceButton.addEventListener('click', async () => { | ||
| try { | ||
| deviceStatus.innerText = 'Deleting WebAuthn device...'; | ||
| deviceStatus.innerText = await deleteWebAuthnDevice(); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| deviceStatus.innerText = `Delete failed: ${message}`; | ||
| } | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * Copyright (c) 2026 Ping Identity Corporation. All rights reserved. | ||
| * | ||
| * This software may be modified and distributed under the terms | ||
| * of the MIT license. See the LICENSE file for details. | ||
| */ | ||
|
|
||
| import { JourneyStep } from '@forgerock/journey-client/types'; | ||
| import { WebAuthn, WebAuthnStepType } from '@forgerock/journey-client/webauthn'; | ||
|
|
||
| export function webauthnComponent(journeyEl: HTMLDivElement, step: JourneyStep, idx: number) { | ||
| const container = document.createElement('div'); | ||
| container.id = `webauthn-container-${idx}`; | ||
| const info = document.createElement('p'); | ||
| info.innerText = 'Please complete the WebAuthn challenge using your authenticator.'; | ||
| container.appendChild(info); | ||
| journeyEl.appendChild(container); | ||
|
|
||
| const webAuthnStepType = WebAuthn.getWebAuthnStepType(step); | ||
|
|
||
| async function handleWebAuthn(): Promise<boolean> { | ||
| try { | ||
| if (webAuthnStepType === WebAuthnStepType.Authentication) { | ||
| console.log('trying authentication'); | ||
| await WebAuthn.authenticate(step); | ||
| return true; | ||
| } | ||
|
|
||
| if (webAuthnStepType === WebAuthnStepType.Registration) { | ||
| console.log('trying registration'); | ||
| await WebAuthn.register(step); | ||
| return true; | ||
| } | ||
| return false; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| return handleWebAuthn(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| /* | ||
| * Copyright (c) 2026 Ping Identity Corporation. All rights reserved. | ||
| * | ||
| * This software may be modified and distributed under the terms | ||
| * of the MIT license. See the LICENSE file for details. | ||
| */ | ||
|
|
||
| import { deviceClient as createDeviceClient } from '@forgerock/device-client'; | ||
| import type { WebAuthnDevice } from '@forgerock/device-client/types'; | ||
| import { JourneyClientConfig } from '@forgerock/journey-client/types'; | ||
|
|
||
| /** | ||
| * Derives the AM base URL from an OIDC well-known URL. | ||
| * | ||
| * Example: `https://example.com/am/oauth2/alpha/.well-known/openid-configuration` | ||
| * becomes `https://example.com/am`. | ||
| * | ||
| * @param wellknown The OIDC well-known URL. | ||
| * @returns The base URL for AM (origin + path prefix before `/oauth2/`). | ||
| */ | ||
| function getBaseUrlFromWellknown(wellknown: string): string { | ||
| const parsed = new URL(wellknown); | ||
| const [pathWithoutOauth] = parsed.pathname.split('/oauth2/'); | ||
| return `${parsed.origin}${pathWithoutOauth}`; | ||
| } | ||
|
|
||
| /** | ||
| * Derives the realm URL path from an OIDC well-known URL. | ||
| * | ||
| * Example: `/am/oauth2/realms/root/realms/alpha/.well-known/openid-configuration` | ||
| * becomes `realms/root/realms/alpha`. | ||
| */ | ||
| function getRealmUrlPathFromWellknown(wellknown: string): string { | ||
| const parsed = new URL(wellknown); | ||
| const [, afterOauth] = parsed.pathname.split('/oauth2/'); | ||
| if (!afterOauth) { | ||
| return 'realms/root'; | ||
| } | ||
|
|
||
| const suffix = '/.well-known/openid-configuration'; | ||
| const realmUrlPath = afterOauth.endsWith(suffix) | ||
| ? afterOauth.slice(0, -suffix.length) | ||
| : afterOauth.replace(/\/.well-known\/openid-configuration\/?$/, ''); | ||
|
|
||
| return realmUrlPath.replace(/^\/+/, '').replace(/\/+$/, '') || 'realms/root'; | ||
| } | ||
|
|
||
| /** | ||
| * Retrieves the AM user id from the session cookie using `idFromSession`. | ||
| * | ||
| * Note: This relies on the browser sending the session cookie; callers should use | ||
| * `credentials: 'include'` and ensure AM CORS allows credentialed requests. | ||
| */ | ||
| async function getUserIdFromSession(baseUrl: string, realmUrlPath: string): Promise<string | null> { | ||
| const url = `${baseUrl}/json/${realmUrlPath}/users?_action=idFromSession`; | ||
|
|
||
| try { | ||
| const response = await fetch(url, { | ||
| method: 'POST', | ||
| credentials: 'include', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Accept-API-Version': 'protocol=2.1,resource=3.0', | ||
| }, | ||
| }); | ||
|
|
||
| const data = await response.json(); | ||
|
|
||
| if (!data || typeof data !== 'object') { | ||
| return null; | ||
| } | ||
|
|
||
| const id = (data as Record<string, unknown>).id; | ||
| return typeof id === 'string' && id.length > 0 ? id : null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a single WebAuthn device by matching its `credentialId`. | ||
| * | ||
| * This queries devices via device-client and deletes the matching device. | ||
| */ | ||
| export async function deleteWebAuthnDevice(config: JourneyClientConfig): Promise<string> { | ||
| const WEBAUTHN_CREDENTIAL_ID_QUERY_PARAM = 'webauthnCredentialId'; | ||
|
|
||
| const params = new URLSearchParams(window.location.search); | ||
| const credentialId = params.get(WEBAUTHN_CREDENTIAL_ID_QUERY_PARAM); | ||
|
|
||
| if (!credentialId) { | ||
| return 'No credential id found. Register a WebAuthn device first.'; | ||
| } | ||
|
|
||
| const wellknown = config.serverConfig.wellknown; | ||
| const baseUrl = getBaseUrlFromWellknown(wellknown); | ||
| const realmUrlPath = getRealmUrlPathFromWellknown(wellknown); | ||
| const userId = await getUserIdFromSession(baseUrl, realmUrlPath); | ||
|
|
||
| if (!userId) { | ||
| throw new Error('Failed to retrieve user id from session. Are you logged in?'); | ||
| } | ||
|
|
||
| const realm = realmUrlPath.replace(/^realms\//, '') || 'root'; | ||
| const deviceClient = createDeviceClient({ | ||
| realmPath: realm, | ||
| serverConfig: { baseUrl }, | ||
| }); | ||
|
|
||
| const devices = await deviceClient.webAuthn.get({ userId }); | ||
| if (!Array.isArray(devices)) { | ||
| throw new Error(`Failed to retrieve devices: ${String(devices.error)}`); | ||
| } | ||
|
|
||
| const device = (devices as WebAuthnDevice[]).find((d) => d.credentialId === credentialId); | ||
| if (!device) { | ||
| return `No WebAuthn device found matching credential id: ${credentialId}`; | ||
| } | ||
|
|
||
| const response = await deviceClient.webAuthn.delete({ | ||
| userId, | ||
| device, | ||
| }); | ||
|
|
||
| if (response && typeof response === 'object' && 'error' in response) { | ||
| const error = (response as { error?: unknown }).error; | ||
| throw new Error(`Failed deleting device ${device.uuid}: ${String(error)}`); | ||
| } | ||
|
|
||
| return `Deleted WebAuthn device ${device.uuid} with credential id ${credentialId} for user ${userId}.`; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.