Skip to content

Commit

Permalink
feat: add isDeviceSupported method
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsonevv committed Jul 4, 2024
1 parent ed54e10 commit 71b2764
Show file tree
Hide file tree
Showing 5 changed files with 1,752 additions and 1,579 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-kings-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@near-js/biometric-ed25519": patch
---

Add isDeviceSupported method
1 change: 1 addition & 0 deletions packages/biometric-ed25519/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"asn1-parser": "1.1.8",
"borsh": "1.0.0",
"buffer": "6.0.3",
"cbor-js": "^0.1.0",
"fido2-lib": "3.4.1"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions packages/biometric-ed25519/src/fido2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import base64 from '@hexagon/base64';
import { Fido2Lib } from 'fido2-lib';
import cbor from "cbor-js";

export class Fido2 {
f2l: Fido2Lib;
Expand Down Expand Up @@ -58,4 +59,12 @@ export class Fido2 {
status: 'ok',
};
}

async checkAlg(res, exp): Promise<any> {
const result = await this.f2l.attestationResult(res, exp);
const cosePublicKey = result.authnrData.get('credentialPublicKeyCose');
const decodedKey = cbor.decode(cosePublicKey);
const algKey = 3; // The key for the "alg" field in COSE
return decodedKey[algKey];
}
}
20 changes: 20 additions & 0 deletions packages/biometric-ed25519/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ export const createKey = async (username: string): Promise<KeyPair> => {

const sanitizedResponse = sanitizeCreateKeyResponse(res);

const alg = await f2l.checkAlg(sanitizedResponse, {
challenge: challengeMakeCred.challenge,
origin,
factor: "either"
});

if (+alg === -257) {
throw new Error('Unsupported device');
}

const result = await f2l.attestation({
clientAttestationResponse: sanitizedResponse,
origin,
Expand Down Expand Up @@ -129,3 +139,13 @@ export const getKeys = async (username: string): Promise<[KeyPair, KeyPair]> =>
export const isPassKeyAvailable = async (): Promise<boolean> => {
return window.PublicKeyCredential?.isUserVerifyingPlatformAuthenticatorAvailable?.() || false;
};

// To check if current device is supported
export const isDeviceSupported = async (): Promise<boolean> => {
try {
await createKey('test-device');
return true;
} catch (e) {
return false;
}
}
Loading

0 comments on commit 71b2764

Please sign in to comment.