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

Add isDeviceSupported method #16

Merged
merged 6 commits into from
Jul 15, 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/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 supports biometric ed25519 authentication
export const isDeviceSupported = async (): Promise<boolean> => {
try {
await createKey('test-device');
return true;
} catch (e) {
return false;
}
};
Loading
Loading