@ideem/zsm-client-sdk
Version:
ZSM makes 2FA easy and invisible for everyone, all the time, using advanced cryptography like MPC to establish cryptographic proof of the origin of any transaction or login attempt, while eliminating opportunities for social engineering. ZSM has no relian
119 lines (105 loc) • 6.35 kB
JavaScript
import eventCoordinator from './EventCoordinator.js';
import {zsmPluginManager} from './PluginManager.js';
class FIDO2ClientBase {
/**
* @constructor
* @param {Object} config - The configuration for the FIDO2Client
*/
constructor(config) {
if(eventCoordinator.done) eventCoordinator.reset();
eventCoordinator.update('FIDO2ClientBase');
this.config = config;
const WebAuthnClient = zsmPluginManager.classes('WEBAUTHNCLIENT');
this.zsmAPI = new WebAuthnClient(config);
this.checkEnrollment = this.checkEnrollment.bind(this);
this.checkIdentity = this.checkIdentity.bind(this);
this.webauthnRetrieve = this.webauthnRetrieve.bind(this);
this.webauthnDelete = this.webauthnDelete.bind(this);
this.resetDevice = this.resetDevice.bind(this);
this.webauthnCreate = this.webauthnCreate.bind(this);
this.webauthnGet = this.webauthnGet.bind(this);
eventCoordinator.update('FIDO2ClientBase', 'READY');
}
get userIdentifier() { return this.zsmAPI.userIdentifier; }
get credentialID() { return this.zsmAPI.credentialID; }
/**
* @name checkEnrollment
* @description Alias for webauthnRetrieve
* @param {string} userIdentifier The identifier for the user
* @returns {Promise<Object>} Returns the result of webauthnRetrieve
* @memberOf FIDO2ClientBase, WebAuthnClient
*/
checkEnrollment (userIdentifier) { return this.webauthnRetrieve(userIdentifier); }
/**
* @name checkIdentity
* @description Pass-through method to call WebAuthnClient's checkIdentity
* @param {string} userIdentifier The identifier for the user
* @param {boolean} primeEnroll Whether to create a new identity if it doesn't exist
* @returns {Promise<Object>} Returns the results of zsmAPI.checkIdentity
* @memberOf FIDO2ClientBase, WebAuthnClient
*/
checkIdentity (userIdentifier, primeEnroll) { return this.zsmAPI.checkIdentity(userIdentifier, primeEnroll); }
/**
* @name webauthnRetrieve
* @description Pass-through method to call WebAuthnClient's webauthnRetrieve
* @param {string} userIdentifier The identifier for the user
* @returns {Promise<Object>} Returns the results of zsmAPI.webauthnRetrieve
* @memberOf FIDO2ClientBase, WebAuthnClient
*/
webauthnRetrieve (userIdentifier) { return this.zsmAPI.webauthnRetrieve(userIdentifier); }
/**
* @name webauthnDelete
* @description Pass-through method to call WebAuthnClient's unbindFromDevice
* @param {string} userIdentifier The identifier for the user
* @returns {Promise<Object>} Returns the results of zsmAPI.unbindFromDevice
* @memberOf FIDO2ClientBase, WebAuthnClient
*/
webauthnDelete (userIdentifier) { return this.zsmAPI.unbindFromDevice(userIdentifier); }
/**
* @name resetDevice
* @description Pass-through method to call WebAuthnClient's webauthnReset
* @returns {Promise<void>} No results are returned
* @memberOf FIDO2ClientBase, WebAuthnClient
*/
resetDevice () { this.zsmAPI.webauthnReset(); }
/**
* @name webauthnCreate
* @description Enrolls a user by creating a ZSM credential on the device
* @param {string} userIdentifier The identifier for the user
* @returns {Promise<Object>|Error} Resolves with the created credential, false if user is already enrolled, or an error if enrollment fails
* @throws {Error} If the userIdentifier is not provided or is empty
* @throws {Error} If an error occurs during enrollment
* @memberOf FIDO2ClientBase
*/
async webauthnCreate (userIdentifier) {
try {
if(userIdentifier == null
|| userIdentifier === '') throw new Error(`[FIDO2Client] :: webauthnCreate :: A userIdentifier String is required! Received: ${userIdentifier}.`);
const createResult = this.zsmAPI.webauthnCreate(userIdentifier);
if(createResult instanceof Error) throw(`[FIDO2Client] :: webauthnCreate :: Unable to associate ${userIdentifier}'s identity with device profile.\nDetails:\n${createResult}`);
return createResult;
}catch(e) {
return new Error(e.message || e || '[FIDO2Client] :: webauthnCreate :: An error occurred during credential creation.');
}
}
/**
* @name webauthnGet
* @description Retrieves the ZSM credential for the specified user
* @param {string} userIdentifier The identifier for the user
* @returns {Promise<Object>} The ZSM credential for the user
* @throws {Error} If the userIdentifier is not provided or is empty
* @throws {Error} If an error occurs during credential retrieval
* @memberOf FIDO2ClientBase
*/
async webauthnGet (userIdentifier) {
try {
if(userIdentifier == null
|| userIdentifier === '') throw new Error(`[FIDO2Client] :: webauthnGet :: A userIdentifier String is required! Received: ${userIdentifier}.`);
let authCredential = await this.zsmAPI.webauthnGet(userIdentifier);
return authCredential.credential;
}catch(e) {
return new Error(e.message || e || '[FIDO2Client] :: webauthnGet :: An error occurred during credential retrieval.');
}
}
}
export default FIDO2ClientBase;