Update selected item color in Pass menu
[ProtonMail-WebClient.git] / packages / pass / lib / passkeys / webauthn.ts
blobbba14e029edb02fe999720f4f6dc5b3cc67d59fc
1 import type { WasmPublicKeyCredentialAssertion, WasmPublicKeyCredentialAttestation } from '@protontech/pass-rust-core';
3 import { SafeUint8Array } from '@proton/pass/utils/buffer/sanitization';
5 export const intoAuthenticatorAttestationResponse = ({
6     response,
7 }: WasmPublicKeyCredentialAttestation): AuthenticatorAttestationResponse =>
8     Object.setPrototypeOf(
9         {
10             attestationObject: new SafeUint8Array(response.attestation_object).buffer,
11             clientDataJSON: new SafeUint8Array(response.client_data_json).buffer,
12             getAuthenticatorData: () => new SafeUint8Array(response.authenticator_data).buffer,
13             getPublicKey: () => (response.public_key ? new SafeUint8Array(response.public_key).buffer : null),
14             getPublicKeyAlgorithm: () => response.public_key_algorithm,
15             getTransports: () => response?.transports ?? [],
16         },
17         AuthenticatorAttestationResponse.prototype
18     );
20 export const intoAuthenticatorAssertionResponse = (
21     credential: WasmPublicKeyCredentialAssertion
22 ): AuthenticatorAssertionResponse =>
23     Object.setPrototypeOf(
24         {
25             clientDataJSON: new SafeUint8Array(credential.response.client_data_json).buffer,
26             authenticatorData: new SafeUint8Array(credential.response.authenticator_data).buffer,
27             signature: new SafeUint8Array(credential.response.signature).buffer,
28             userHandle: credential.response.user_handle
29                 ? new SafeUint8Array(credential.response.user_handle).buffer
30                 : null,
31         },
32         AuthenticatorAssertionResponse.prototype
33     );
35 export const intoPublicKeyCredential = (
36     result: WasmPublicKeyCredentialAttestation | WasmPublicKeyCredentialAssertion,
37     response: AuthenticatorAttestationResponse | AuthenticatorAssertionResponse,
38     clone: <T>(obj: T) => T
39 ): PublicKeyCredential =>
40     Object.setPrototypeOf(
41         {
42             authenticatorAttachment: result.authenticator_attachment,
43             id: result.id,
44             rawId: new SafeUint8Array(result.raw_id).buffer,
45             response,
46             type: result.type,
47             getClientExtensionResults: () => clone(result.client_extension_results ?? {}),
48         },
49         PublicKeyCredential.prototype
50     );