Skip to content

Commit

Permalink
Create biometricAuth.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent 5b41950 commit bb8823b
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions src/security/biometricAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// biometricAuth.js

class BiometricAuth {
constructor() {
this.isBiometricSupported = this.checkBiometricSupport();
}

// Check if the device supports WebAuthn
checkBiometricSupport() {
return window.PublicKeyCredential !== undefined;
}

// Enroll a user for biometric authentication
async enrollBiometric(userId) {
if (!this.isBiometricSupported) {
throw new Error("Biometric authentication is not supported on this device.");
}

const publicKey = {
challenge: new Uint8Array(32), // Random challenge
rp: { name: "My Bank" },
user: {
id: new Uint8Array(16), // User ID
name: userId,
displayName: userId,
},
pubKeyCredParams: [{ type: "public-key", alg: -7 }], // ECDSA with SHA-256
timeout: 60000,
attestation: "direct",
};

try {
const credential = await navigator.credentials.create({ publicKey });
// Store the credential in your database
console.log("Credential created:", credential);
return credential;
} catch (error) {
console.error("Error during enrollment:", error);
throw error;
}
}

// Authenticate a user using biometric data
async authenticate(userId) {
if (!this.isBiometricSupported) {
throw new Error("Biometric authentication is not supported on this device.");
}

const publicKey = {
challenge: new Uint8Array(32), // Random challenge
allowCredentials: [{
id: new Uint8Array(32), // The ID of the stored credential
type: "public-key",
}],
timeout: 60000,
};

try {
const assertion = await navigator.credentials.get({ publicKey });
// Verify the assertion with your server
console.log("Authentication successful:", assertion);
return assertion;
} catch (error) {
console.error("Error during authentication:", error);
throw error;
}
}
}

// Example usage
(async () => {
const biometricAuth = new BiometricAuth();
const userId = 'user123';

try {
await biometricAuth.enrollBiometric(userId);
const isAuthenticated = await biometricAuth.authenticate(userId);
console.log(`User authenticated: ${isAuthenticated}`);
} catch (error) {
console.error(error.message);
}
})();

export default BiometricAuth;

0 comments on commit bb8823b

Please sign in to comment.