Skip to content

Commit

Permalink
Merge pull request #1331 from near/support-bitwarden-webauthn
Browse files Browse the repository at this point in the history
biometric-ed25519 update to support BitWarden password manager
  • Loading branch information
vikinatora authored Apr 9, 2024
2 parents f287962 + 30e3b34 commit 14256aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/mighty-paws-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@near-js/biometric-ed25519": minor
---

Include sanitization on navigator.credentials response to support Bitwarden password manager
10 changes: 7 additions & 3 deletions packages/biometric-ed25519/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
preformatGetAssertReq,
publicKeyCredentialToJSON,
recoverPublicKey,
uint8ArrayToBigInt
uint8ArrayToBigInt,
convertToArrayBuffer
} from './utils';
import { Fido2 } from './fido2';
import { AssertionResponse } from './index.d';
Expand Down Expand Up @@ -63,8 +64,10 @@ export const createKey = async (username: string): Promise<KeyPair> => {
throw new PasskeyProcessCanceled('Failed to retrieve response from navigator.credentials.create');
}

const sanitizedResponse = convertToArrayBuffer(res);

const result = await f2l.attestation({
clientAttestationResponse: res,
clientAttestationResponse: sanitizedResponse,
origin,
challenge: challengeMakeCred.challenge
});
Expand Down Expand Up @@ -93,7 +96,8 @@ export const getKeys = async (username: string): Promise<[KeyPair, KeyPair]> =>
setBufferIfUndefined();
return navigator.credentials.get({ publicKey })
.then(async (response: Credential) => {
const getAssertionResponse: AssertionResponse = publicKeyCredentialToJSON(response);
const sanitizedResponse = convertToArrayBuffer(response);
const getAssertionResponse: AssertionResponse = publicKeyCredentialToJSON(sanitizedResponse);
const signature = base64.toArrayBuffer(getAssertionResponse.response.signature, true);

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
15 changes: 15 additions & 0 deletions packages/biometric-ed25519/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,19 @@ export const recoverPublicKey = async (r, s, message, recovery) => {
export const uint8ArrayToBigInt = (uint8Array: Uint8Array) => {
const array = Array.from(uint8Array);
return BigInt('0x' + array.map(byte => byte.toString(16).padStart(2, '0')).join(''));
};

// This function is tries converts Uint8Array, Array or object to ArrayBuffer. Returns the original object if it doesn't match any of the aforementioned types.
export const convertToArrayBuffer = (obj) => {
if (obj instanceof Uint8Array) {
return obj.buffer.slice(obj.byteOffset, obj.byteOffset + obj.byteLength);
} else if (Array.isArray(obj)) {
return obj.map(convertToArrayBuffer);
} else if (obj !== null && typeof obj === 'object') {
return Object.keys(obj).reduce((acc, key) => {
acc[key] = convertToArrayBuffer(obj[key]);
return acc;
}, {});
}
return obj;
};

0 comments on commit 14256aa

Please sign in to comment.