|
| 1 | +import { Base64, type WebAuthnP256 } from "ox" |
| 2 | +// import { Base64 } from "ox" |
| 3 | +import type { Account, Chain, Client, Hex, Transport } from "viem" |
| 4 | +import type { PasskeyServerRpcSchema } from "../../types/passkeyServer.js" |
| 5 | + |
| 6 | +export type VerifyAuthenticationParameters = WebAuthnP256.sign.ReturnType & { |
| 7 | + uuid: string |
| 8 | +} |
| 9 | + |
| 10 | +export type VerifyAuthenticationReturnType = { |
| 11 | + success: boolean |
| 12 | + id: string |
| 13 | + publicKey: Hex |
| 14 | +} |
| 15 | + |
| 16 | +export const verifyAuthentication = async ( |
| 17 | + client: Client< |
| 18 | + Transport, |
| 19 | + Chain | undefined, |
| 20 | + Account | undefined, |
| 21 | + PasskeyServerRpcSchema |
| 22 | + >, |
| 23 | + args: VerifyAuthenticationParameters |
| 24 | +): Promise<VerifyAuthenticationReturnType> => { |
| 25 | + const { raw, uuid } = args |
| 26 | + |
| 27 | + let responseAuthenticatorData: string |
| 28 | + |
| 29 | + if ("authenticatorData" in raw.response) { |
| 30 | + responseAuthenticatorData = Base64.fromBytes( |
| 31 | + new Uint8Array(raw.response.authenticatorData as ArrayBuffer), |
| 32 | + { |
| 33 | + url: true |
| 34 | + } |
| 35 | + ) |
| 36 | + } else { |
| 37 | + throw new Error("authenticatorData not found in the signature") |
| 38 | + } |
| 39 | + |
| 40 | + let signature: string |
| 41 | + if ("signature" in raw.response) { |
| 42 | + signature = Base64.fromBytes( |
| 43 | + new Uint8Array(raw.response.signature as ArrayBuffer), |
| 44 | + { |
| 45 | + pad: false, |
| 46 | + url: true |
| 47 | + } |
| 48 | + ) |
| 49 | + } else { |
| 50 | + throw new Error("signature not found in the signature") |
| 51 | + } |
| 52 | + |
| 53 | + let userHandle: string | undefined |
| 54 | + if ("userHandle" in raw.response) { |
| 55 | + userHandle = Base64.fromBytes( |
| 56 | + new Uint8Array(raw.response.userHandle as ArrayBuffer), |
| 57 | + { |
| 58 | + pad: false, |
| 59 | + url: true |
| 60 | + } |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + const serverResponse = await client.request( |
| 65 | + { |
| 66 | + method: "pks_verifyAuthentication", |
| 67 | + params: [ |
| 68 | + { |
| 69 | + id: raw.id, |
| 70 | + rawId: Base64.fromBytes(new Uint8Array(raw.rawId), { |
| 71 | + pad: false, |
| 72 | + url: true |
| 73 | + }), |
| 74 | + authenticatorAttachment: raw.authenticatorAttachment as |
| 75 | + | "cross-platform" |
| 76 | + | "platform", |
| 77 | + response: { |
| 78 | + clientDataJSON: Base64.fromBytes( |
| 79 | + new Uint8Array(raw.response.clientDataJSON), |
| 80 | + { |
| 81 | + pad: false, |
| 82 | + url: true |
| 83 | + } |
| 84 | + ), |
| 85 | + authenticatorData: responseAuthenticatorData, |
| 86 | + signature, |
| 87 | + userHandle |
| 88 | + }, |
| 89 | + clientExtensionResults: raw.getClientExtensionResults(), |
| 90 | + type: raw.type as "public-key" |
| 91 | + }, |
| 92 | + { |
| 93 | + uuid |
| 94 | + } |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + retryCount: 0 |
| 99 | + } |
| 100 | + ) |
| 101 | + |
| 102 | + const success = Boolean(serverResponse?.success) |
| 103 | + const id = serverResponse?.id |
| 104 | + const publicKey = serverResponse?.publicKey |
| 105 | + |
| 106 | + if (typeof id !== "string") { |
| 107 | + throw new Error("Invalid passkey id returned from server") |
| 108 | + } |
| 109 | + |
| 110 | + if (typeof publicKey !== "string" || !publicKey.startsWith("0x")) { |
| 111 | + throw new Error( |
| 112 | + "Invalid public key returned from server - must be hex string starting with 0x" |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + return { |
| 117 | + success, |
| 118 | + id, |
| 119 | + publicKey: publicKey as Hex |
| 120 | + } |
| 121 | +} |
0 commit comments