Skip to content
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

biometric-ed25519 update to support BitWarden password manager #1331

Merged
merged 3 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};
Loading