/** * Passkey adapter for Web environment * Provides an API compatible with react-native-passkeys, but implemented using Web Authentication API */ import { startRegistration, startAuthentication, browserSupportsWebAuthn, } from '@simplewebauthn/browser'; import type { PublicKeyCredentialCreationOptionsJSON, PublicKeyCredentialRequestOptionsJSON, } from '@simplewebauthn/types'; /** * Check if the browser supports WebAuthn/Passkey * Compatible with the isSupported method of react-native-passkeys */ export const isSupported = async (): Promise => { try { return browserSupportsWebAuthn(); } catch (error) { console.error('Error checking WebAuthn support:', error); return false; } }; /** * Create a Passkey credential * Implements an interface compatible with the create method of react-native-passkeys * * @param options Options for creating the credential, compatible with the create method parameters of react-native-passkeys * @returns Result of creating the credential, compatible with the return value of the create method of react-native-passkeys */ export const create = async (options: any): Promise => { try { // Convert input options to the format required by SimpleWebAuthn const registrationOptions: PublicKeyCredentialCreationOptionsJSON = { challenge: options.challenge, rp: { id: options.rp.id, name: options.rp.name, }, user: { id: options.user.id, name: options.user.name, displayName: options.user.displayName, }, pubKeyCredParams: options.pubKeyCredParams || [ { type: 'public-key', alg: -7 }, // ES256 { type: 'public-key', alg: -257 }, // RS256 ], timeout: options.timeout, excludeCredentials: options.excludeCredentials?.map((cred: any) => ({ id: cred.id, type: cred.type || 'public-key', transports: cred.transports || ['internal'], })) || [], authenticatorSelection: options.authenticatorSelection || { authenticatorAttachment: 'platform', requireResidentKey: true, residentKey: 'required', userVerification: 'required', }, attestation: options.attestation || 'none', }; // Call SimpleWebAuthn's registration method const registrationResponse = await startRegistration({ optionsJSON: registrationOptions, }); // Convert to a format compatible with react-native-passkeys return registrationResponse; } catch (error) { console.error('Failed to create Passkey:', error); throw error; } }; /** * Get a Passkey credential * Implements an interface compatible with the get method of react-native-passkeys * * @param options Options for getting the credential, compatible with the get method parameters of react-native-passkeys * @returns Result of getting the credential, compatible with the return value of the get method of react-native-passkeys */ export const get = async (options: any): Promise => { try { // Convert input options to the format required by SimpleWebAuthn const authenticationOptions: PublicKeyCredentialRequestOptionsJSON = { challenge: options.challenge, rpId: options.rpId, allowCredentials: options.allowCredentials?.map((cred: any) => ({ id: cred.id, type: cred.type || 'public-key', transports: cred.transports || ['internal'], })) || [], timeout: options.timeout, userVerification: options.userVerification || 'required', }; // Call SimpleWebAuthn's authentication method const authenticationResponse = await startAuthentication({ optionsJSON: authenticationOptions, }); // Convert to a format compatible with react-native-passkeys return authenticationResponse; } catch (error) { console.error('Failed to get Passkey:', error); throw error; } }; // Export API compatible with react-native-passkeys export default { isSupported, create, get, };